port.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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 MicroBlaze port.
  30. *----------------------------------------------------------*/
  31. /* Scheduler includes. */
  32. #include "FreeRTOS.h"
  33. #include "task.h"
  34. /* Standard includes. */
  35. #include <string.h>
  36. /* Hardware includes. */
  37. #include <xintc.h>
  38. #include <xintc_i.h>
  39. #include <xtmrctr.h>
  40. #if( configSUPPORT_DYNAMIC_ALLOCATION == 0 )
  41. #error configSUPPORT_DYNAMIC_ALLOCATION must be set to 1 to use this port.
  42. #endif
  43. /* Tasks are started with interrupts enabled. */
  44. #define portINITIAL_MSR_STATE ( ( StackType_t ) 0x02 )
  45. /* Tasks are started with a critical section nesting of 0 - however prior
  46. to the scheduler being commenced we don't want the critical nesting level
  47. to reach zero, so it is initialised to a high value. */
  48. #define portINITIAL_NESTING_VALUE ( 0xff )
  49. /* Our hardware setup only uses one counter. */
  50. #define portCOUNTER_0 0
  51. /* The stack used by the ISR is filled with a known value to assist in
  52. debugging. */
  53. #define portISR_STACK_FILL_VALUE 0x55555555
  54. /* Counts the nesting depth of calls to portENTER_CRITICAL(). Each task
  55. maintains it's own count, so this variable is saved as part of the task
  56. context. */
  57. volatile UBaseType_t uxCriticalNesting = portINITIAL_NESTING_VALUE;
  58. /* To limit the amount of stack required by each task, this port uses a
  59. separate stack for interrupts. */
  60. uint32_t *pulISRStack;
  61. /*-----------------------------------------------------------*/
  62. /*
  63. * Sets up the periodic ISR used for the RTOS tick. This uses timer 0, but
  64. * could have alternatively used the watchdog timer or timer 1.
  65. */
  66. static void prvSetupTimerInterrupt( void );
  67. /*-----------------------------------------------------------*/
  68. /*
  69. * Initialise the stack of a task to look exactly as if a call to
  70. * portSAVE_CONTEXT had been made.
  71. *
  72. * See the header file portable.h.
  73. */
  74. StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
  75. {
  76. extern void *_SDA2_BASE_, *_SDA_BASE_;
  77. const uint32_t ulR2 = ( uint32_t ) &_SDA2_BASE_;
  78. const uint32_t ulR13 = ( uint32_t ) &_SDA_BASE_;
  79. /* Place a few bytes of known values on the bottom of the stack.
  80. This is essential for the Microblaze port and these lines must
  81. not be omitted. The parameter value will overwrite the
  82. 0x22222222 value during the function prologue. */
  83. *pxTopOfStack = ( StackType_t ) 0x11111111;
  84. pxTopOfStack--;
  85. *pxTopOfStack = ( StackType_t ) 0x22222222;
  86. pxTopOfStack--;
  87. *pxTopOfStack = ( StackType_t ) 0x33333333;
  88. pxTopOfStack--;
  89. /* First stack an initial value for the critical section nesting. This
  90. is initialised to zero as tasks are started with interrupts enabled. */
  91. *pxTopOfStack = ( StackType_t ) 0x00; /* R0. */
  92. /* Place an initial value for all the general purpose registers. */
  93. pxTopOfStack--;
  94. *pxTopOfStack = ( StackType_t ) ulR2; /* R2 - small data area. */
  95. pxTopOfStack--;
  96. *pxTopOfStack = ( StackType_t ) 0x03; /* R3. */
  97. pxTopOfStack--;
  98. *pxTopOfStack = ( StackType_t ) 0x04; /* R4. */
  99. pxTopOfStack--;
  100. *pxTopOfStack = ( StackType_t ) pvParameters;/* R5 contains the function call parameters. */
  101. pxTopOfStack--;
  102. *pxTopOfStack = ( StackType_t ) 0x06; /* R6. */
  103. pxTopOfStack--;
  104. *pxTopOfStack = ( StackType_t ) 0x07; /* R7. */
  105. pxTopOfStack--;
  106. *pxTopOfStack = ( StackType_t ) 0x08; /* R8. */
  107. pxTopOfStack--;
  108. *pxTopOfStack = ( StackType_t ) 0x09; /* R9. */
  109. pxTopOfStack--;
  110. *pxTopOfStack = ( StackType_t ) 0x0a; /* R10. */
  111. pxTopOfStack--;
  112. *pxTopOfStack = ( StackType_t ) 0x0b; /* R11. */
  113. pxTopOfStack--;
  114. *pxTopOfStack = ( StackType_t ) 0x0c; /* R12. */
  115. pxTopOfStack--;
  116. *pxTopOfStack = ( StackType_t ) ulR13; /* R13 - small data read write area. */
  117. pxTopOfStack--;
  118. *pxTopOfStack = ( StackType_t ) pxCode; /* R14. */
  119. pxTopOfStack--;
  120. *pxTopOfStack = ( StackType_t ) 0x0f; /* R15. */
  121. pxTopOfStack--;
  122. *pxTopOfStack = ( StackType_t ) 0x10; /* R16. */
  123. pxTopOfStack--;
  124. *pxTopOfStack = ( StackType_t ) 0x11; /* R17. */
  125. pxTopOfStack--;
  126. *pxTopOfStack = ( StackType_t ) 0x12; /* R18. */
  127. pxTopOfStack--;
  128. *pxTopOfStack = ( StackType_t ) 0x13; /* R19. */
  129. pxTopOfStack--;
  130. *pxTopOfStack = ( StackType_t ) 0x14; /* R20. */
  131. pxTopOfStack--;
  132. *pxTopOfStack = ( StackType_t ) 0x15; /* R21. */
  133. pxTopOfStack--;
  134. *pxTopOfStack = ( StackType_t ) 0x16; /* R22. */
  135. pxTopOfStack--;
  136. *pxTopOfStack = ( StackType_t ) 0x17; /* R23. */
  137. pxTopOfStack--;
  138. *pxTopOfStack = ( StackType_t ) 0x18; /* R24. */
  139. pxTopOfStack--;
  140. *pxTopOfStack = ( StackType_t ) 0x19; /* R25. */
  141. pxTopOfStack--;
  142. *pxTopOfStack = ( StackType_t ) 0x1a; /* R26. */
  143. pxTopOfStack--;
  144. *pxTopOfStack = ( StackType_t ) 0x1b; /* R27. */
  145. pxTopOfStack--;
  146. *pxTopOfStack = ( StackType_t ) 0x1c; /* R28. */
  147. pxTopOfStack--;
  148. *pxTopOfStack = ( StackType_t ) 0x1d; /* R29. */
  149. pxTopOfStack--;
  150. *pxTopOfStack = ( StackType_t ) 0x1e; /* R30. */
  151. pxTopOfStack--;
  152. /* The MSR is stacked between R30 and R31. */
  153. *pxTopOfStack = portINITIAL_MSR_STATE;
  154. pxTopOfStack--;
  155. *pxTopOfStack = ( StackType_t ) 0x1f; /* R31. */
  156. pxTopOfStack--;
  157. /* Return a pointer to the top of the stack we have generated so this can
  158. be stored in the task control block for the task. */
  159. return pxTopOfStack;
  160. }
  161. /*-----------------------------------------------------------*/
  162. BaseType_t xPortStartScheduler( void )
  163. {
  164. extern void ( __FreeRTOS_interrupt_Handler )( void );
  165. extern void ( vStartFirstTask )( void );
  166. /* Setup the FreeRTOS interrupt handler. Code copied from crt0.s. */
  167. asm volatile ( "la r6, r0, __FreeRTOS_interrupt_handler \n\t" \
  168. "sw r6, r1, r0 \n\t" \
  169. "lhu r7, r1, r0 \n\t" \
  170. "shi r7, r0, 0x12 \n\t" \
  171. "shi r6, r0, 0x16 " );
  172. /* Setup the hardware to generate the tick. Interrupts are disabled when
  173. this function is called. */
  174. prvSetupTimerInterrupt();
  175. /* Allocate the stack to be used by the interrupt handler. */
  176. pulISRStack = ( uint32_t * ) pvPortMalloc( configMINIMAL_STACK_SIZE * sizeof( StackType_t ) );
  177. /* Restore the context of the first task that is going to run. */
  178. if( pulISRStack != NULL )
  179. {
  180. /* Fill the ISR stack with a known value to facilitate debugging. */
  181. memset( pulISRStack, portISR_STACK_FILL_VALUE, configMINIMAL_STACK_SIZE * sizeof( StackType_t ) );
  182. pulISRStack += ( configMINIMAL_STACK_SIZE - 1 );
  183. /* Kick off the first task. */
  184. vStartFirstTask();
  185. }
  186. /* Should not get here as the tasks are now running! */
  187. return pdFALSE;
  188. }
  189. /*-----------------------------------------------------------*/
  190. void vPortEndScheduler( void )
  191. {
  192. /* Not implemented. */
  193. }
  194. /*-----------------------------------------------------------*/
  195. /*
  196. * Manual context switch called by portYIELD or taskYIELD.
  197. */
  198. void vPortYield( void )
  199. {
  200. extern void VPortYieldASM( void );
  201. /* Perform the context switch in a critical section to assure it is
  202. not interrupted by the tick ISR. It is not a problem to do this as
  203. each task maintains it's own interrupt status. */
  204. portENTER_CRITICAL();
  205. /* Jump directly to the yield function to ensure there is no
  206. compiler generated prologue code. */
  207. asm volatile ( "bralid r14, VPortYieldASM \n\t" \
  208. "or r0, r0, r0 \n\t" );
  209. portEXIT_CRITICAL();
  210. }
  211. /*-----------------------------------------------------------*/
  212. /*
  213. * Hardware initialisation to generate the RTOS tick.
  214. */
  215. static void prvSetupTimerInterrupt( void )
  216. {
  217. XTmrCtr xTimer;
  218. const uint32_t ulCounterValue = configCPU_CLOCK_HZ / configTICK_RATE_HZ;
  219. UBaseType_t uxMask;
  220. /* The OPB timer1 is used to generate the tick. Use the provided library
  221. functions to enable the timer and set the tick frequency. */
  222. XTmrCtr_mDisable( XPAR_OPB_TIMER_1_BASEADDR, XPAR_OPB_TIMER_1_DEVICE_ID );
  223. XTmrCtr_Initialize( &xTimer, XPAR_OPB_TIMER_1_DEVICE_ID );
  224. XTmrCtr_mSetLoadReg( XPAR_OPB_TIMER_1_BASEADDR, portCOUNTER_0, ulCounterValue );
  225. XTmrCtr_mSetControlStatusReg( XPAR_OPB_TIMER_1_BASEADDR, portCOUNTER_0, XTC_CSR_LOAD_MASK | XTC_CSR_INT_OCCURED_MASK );
  226. /* Set the timer interrupt enable bit while maintaining the other bit
  227. states. */
  228. uxMask = XIntc_In32( ( XPAR_OPB_INTC_0_BASEADDR + XIN_IER_OFFSET ) );
  229. uxMask |= XPAR_OPB_TIMER_1_INTERRUPT_MASK;
  230. XIntc_Out32( ( XPAR_OPB_INTC_0_BASEADDR + XIN_IER_OFFSET ), ( uxMask ) );
  231. XTmrCtr_Start( &xTimer, XPAR_OPB_TIMER_1_DEVICE_ID );
  232. XTmrCtr_mSetControlStatusReg(XPAR_OPB_TIMER_1_BASEADDR, portCOUNTER_0, XTC_CSR_ENABLE_TMR_MASK | XTC_CSR_ENABLE_INT_MASK | XTC_CSR_AUTO_RELOAD_MASK | XTC_CSR_DOWN_COUNT_MASK | XTC_CSR_INT_OCCURED_MASK );
  233. XIntc_mAckIntr( XPAR_INTC_SINGLE_BASEADDR, 1 );
  234. }
  235. /*-----------------------------------------------------------*/
  236. /*
  237. * The interrupt handler placed in the interrupt vector when the scheduler is
  238. * started. The task context has already been saved when this is called.
  239. * This handler determines the interrupt source and calls the relevant
  240. * peripheral handler.
  241. */
  242. void vTaskISRHandler( void )
  243. {
  244. static uint32_t ulPending;
  245. /* Which interrupts are pending? */
  246. ulPending = XIntc_In32( ( XPAR_INTC_SINGLE_BASEADDR + XIN_IVR_OFFSET ) );
  247. if( ulPending < XPAR_INTC_MAX_NUM_INTR_INPUTS )
  248. {
  249. static XIntc_VectorTableEntry *pxTablePtr;
  250. static XIntc_Config *pxConfig;
  251. static uint32_t ulInterruptMask;
  252. ulInterruptMask = ( uint32_t ) 1 << ulPending;
  253. /* Get the configuration data using the device ID */
  254. pxConfig = &XIntc_ConfigTable[ ( uint32_t ) XPAR_INTC_SINGLE_DEVICE_ID ];
  255. pxTablePtr = &( pxConfig->HandlerTable[ ulPending ] );
  256. if( pxConfig->AckBeforeService & ( ulInterruptMask ) )
  257. {
  258. XIntc_mAckIntr( pxConfig->BaseAddress, ulInterruptMask );
  259. pxTablePtr->Handler( pxTablePtr->CallBackRef );
  260. }
  261. else
  262. {
  263. pxTablePtr->Handler( pxTablePtr->CallBackRef );
  264. XIntc_mAckIntr( pxConfig->BaseAddress, ulInterruptMask );
  265. }
  266. }
  267. }
  268. /*-----------------------------------------------------------*/
  269. /*
  270. * Handler for the timer interrupt.
  271. */
  272. void vTickISR( void *pvBaseAddress )
  273. {
  274. uint32_t ulCSR;
  275. /* Increment the RTOS tick - this might cause a task to unblock. */
  276. if( xTaskIncrementTick() != pdFALSE )
  277. {
  278. vTaskSwitchContext();
  279. }
  280. /* Clear the timer interrupt */
  281. ulCSR = XTmrCtr_mGetControlStatusReg(XPAR_OPB_TIMER_1_BASEADDR, 0);
  282. XTmrCtr_mSetControlStatusReg( XPAR_OPB_TIMER_1_BASEADDR, portCOUNTER_0, ulCSR );
  283. }
  284. /*-----------------------------------------------------------*/