port.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /*
  2. * FreeRTOS Kernel V10.4.6
  3. * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * SPDX-License-Identifier: MIT
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  8. * this software and associated documentation files (the "Software"), to deal in
  9. * the Software without restriction, including without limitation the rights to
  10. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  11. * the Software, and to permit persons to whom the Software is furnished to do so,
  12. * subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in all
  15. * copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  19. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  20. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  21. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  22. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. *
  24. * https://www.FreeRTOS.org
  25. * https://github.com/FreeRTOS
  26. *
  27. */
  28. /*-----------------------------------------------------------
  29. * Implementation of functions defined in portable.h for the PIC32MX port.
  30. *----------------------------------------------------------*/
  31. #ifndef __XC
  32. #error This port is designed to work with XC32. Please update your C compiler version.
  33. #endif
  34. /* Scheduler include files. */
  35. #include "FreeRTOS.h"
  36. #include "task.h"
  37. /* Hardware specifics. */
  38. #define portTIMER_PRESCALE 8
  39. #define portPRESCALE_BITS 1
  40. /* Bits within various registers. */
  41. #define portIE_BIT ( 0x00000001 )
  42. #define portEXL_BIT ( 0x00000002 )
  43. /* Bits within the CAUSE register. */
  44. #define portCORE_SW_0 ( 0x00000100 )
  45. #define portCORE_SW_1 ( 0x00000200 )
  46. /* The EXL bit is set to ensure interrupts do not occur while the context of
  47. the first task is being restored. */
  48. #define portINITIAL_SR ( portIE_BIT | portEXL_BIT )
  49. /*
  50. By default port.c generates its tick interrupt from TIMER1. The user can
  51. override this behaviour by:
  52. 1: Providing their own implementation of vApplicationSetupTickTimerInterrupt(),
  53. which is the function that configures the timer. The function is defined
  54. as a weak symbol in this file so if the same function name is used in the
  55. application code then the version in the application code will be linked
  56. into the application in preference to the version defined in this file.
  57. 2: Define configTICK_INTERRUPT_VECTOR to the vector number of the timer used
  58. to generate the tick interrupt. For example, when timer 1 is used then
  59. configTICK_INTERRUPT_VECTOR is set to _TIMER_1_VECTOR.
  60. configTICK_INTERRUPT_VECTOR should be defined in FreeRTOSConfig.h.
  61. 3: Define configCLEAR_TICK_TIMER_INTERRUPT() to clear the interrupt in the
  62. timer used to generate the tick interrupt. For example, when timer 1 is
  63. used configCLEAR_TICK_TIMER_INTERRUPT() is defined to
  64. IFS0CLR = _IFS0_T1IF_MASK.
  65. */
  66. #ifndef configTICK_INTERRUPT_VECTOR
  67. #define configTICK_INTERRUPT_VECTOR _TIMER_1_VECTOR
  68. #define configCLEAR_TICK_TIMER_INTERRUPT() IFS0CLR = _IFS0_T1IF_MASK
  69. #else
  70. #ifndef configCLEAR_TICK_TIMER_INTERRUPT
  71. #error If configTICK_INTERRUPT_VECTOR is defined in application code then configCLEAR_TICK_TIMER_INTERRUPT must also be defined in application code.
  72. #endif
  73. #endif
  74. /* Let the user override the pre-loading of the initial RA with the address of
  75. prvTaskExitError() in case it messes up unwinding of the stack in the
  76. debugger - in which case configTASK_RETURN_ADDRESS can be defined as 0 (NULL). */
  77. #ifdef configTASK_RETURN_ADDRESS
  78. #define portTASK_RETURN_ADDRESS configTASK_RETURN_ADDRESS
  79. #else
  80. #define portTASK_RETURN_ADDRESS prvTaskExitError
  81. #endif
  82. /* Set configCHECK_FOR_STACK_OVERFLOW to 3 to add ISR stack checking to task
  83. stack checking. A problem in the ISR stack will trigger an assert, not call the
  84. stack overflow hook function (because the stack overflow hook is specific to a
  85. task stack, not the ISR stack). */
  86. #if( configCHECK_FOR_STACK_OVERFLOW > 2 )
  87. /* Don't use 0xa5 as the stack fill bytes as that is used by the kernerl for
  88. the task stacks, and so will legitimately appear in many positions within
  89. the ISR stack. */
  90. #define portISR_STACK_FILL_BYTE 0xee
  91. static const uint8_t ucExpectedStackBytes[] = {
  92. portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, \
  93. portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, \
  94. portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, \
  95. portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, \
  96. portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE }; \
  97. #define portCHECK_ISR_STACK() configASSERT( ( memcmp( ( void * ) xISRStack, ( void * ) ucExpectedStackBytes, sizeof( ucExpectedStackBytes ) ) == 0 ) )
  98. #else
  99. /* Define the function away. */
  100. #define portCHECK_ISR_STACK()
  101. #endif /* configCHECK_FOR_STACK_OVERFLOW > 2 */
  102. /*-----------------------------------------------------------*/
  103. /*
  104. * Place the prototype here to ensure the interrupt vector is correctly installed.
  105. * Note that because the interrupt is written in assembly, the IPL setting in the
  106. * following line of code has no effect. The interrupt priority is set by the
  107. * call to ConfigIntTimer1() in vApplicationSetupTickTimerInterrupt().
  108. */
  109. extern void __attribute__( (interrupt(IPL1AUTO), vector( configTICK_INTERRUPT_VECTOR ))) vPortTickInterruptHandler( void );
  110. /*
  111. * The software interrupt handler that performs the yield. Note that, because
  112. * the interrupt is written in assembly, the IPL setting in the following line of
  113. * code has no effect. The interrupt priority is set by the call to
  114. * mConfigIntCoreSW0() in xPortStartScheduler().
  115. */
  116. void __attribute__( (interrupt(IPL1AUTO), vector(_CORE_SOFTWARE_0_VECTOR))) vPortYieldISR( void );
  117. /*
  118. * Used to catch tasks that attempt to return from their implementing function.
  119. */
  120. static void prvTaskExitError( void );
  121. /*-----------------------------------------------------------*/
  122. /* Records the interrupt nesting depth. This is initialised to one as it is
  123. decremented to 0 when the first task starts. */
  124. volatile UBaseType_t uxInterruptNesting = 0x01;
  125. /* Stores the task stack pointer when a switch is made to use the system stack. */
  126. UBaseType_t uxSavedTaskStackPointer = 0;
  127. /* The stack used by interrupt service routines that cause a context switch. */
  128. __attribute__ ((aligned(8))) StackType_t xISRStack[ configISR_STACK_SIZE ] = { 0 };
  129. /* The top of stack value ensures there is enough space to store 6 registers on
  130. the callers stack, as some functions seem to want to do this. */
  131. const StackType_t * const xISRStackTop = &( xISRStack[ ( configISR_STACK_SIZE & ~portBYTE_ALIGNMENT_MASK ) - 8 ] );
  132. /*-----------------------------------------------------------*/
  133. /*
  134. * See header file for description.
  135. */
  136. StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
  137. {
  138. /* Ensure 8 byte alignment is maintained when the context is popped from
  139. * stack. The size of the context is 33 words (132 bytes). */
  140. pxTopOfStack--;
  141. pxTopOfStack--;
  142. *pxTopOfStack = (StackType_t) 0xDEADBEEF;
  143. pxTopOfStack--;
  144. *pxTopOfStack = (StackType_t) 0x12345678; /* Word to which the stack pointer will be left pointing after context restore. */
  145. pxTopOfStack--;
  146. *pxTopOfStack = (StackType_t) _CP0_GET_CAUSE();
  147. pxTopOfStack--;
  148. *pxTopOfStack = (StackType_t) portINITIAL_SR;/* CP0_STATUS */
  149. pxTopOfStack--;
  150. *pxTopOfStack = (StackType_t) pxCode; /* CP0_EPC */
  151. pxTopOfStack--;
  152. *pxTopOfStack = (StackType_t) portTASK_RETURN_ADDRESS; /* ra */
  153. pxTopOfStack -= 15;
  154. *pxTopOfStack = (StackType_t) pvParameters; /* Parameters to pass in. */
  155. pxTopOfStack -= 15;
  156. return pxTopOfStack;
  157. }
  158. /*-----------------------------------------------------------*/
  159. static void prvTaskExitError( void )
  160. {
  161. /* A function that implements a task must not exit or attempt to return to
  162. its caller as there is nothing to return to. If a task wants to exit it
  163. should instead call vTaskDelete( NULL ).
  164. Artificially force an assert() to be triggered if configASSERT() is
  165. defined, then stop here so application writers can catch the error. */
  166. configASSERT( uxSavedTaskStackPointer == 0UL );
  167. portDISABLE_INTERRUPTS();
  168. for( ;; );
  169. }
  170. /*-----------------------------------------------------------*/
  171. /*
  172. * Setup a timer for a regular tick. This function uses peripheral timer 1.
  173. * The function is declared weak so an application writer can use a different
  174. * timer by redefining this implementation. If a different timer is used then
  175. * configTICK_INTERRUPT_VECTOR must also be defined in FreeRTOSConfig.h to
  176. * ensure the RTOS provided tick interrupt handler is installed on the correct
  177. * vector number. When Timer 1 is used the vector number is defined as
  178. * _TIMER_1_VECTOR.
  179. */
  180. __attribute__(( weak )) void vApplicationSetupTickTimerInterrupt( void )
  181. {
  182. const uint32_t ulCompareMatch = ( (configPERIPHERAL_CLOCK_HZ / portTIMER_PRESCALE) / configTICK_RATE_HZ ) - 1;
  183. T1CON = 0x0000;
  184. T1CONbits.TCKPS = portPRESCALE_BITS;
  185. PR1 = ulCompareMatch;
  186. IPC1bits.T1IP = configKERNEL_INTERRUPT_PRIORITY;
  187. /* Clear the interrupt as a starting condition. */
  188. IFS0bits.T1IF = 0;
  189. /* Enable the interrupt. */
  190. IEC0bits.T1IE = 1;
  191. /* Start the timer. */
  192. T1CONbits.TON = 1;
  193. }
  194. /*-----------------------------------------------------------*/
  195. void vPortEndScheduler(void)
  196. {
  197. /* Not implemented in ports where there is nothing to return to.
  198. Artificially force an assert. */
  199. configASSERT( uxInterruptNesting == 1000UL );
  200. }
  201. /*-----------------------------------------------------------*/
  202. BaseType_t xPortStartScheduler( void )
  203. {
  204. extern void vPortStartFirstTask( void );
  205. extern void *pxCurrentTCB;
  206. #if ( configCHECK_FOR_STACK_OVERFLOW > 2 )
  207. {
  208. /* Fill the ISR stack to make it easy to asses how much is being used. */
  209. memset( ( void * ) xISRStack, portISR_STACK_FILL_BYTE, sizeof( xISRStack ) );
  210. }
  211. #endif /* configCHECK_FOR_STACK_OVERFLOW > 2 */
  212. /* Clear the software interrupt flag. */
  213. IFS0CLR = _IFS0_CS0IF_MASK;
  214. /* Set software timer priority. */
  215. IPC0CLR = _IPC0_CS0IP_MASK;
  216. IPC0SET = ( configKERNEL_INTERRUPT_PRIORITY << _IPC0_CS0IP_POSITION );
  217. /* Enable software interrupt. */
  218. IEC0CLR = _IEC0_CS0IE_MASK;
  219. IEC0SET = 1 << _IEC0_CS0IE_POSITION;
  220. /* Setup the timer to generate the tick. Interrupts will have been
  221. disabled by the time we get here. */
  222. vApplicationSetupTickTimerInterrupt();
  223. /* Kick off the highest priority task that has been created so far.
  224. Its stack location is loaded into uxSavedTaskStackPointer. */
  225. uxSavedTaskStackPointer = *( UBaseType_t * ) pxCurrentTCB;
  226. vPortStartFirstTask();
  227. /* Should never get here as the tasks will now be executing! Call the task
  228. exit error function to prevent compiler warnings about a static function
  229. not being called in the case that the application writer overrides this
  230. functionality by defining configTASK_RETURN_ADDRESS. */
  231. prvTaskExitError();
  232. return pdFALSE;
  233. }
  234. /*-----------------------------------------------------------*/
  235. void vPortIncrementTick( void )
  236. {
  237. UBaseType_t uxSavedStatus;
  238. uxSavedStatus = uxPortSetInterruptMaskFromISR();
  239. {
  240. if( xTaskIncrementTick() != pdFALSE )
  241. {
  242. /* Pend a context switch. */
  243. _CP0_BIS_CAUSE( portCORE_SW_0 );
  244. }
  245. }
  246. vPortClearInterruptMaskFromISR( uxSavedStatus );
  247. /* Look for the ISR stack getting near or past its limit. */
  248. portCHECK_ISR_STACK();
  249. /* Clear timer interrupt. */
  250. configCLEAR_TICK_TIMER_INTERRUPT();
  251. }
  252. /*-----------------------------------------------------------*/
  253. UBaseType_t uxPortSetInterruptMaskFromISR( void )
  254. {
  255. UBaseType_t uxSavedStatusRegister;
  256. __builtin_disable_interrupts();
  257. uxSavedStatusRegister = _CP0_GET_STATUS() | 0x01;
  258. /* This clears the IPL bits, then sets them to
  259. configMAX_SYSCALL_INTERRUPT_PRIORITY. This function should not be called
  260. from an interrupt that has a priority above
  261. configMAX_SYSCALL_INTERRUPT_PRIORITY so, when used correctly, the action
  262. can only result in the IPL being unchanged or raised, and therefore never
  263. lowered. */
  264. _CP0_SET_STATUS( ( ( uxSavedStatusRegister & ( ~portALL_IPL_BITS ) ) ) | ( configMAX_SYSCALL_INTERRUPT_PRIORITY << portIPL_SHIFT ) );
  265. return uxSavedStatusRegister;
  266. }
  267. /*-----------------------------------------------------------*/
  268. void vPortClearInterruptMaskFromISR( UBaseType_t uxSavedStatusRegister )
  269. {
  270. _CP0_SET_STATUS( uxSavedStatusRegister );
  271. }
  272. /*-----------------------------------------------------------*/