STM32F769IDiscovery  1.00
uDANTE Audio Networking with STM32F7 DISCO board
port.c
Go to the documentation of this file.
1 /*
2  FreeRTOS V8.2.3 - Copyright (C) 2015 Real Time Engineers Ltd.
3  All rights reserved
4 
5  VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
6 
7  This file is part of the FreeRTOS distribution.
8 
9  FreeRTOS is free software; you can redistribute it and/or modify it under
10  the terms of the GNU General Public License (version 2) as published by the
11  Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.
12 
13  ***************************************************************************
14  >>! NOTE: The modification to the GPL is included to allow you to !<<
15  >>! distribute a combined work that includes FreeRTOS without being !<<
16  >>! obliged to provide the source code for proprietary components !<<
17  >>! outside of the FreeRTOS kernel. !<<
18  ***************************************************************************
19 
20  FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
21  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
22  FOR A PARTICULAR PURPOSE. Full license text is available on the following
23  link: http://www.freertos.org/a00114.html
24 
25  ***************************************************************************
26  * *
27  * FreeRTOS provides completely free yet professionally developed, *
28  * robust, strictly quality controlled, supported, and cross *
29  * platform software that is more than just the market leader, it *
30  * is the industry's de facto standard. *
31  * *
32  * Help yourself get started quickly while simultaneously helping *
33  * to support the FreeRTOS project by purchasing a FreeRTOS *
34  * tutorial book, reference manual, or both: *
35  * http://www.FreeRTOS.org/Documentation *
36  * *
37  ***************************************************************************
38 
39  http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading
40  the FAQ page "My application does not run, what could be wrong?". Have you
41  defined configASSERT()?
42 
43  http://www.FreeRTOS.org/support - In return for receiving this top quality
44  embedded software for free we request you assist our global community by
45  participating in the support forum.
46 
47  http://www.FreeRTOS.org/training - Investing in training allows your team to
48  be as productive as possible as early as possible. Now you can receive
49  FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
50  Ltd, and the world's leading authority on the world's leading RTOS.
51 
52  http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
53  including FreeRTOS+Trace - an indispensable productivity tool, a DOS
54  compatible FAT file system, and our tiny thread aware UDP/IP stack.
55 
56  http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
57  Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
58 
59  http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
60  Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
61  licenses offer ticketed support, indemnification and commercial middleware.
62 
63  http://www.SafeRTOS.com - High Integrity Systems also provide a safety
64  engineered and independently SIL3 certified version for use in safety and
65  mission critical applications that require provable dependability.
66 
67  1 tab == 4 spaces!
68 */
69 
70 /*-----------------------------------------------------------
71  * Implementation of functions defined in portable.h for the ARM CM4F port.
72  *----------------------------------------------------------*/
73 
74 /* Scheduler includes. */
75 #include "FreeRTOS.h"
76 #include "task.h"
77 
78 #ifndef __VFP_FP__
79  #error This port can only be used when the project options are configured to enable hardware floating point support.
80 #endif
81 
82 #ifndef configSYSTICK_CLOCK_HZ
83  #define configSYSTICK_CLOCK_HZ configCPU_CLOCK_HZ
84  /* Ensure the SysTick is clocked at the same frequency as the core. */
85  #define portNVIC_SYSTICK_CLK_BIT ( 1UL << 2UL )
86 #else
87  /* The way the SysTick is clocked is not modified in case it is not the same
88  as the core. */
89  #define portNVIC_SYSTICK_CLK_BIT ( 0 )
90 #endif
91 
92 /* Constants required to manipulate the core. Registers first... */
93 #define portNVIC_SYSTICK_CTRL_REG ( * ( ( volatile uint32_t * ) 0xe000e010 ) )
94 #define portNVIC_SYSTICK_LOAD_REG ( * ( ( volatile uint32_t * ) 0xe000e014 ) )
95 #define portNVIC_SYSTICK_CURRENT_VALUE_REG ( * ( ( volatile uint32_t * ) 0xe000e018 ) )
96 #define portNVIC_SYSPRI2_REG ( * ( ( volatile uint32_t * ) 0xe000ed20 ) )
97 /* ...then bits in the registers. */
98 #define portNVIC_SYSTICK_INT_BIT ( 1UL << 1UL )
99 #define portNVIC_SYSTICK_ENABLE_BIT ( 1UL << 0UL )
100 #define portNVIC_SYSTICK_COUNT_FLAG_BIT ( 1UL << 16UL )
101 #define portNVIC_PENDSVCLEAR_BIT ( 1UL << 27UL )
102 #define portNVIC_PEND_SYSTICK_CLEAR_BIT ( 1UL << 25UL )
103 
104 #define portNVIC_PENDSV_PRI ( ( ( uint32_t ) configKERNEL_INTERRUPT_PRIORITY ) << 16UL )
105 #define portNVIC_SYSTICK_PRI ( ( ( uint32_t ) configKERNEL_INTERRUPT_PRIORITY ) << 24UL )
106 
107 /* Constants required to check the validity of an interrupt priority. */
108 #define portFIRST_USER_INTERRUPT_NUMBER ( 16 )
109 #define portNVIC_IP_REGISTERS_OFFSET_16 ( 0xE000E3F0 )
110 #define portAIRCR_REG ( * ( ( volatile uint32_t * ) 0xE000ED0C ) )
111 #define portMAX_8_BIT_VALUE ( ( uint8_t ) 0xff )
112 #define portTOP_BIT_OF_BYTE ( ( uint8_t ) 0x80 )
113 #define portMAX_PRIGROUP_BITS ( ( uint8_t ) 7 )
114 #define portPRIORITY_GROUP_MASK ( 0x07UL << 8UL )
115 #define portPRIGROUP_SHIFT ( 8UL )
116 
117 /* Masks off all bits but the VECTACTIVE bits in the ICSR register. */
118 #define portVECTACTIVE_MASK ( 0xFFUL )
119 
120 /* Constants required to manipulate the VFP. */
121 #define portFPCCR ( ( volatile uint32_t * ) 0xe000ef34 ) /* Floating point context control register. */
122 #define portASPEN_AND_LSPEN_BITS ( 0x3UL << 30UL )
123 
124 /* Constants required to set up the initial stack. */
125 #define portINITIAL_XPSR ( 0x01000000 )
126 #define portINITIAL_EXEC_RETURN ( 0xfffffffd )
127 
128 /* The systick is a 24-bit counter. */
129 #define portMAX_24_BIT_NUMBER ( 0xffffffUL )
130 
131 /* A fiddle factor to estimate the number of SysTick counts that would have
132 occurred while the SysTick counter is stopped during tickless idle
133 calculations. */
134 #define portMISSED_COUNTS_FACTOR ( 45UL )
135 
136 /* Let the user override the pre-loading of the initial LR with the address of
137 prvTaskExitError() in case it messes up unwinding of the stack in the
138 debugger. */
139 #ifdef configTASK_RETURN_ADDRESS
140  #define portTASK_RETURN_ADDRESS configTASK_RETURN_ADDRESS
141 #else
142  #define portTASK_RETURN_ADDRESS prvTaskExitError
143 #endif
144 
145 /* Each task maintains its own interrupt status in the critical nesting
146 variable. */
147 static UBaseType_t uxCriticalNesting = 0xaaaaaaaa;
148 
149 /*
150  * Setup the timer to generate the tick interrupts. The implementation in this
151  * file is weak to allow application writers to change the timer used to
152  * generate the tick interrupt.
153  */
154 void vPortSetupTimerInterrupt( void );
155 
156 /*
157  * Exception handlers.
158  */
159 void xPortPendSVHandler( void ) __attribute__ (( naked ));
160 void xPortSysTickHandler( void );
161 void vPortSVCHandler( void ) __attribute__ (( naked ));
162 
163 /*
164  * Start first task is a separate function so it can be tested in isolation.
165  */
166 static void prvPortStartFirstTask( void ) __attribute__ (( naked ));
167 
168 /*
169  * Function to enable the VFP.
170  */
171  static void vPortEnableVFP( void ) __attribute__ (( naked ));
172 
173 /*
174  * Used to catch tasks that attempt to return from their implementing function.
175  */
176 static void prvTaskExitError( void );
177 
178 /*-----------------------------------------------------------*/
179 
180 /*
181  * The number of SysTick increments that make up one tick period.
182  */
183 #if configUSE_TICKLESS_IDLE == 1
184  static uint32_t ulTimerCountsForOneTick = 0;
185 #endif /* configUSE_TICKLESS_IDLE */
186 
187 /*
188  * The maximum number of tick periods that can be suppressed is limited by the
189  * 24 bit resolution of the SysTick timer.
190  */
191 #if configUSE_TICKLESS_IDLE == 1
192  static uint32_t xMaximumPossibleSuppressedTicks = 0;
193 #endif /* configUSE_TICKLESS_IDLE */
194 
195 /*
196  * Compensate for the CPU cycles that pass while the SysTick is stopped (low
197  * power functionality only.
198  */
199 #if configUSE_TICKLESS_IDLE == 1
200  static uint32_t ulStoppedTimerCompensation = 0;
201 #endif /* configUSE_TICKLESS_IDLE */
202 
203 /*
204  * Used by the portASSERT_IF_INTERRUPT_PRIORITY_INVALID() macro to ensure
205  * FreeRTOS API functions are not called from interrupts that have been assigned
206  * a priority above configMAX_SYSCALL_INTERRUPT_PRIORITY.
207  */
208 #if ( configASSERT_DEFINED == 1 )
209  static uint8_t ucMaxSysCallPriority = 0;
210  static uint32_t ulMaxPRIGROUPValue = 0;
211  static const volatile uint8_t * const pcInterruptPriorityRegisters = ( const volatile uint8_t * const ) portNVIC_IP_REGISTERS_OFFSET_16;
212 #endif /* configASSERT_DEFINED */
213 
214 /*-----------------------------------------------------------*/
215 
216 /*
217  * See header file for description.
218  */
219 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
220 {
221  /* Simulate the stack frame as it would be created by a context switch
222  interrupt. */
223 
224  /* Offset added to account for the way the MCU uses the stack on entry/exit
225  of interrupts, and to ensure alignment. */
226  pxTopOfStack--;
227 
228  *pxTopOfStack = portINITIAL_XPSR; /* xPSR */
229  pxTopOfStack--;
230  *pxTopOfStack = ( StackType_t ) pxCode; /* PC */
231  pxTopOfStack--;
232  *pxTopOfStack = ( StackType_t ) portTASK_RETURN_ADDRESS; /* LR */
233 
234  /* Save code space by skipping register initialisation. */
235  pxTopOfStack -= 5; /* R12, R3, R2 and R1. */
236  *pxTopOfStack = ( StackType_t ) pvParameters; /* R0 */
237 
238  /* A save method is being used that requires each task to maintain its
239  own exec return value. */
240  pxTopOfStack--;
241  *pxTopOfStack = portINITIAL_EXEC_RETURN;
242 
243  pxTopOfStack -= 8; /* R11, R10, R9, R8, R7, R6, R5 and R4. */
244 
245  return pxTopOfStack;
246 }
247 /*-----------------------------------------------------------*/
248 
249 static void prvTaskExitError( void )
250 {
251  /* A function that implements a task must not exit or attempt to return to
252  its caller as there is nothing to return to. If a task wants to exit it
253  should instead call vTaskDelete( NULL ).
254 
255  Artificially force an assert() to be triggered if configASSERT() is
256  defined, then stop here so application writers can catch the error. */
257  configASSERT( uxCriticalNesting == ~0UL );
259  for( ;; );
260 }
261 /*-----------------------------------------------------------*/
262 
263 void vPortSVCHandler( void )
264 {
265  __asm volatile (
266  " ldr r3, pxCurrentTCBConst2 \n" /* Restore the context. */
267  " ldr r1, [r3] \n" /* Use pxCurrentTCBConst to get the pxCurrentTCB address. */
268  " ldr r0, [r1] \n" /* The first item in pxCurrentTCB is the task top of stack. */
269  " ldmia r0!, {r4-r11, r14} \n" /* Pop the registers that are not automatically saved on exception entry and the critical nesting count. */
270  " msr psp, r0 \n" /* Restore the task stack pointer. */
271  " isb \n"
272  " mov r0, #0 \n"
273  " msr basepri, r0 \n"
274  " bx r14 \n"
275  " \n"
276  " .align 2 \n"
277  "pxCurrentTCBConst2: .word pxCurrentTCB \n"
278  );
279 }
280 /*-----------------------------------------------------------*/
281 
282 static void prvPortStartFirstTask( void )
283 {
284  __asm volatile(
285  " ldr r0, =0xE000ED08 \n" /* Use the NVIC offset register to locate the stack. */
286  " ldr r0, [r0] \n"
287  " ldr r0, [r0] \n"
288  " msr msp, r0 \n" /* Set the msp back to the start of the stack. */
289  " cpsie i \n" /* Globally enable interrupts. */
290  " cpsie f \n"
291  " dsb \n"
292  " isb \n"
293  " svc 0 \n" /* System call to start first task. */
294  " nop \n"
295  );
296 }
297 /*-----------------------------------------------------------*/
298 
299 /*
300  * See header file for description.
301  */
303 {
304  /* configMAX_SYSCALL_INTERRUPT_PRIORITY must not be set to 0.
305  See http://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
307 
308  #if( configASSERT_DEFINED == 1 )
309  {
310  volatile uint32_t ulOriginalPriority;
311  volatile uint8_t * const pucFirstUserPriorityRegister = ( volatile uint8_t * const ) ( portNVIC_IP_REGISTERS_OFFSET_16 + portFIRST_USER_INTERRUPT_NUMBER );
312  volatile uint8_t ucMaxPriorityValue;
313 
314  /* Determine the maximum priority from which ISR safe FreeRTOS API
315  functions can be called. ISR safe functions are those that end in
316  "FromISR". FreeRTOS maintains separate thread and ISR API functions to
317  ensure interrupt entry is as fast and simple as possible.
318 
319  Save the interrupt priority value that is about to be clobbered. */
320  ulOriginalPriority = *pucFirstUserPriorityRegister;
321 
322  /* Determine the number of priority bits available. First write to all
323  possible bits. */
324  *pucFirstUserPriorityRegister = portMAX_8_BIT_VALUE;
325 
326  /* Read the value back to see how many bits stuck. */
327  ucMaxPriorityValue = *pucFirstUserPriorityRegister;
328 
329  /* Use the same mask on the maximum system call priority. */
330  ucMaxSysCallPriority = configMAX_SYSCALL_INTERRUPT_PRIORITY & ucMaxPriorityValue;
331 
332  /* Calculate the maximum acceptable priority group value for the number
333  of bits read back. */
334  ulMaxPRIGROUPValue = portMAX_PRIGROUP_BITS;
335  while( ( ucMaxPriorityValue & portTOP_BIT_OF_BYTE ) == portTOP_BIT_OF_BYTE )
336  {
337  ulMaxPRIGROUPValue--;
338  ucMaxPriorityValue <<= ( uint8_t ) 0x01;
339  }
340 
341  /* Shift the priority group value back to its position within the AIRCR
342  register. */
343  ulMaxPRIGROUPValue <<= portPRIGROUP_SHIFT;
344  ulMaxPRIGROUPValue &= portPRIORITY_GROUP_MASK;
345 
346  /* Restore the clobbered interrupt priority register to its original
347  value. */
348  *pucFirstUserPriorityRegister = ulOriginalPriority;
349  }
350  #endif /* conifgASSERT_DEFINED */
351 
352  /* Make PendSV and SysTick the lowest priority interrupts. */
355 
356  /* Start the timer that generates the tick ISR. Interrupts are disabled
357  here already. */
359 
360  /* Initialise the critical nesting count ready for the first task. */
361  uxCriticalNesting = 0;
362 
363  /* Ensure the VFP is enabled - it should be anyway. */
364  vPortEnableVFP();
365 
366  /* Lazy save always. */
368 
369  /* Start the first task. */
370  prvPortStartFirstTask();
371 
372  /* Should never get here as the tasks will now be executing! Call the task
373  exit error function to prevent compiler warnings about a static function
374  not being called in the case that the application writer overrides this
375  functionality by defining configTASK_RETURN_ADDRESS. */
376  prvTaskExitError();
377 
378  /* Should not get here! */
379  return 0;
380 }
381 /*-----------------------------------------------------------*/
382 
383 void vPortEndScheduler( void )
384 {
385  /* Not implemented in ports where there is nothing to return to.
386  Artificially force an assert. */
387  configASSERT( uxCriticalNesting == 1000UL );
388 }
389 /*-----------------------------------------------------------*/
390 
391 void vPortEnterCritical( void )
392 {
394  uxCriticalNesting++;
395 
396  /* This is not the interrupt safe version of the enter critical function so
397  assert() if it is being called from an interrupt context. Only API
398  functions that end in "FromISR" can be used in an interrupt. Only assert if
399  the critical nesting count is 1 to protect against recursive calls if the
400  assert function also uses a critical section. */
401  if( uxCriticalNesting == 1 )
402  {
404  }
405 }
406 /*-----------------------------------------------------------*/
407 
408 void vPortExitCritical( void )
409 {
410  configASSERT( uxCriticalNesting );
411  uxCriticalNesting--;
412  if( uxCriticalNesting == 0 )
413  {
415  }
416 }
417 /*-----------------------------------------------------------*/
418 
419 void xPortPendSVHandler( void )
420 {
421  /* This is a naked function. */
422 
423  __asm volatile
424  (
425  " mrs r0, psp \n"
426  " isb \n"
427  " \n"
428  " ldr r3, pxCurrentTCBConst \n" /* Get the location of the current TCB. */
429  " ldr r2, [r3] \n"
430  " \n"
431  " tst r14, #0x10 \n" /* Is the task using the FPU context? If so, push high vfp registers. */
432  " it eq \n"
433  " vstmdbeq r0!, {s16-s31} \n"
434  " \n"
435  " stmdb r0!, {r4-r11, r14} \n" /* Save the core registers. */
436  " \n"
437  " str r0, [r2] \n" /* Save the new top of stack into the first member of the TCB. */
438  " \n"
439  " stmdb sp!, {r3} \n"
440  " mov r0, %0 \n"
441  " cpsid i \n" /* Errata workaround. */
442  " msr basepri, r0 \n"
443  " dsb \n"
444  " isb \n"
445  " cpsie i \n" /* Errata workaround. */
446  " bl vTaskSwitchContext \n"
447  " mov r0, #0 \n"
448  " msr basepri, r0 \n"
449  " ldmia sp!, {r3} \n"
450  " \n"
451  " ldr r1, [r3] \n" /* The first item in pxCurrentTCB is the task top of stack. */
452  " ldr r0, [r1] \n"
453  " \n"
454  " ldmia r0!, {r4-r11, r14} \n" /* Pop the core registers. */
455  " \n"
456  " tst r14, #0x10 \n" /* Is the task using the FPU context? If so, pop the high vfp registers too. */
457  " it eq \n"
458  " vldmiaeq r0!, {s16-s31} \n"
459  " \n"
460  " msr psp, r0 \n"
461  " isb \n"
462  " \n"
463  #ifdef WORKAROUND_PMU_CM001 /* XMC4000 specific errata workaround. */
464  #if WORKAROUND_PMU_CM001 == 1
465  " push { r14 } \n"
466  " pop { pc } \n"
467  #endif
468  #endif
469  " \n"
470  " bx r14 \n"
471  " \n"
472  " .align 2 \n"
473  "pxCurrentTCBConst: .word pxCurrentTCB \n"
475  );
476 }
477 /*-----------------------------------------------------------*/
478 
480 {
481  /* The SysTick runs at the lowest interrupt priority, so when this interrupt
482  executes all interrupts must be unmasked. There is therefore no need to
483  save and then restore the interrupt mask value as its value is already
484  known. */
486  {
487  /* Increment the RTOS tick. */
488  if( xTaskIncrementTick() != pdFALSE )
489  {
490  /* A context switch is required. Context switching is performed in
491  the PendSV interrupt. Pend the PendSV interrupt. */
493  }
494  }
496 }
497 /*-----------------------------------------------------------*/
498 
499 #if configUSE_TICKLESS_IDLE == 1
500 
501  __attribute__((weak)) void vPortSuppressTicksAndSleep( TickType_t xExpectedIdleTime )
502  {
503  uint32_t ulReloadValue, ulCompleteTickPeriods, ulCompletedSysTickDecrements, ulSysTickCTRL;
504  TickType_t xModifiableIdleTime;
505 
506  /* Make sure the SysTick reload value does not overflow the counter. */
507  if( xExpectedIdleTime > xMaximumPossibleSuppressedTicks )
508  {
509  xExpectedIdleTime = xMaximumPossibleSuppressedTicks;
510  }
511 
512  /* Stop the SysTick momentarily. The time the SysTick is stopped for
513  is accounted for as best it can be, but using the tickless mode will
514  inevitably result in some tiny drift of the time maintained by the
515  kernel with respect to calendar time. */
517 
518  /* Calculate the reload value required to wait xExpectedIdleTime
519  tick periods. -1 is used because this code will execute part way
520  through one of the tick periods. */
521  ulReloadValue = portNVIC_SYSTICK_CURRENT_VALUE_REG + ( ulTimerCountsForOneTick * ( xExpectedIdleTime - 1UL ) );
522  if( ulReloadValue > ulStoppedTimerCompensation )
523  {
524  ulReloadValue -= ulStoppedTimerCompensation;
525  }
526 
527  /* Enter a critical section but don't use the taskENTER_CRITICAL()
528  method as that will mask interrupts that should exit sleep mode. */
529  __asm volatile( "cpsid i" );
530 
531  /* If a context switch is pending or a task is waiting for the scheduler
532  to be unsuspended then abandon the low power entry. */
534  {
535  /* Restart from whatever is left in the count register to complete
536  this tick period. */
538 
539  /* Restart SysTick. */
541 
542  /* Reset the reload register to the value required for normal tick
543  periods. */
544  portNVIC_SYSTICK_LOAD_REG = ulTimerCountsForOneTick - 1UL;
545 
546  /* Re-enable interrupts - see comments above the cpsid instruction()
547  above. */
548  __asm volatile( "cpsie i" );
549  }
550  else
551  {
552  /* Set the new reload value. */
553  portNVIC_SYSTICK_LOAD_REG = ulReloadValue;
554 
555  /* Clear the SysTick count flag and set the count value back to
556  zero. */
558 
559  /* Restart SysTick. */
561 
562  /* Sleep until something happens. configPRE_SLEEP_PROCESSING() can
563  set its parameter to 0 to indicate that its implementation contains
564  its own wait for interrupt or wait for event instruction, and so wfi
565  should not be executed again. However, the original expected idle
566  time variable must remain unmodified, so a copy is taken. */
567  xModifiableIdleTime = xExpectedIdleTime;
568  configPRE_SLEEP_PROCESSING( &xModifiableIdleTime );
569  if( xModifiableIdleTime > 0 )
570  {
571  __asm volatile( "dsb" );
572  __asm volatile( "wfi" );
573  __asm volatile( "isb" );
574  }
575  configPOST_SLEEP_PROCESSING( &xExpectedIdleTime );
576 
577  /* Stop SysTick. Again, the time the SysTick is stopped for is
578  accounted for as best it can be, but using the tickless mode will
579  inevitably result in some tiny drift of the time maintained by the
580  kernel with respect to calendar time. */
581  ulSysTickCTRL = portNVIC_SYSTICK_CTRL_REG;
583 
584  /* Re-enable interrupts - see comments above the cpsid instruction()
585  above. */
586  __asm volatile( "cpsie i" );
587 
588  if( ( ulSysTickCTRL & portNVIC_SYSTICK_COUNT_FLAG_BIT ) != 0 )
589  {
590  uint32_t ulCalculatedLoadValue;
591 
592  /* The tick interrupt has already executed, and the SysTick
593  count reloaded with ulReloadValue. Reset the
594  portNVIC_SYSTICK_LOAD_REG with whatever remains of this tick
595  period. */
596  ulCalculatedLoadValue = ( ulTimerCountsForOneTick - 1UL ) - ( ulReloadValue - portNVIC_SYSTICK_CURRENT_VALUE_REG );
597 
598  /* Don't allow a tiny value, or values that have somehow
599  underflowed because the post sleep hook did something
600  that took too long. */
601  if( ( ulCalculatedLoadValue < ulStoppedTimerCompensation ) || ( ulCalculatedLoadValue > ulTimerCountsForOneTick ) )
602  {
603  ulCalculatedLoadValue = ( ulTimerCountsForOneTick - 1UL );
604  }
605 
606  portNVIC_SYSTICK_LOAD_REG = ulCalculatedLoadValue;
607 
608  /* The tick interrupt handler will already have pended the tick
609  processing in the kernel. As the pending tick will be
610  processed as soon as this function exits, the tick value
611  maintained by the tick is stepped forward by one less than the
612  time spent waiting. */
613  ulCompleteTickPeriods = xExpectedIdleTime - 1UL;
614  }
615  else
616  {
617  /* Something other than the tick interrupt ended the sleep.
618  Work out how long the sleep lasted rounded to complete tick
619  periods (not the ulReload value which accounted for part
620  ticks). */
621  ulCompletedSysTickDecrements = ( xExpectedIdleTime * ulTimerCountsForOneTick ) - portNVIC_SYSTICK_CURRENT_VALUE_REG;
622 
623  /* How many complete tick periods passed while the processor
624  was waiting? */
625  ulCompleteTickPeriods = ulCompletedSysTickDecrements / ulTimerCountsForOneTick;
626 
627  /* The reload value is set to whatever fraction of a single tick
628  period remains. */
629  portNVIC_SYSTICK_LOAD_REG = ( ( ulCompleteTickPeriods + 1 ) * ulTimerCountsForOneTick ) - ulCompletedSysTickDecrements;
630  }
631 
632  /* Restart SysTick so it runs from portNVIC_SYSTICK_LOAD_REG
633  again, then set portNVIC_SYSTICK_LOAD_REG back to its standard
634  value. The critical section is used to ensure the tick interrupt
635  can only execute once in the case that the reload register is near
636  zero. */
639  {
641  vTaskStepTick( ulCompleteTickPeriods );
642  portNVIC_SYSTICK_LOAD_REG = ulTimerCountsForOneTick - 1UL;
643  }
645  }
646  }
647 
648 #endif /* #if configUSE_TICKLESS_IDLE */
649 /*-----------------------------------------------------------*/
650 
651 /*
652  * Setup the systick timer to generate the tick interrupts at the required
653  * frequency.
654  */
656 {
657  /* Calculate the constants required to configure the tick interrupt. */
658  #if configUSE_TICKLESS_IDLE == 1
659  {
660  ulTimerCountsForOneTick = ( configSYSTICK_CLOCK_HZ / configTICK_RATE_HZ );
661  xMaximumPossibleSuppressedTicks = portMAX_24_BIT_NUMBER / ulTimerCountsForOneTick;
662  ulStoppedTimerCompensation = portMISSED_COUNTS_FACTOR / ( configCPU_CLOCK_HZ / configSYSTICK_CLOCK_HZ );
663  }
664  #endif /* configUSE_TICKLESS_IDLE */
665 
666  /* Configure SysTick to interrupt at the requested rate. */
669 }
670 /*-----------------------------------------------------------*/
671 
672 /* This is a naked function. */
673 static void vPortEnableVFP( void )
674 {
675  __asm volatile
676  (
677  " ldr.w r0, =0xE000ED88 \n" /* The FPU enable bits are in the CPACR. */
678  " ldr r1, [r0] \n"
679  " \n"
680  " orr r1, r1, #( 0xf << 20 ) \n" /* Enable CP10 and CP11 coprocessors, then save back. */
681  " str r1, [r0] \n"
682  " bx r14 "
683  );
684 }
685 /*-----------------------------------------------------------*/
686 
687 #if( configASSERT_DEFINED == 1 )
688 
689  void vPortValidateInterruptPriority( void )
690  {
691  uint32_t ulCurrentInterrupt;
692  uint8_t ucCurrentPriority;
693 
694  /* Obtain the number of the currently executing interrupt. */
695  __asm volatile( "mrs %0, ipsr" : "=r"( ulCurrentInterrupt ) );
696 
697  /* Is the interrupt number a user defined interrupt? */
698  if( ulCurrentInterrupt >= portFIRST_USER_INTERRUPT_NUMBER )
699  {
700  /* Look up the interrupt's priority. */
701  ucCurrentPriority = pcInterruptPriorityRegisters[ ulCurrentInterrupt ];
702 
703  /* The following assertion will fail if a service routine (ISR) for
704  an interrupt that has been assigned a priority above
705  configMAX_SYSCALL_INTERRUPT_PRIORITY calls an ISR safe FreeRTOS API
706  function. ISR safe FreeRTOS API functions must *only* be called
707  from interrupts that have been assigned a priority at or below
708  configMAX_SYSCALL_INTERRUPT_PRIORITY.
709 
710  Numerically low interrupt priority numbers represent logically high
711  interrupt priorities, therefore the priority of the interrupt must
712  be set to a value equal to or numerically *higher* than
713  configMAX_SYSCALL_INTERRUPT_PRIORITY.
714 
715  Interrupts that use the FreeRTOS API must not be left at their
716  default priority of zero as that is the highest possible priority,
717  which is guaranteed to be above configMAX_SYSCALL_INTERRUPT_PRIORITY,
718  and therefore also guaranteed to be invalid.
719 
720  FreeRTOS maintains separate thread and ISR API functions to ensure
721  interrupt entry is as fast and simple as possible.
722 
723  The following links provide detailed information:
724  http://www.freertos.org/RTOS-Cortex-M3-M4.html
725  http://www.freertos.org/FAQHelp.html */
726  configASSERT( ucCurrentPriority >= ucMaxSysCallPriority );
727  }
728 
729  /* Priority grouping: The interrupt controller (NVIC) allows the bits
730  that define each interrupt's priority to be split between bits that
731  define the interrupt's pre-emption priority bits and bits that define
732  the interrupt's sub-priority. For simplicity all bits must be defined
733  to be pre-emption priority bits. The following assertion will fail if
734  this is not the case (if some bits represent a sub-priority).
735 
736  If the application only uses CMSIS libraries for interrupt
737  configuration then the correct setting can be achieved on all Cortex-M
738  devices by calling NVIC_SetPriorityGrouping( 0 ); before starting the
739  scheduler. Note however that some vendor specific peripheral libraries
740  assume a non-zero priority group setting, in which cases using a value
741  of zero will result in unpredicable behaviour. */
742  configASSERT( ( portAIRCR_REG & portPRIORITY_GROUP_MASK ) <= ulMaxPRIGROUPValue );
743  }
744 
745 #endif /* configASSERT_DEFINED */
746 
747 
#define portNVIC_SYSTICK_CLK_BIT
Definition: port.c:85
BaseType_t xPortStartScheduler(void)
Definition: port.c:302
void xPortPendSVHandler(void xPortSysTickHandler void)
Definition: port.c:159
#define portMISSED_COUNTS_FACTOR
Definition: port.c:134
void xPortSysTickHandler(void)
Definition: port.c:479
#define portENABLE_INTERRUPTS()
Definition: portmacro.h:144
#define configASSERT(x)
#define portDISABLE_INTERRUPTS()
Definition: portmacro.h:143
#define portPRIGROUP_SHIFT
Definition: port.c:115
#define portCLEAR_INTERRUPT_MASK_FROM_ISR(uxSavedStatusValue)
Definition: FreeRTOS.h:301
__attribute__((weak))
Definition: port.c:655
#define portTOP_BIT_OF_BYTE
Definition: port.c:112
void vPortEnterCritical(void)
Definition: port.c:391
#define portNVIC_SYSPRI2_REG
Definition: port.c:96
void vPortSVCHandler(void)
Definition: port.c:263
unsigned long UBaseType_t
Definition: portmacro.h:99
void vPortEndScheduler(void)
Definition: port.c:383
#define portNVIC_SYSTICK_PRI
Definition: port.c:105
void vPortSuppressTicksAndSleep(TickType_t xExpectedIdleTime)
uint32_t TickType_t
Definition: portmacro.h:105
#define portINITIAL_XPSR
Definition: port.c:125
void vTaskStepTick(const TickType_t xTicksToJump) PRIVILEGED_FUNCTION
#define portSET_INTERRUPT_MASK_FROM_ISR()
Definition: FreeRTOS.h:297
#define configPOST_SLEEP_PROCESSING(x)
Definition: FreeRTOS.h:716
#define portPRIORITY_GROUP_MASK
Definition: port.c:114
StackType_t * pxPortInitialiseStack(StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters) PRIVILEGED_FUNCTION
#define configCPU_CLOCK_HZ
#define portNVIC_PENDSV_PRI
Definition: port.c:104
#define configMAX_SYSCALL_INTERRUPT_PRIORITY
#define portAIRCR_REG
Definition: port.c:110
#define portNVIC_SYSTICK_LOAD_REG
Definition: port.c:94
#define portNVIC_SYSTICK_CURRENT_VALUE_REG
Definition: port.c:95
#define configTICK_RATE_HZ
#define portMAX_PRIGROUP_BITS
Definition: port.c:113
long BaseType_t
Definition: portmacro.h:98
#define portFPCCR
Definition: port.c:121
#define portEXIT_CRITICAL()
Definition: portmacro.h:146
#define portFIRST_USER_INTERRUPT_NUMBER
Definition: port.c:108
#define portVECTACTIVE_MASK
Definition: port.c:118
#define configPRE_SLEEP_PROCESSING(x)
Definition: FreeRTOS.h:712
#define configSYSTICK_CLOCK_HZ
Definition: port.c:83
#define portTASK_RETURN_ADDRESS
Definition: port.c:142
#define pdFALSE
Definition: projdefs.h:82
BaseType_t xTaskIncrementTick(void) PRIVILEGED_FUNCTION
Definition: tasks.c:1939
#define portENTER_CRITICAL()
Definition: portmacro.h:145
#define portNVIC_SYSTICK_INT_BIT
Definition: port.c:98
void vPortSetupTimerInterrupt(void)
eSleepModeStatus eTaskConfirmSleepModeStatus(void) PRIVILEGED_FUNCTION
#define portNVIC_SYSTICK_COUNT_FLAG_BIT
Definition: port.c:100
#define portINITIAL_EXEC_RETURN
Definition: port.c:126
#define portNVIC_SYSTICK_CTRL_REG
Definition: port.c:93
#define portNVIC_IP_REGISTERS_OFFSET_16
Definition: port.c:109
#define portMAX_8_BIT_VALUE
Definition: port.c:111
#define portNVIC_SYSTICK_ENABLE_BIT
Definition: port.c:99
#define portMAX_24_BIT_NUMBER
Definition: port.c:129
void(* TaskFunction_t)(void *)
Definition: projdefs.h:77
#define portASPEN_AND_LSPEN_BITS
Definition: port.c:122
portSTACK_TYPE StackType_t
Definition: portmacro.h:97
#define portNVIC_PENDSVSET_BIT
Definition: portmacro.h:133
#define portNVIC_INT_CTRL_REG
Definition: portmacro.h:132
void vPortExitCritical(void)
Definition: port.c:408