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