port.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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 PIC32MEC14xx port.
  30. *----------------------------------------------------------*/
  31. /* Scheduler include files. */
  32. #include "FreeRTOS.h"
  33. #include "task.h"
  34. /* Microchip includes. */
  35. #include <xc.h>
  36. #include <cp0defs.h>
  37. #if !defined(__MEC__)
  38. #error This port is designed to work with XC32 on MEC14xx. Please update your C compiler version or settings.
  39. #endif
  40. #if( ( configMAX_SYSCALL_INTERRUPT_PRIORITY >= 0x7 ) || ( configMAX_SYSCALL_INTERRUPT_PRIORITY == 0 ) )
  41. #error configMAX_SYSCALL_INTERRUPT_PRIORITY must be less than 7 and greater than 0
  42. #endif
  43. /* Bits within various registers. */
  44. #define portIE_BIT ( 0x00000001 )
  45. #define portEXL_BIT ( 0x00000002 )
  46. /* The EXL bit is set to ensure interrupts do not occur while the context of
  47. the first task is being restored. MEC14xx does not have DSP HW. */
  48. #define portINITIAL_SR ( portIE_BIT | portEXL_BIT )
  49. /* MEC14xx RTOS Timer MMCR's. */
  50. #define portMMCR_RTMR_PRELOAD *((volatile uint32_t *)(0xA0007404ul))
  51. #define portMMCR_RTMR_CONTROL *((volatile uint32_t *)(0xA0007408ul))
  52. /* MEC14xx JTVIC external interrupt controller is mapped to M14K closely-coupled
  53. peripheral space. */
  54. #define portGIRQ23_RTOS_TIMER_BITPOS ( 4 )
  55. #define portGIRQ23_RTOS_TIMER_MASK ( 1ul << ( portGIRQ23_RTOS_TIMER_BITPOS ) )
  56. #define portMMCR_JTVIC_GIRQ23_SRC *((volatile uint32_t *)(0xBFFFC0F0ul))
  57. #define portMMCR_JTVIC_GIRQ23_SETEN *((volatile uint32_t *)(0xBFFFC0F4ul))
  58. #define portMMCR_JTVIC_GIRQ23_PRIA *((volatile uint32_t *)(0xBFFFC3F0ul))
  59. /* MIPS Software Interrupts are routed through JTVIC GIRQ24 */
  60. #define portGIRQ24_M14K_SOFTIRQ0_BITPOS ( 1 )
  61. #define portGIRQ24_M14K_SOFTIRQ0_MASK ( 1ul << ( portGIRQ24_M14K_SOFTIRQ0_BITPOS ) )
  62. #define portMMCR_JTVIC_GIRQ24_SRC *((volatile uint32_t *)(0xBFFFC100ul))
  63. #define portMMCR_JTVIC_GIRQ24_SETEN *((volatile uint32_t *)(0xBFFFC104ul))
  64. #define portMMCR_JTVIC_GIRQ24_PRIA *((volatile uint32_t *)(0xBFFFC400ul))
  65. /*
  66. By default port.c generates its tick interrupt from the RTOS timer. The user
  67. can override this behaviour by:
  68. 1: Providing their own implementation of vApplicationSetupTickTimerInterrupt(),
  69. which is the function that configures the timer. The function is defined
  70. as a weak symbol in this file so if the same function name is used in the
  71. application code then the version in the application code will be linked
  72. into the application in preference to the version defined in this file.
  73. 2: Provide a vector implementation in port_asm.S that overrides the default
  74. behaviour for the specified interrupt vector.
  75. 3: Specify the correct bit to clear the interrupt during the timer interrupt
  76. handler.
  77. */
  78. #ifndef configTICK_INTERRUPT_VECTOR
  79. #define configTICK_INTERRUPT_VECTOR girq23_b4
  80. #define configCLEAR_TICK_TIMER_INTERRUPT() portMMCR_JTVIC_GIRQ23_SRC = portGIRQ23_RTOS_TIMER_MASK
  81. #else
  82. #ifndef configCLEAR_TICK_TIMER_INTERRUPT
  83. #error If configTICK_INTERRUPT_VECTOR is defined in application code then configCLEAR_TICK_TIMER_INTERRUPT must also be defined in application code.
  84. #endif
  85. #endif
  86. /* Let the user override the pre-loading of the initial RA with the address of
  87. prvTaskExitError() in case it messes up unwinding of the stack in the debugger -
  88. in which case configTASK_RETURN_ADDRESS can be defined as 0 (NULL). */
  89. #ifdef configTASK_RETURN_ADDRESS
  90. #define portTASK_RETURN_ADDRESS configTASK_RETURN_ADDRESS
  91. #else
  92. #define portTASK_RETURN_ADDRESS prvTaskExitError
  93. #endif
  94. /* Set configCHECK_FOR_STACK_OVERFLOW to 3 to add ISR stack checking to task
  95. stack checking. A problem in the ISR stack will trigger an assert, not call the
  96. stack overflow hook function (because the stack overflow hook is specific to a
  97. task stack, not the ISR stack). */
  98. #if( configCHECK_FOR_STACK_OVERFLOW > 2 )
  99. /* Don't use 0xa5 as the stack fill bytes as that is used by the kernel for
  100. the task stacks, and so will legitimately appear in many positions within
  101. the ISR stack. */
  102. #define portISR_STACK_FILL_BYTE 0xee
  103. static const uint8_t ucExpectedStackBytes[] = {
  104. portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, \
  105. portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, \
  106. portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, \
  107. portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, \
  108. portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE }; \
  109. #define portCHECK_ISR_STACK() configASSERT( ( memcmp( ( void * ) xISRStack, ( void * ) ucExpectedStackBytes, sizeof( ucExpectedStackBytes ) ) == 0 ) )
  110. #else
  111. /* Define the function away. */
  112. #define portCHECK_ISR_STACK()
  113. #endif /* configCHECK_FOR_STACK_OVERFLOW > 2 */
  114. /*-----------------------------------------------------------*/
  115. /*
  116. * Used to catch tasks that attempt to return from their implementing function.
  117. */
  118. static void prvTaskExitError( void );
  119. /*-----------------------------------------------------------*/
  120. /* Records the interrupt nesting depth. This is initialised to one as it is
  121. decremented to 0 when the first task starts. */
  122. volatile UBaseType_t uxInterruptNesting = 0x01;
  123. /* Stores the task stack pointer when a switch is made to use the system stack. */
  124. UBaseType_t uxSavedTaskStackPointer = 0;
  125. /* The stack used by interrupt service routines that cause a context switch. */
  126. StackType_t xISRStack[ configISR_STACK_SIZE ] = { 0 };
  127. /* The top of stack value ensures there is enough space to store 6 registers on
  128. the callers stack, as some functions seem to want to do this. */
  129. const StackType_t * const xISRStackTop = &( xISRStack[ configISR_STACK_SIZE - 7 ] );
  130. /*-----------------------------------------------------------*/
  131. /*
  132. * See header file for description.
  133. */
  134. StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
  135. {
  136. /* Ensure byte alignment is maintained when leaving this function. */
  137. pxTopOfStack--;
  138. *pxTopOfStack = (StackType_t) 0xDEADBEEF;
  139. pxTopOfStack--;
  140. *pxTopOfStack = (StackType_t) 0x12345678; /* Word to which the stack pointer will be left pointing after context restore. */
  141. pxTopOfStack--;
  142. *pxTopOfStack = (StackType_t) ulPortGetCP0Cause();
  143. pxTopOfStack--;
  144. *pxTopOfStack = (StackType_t) portINITIAL_SR; /* CP0_STATUS */
  145. pxTopOfStack--;
  146. *pxTopOfStack = (StackType_t) pxCode; /* CP0_EPC */
  147. pxTopOfStack--;
  148. *pxTopOfStack = (StackType_t) portTASK_RETURN_ADDRESS; /* ra */
  149. pxTopOfStack -= 15;
  150. *pxTopOfStack = (StackType_t) pvParameters; /* Parameters to pass in. */
  151. pxTopOfStack -= 15;
  152. return pxTopOfStack;
  153. }
  154. /*-----------------------------------------------------------*/
  155. static __inline uint32_t prvDisableInterrupt( void )
  156. {
  157. uint32_t prev_state;
  158. __asm volatile( "di %0; ehb" : "=r" ( prev_state ) :: "memory" );
  159. return prev_state;
  160. }
  161. /*-----------------------------------------------------------*/
  162. static void prvTaskExitError( void )
  163. {
  164. /* A function that implements a task must not exit or attempt to return to
  165. its caller as there is nothing to return to. If a task wants to exit it
  166. should instead call vTaskDelete( NULL ).
  167. Artificially force an assert() to be triggered if configASSERT() is
  168. defined, then stop here so application writers can catch the error. */
  169. configASSERT( uxSavedTaskStackPointer == 0UL );
  170. portDISABLE_INTERRUPTS();
  171. for( ;; );
  172. }
  173. /*-----------------------------------------------------------*/
  174. /*
  175. * Setup a timer for a regular tick. This function uses the RTOS timer.
  176. * The function is declared weak so an application writer can use a different
  177. * timer by redefining this implementation. If a different timer is used then
  178. * configTICK_INTERRUPT_VECTOR must also be defined in FreeRTOSConfig.h to
  179. * ensure the RTOS provided tick interrupt handler is installed on the correct
  180. * vector number.
  181. */
  182. __attribute__(( weak )) void vApplicationSetupTickTimerInterrupt( void )
  183. {
  184. /* MEC14xx RTOS Timer whose input clock is 32KHz. */
  185. const uint32_t ulPreload = ( 32768ul / ( configTICK_RATE_HZ ) );
  186. configASSERT( ulPreload != 0UL );
  187. /* Configure the RTOS timer. */
  188. portMMCR_RTMR_CONTROL = 0ul;
  189. portMMCR_RTMR_PRELOAD = ulPreload;
  190. /* Configure interrupts from the RTOS timer. */
  191. portMMCR_JTVIC_GIRQ23_SRC = ( portGIRQ23_RTOS_TIMER_MASK );
  192. portMMCR_JTVIC_GIRQ23_PRIA &= ~( 0x0Ful << 16 );
  193. portMMCR_JTVIC_GIRQ23_PRIA |= ( ( portIPL_TO_CODE( configKERNEL_INTERRUPT_PRIORITY ) ) << 16 );
  194. portMMCR_JTVIC_GIRQ23_SETEN = ( portGIRQ23_RTOS_TIMER_MASK );
  195. /* Enable the RTOS timer. */
  196. portMMCR_RTMR_CONTROL = 0x0Fu;
  197. }
  198. /*-----------------------------------------------------------*/
  199. void vPortEndScheduler(void)
  200. {
  201. /* Not implemented in ports where there is nothing to return to.
  202. Artificially force an assert. */
  203. configASSERT( uxInterruptNesting == 1000UL );
  204. }
  205. /*-----------------------------------------------------------*/
  206. BaseType_t xPortStartScheduler( void )
  207. {
  208. extern void vPortStartFirstTask( void );
  209. extern void *pxCurrentTCB;
  210. #if ( configCHECK_FOR_STACK_OVERFLOW > 2 )
  211. {
  212. /* Fill the ISR stack to make it easy to asses how much is being used. */
  213. memset( ( void * ) xISRStack, portISR_STACK_FILL_BYTE, sizeof( xISRStack ) );
  214. }
  215. #endif /* configCHECK_FOR_STACK_OVERFLOW > 2 */
  216. /* Clear the software interrupt flag. */
  217. portMMCR_JTVIC_GIRQ24_SRC = (portGIRQ24_M14K_SOFTIRQ0_MASK);
  218. /* Set software timer priority. Each GIRQn has one nibble containing its
  219. priority */
  220. portMMCR_JTVIC_GIRQ24_PRIA &= ~(0xF0ul);
  221. portMMCR_JTVIC_GIRQ24_PRIA |= ( portIPL_TO_CODE( configKERNEL_INTERRUPT_PRIORITY ) << 4 );
  222. /* Enable software interrupt. */
  223. portMMCR_JTVIC_GIRQ24_SETEN = ( portGIRQ24_M14K_SOFTIRQ0_MASK );
  224. /* Setup the timer to generate the tick. Interrupts will have been disabled
  225. by the time we get here. */
  226. vApplicationSetupTickTimerInterrupt();
  227. /* Start the highest priority task that has been created so far. Its stack
  228. location is loaded into uxSavedTaskStackPointer. */
  229. uxSavedTaskStackPointer = *( UBaseType_t * ) pxCurrentTCB;
  230. vPortStartFirstTask();
  231. /* Should never get here as the tasks will now be executing! Call the task
  232. exit error function to prevent compiler warnings about a static function
  233. not being called in the case that the application writer overrides this
  234. functionality by defining configTASK_RETURN_ADDRESS. */
  235. prvTaskExitError();
  236. return pdFALSE;
  237. }
  238. /*-----------------------------------------------------------*/
  239. void vPortIncrementTick( void )
  240. {
  241. UBaseType_t uxSavedStatus;
  242. uint32_t ulCause;
  243. uxSavedStatus = uxPortSetInterruptMaskFromISR();
  244. {
  245. if( xTaskIncrementTick() != pdFALSE )
  246. {
  247. /* Pend a context switch. */
  248. ulCause = ulPortGetCP0Cause();
  249. ulCause |= ( 1ul << 8UL );
  250. vPortSetCP0Cause( ulCause );
  251. }
  252. }
  253. vPortClearInterruptMaskFromISR( uxSavedStatus );
  254. /* Look for the ISR stack getting near or past its limit. */
  255. portCHECK_ISR_STACK();
  256. /* Clear timer interrupt. */
  257. configCLEAR_TICK_TIMER_INTERRUPT();
  258. }
  259. /*-----------------------------------------------------------*/
  260. UBaseType_t uxPortSetInterruptMaskFromISR( void )
  261. {
  262. UBaseType_t uxSavedStatusRegister;
  263. prvDisableInterrupt();
  264. uxSavedStatusRegister = ulPortGetCP0Status() | 0x01;
  265. /* This clears the IPL bits, then sets them to
  266. configMAX_SYSCALL_INTERRUPT_PRIORITY. This function should not be called
  267. from an interrupt that has a priority above
  268. configMAX_SYSCALL_INTERRUPT_PRIORITY so, when used correctly, the action
  269. can only result in the IPL being unchanged or raised, and therefore never
  270. lowered. */
  271. vPortSetCP0Status( ( ( uxSavedStatusRegister & ( ~portALL_IPL_BITS ) ) ) | ( configMAX_SYSCALL_INTERRUPT_PRIORITY << portIPL_SHIFT ) );
  272. return uxSavedStatusRegister;
  273. }
  274. /*-----------------------------------------------------------*/
  275. void vPortClearInterruptMaskFromISR( UBaseType_t uxSavedStatusRegister )
  276. {
  277. vPortSetCP0Status( uxSavedStatusRegister );
  278. }
  279. /*-----------------------------------------------------------*/