]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/ThirdParty/GCC/nrf52840-dk/port_cmsis_systick.c
Remove the FreeRTOS-IoT-Libraries from FreeRTOS-Plus as it was an old copy with a...
[freertos] / FreeRTOS / Source / portable / ThirdParty / GCC / nrf52840-dk / port_cmsis_systick.c
1 /*\r
2  * FreeRTOS Kernel V10.0.0\r
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\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
11  *\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
15  *\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
22  *\r
23  * http://www.FreeRTOS.org\r
24  * http://aws.amazon.com/freertos\r
25  *\r
26  * 1 tab == 4 spaces!\r
27  */\r
28 \r
29 /* Scheduler includes. */\r
30 #include "FreeRTOS.h"\r
31 #include "task.h"\r
32 #include "app_util.h"\r
33 \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
39 #endif\r
40 \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
45 \r
46 #if configTICK_SOURCE == FREERTOS_USE_SYSTICK\r
47 \r
48 \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
53 #else\r
54     /* The way the SysTick is clocked is not modified in case it is not the same\r
55     as the core. */\r
56     #define portNVIC_SYSTICK_CLK_BIT    ( 0 )\r
57 #endif\r
58 \r
59 \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
63 \r
64 /*-----------------------------------------------------------*/\r
65 \r
66 void xPortSysTickHandler( void )\r
67 {\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
71     known. */\r
72     ( void ) portSET_INTERRUPT_MASK_FROM_ISR();\r
73     {\r
74         /* Increment the RTOS tick. */\r
75         if ( xTaskIncrementTick() != pdFALSE )\r
76         {\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
80             __SEV();\r
81         }\r
82     }\r
83     portCLEAR_INTERRUPT_MASK_FROM_ISR( 0 );\r
84 }\r
85 \r
86 /*-----------------------------------------------------------*/\r
87 \r
88 /*\r
89  * Setup the systick timer to generate the tick interrupts at the required\r
90  * frequency.\r
91  */\r
92 void vPortSetupTimerInterrupt( void )\r
93 {\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
99 }\r
100 \r
101 /*-----------------------------------------------------------*/\r
102 \r
103 #elif configTICK_SOURCE == FREERTOS_USE_RTC\r
104 \r
105 #if configUSE_16_BIT_TICKS == 1\r
106 #error This port does not support 16 bit ticks.\r
107 #endif\r
108 \r
109 #include "nrf_rtc.h"\r
110 #include "nrf_drv_clock.h"\r
111 \r
112 /*-----------------------------------------------------------*/\r
113 \r
114 void xPortSysTickHandler( void )\r
115 {\r
116 #if configUSE_TICKLESS_IDLE == 1\r
117     nrf_rtc_event_clear(portNRF_RTC_REG, NRF_RTC_EVENT_COMPARE_0);\r
118 #endif\r
119 \r
120     BaseType_t switch_req = pdFALSE;\r
121     uint32_t isrstate = portSET_INTERRUPT_MASK_FROM_ISR();\r
122 \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
125 \r
126     if (configUSE_DISABLE_TICK_AUTO_CORRECTION_DEBUG == 0)\r
127     {\r
128         /* check FreeRTOSConfig.h file for more details on configUSE_DISABLE_TICK_AUTO_CORRECTION_DEBUG */\r
129         TickType_t diff;\r
130         diff = (systick_counter - xTaskGetTickCount()) & portNRF_RTC_MAXTICKS;\r
131 \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
135         {\r
136             diff = 1;\r
137         }\r
138         while ((diff--) > 0)\r
139         {\r
140             switch_req |= xTaskIncrementTick();\r
141         }\r
142     }\r
143     else\r
144     {\r
145         switch_req = xTaskIncrementTick();\r
146     }\r
147 \r
148     /* Increment the RTOS tick as usual which checks if there is a need for rescheduling */\r
149     if ( switch_req != pdFALSE )\r
150     {\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
154         __SEV();\r
155     }\r
156 \r
157     portCLEAR_INTERRUPT_MASK_FROM_ISR( isrstate );\r
158 }\r
159 \r
160 /*\r
161  * Setup the RTC time to generate the tick interrupts at the required\r
162  * frequency.\r
163  */\r
164 void vPortSetupTimerInterrupt( void )\r
165 {\r
166     /* Request LF clock */\r
167     nrf_drv_clock_lfclk_request(NULL);\r
168 \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
175 \r
176     NVIC_SetPriority(portNRF_RTC_IRQn, configKERNEL_INTERRUPT_PRIORITY);\r
177     NVIC_EnableIRQ(portNRF_RTC_IRQn);\r
178 }\r
179 \r
180 #if configUSE_TICKLESS_IDLE == 1\r
181 \r
182 void vPortSuppressTicksAndSleep( TickType_t xExpectedIdleTime )\r
183 {\r
184     /*\r
185      * Implementation note:\r
186      *\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
190      *\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
193      */\r
194     TickType_t enterTime;\r
195 \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
198     {\r
199         xExpectedIdleTime = portNRF_RTC_MAXTICKS - configEXPECTED_IDLE_TIME_BEFORE_SLEEP;\r
200     }\r
201     /* Block all the interrupts globally */\r
202 #ifdef SOFTDEVICE_PRESENT\r
203     do{\r
204         uint8_t dummy = 0;\r
205         uint32_t err_code = sd_nvic_critical_region_enter(&dummy);\r
206         APP_ERROR_CHECK(err_code);\r
207     }while (0);\r
208 #else\r
209     __disable_irq();\r
210 #endif\r
211 \r
212     enterTime = nrf_rtc_counter_get(portNRF_RTC_REG);\r
213 \r
214     if ( eTaskConfirmSleepModeStatus() != eAbortSleep )\r
215     {\r
216         TickType_t xModifiableIdleTime;\r
217         TickType_t wakeupTime = (enterTime + xExpectedIdleTime) & portNRF_RTC_MAXTICKS;\r
218 \r
219         /* Stop tick events */\r
220         nrf_rtc_int_disable(portNRF_RTC_REG, NRF_RTC_INT_TICK_MASK);\r
221 \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
226 \r
227         __DSB();\r
228 \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
237         {\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
241             {\r
242                 uint32_t err_code = sd_app_evt_wait();\r
243                 APP_ERROR_CHECK(err_code);\r
244             }\r
245             else\r
246 #endif\r
247 #endif // (NRFFOSDK-11174)\r
248             {\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
251                 */\r
252                 do{\r
253                     __WFE();\r
254                 } while (0 == (NVIC->ISPR[0] | NVIC->ISPR[1]));\r
255             }\r
256         }\r
257         configPOST_SLEEP_PROCESSING( xExpectedIdleTime );\r
258 \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
261 \r
262         /* Correct the system ticks */\r
263         {\r
264             TickType_t diff;\r
265             TickType_t exitTime;\r
266 \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
269 \r
270             exitTime = nrf_rtc_counter_get(portNRF_RTC_REG);\r
271             diff =  (exitTime - enterTime) & portNRF_RTC_MAXTICKS;\r
272 \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
275 \r
276             if ((configUSE_TICKLESS_IDLE_SIMPLE_DEBUG) && (diff > xExpectedIdleTime))\r
277             {\r
278                 diff = xExpectedIdleTime;\r
279             }\r
280 \r
281             if (diff > 0)\r
282             {\r
283                 vTaskStepTick(diff);\r
284             }\r
285         }\r
286     }\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
290 #else\r
291     __enable_irq();\r
292 #endif\r
293 }\r
294 \r
295 #endif // configUSE_TICKLESS_IDLE\r
296 \r
297 #else // configTICK_SOURCE\r
298     #error  Unsupported configTICK_SOURCE value\r
299 #endif // configTICK_SOURCE == FREERTOS_USE_SYSTICK\r