Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 

523 lignes
12 KiB

  1. /**
  2. *
  3. * \file
  4. *
  5. * \brief This module contains SAMD21 BSP APIs implementation.
  6. *
  7. * Copyright (c) 2017-2018 Microchip Technology Inc. and its subsidiaries.
  8. *
  9. * \asf_license_start
  10. *
  11. * \page License
  12. *
  13. * Subject to your compliance with these terms, you may use Microchip
  14. * software and any derivatives exclusively with Microchip products.
  15. * It is your responsibility to comply with third party license terms applicable
  16. * to your use of third party software (including open source software) that
  17. * may accompany Microchip software.
  18. *
  19. * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES,
  20. * WHETHER EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE,
  21. * INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY,
  22. * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE
  23. * LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL
  24. * LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND WHATSOEVER RELATED TO THE
  25. * SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS BEEN ADVISED OF THE
  26. * POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE FULLEST EXTENT
  27. * ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN ANY WAY
  28. * RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY,
  29. * THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
  30. *
  31. * \asf_license_stop
  32. *
  33. */
  34. #include "bsp/include/nm_bsp_samd21_app.h"
  35. #include "common/include/nm_common.h"
  36. #define BSP_MIN(x,y) ((x)>(y)?(y):(x))
  37. #define SW1_PIN
  38. #define SW1_MUX
  39. #define SW1_LINE
  40. #ifdef WING_BOARD_WITH_LEDS_BUTTON
  41. #define SW2_LINE 3
  42. #define SW2_PIN PIN_PB03
  43. #define SW2_MUX MUX_PB03A_EIC_EXTINT3
  44. #endif /* WING_BOARD_WITH_LEDS_BUTTON */
  45. /*
  46. ---------------------------------
  47. ------ Module Pin Settings ------
  48. ---------------------------------
  49. */
  50. #define LONG_PRESS_TIME (1500/TICK_RES) // ~2 sec
  51. #define DEBOUNCE_TIME (40/TICK_RES) // ~50ms
  52. /*
  53. * Structure
  54. *
  55. */
  56. typedef struct
  57. {
  58. tpfNmBspTimerCb pfCb;
  59. uint32 u32Timeout;
  60. uint32 u32Period;
  61. } tstrTimer;
  62. typedef struct
  63. {
  64. tstrTimer strTimer;
  65. uint8 u8Enabled;
  66. } tstrWakeTimer;
  67. /**
  68. *
  69. * Global variables
  70. */
  71. uint32 gu32Jiffies1ms;
  72. uint32 gu32Jiffies20ms;
  73. static uint16 gu16Btn1Cnt, gu16Btn2Cnt;
  74. static tpfNmBspBtnPress gpfBtns;
  75. static uint8 gu8BtnIfg;
  76. static tstrTimer gstrTimer20ms, gstrTimer1ms, gstrConfigurableTimer;
  77. #if (defined _STATIC_PS_)||(defined _DYNAMIC_PS_)
  78. static tstrWakeTimer gstrWakeTimer;
  79. #endif
  80. struct tcc_module tcc_instance;
  81. struct tcc_module configurable_tcc;
  82. /**
  83. *
  84. * Static functions
  85. */
  86. static void btn_poll(void)
  87. {
  88. bool btn_inactive;
  89. if (gu8BtnIfg & SW1) {
  90. if (gu16Btn1Cnt <= LONG_PRESS_TIME) {
  91. gu16Btn1Cnt++;
  92. }
  93. if (gu16Btn1Cnt == LONG_PRESS_TIME) {
  94. gpfBtns(SW1, 1); /* long press callback */
  95. }
  96. btn_inactive = (port_pin_get_input_level(BUTTON_0_PIN) == BUTTON_0_INACTIVE);
  97. if ((gu16Btn1Cnt >= DEBOUNCE_TIME) && (gu16Btn1Cnt < LONG_PRESS_TIME)
  98. && btn_inactive) {
  99. gpfBtns(SW1, 0); /* Short press callback */
  100. }
  101. if (btn_inactive)
  102. {
  103. gu8BtnIfg &= ~SW1;
  104. gu16Btn1Cnt = 0;
  105. extint_chan_enable_callback(BUTTON_0_EIC_LINE, EXTINT_CALLBACK_TYPE_DETECT);
  106. }
  107. }
  108. #ifdef WING_BOARD_WITH_LEDS_BUTTON
  109. if (gu8BtnIfg & SW2) {
  110. if (gu16Btn2Cnt <= LONG_PRESS_TIME) {
  111. gu16Btn2Cnt++;
  112. }
  113. if (gu16Btn2Cnt == LONG_PRESS_TIME) {
  114. gpfBtns(SW2, 1); /* long press callback */
  115. }
  116. btn_inactive = (port_pin_get_input_level(SW2_PIN) == BUTTON_0_INACTIVE);
  117. if ((gu16Btn2Cnt >= DEBOUNCE_TIME) && (gu16Btn2Cnt < LONG_PRESS_TIME)
  118. && btn_inactive) {
  119. gpfBtns(SW2, 0); /* Short press callback */
  120. }
  121. if (btn_inactive)
  122. {
  123. gu8BtnIfg &= ~SW2;
  124. gu16Btn2Cnt = 0;
  125. extint_chan_enable_callback(SW2_LINE, EXTINT_CALLBACK_TYPE_DETECT);
  126. }
  127. }
  128. #endif /* WING_BOARD_WITH_LEDS_BUTTON */
  129. }
  130. static void _tcc_callback_to_change_duty_cycle(struct tcc_module *const module_inst)
  131. {
  132. gu32Jiffies1ms++;
  133. if(gstrTimer1ms.pfCb)
  134. gstrTimer1ms.pfCb();
  135. if(gu32Jiffies1ms%20 == 0)
  136. {
  137. #ifdef _STATIC_PS_
  138. if((gstrWakeTimer.strTimer.pfCb)&&(gstrWakeTimer.u8Enabled))
  139. {
  140. gu32Jiffies20ms+=(TICK_RES_SLEEP/TICK_RES);
  141. }
  142. else
  143. #endif
  144. {
  145. gu32Jiffies20ms++;
  146. }
  147. #ifdef _STATIC_PS_
  148. if(gstrWakeTimer.strTimer.pfCb)
  149. {
  150. if(NM_BSP_TIME_MSEC >= gstrWakeTimer.strTimer.u32Timeout)
  151. {
  152. nm_bsp_wake_ctrl(0);
  153. gstrWakeTimer.strTimer.pfCb();
  154. gstrWakeTimer.strTimer.u32Timeout = NM_BSP_TIME_MSEC + gstrWakeTimer.strTimer.u32Period;
  155. }
  156. }
  157. #endif
  158. if(gstrTimer20ms.pfCb)
  159. {
  160. if(NM_BSP_TIME_MSEC >= gstrTimer20ms.u32Timeout)
  161. {
  162. gstrTimer20ms.pfCb();
  163. gstrTimer20ms.u32Timeout = NM_BSP_TIME_MSEC + gstrTimer20ms.u32Period;
  164. }
  165. }
  166. btn_poll();
  167. }
  168. }
  169. /*
  170. * @fn _tcc_configurable_timer_callback
  171. * @brief Configurable Timer Callback
  172. * @date 08 October 2015
  173. * @version 1.0
  174. */
  175. static void _tcc_configurable_timer_callback(struct tcc_module *const module_inst)
  176. {
  177. if(gstrConfigurableTimer.pfCb)
  178. gstrConfigurableTimer.pfCb();
  179. }
  180. static void timer0_init(void)
  181. {
  182. struct tcc_config config_tcc;
  183. tcc_get_config_defaults(&config_tcc, TCC0);
  184. //! [setup_change_config]
  185. config_tcc.counter.period = 750;
  186. config_tcc.counter.reload_action = TCC_RELOAD_ACTION_GCLK;
  187. config_tcc.counter.clock_prescaler = TCC_CLOCK_PRESCALER_DIV64;
  188. tcc_init(&tcc_instance, TCC0, &config_tcc);
  189. tcc_enable(&tcc_instance);
  190. //! [setup_register_callback]
  191. tcc_register_callback(
  192. &tcc_instance,
  193. _tcc_callback_to_change_duty_cycle,
  194. TCC_CALLBACK_CHANNEL_0);
  195. tcc_enable_callback(&tcc_instance,
  196. TCC_CALLBACK_CHANNEL_0);
  197. }
  198. /*
  199. * @fn configurable_timer_init
  200. * @brief Initialize the Configurable Timer
  201. * @date 08 October 2015
  202. * @version 1.0
  203. */
  204. static void configurable_timer_init(uint32_t u32Period)
  205. {
  206. struct tcc_config config_tcc;
  207. tcc_get_config_defaults(&config_tcc, TCC1);
  208. M2M_DBG("Timer period: %lu\r\n", u32Period);
  209. //! [setup_change_config]
  210. config_tcc.counter.period = u32Period;
  211. tcc_init(&configurable_tcc, TCC1, &config_tcc);
  212. tcc_enable(&configurable_tcc);
  213. //! [setup_register_callback]
  214. tcc_register_callback(
  215. &configurable_tcc,
  216. _tcc_configurable_timer_callback,
  217. TCC_CALLBACK_OVERFLOW);
  218. }
  219. static void btn_isr(void)
  220. {
  221. gu8BtnIfg |= SW1;
  222. extint_chan_disable_callback(BUTTON_0_EIC_LINE,
  223. EXTINT_CALLBACK_TYPE_DETECT);
  224. }
  225. #ifdef WING_BOARD_WITH_LEDS_BUTTON
  226. static void btn2_isr(void)
  227. {
  228. gu8BtnIfg |= SW2;
  229. extint_chan_disable_callback(SW2_LINE,
  230. EXTINT_CALLBACK_TYPE_DETECT);
  231. }
  232. #endif /* WING_BOARD_WITH_LEDS_BUTTON */
  233. /*
  234. * @fn nm_bsp_init
  235. * @brief Initialize BSP
  236. * @return 0 in case of success and -1 in case of failure
  237. * @author M.S.M
  238. * @date 11 July 2012
  239. * @version 1.0
  240. */
  241. sint8 nm_bsp_app_init(void)
  242. {
  243. gstrTimer20ms.pfCb = NULL;
  244. gstrTimer20ms.u32Timeout = 0;
  245. gstrTimer20ms.u32Period = 0;
  246. #ifdef _STATIC_PS_
  247. gstrWakeTimer.strTimer.pfCb = NULL;
  248. gstrWakeTimer.strTimer.u32Timeout = 0;
  249. gstrWakeTimer.strTimer.u32Period = ((uint32)-1);
  250. gstrWakeTimer.u8Enabled = 0;
  251. #endif
  252. gstrTimer1ms.pfCb = NULL;
  253. gstrTimer1ms.u32Timeout = 0;
  254. gstrTimer1ms.u32Period = 0;
  255. gstrConfigurableTimer.pfCb = NULL;
  256. gstrConfigurableTimer.u32Timeout = 0;
  257. gstrConfigurableTimer.u32Period = 0;
  258. timer0_init();
  259. return M2M_SUCCESS;
  260. }
  261. /*
  262. * @fn nm_bsp_app_configurable_timer_init
  263. * @brief Initialize the Configurable Timer
  264. * @date 08 October 2015
  265. * @version 1.0
  266. */
  267. void nm_bsp_app_configurable_timer_init(uint32_t u32Period)
  268. {
  269. configurable_timer_init(u32Period);
  270. }
  271. /**
  272. * @fn nm_bsp_deinit
  273. * @brief De-iInitialize BSP
  274. * @return 0 in case of success and -1 in case of failure
  275. * @author M. Abdelmawla
  276. * @date 11 July 2012
  277. * @version 1.0
  278. */
  279. sint8 nm_bsp_app_deinit(void)
  280. {
  281. return M2M_SUCCESS;
  282. }
  283. /*
  284. * @fn nm_bsp_btn_init
  285. * @brief Initialize buttons driver
  286. * @author M.S.M
  287. * @date 28 OCT 2013
  288. * @version 1.0
  289. */
  290. void nm_bsp_btn_init(tpfNmBspBtnPress pfBtnCb)
  291. {
  292. //struct port_config pin_conf;
  293. struct extint_chan_conf config_extint_chan;
  294. gpfBtns = pfBtnCb;
  295. gu8BtnIfg = 0;
  296. gu16Btn1Cnt = 0;
  297. gu16Btn2Cnt = 0;
  298. #ifdef WING_BOARD_WITH_LEDS_BUTTON
  299. port_get_config_defaults(&pin_conf);
  300. /* Set buttons as inputs */
  301. pin_conf.direction = PORT_PIN_DIR_INPUT;
  302. pin_conf.input_pull = PORT_PIN_PULL_UP;
  303. port_pin_set_config(SW2_PIN, &pin_conf);
  304. /*Configure SW1*/
  305. extint_chan_get_config_defaults(&config_extint_chan);
  306. config_extint_chan.gpio_pin = SW2_PIN;
  307. config_extint_chan.gpio_pin_mux = SW2_MUX;
  308. config_extint_chan.gpio_pin_pull = EXTINT_PULL_UP;
  309. config_extint_chan.detection_criteria = EXTINT_DETECT_FALLING;
  310. extint_chan_set_config(SW2_LINE, &config_extint_chan);
  311. extint_register_callback(btn2_isr,
  312. SW2_LINE,
  313. EXTINT_CALLBACK_TYPE_DETECT);
  314. extint_chan_enable_callback(SW2_LINE,
  315. EXTINT_CALLBACK_TYPE_DETECT);
  316. #endif /* WING_BOARD_WITH_LEDS_BUTTON */
  317. /*Configure SW2*/
  318. extint_chan_get_config_defaults(&config_extint_chan);
  319. config_extint_chan.gpio_pin = BUTTON_0_EIC_PIN;
  320. config_extint_chan.gpio_pin_mux = BUTTON_0_EIC_MUX;
  321. config_extint_chan.gpio_pin_pull = EXTINT_PULL_UP;
  322. config_extint_chan.detection_criteria = EXTINT_DETECT_FALLING;
  323. extint_chan_set_config(BUTTON_0_EIC_LINE, &config_extint_chan);
  324. extint_register_callback(btn_isr,
  325. BUTTON_0_EIC_LINE,
  326. EXTINT_CALLBACK_TYPE_DETECT);
  327. extint_chan_enable_callback(BUTTON_0_EIC_LINE,
  328. EXTINT_CALLBACK_TYPE_DETECT);
  329. }
  330. /*
  331. * @fn nm_bsp_uart_sendnm_bsp_uart_send
  332. * @author M.S.M
  333. * @date 28 OCT 2013
  334. * @version 1.0
  335. */
  336. void nm_bsp_uart_send(const uint8 *pu8Buf, uint16 u16Sz)
  337. {
  338. }
  339. /**
  340. * @fn nm_bsp_start_timer
  341. * @brief Start 20ms timer
  342. * @author M.S.M
  343. * @date 28 OCT 2013
  344. * @version 1.0
  345. */
  346. void nm_bsp_start_timer(tpfNmBspTimerCb pfCb, uint32 u32Period)
  347. {
  348. gstrTimer20ms.pfCb = pfCb;
  349. gstrTimer20ms.u32Timeout = u32Period+ NM_BSP_TIME_MSEC;
  350. gstrTimer20ms.u32Period = u32Period;
  351. }
  352. /*
  353. * @fn nm_bsp_start_1ms_timer
  354. * @brief Start 1ms timer
  355. * @date 08 October 2015
  356. * @version 1.0
  357. */
  358. void nm_bsp_start_1ms_timer(tpfNmBspTimerCb pfCb)
  359. {
  360. gstrTimer1ms.pfCb = pfCb;
  361. }
  362. /*
  363. * @fn nm_bsp_start_configurable_timer
  364. * @brief Start configurable timer
  365. * @date 08 October 2015
  366. * @version 1.0
  367. */
  368. void nm_bsp_start_configurable_timer(tpfNmBspTimerCb pfCb)
  369. {
  370. tcc_enable_callback(&configurable_tcc, TCC_CALLBACK_OVERFLOW);
  371. gstrConfigurableTimer.pfCb = pfCb;
  372. }
  373. /**
  374. * @fn nm_bsp_stop_timer
  375. * @brief Start 20ms timer
  376. * @author M.S.M
  377. * @date 28 OCT 2013
  378. * @version 1.0
  379. */
  380. void nm_bsp_stop_timer(void)
  381. {
  382. gstrTimer20ms.pfCb = NULL;
  383. }
  384. /*
  385. * @fn nm_bsp_stop_1ms_timer
  386. * @brief Stop 1ms timer
  387. * @date 08 October 2015
  388. * @version 1.0
  389. */
  390. void nm_bsp_stop_1ms_timer(void)
  391. {
  392. gstrTimer1ms.pfCb = NULL;
  393. }
  394. /*
  395. * @fn nm_bsp_stop_configurable_timer
  396. * @brief Stop configurable timer
  397. * @date 08 October 2015
  398. * @version 1.0
  399. */
  400. void nm_bsp_stop_configurable_timer(void)
  401. {
  402. gstrConfigurableTimer.pfCb = NULL;
  403. tcc_disable_callback(&configurable_tcc, TCC_CALLBACK_OVERFLOW);
  404. tcc_disable(&configurable_tcc);
  405. }
  406. #ifdef _STATIC_PS_
  407. /**
  408. * @fn nm_bsp_register_wake_isr
  409. * @brief REGISTER wake up timer
  410. * @author M.S.M
  411. * @date 28 OCT 2013
  412. * @version 1.0
  413. */
  414. void nm_bsp_register_wake_isr(tpfNmBspIsr pfIsr,uint32 u32MsPeriod)
  415. {
  416. gstrWakeTimer.strTimer.pfCb = pfIsr;
  417. gstrWakeTimer.strTimer.u32Timeout = u32MsPeriod + NM_BSP_TIME_MSEC;
  418. gstrWakeTimer.strTimer.u32Period = u32MsPeriod;
  419. gstrWakeTimer.u8Enabled = 0;
  420. }
  421. /**
  422. * @fn nm_bsp_wake_ctrl
  423. * @brief control wake up timer
  424. * @author M.S.M
  425. * @date 28 OCT 2013
  426. * @version 1.0
  427. */
  428. void nm_bsp_wake_ctrl(uint8 en)
  429. {
  430. gstrWakeTimer.u8Enabled = en;
  431. if(en)
  432. {
  433. gstrWakeTimer.strTimer.u32Timeout = gstrWakeTimer.strTimer.u32Period + NM_BSP_TIME_MSEC;
  434. }
  435. }
  436. #endif
  437. #if (defined _STATIC_PS_)||(defined _DYNAMIC_PS_)
  438. /**
  439. * @fn nm_bsp_enable_mcu_ps
  440. * @brief Start POWER SAVE FOR MCU
  441. * @author M.S.M
  442. * @date 28 OCT 2013
  443. * @version 1.0
  444. */
  445. void nm_bsp_enable_mcu_ps(void)
  446. {
  447. if(!gu8BtnIfg)
  448. {
  449. if(gstrWakeTimer.u8Enabled)
  450. {
  451. }
  452. }
  453. }
  454. #endif