port.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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 PIC32MZ port.
  30. *----------------------------------------------------------*/
  31. /* Microchip specific headers. */
  32. #include <xc.h>
  33. /* Standard headers. */
  34. #include <string.h>
  35. /* Scheduler include files. */
  36. #include "FreeRTOS.h"
  37. #include "task.h"
  38. #if !defined(__PIC32MZ__)
  39. #error This port is designed to work with XC32 on PIC32MZ MCUs. Please update your C compiler version or settings.
  40. #endif
  41. #if( ( configMAX_SYSCALL_INTERRUPT_PRIORITY >= 0x7 ) || ( configMAX_SYSCALL_INTERRUPT_PRIORITY == 0 ) )
  42. #error configMAX_SYSCALL_INTERRUPT_PRIORITY must be less than 7 and greater than 0
  43. #endif
  44. /* Hardware specifics. */
  45. #define portTIMER_PRESCALE 8
  46. #define portPRESCALE_BITS 1
  47. /* Bits within various registers. */
  48. #define portIE_BIT ( 0x00000001 )
  49. #define portEXL_BIT ( 0x00000002 )
  50. #define portMX_BIT ( 0x01000000 ) /* Allow access to DSP instructions. */
  51. #define portCU1_BIT ( 0x20000000 ) /* enable CP1 for parts with hardware. */
  52. #define portFR_BIT ( 0x04000000 ) /* Enable 64 bit floating point registers. */
  53. /* Bits within the CAUSE register. */
  54. #define portCORE_SW_0 ( 0x00000100 )
  55. #define portCORE_SW_1 ( 0x00000200 )
  56. /* The EXL bit is set to ensure interrupts do not occur while the context of
  57. the first task is being restored. */
  58. #if ( __mips_hard_float == 1 )
  59. #define portINITIAL_SR ( portIE_BIT | portEXL_BIT | portMX_BIT | portFR_BIT | portCU1_BIT )
  60. #else
  61. #define portINITIAL_SR ( portIE_BIT | portEXL_BIT | portMX_BIT )
  62. #endif
  63. /* The initial value to store into the FPU status and control register. This is
  64. only used on parts that support a hardware FPU. */
  65. #define portINITIAL_FPSCR (0x1000000) /* High perf on denormal ops */
  66. /*
  67. By default port.c generates its tick interrupt from TIMER1. The user can
  68. override this behaviour by:
  69. 1: Providing their own implementation of vApplicationSetupTickTimerInterrupt(),
  70. which is the function that configures the timer. The function is defined
  71. as a weak symbol in this file so if the same function name is used in the
  72. application code then the version in the application code will be linked
  73. into the application in preference to the version defined in this file.
  74. 2: Define configTICK_INTERRUPT_VECTOR to the vector number of the timer used
  75. to generate the tick interrupt. For example, when timer 1 is used then
  76. configTICK_INTERRUPT_VECTOR is set to _TIMER_1_VECTOR.
  77. configTICK_INTERRUPT_VECTOR should be defined in FreeRTOSConfig.h.
  78. 3: Define configCLEAR_TICK_TIMER_INTERRUPT() to clear the interrupt in the
  79. timer used to generate the tick interrupt. For example, when timer 1 is
  80. used configCLEAR_TICK_TIMER_INTERRUPT() is defined to
  81. IFS0CLR = _IFS0_T1IF_MASK.
  82. */
  83. #ifndef configTICK_INTERRUPT_VECTOR
  84. #define configTICK_INTERRUPT_VECTOR _TIMER_1_VECTOR
  85. #define configCLEAR_TICK_TIMER_INTERRUPT() IFS0CLR = _IFS0_T1IF_MASK
  86. #else
  87. #ifndef configCLEAR_TICK_TIMER_INTERRUPT
  88. #error If configTICK_INTERRUPT_VECTOR is defined in application code then configCLEAR_TICK_TIMER_INTERRUPT must also be defined in application code.
  89. #endif
  90. #endif
  91. /* Let the user override the pre-loading of the initial RA with the address of
  92. prvTaskExitError() in case it messes up unwinding of the stack in the
  93. debugger - in which case configTASK_RETURN_ADDRESS can be defined as 0 (NULL). */
  94. #ifdef configTASK_RETURN_ADDRESS
  95. #define portTASK_RETURN_ADDRESS configTASK_RETURN_ADDRESS
  96. #else
  97. #define portTASK_RETURN_ADDRESS prvTaskExitError
  98. #endif
  99. /* Set configCHECK_FOR_STACK_OVERFLOW to 3 to add ISR stack checking to task
  100. stack checking. A problem in the ISR stack will trigger an assert, not call the
  101. stack overflow hook function (because the stack overflow hook is specific to a
  102. task stack, not the ISR stack). */
  103. #if( configCHECK_FOR_STACK_OVERFLOW > 2 )
  104. /* Don't use 0xa5 as the stack fill bytes as that is used by the kernerl for
  105. the task stacks, and so will legitimately appear in many positions within
  106. the ISR stack. */
  107. #define portISR_STACK_FILL_BYTE 0xee
  108. static const uint8_t ucExpectedStackBytes[] = {
  109. portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, \
  110. portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, \
  111. portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, \
  112. portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, \
  113. portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE }; \
  114. #define portCHECK_ISR_STACK() configASSERT( ( memcmp( ( void * ) xISRStack, ( void * ) ucExpectedStackBytes, sizeof( ucExpectedStackBytes ) ) == 0 ) )
  115. #else
  116. /* Define the function away. */
  117. #define portCHECK_ISR_STACK()
  118. #endif /* configCHECK_FOR_STACK_OVERFLOW > 2 */
  119. /*-----------------------------------------------------------*/
  120. /*
  121. * Used to catch tasks that attempt to return from their implementing function.
  122. */
  123. static void prvTaskExitError( void );
  124. /*-----------------------------------------------------------*/
  125. /* Records the interrupt nesting depth. This is initialised to one as it is
  126. decremented to 0 when the first task starts. */
  127. volatile UBaseType_t uxInterruptNesting = 0x01;
  128. /* Stores the task stack pointer when a switch is made to use the system stack. */
  129. UBaseType_t uxSavedTaskStackPointer = 0;
  130. /* The stack used by interrupt service routines that cause a context switch. */
  131. __attribute__ ((aligned(8))) StackType_t xISRStack[ configISR_STACK_SIZE ] = { 0 };
  132. /* The top of stack value ensures there is enough space to store 6 registers on
  133. the callers stack, as some functions seem to want to do this. 8 byte alignment
  134. is required to allow double word floating point stack pushes generated by the
  135. compiler. */
  136. const StackType_t * const xISRStackTop = &( xISRStack[ ( configISR_STACK_SIZE & ~portBYTE_ALIGNMENT_MASK ) - 8 ] );
  137. /* Saved as part of the task context. Set to pdFALSE if the task does not
  138. require an FPU context. */
  139. #if ( __mips_hard_float == 1 ) && ( configUSE_TASK_FPU_SUPPORT == 1 )
  140. uint32_t ulTaskHasFPUContext = 0;
  141. #endif
  142. /*-----------------------------------------------------------*/
  143. /*
  144. * See header file for description.
  145. */
  146. StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
  147. {
  148. /* Ensure 8 byte alignment is maintained when leaving this function. */
  149. pxTopOfStack--;
  150. pxTopOfStack--;
  151. *pxTopOfStack = (StackType_t) 0xDEADBEEF;
  152. pxTopOfStack--;
  153. *pxTopOfStack = (StackType_t) 0x12345678; /* Word to which the stack pointer will be left pointing after context restore. */
  154. pxTopOfStack--;
  155. *pxTopOfStack = (StackType_t) _CP0_GET_CAUSE();
  156. pxTopOfStack--;
  157. *pxTopOfStack = (StackType_t) portINITIAL_SR;/* CP0_STATUS */
  158. pxTopOfStack--;
  159. *pxTopOfStack = (StackType_t) pxCode; /* CP0_EPC */
  160. pxTopOfStack--;
  161. *pxTopOfStack = (StackType_t) 0x00000000; /* DSPControl */
  162. pxTopOfStack -= 7; /* Includes space for AC1 - AC3. */
  163. *pxTopOfStack = (StackType_t) portTASK_RETURN_ADDRESS; /* ra */
  164. pxTopOfStack -= 15;
  165. *pxTopOfStack = (StackType_t) pvParameters; /* Parameters to pass in. */
  166. pxTopOfStack -= 15;
  167. *pxTopOfStack = (StackType_t) pdFALSE; /*by default disable FPU context save on parts with FPU */
  168. return pxTopOfStack;
  169. }
  170. /*-----------------------------------------------------------*/
  171. static void prvTaskExitError( void )
  172. {
  173. /* A function that implements a task must not exit or attempt to return to
  174. its caller as there is nothing to return to. If a task wants to exit it
  175. should instead call vTaskDelete( NULL ).
  176. Artificially force an assert() to be triggered if configASSERT() is
  177. defined, then stop here so application writers can catch the error. */
  178. configASSERT( uxSavedTaskStackPointer == 0UL );
  179. portDISABLE_INTERRUPTS();
  180. for( ;; );
  181. }
  182. /*-----------------------------------------------------------*/
  183. /*
  184. * Setup a timer for a regular tick. This function uses peripheral timer 1.
  185. * The function is declared weak so an application writer can use a different
  186. * timer by redefining this implementation. If a different timer is used then
  187. * configTICK_INTERRUPT_VECTOR must also be defined in FreeRTOSConfig.h to
  188. * ensure the RTOS provided tick interrupt handler is installed on the correct
  189. * vector number. When Timer 1 is used the vector number is defined as
  190. * _TIMER_1_VECTOR.
  191. */
  192. __attribute__(( weak )) void vApplicationSetupTickTimerInterrupt( void )
  193. {
  194. const uint32_t ulCompareMatch = ( (configPERIPHERAL_CLOCK_HZ / portTIMER_PRESCALE) / configTICK_RATE_HZ ) - 1UL;
  195. T1CON = 0x0000;
  196. T1CONbits.TCKPS = portPRESCALE_BITS;
  197. PR1 = ulCompareMatch;
  198. IPC1bits.T1IP = configKERNEL_INTERRUPT_PRIORITY;
  199. /* Clear the interrupt as a starting condition. */
  200. IFS0bits.T1IF = 0;
  201. /* Enable the interrupt. */
  202. IEC0bits.T1IE = 1;
  203. /* Start the timer. */
  204. T1CONbits.TON = 1;
  205. }
  206. /*-----------------------------------------------------------*/
  207. void vPortEndScheduler(void)
  208. {
  209. /* Not implemented in ports where there is nothing to return to.
  210. Artificially force an assert. */
  211. configASSERT( uxInterruptNesting == 1000UL );
  212. }
  213. /*-----------------------------------------------------------*/
  214. BaseType_t xPortStartScheduler( void )
  215. {
  216. extern void vPortStartFirstTask( void );
  217. extern void *pxCurrentTCB;
  218. #if ( configCHECK_FOR_STACK_OVERFLOW > 2 )
  219. {
  220. /* Fill the ISR stack to make it easy to asses how much is being used. */
  221. memset( ( void * ) xISRStack, portISR_STACK_FILL_BYTE, sizeof( xISRStack ) );
  222. }
  223. #endif /* configCHECK_FOR_STACK_OVERFLOW > 2 */
  224. /* Clear the software interrupt flag. */
  225. IFS0CLR = _IFS0_CS0IF_MASK;
  226. /* Set software timer priority. */
  227. IPC0CLR = _IPC0_CS0IP_MASK;
  228. IPC0SET = ( configKERNEL_INTERRUPT_PRIORITY << _IPC0_CS0IP_POSITION );
  229. /* Enable software interrupt. */
  230. IEC0CLR = _IEC0_CS0IE_MASK;
  231. IEC0SET = 1 << _IEC0_CS0IE_POSITION;
  232. /* Setup the timer to generate the tick. Interrupts will have been
  233. disabled by the time we get here. */
  234. vApplicationSetupTickTimerInterrupt();
  235. /* Kick off the highest priority task that has been created so far.
  236. Its stack location is loaded into uxSavedTaskStackPointer. */
  237. uxSavedTaskStackPointer = *( UBaseType_t * ) pxCurrentTCB;
  238. vPortStartFirstTask();
  239. /* Should never get here as the tasks will now be executing! Call the task
  240. exit error function to prevent compiler warnings about a static function
  241. not being called in the case that the application writer overrides this
  242. functionality by defining configTASK_RETURN_ADDRESS. */
  243. prvTaskExitError();
  244. return pdFALSE;
  245. }
  246. /*-----------------------------------------------------------*/
  247. void vPortIncrementTick( void )
  248. {
  249. UBaseType_t uxSavedStatus;
  250. uxSavedStatus = uxPortSetInterruptMaskFromISR();
  251. {
  252. if( xTaskIncrementTick() != pdFALSE )
  253. {
  254. /* Pend a context switch. */
  255. _CP0_BIS_CAUSE( portCORE_SW_0 );
  256. }
  257. }
  258. vPortClearInterruptMaskFromISR( uxSavedStatus );
  259. /* Look for the ISR stack getting near or past its limit. */
  260. portCHECK_ISR_STACK();
  261. /* Clear timer interrupt. */
  262. configCLEAR_TICK_TIMER_INTERRUPT();
  263. }
  264. /*-----------------------------------------------------------*/
  265. UBaseType_t uxPortSetInterruptMaskFromISR( void )
  266. {
  267. UBaseType_t uxSavedStatusRegister;
  268. __builtin_disable_interrupts();
  269. uxSavedStatusRegister = _CP0_GET_STATUS() | 0x01;
  270. /* This clears the IPL bits, then sets them to
  271. configMAX_SYSCALL_INTERRUPT_PRIORITY. This function should not be called
  272. from an interrupt that has a priority above
  273. configMAX_SYSCALL_INTERRUPT_PRIORITY so, when used correctly, the action
  274. can only result in the IPL being unchanged or raised, and therefore never
  275. lowered. */
  276. _CP0_SET_STATUS( ( ( uxSavedStatusRegister & ( ~portALL_IPL_BITS ) ) ) | ( configMAX_SYSCALL_INTERRUPT_PRIORITY << portIPL_SHIFT ) );
  277. return uxSavedStatusRegister;
  278. }
  279. /*-----------------------------------------------------------*/
  280. void vPortClearInterruptMaskFromISR( UBaseType_t uxSavedStatusRegister )
  281. {
  282. _CP0_SET_STATUS( uxSavedStatusRegister );
  283. }
  284. /*-----------------------------------------------------------*/
  285. #if ( __mips_hard_float == 1 ) && ( configUSE_TASK_FPU_SUPPORT == 1 )
  286. void vPortTaskUsesFPU(void)
  287. {
  288. extern void vPortInitialiseFPSCR( uint32_t uxFPSCRInit );
  289. portENTER_CRITICAL();
  290. /* Initialise the floating point status register. */
  291. vPortInitialiseFPSCR(portINITIAL_FPSCR);
  292. /* A task is registering the fact that it needs a FPU context. Set the
  293. FPU flag (saved as part of the task context). */
  294. ulTaskHasFPUContext = pdTRUE;
  295. portEXIT_CRITICAL();
  296. }
  297. #endif /* __mips_hard_float == 1 */
  298. /*-----------------------------------------------------------*/