port_cmsis_systick.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. * FreeRTOS Kernel V10.0.0
  3. * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  6. * this software and associated documentation files (the "Software"), to deal in
  7. * the Software without restriction, including without limitation the rights to
  8. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  9. * the Software, and to permit persons to whom the Software is furnished to do so,
  10. * subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in all
  13. * copies or substantial portions of the Software. If you wish to use our Amazon
  14. * FreeRTOS name, please do so in a fair use way that does not cause confusion.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  18. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  19. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  20. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. *
  23. * http://www.FreeRTOS.org
  24. * http://aws.amazon.com/freertos
  25. *
  26. * 1 tab == 4 spaces!
  27. */
  28. /* Scheduler includes. */
  29. #include "FreeRTOS.h"
  30. #include "task.h"
  31. #include "app_util.h"
  32. #ifdef SOFTDEVICE_PRESENT
  33. #include "nrf_soc.h"
  34. #include "nrf_sdh.h"
  35. #include "app_error.h"
  36. #include "app_util_platform.h"
  37. #endif
  38. /*-----------------------------------------------------------
  39. * Implementation of functions defined in portable.h for the ARM CM4F port.
  40. * CMSIS compatible layer to menage SysTick ticking source.
  41. *----------------------------------------------------------*/
  42. #if configTICK_SOURCE == FREERTOS_USE_SYSTICK
  43. #ifndef configSYSTICK_CLOCK_HZ
  44. #define configSYSTICK_CLOCK_HZ configCPU_CLOCK_HZ
  45. /* Ensure the SysTick is clocked at the same frequency as the core. */
  46. #define portNVIC_SYSTICK_CLK_BIT ( SysTick_CTRL_CLKSOURCE_Msk )
  47. #else
  48. /* The way the SysTick is clocked is not modified in case it is not the same
  49. as the core. */
  50. #define portNVIC_SYSTICK_CLK_BIT ( 0 )
  51. #endif
  52. #if configUSE_TICKLESS_IDLE == 1
  53. #error SysTick port for RF52 does not support tickless idle. Use RTC mode instead.
  54. #endif /* configUSE_TICKLESS_IDLE */
  55. /*-----------------------------------------------------------*/
  56. void xPortSysTickHandler( void )
  57. {
  58. /* The SysTick runs at the lowest interrupt priority, so when this interrupt
  59. executes all interrupts must be unmasked. There is therefore no need to
  60. save and then restore the interrupt mask value as its value is already
  61. known. */
  62. ( void ) portSET_INTERRUPT_MASK_FROM_ISR();
  63. {
  64. /* Increment the RTOS tick. */
  65. if ( xTaskIncrementTick() != pdFALSE )
  66. {
  67. /* A context switch is required. Context switching is performed in
  68. the PendSV interrupt. Pend the PendSV interrupt. */
  69. SCB->ICSR = SCB_ICSR_PENDSVSET_Msk;
  70. __SEV();
  71. }
  72. }
  73. portCLEAR_INTERRUPT_MASK_FROM_ISR( 0 );
  74. }
  75. /*-----------------------------------------------------------*/
  76. /*
  77. * Setup the systick timer to generate the tick interrupts at the required
  78. * frequency.
  79. */
  80. void vPortSetupTimerInterrupt( void )
  81. {
  82. /* Set interrupt priority */
  83. NVIC_SetPriority(SysTick_IRQn, configKERNEL_INTERRUPT_PRIORITY);
  84. /* Configure SysTick to interrupt at the requested rate. */
  85. SysTick->LOAD = ROUNDED_DIV(configSYSTICK_CLOCK_HZ, configTICK_RATE_HZ) - 1UL;
  86. SysTick->CTRL = ( portNVIC_SYSTICK_CLK_BIT | SysTick_CTRL_TICKINT_Msk | SysTick_CTRL_ENABLE_Msk );
  87. }
  88. /*-----------------------------------------------------------*/
  89. #elif configTICK_SOURCE == FREERTOS_USE_RTC
  90. #if configUSE_16_BIT_TICKS == 1
  91. #error This port does not support 16 bit ticks.
  92. #endif
  93. #include "nrf_rtc.h"
  94. #include "nrf_drv_clock.h"
  95. /*-----------------------------------------------------------*/
  96. void xPortSysTickHandler( void )
  97. {
  98. #if configUSE_TICKLESS_IDLE == 1
  99. nrf_rtc_event_clear(portNRF_RTC_REG, NRF_RTC_EVENT_COMPARE_0);
  100. #endif
  101. BaseType_t switch_req = pdFALSE;
  102. uint32_t isrstate = portSET_INTERRUPT_MASK_FROM_ISR();
  103. uint32_t systick_counter = nrf_rtc_counter_get(portNRF_RTC_REG);
  104. nrf_rtc_event_clear(portNRF_RTC_REG, NRF_RTC_EVENT_TICK);
  105. if (configUSE_DISABLE_TICK_AUTO_CORRECTION_DEBUG == 0)
  106. {
  107. /* check FreeRTOSConfig.h file for more details on configUSE_DISABLE_TICK_AUTO_CORRECTION_DEBUG */
  108. TickType_t diff;
  109. diff = (systick_counter - xTaskGetTickCount()) & portNRF_RTC_MAXTICKS;
  110. /* At most 1 step if scheduler is suspended - the xTaskIncrementTick
  111. * would return the tick state from the moment when suspend function was called. */
  112. if ((diff > 1) && (xTaskGetSchedulerState() != taskSCHEDULER_RUNNING))
  113. {
  114. diff = 1;
  115. }
  116. while ((diff--) > 0)
  117. {
  118. switch_req |= xTaskIncrementTick();
  119. }
  120. }
  121. else
  122. {
  123. switch_req = xTaskIncrementTick();
  124. }
  125. /* Increment the RTOS tick as usual which checks if there is a need for rescheduling */
  126. if ( switch_req != pdFALSE )
  127. {
  128. /* A context switch is required. Context switching is performed in
  129. the PendSV interrupt. Pend the PendSV interrupt. */
  130. SCB->ICSR = SCB_ICSR_PENDSVSET_Msk;
  131. __SEV();
  132. }
  133. portCLEAR_INTERRUPT_MASK_FROM_ISR( isrstate );
  134. }
  135. /*
  136. * Setup the RTC time to generate the tick interrupts at the required
  137. * frequency.
  138. */
  139. void vPortSetupTimerInterrupt( void )
  140. {
  141. /* Request LF clock */
  142. nrf_drv_clock_lfclk_request(NULL);
  143. /* Configure SysTick to interrupt at the requested rate. */
  144. nrf_rtc_prescaler_set(portNRF_RTC_REG, portNRF_RTC_PRESCALER);
  145. nrf_rtc_int_enable (portNRF_RTC_REG, RTC_INTENSET_TICK_Msk);
  146. nrf_rtc_task_trigger (portNRF_RTC_REG, NRF_RTC_TASK_CLEAR);
  147. nrf_rtc_task_trigger (portNRF_RTC_REG, NRF_RTC_TASK_START);
  148. nrf_rtc_event_enable(portNRF_RTC_REG, RTC_EVTEN_OVRFLW_Msk);
  149. NVIC_SetPriority(portNRF_RTC_IRQn, configKERNEL_INTERRUPT_PRIORITY);
  150. NVIC_EnableIRQ(portNRF_RTC_IRQn);
  151. }
  152. #if configUSE_TICKLESS_IDLE == 1
  153. void vPortSuppressTicksAndSleep( TickType_t xExpectedIdleTime )
  154. {
  155. /*
  156. * Implementation note:
  157. *
  158. * To help debugging the option configUSE_TICKLESS_IDLE_SIMPLE_DEBUG was presented.
  159. * This option would make sure that even if program execution was stopped inside
  160. * this function no more than expected number of ticks would be skipped.
  161. *
  162. * Normally RTC works all the time even if firmware execution was stopped
  163. * and that may lead to skipping too much of ticks.
  164. */
  165. TickType_t enterTime;
  166. /* Make sure the SysTick reload value does not overflow the counter. */
  167. if ( xExpectedIdleTime > portNRF_RTC_MAXTICKS - configEXPECTED_IDLE_TIME_BEFORE_SLEEP )
  168. {
  169. xExpectedIdleTime = portNRF_RTC_MAXTICKS - configEXPECTED_IDLE_TIME_BEFORE_SLEEP;
  170. }
  171. /* Block all the interrupts globally */
  172. #ifdef SOFTDEVICE_PRESENT
  173. do{
  174. uint8_t dummy = 0;
  175. uint32_t err_code = sd_nvic_critical_region_enter(&dummy);
  176. APP_ERROR_CHECK(err_code);
  177. }while (0);
  178. #else
  179. __disable_irq();
  180. #endif
  181. enterTime = nrf_rtc_counter_get(portNRF_RTC_REG);
  182. if ( eTaskConfirmSleepModeStatus() != eAbortSleep )
  183. {
  184. TickType_t xModifiableIdleTime;
  185. TickType_t wakeupTime = (enterTime + xExpectedIdleTime) & portNRF_RTC_MAXTICKS;
  186. /* Stop tick events */
  187. nrf_rtc_int_disable(portNRF_RTC_REG, NRF_RTC_INT_TICK_MASK);
  188. /* Configure CTC interrupt */
  189. nrf_rtc_cc_set(portNRF_RTC_REG, 0, wakeupTime);
  190. nrf_rtc_event_clear(portNRF_RTC_REG, NRF_RTC_EVENT_COMPARE_0);
  191. nrf_rtc_int_enable(portNRF_RTC_REG, NRF_RTC_INT_COMPARE0_MASK);
  192. __DSB();
  193. /* Sleep until something happens. configPRE_SLEEP_PROCESSING() can
  194. * set its parameter to 0 to indicate that its implementation contains
  195. * its own wait for interrupt or wait for event instruction, and so wfi
  196. * should not be executed again. However, the original expected idle
  197. * time variable must remain unmodified, so a copy is taken. */
  198. xModifiableIdleTime = xExpectedIdleTime;
  199. configPRE_SLEEP_PROCESSING( xModifiableIdleTime );
  200. if ( xModifiableIdleTime > 0 )
  201. {
  202. #ifdef SOFTDEVICE_PRESENT
  203. if (nrf_sdh_is_enabled())
  204. {
  205. uint32_t err_code = sd_app_evt_wait();
  206. APP_ERROR_CHECK(err_code);
  207. }
  208. else
  209. #endif
  210. {
  211. /* No SD - we would just block interrupts globally.
  212. * BASEPRI cannot be used for that because it would prevent WFE from wake up.
  213. */
  214. do{
  215. __WFE();
  216. } while (0 == (NVIC->ISPR[0] | NVIC->ISPR[1]));
  217. }
  218. }
  219. configPOST_SLEEP_PROCESSING( xExpectedIdleTime );
  220. nrf_rtc_int_disable(portNRF_RTC_REG, NRF_RTC_INT_COMPARE0_MASK);
  221. nrf_rtc_event_clear(portNRF_RTC_REG, NRF_RTC_EVENT_COMPARE_0);
  222. /* Correct the system ticks */
  223. {
  224. TickType_t diff;
  225. TickType_t exitTime;
  226. nrf_rtc_event_clear(portNRF_RTC_REG, NRF_RTC_EVENT_TICK);
  227. nrf_rtc_int_enable (portNRF_RTC_REG, NRF_RTC_INT_TICK_MASK);
  228. exitTime = nrf_rtc_counter_get(portNRF_RTC_REG);
  229. diff = (exitTime - enterTime) & portNRF_RTC_MAXTICKS;
  230. /* It is important that we clear pending here so that our corrections are latest and in sync with tick_interrupt handler */
  231. NVIC_ClearPendingIRQ(portNRF_RTC_IRQn);
  232. if ((configUSE_TICKLESS_IDLE_SIMPLE_DEBUG) && (diff > xExpectedIdleTime))
  233. {
  234. diff = xExpectedIdleTime;
  235. }
  236. if (diff > 0)
  237. {
  238. vTaskStepTick(diff);
  239. }
  240. }
  241. }
  242. #ifdef SOFTDEVICE_PRESENT
  243. uint32_t err_code = sd_nvic_critical_region_exit(0);
  244. APP_ERROR_CHECK(err_code);
  245. #else
  246. __enable_irq();
  247. #endif
  248. }
  249. #endif // configUSE_TICKLESS_IDLE
  250. #else // configTICK_SOURCE
  251. #error Unsupported configTICK_SOURCE value
  252. #endif // configTICK_SOURCE == FREERTOS_USE_SYSTICK