port.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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_i.h>
  38. #include <xil_exception.h>
  39. #include <microblaze_exceptions_g.h>
  40. /* Tasks are started with a critical section nesting of 0 - however, prior to
  41. the scheduler being commenced interrupts should not be enabled, so the critical
  42. nesting variable is initialised to a non-zero value. */
  43. #define portINITIAL_NESTING_VALUE ( 0xff )
  44. /* The bit within the MSR register that enabled/disables interrupts and
  45. exceptions respectively. */
  46. #define portMSR_IE ( 0x02U )
  47. #define portMSR_EE ( 0x100U )
  48. /* If the floating point unit is included in the MicroBlaze build, then the
  49. FSR register is saved as part of the task context. portINITIAL_FSR is the value
  50. given to the FSR register when the initial context is set up for a task being
  51. created. */
  52. #define portINITIAL_FSR ( 0U )
  53. /*-----------------------------------------------------------*/
  54. /*
  55. * Initialise the interrupt controller instance.
  56. */
  57. static int32_t prvInitialiseInterruptController( void );
  58. /* Ensure the interrupt controller instance variable is initialised before it is
  59. * used, and that the initialisation only happens once.
  60. */
  61. static int32_t prvEnsureInterruptControllerIsInitialised( void );
  62. /*-----------------------------------------------------------*/
  63. /* Counts the nesting depth of calls to portENTER_CRITICAL(). Each task
  64. maintains its own count, so this variable is saved as part of the task
  65. context. */
  66. volatile UBaseType_t uxCriticalNesting = portINITIAL_NESTING_VALUE;
  67. /* This port uses a separate stack for interrupts. This prevents the stack of
  68. every task needing to be large enough to hold an entire interrupt stack on top
  69. of the task stack. */
  70. uint32_t *pulISRStack;
  71. /* If an interrupt requests a context switch, then ulTaskSwitchRequested will
  72. get set to 1. ulTaskSwitchRequested is inspected just before the main interrupt
  73. handler exits. If, at that time, ulTaskSwitchRequested is set to 1, the kernel
  74. will call vTaskSwitchContext() to ensure the task that runs immediately after
  75. the interrupt exists is the highest priority task that is able to run. This is
  76. an unusual mechanism, but is used for this port because a single interrupt can
  77. cause the servicing of multiple peripherals - and it is inefficient to call
  78. vTaskSwitchContext() multiple times as each peripheral is serviced. */
  79. volatile uint32_t ulTaskSwitchRequested = 0UL;
  80. /* The instance of the interrupt controller used by this port. This is required
  81. by the Xilinx library API functions. */
  82. static XIntc xInterruptControllerInstance;
  83. /*-----------------------------------------------------------*/
  84. /*
  85. * Initialise the stack of a task to look exactly as if a call to
  86. * portSAVE_CONTEXT had been made.
  87. *
  88. * See the portable.h header file.
  89. */
  90. StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
  91. {
  92. extern void *_SDA2_BASE_, *_SDA_BASE_;
  93. const uint32_t ulR2 = ( uint32_t ) &_SDA2_BASE_;
  94. const uint32_t ulR13 = ( uint32_t ) &_SDA_BASE_;
  95. /* Place a few bytes of known values on the bottom of the stack.
  96. This is essential for the Microblaze port and these lines must
  97. not be omitted. */
  98. *pxTopOfStack = ( StackType_t ) 0x00000000;
  99. pxTopOfStack--;
  100. *pxTopOfStack = ( StackType_t ) 0x00000000;
  101. pxTopOfStack--;
  102. *pxTopOfStack = ( StackType_t ) 0x00000000;
  103. pxTopOfStack--;
  104. #if( XPAR_MICROBLAZE_USE_FPU != 0 )
  105. /* The FSR value placed in the initial task context is just 0. */
  106. *pxTopOfStack = portINITIAL_FSR;
  107. pxTopOfStack--;
  108. #endif
  109. /* The MSR value placed in the initial task context should have interrupts
  110. disabled. Each task will enable interrupts automatically when it enters
  111. the running state for the first time. */
  112. *pxTopOfStack = mfmsr() & ~portMSR_IE;
  113. #if( MICROBLAZE_EXCEPTIONS_ENABLED == 1 )
  114. {
  115. /* Ensure exceptions are enabled for the task. */
  116. *pxTopOfStack |= portMSR_EE;
  117. }
  118. #endif
  119. pxTopOfStack--;
  120. /* First stack an initial value for the critical section nesting. This
  121. is initialised to zero. */
  122. *pxTopOfStack = ( StackType_t ) 0x00;
  123. /* R0 is always zero. */
  124. /* R1 is the SP. */
  125. /* Place an initial value for all the general purpose registers. */
  126. pxTopOfStack--;
  127. *pxTopOfStack = ( StackType_t ) ulR2; /* R2 - read only small data area. */
  128. pxTopOfStack--;
  129. *pxTopOfStack = ( StackType_t ) 0x03; /* R3 - return values and temporaries. */
  130. pxTopOfStack--;
  131. *pxTopOfStack = ( StackType_t ) 0x04; /* R4 - return values and temporaries. */
  132. pxTopOfStack--;
  133. *pxTopOfStack = ( StackType_t ) pvParameters;/* R5 contains the function call parameters. */
  134. #ifdef portPRE_LOAD_STACK_FOR_DEBUGGING
  135. pxTopOfStack--;
  136. *pxTopOfStack = ( StackType_t ) 0x06; /* R6 - other parameters and temporaries. Used as the return address from vPortTaskEntryPoint. */
  137. pxTopOfStack--;
  138. *pxTopOfStack = ( StackType_t ) 0x07; /* R7 - other parameters and temporaries. */
  139. pxTopOfStack--;
  140. *pxTopOfStack = ( StackType_t ) 0x08; /* R8 - other parameters and temporaries. */
  141. pxTopOfStack--;
  142. *pxTopOfStack = ( StackType_t ) 0x09; /* R9 - other parameters and temporaries. */
  143. pxTopOfStack--;
  144. *pxTopOfStack = ( StackType_t ) 0x0a; /* R10 - other parameters and temporaries. */
  145. pxTopOfStack--;
  146. *pxTopOfStack = ( StackType_t ) 0x0b; /* R11 - temporaries. */
  147. pxTopOfStack--;
  148. *pxTopOfStack = ( StackType_t ) 0x0c; /* R12 - temporaries. */
  149. pxTopOfStack--;
  150. #else
  151. pxTopOfStack-= 8;
  152. #endif
  153. *pxTopOfStack = ( StackType_t ) ulR13; /* R13 - read/write small data area. */
  154. pxTopOfStack--;
  155. *pxTopOfStack = ( StackType_t ) pxCode; /* R14 - return address for interrupt. */
  156. pxTopOfStack--;
  157. *pxTopOfStack = ( StackType_t ) NULL; /* R15 - return address for subroutine. */
  158. #ifdef portPRE_LOAD_STACK_FOR_DEBUGGING
  159. pxTopOfStack--;
  160. *pxTopOfStack = ( StackType_t ) 0x10; /* R16 - return address for trap (debugger). */
  161. pxTopOfStack--;
  162. *pxTopOfStack = ( StackType_t ) 0x11; /* R17 - return address for exceptions, if configured. */
  163. pxTopOfStack--;
  164. *pxTopOfStack = ( StackType_t ) 0x12; /* R18 - reserved for assembler and compiler temporaries. */
  165. pxTopOfStack--;
  166. #else
  167. pxTopOfStack -= 4;
  168. #endif
  169. *pxTopOfStack = ( StackType_t ) 0x00; /* R19 - must be saved across function calls. Callee-save. Seems to be interpreted as the frame pointer. */
  170. #ifdef portPRE_LOAD_STACK_FOR_DEBUGGING
  171. pxTopOfStack--;
  172. *pxTopOfStack = ( StackType_t ) 0x14; /* R20 - reserved for storing a pointer to the Global Offset Table (GOT) in Position Independent Code (PIC). Non-volatile in non-PIC code. Must be saved across function calls. Callee-save. Not used by FreeRTOS. */
  173. pxTopOfStack--;
  174. *pxTopOfStack = ( StackType_t ) 0x15; /* R21 - must be saved across function calls. Callee-save. */
  175. pxTopOfStack--;
  176. *pxTopOfStack = ( StackType_t ) 0x16; /* R22 - must be saved across function calls. Callee-save. */
  177. pxTopOfStack--;
  178. *pxTopOfStack = ( StackType_t ) 0x17; /* R23 - must be saved across function calls. Callee-save. */
  179. pxTopOfStack--;
  180. *pxTopOfStack = ( StackType_t ) 0x18; /* R24 - must be saved across function calls. Callee-save. */
  181. pxTopOfStack--;
  182. *pxTopOfStack = ( StackType_t ) 0x19; /* R25 - must be saved across function calls. Callee-save. */
  183. pxTopOfStack--;
  184. *pxTopOfStack = ( StackType_t ) 0x1a; /* R26 - must be saved across function calls. Callee-save. */
  185. pxTopOfStack--;
  186. *pxTopOfStack = ( StackType_t ) 0x1b; /* R27 - must be saved across function calls. Callee-save. */
  187. pxTopOfStack--;
  188. *pxTopOfStack = ( StackType_t ) 0x1c; /* R28 - must be saved across function calls. Callee-save. */
  189. pxTopOfStack--;
  190. *pxTopOfStack = ( StackType_t ) 0x1d; /* R29 - must be saved across function calls. Callee-save. */
  191. pxTopOfStack--;
  192. *pxTopOfStack = ( StackType_t ) 0x1e; /* R30 - must be saved across function calls. Callee-save. */
  193. pxTopOfStack--;
  194. *pxTopOfStack = ( StackType_t ) 0x1f; /* R31 - must be saved across function calls. Callee-save. */
  195. pxTopOfStack--;
  196. #else
  197. pxTopOfStack -= 13;
  198. #endif
  199. /* Return a pointer to the top of the stack that has been generated so this
  200. can be stored in the task control block for the task. */
  201. return pxTopOfStack;
  202. }
  203. /*-----------------------------------------------------------*/
  204. BaseType_t xPortStartScheduler( void )
  205. {
  206. extern void ( vPortStartFirstTask )( void );
  207. extern uint32_t _stack[];
  208. /* Setup the hardware to generate the tick. Interrupts are disabled when
  209. this function is called.
  210. This port uses an application defined callback function to install the tick
  211. interrupt handler because the kernel will run on lots of different
  212. MicroBlaze and FPGA configurations - not all of which will have the same
  213. timer peripherals defined or available. An example definition of
  214. vApplicationSetupTimerInterrupt() is provided in the official demo
  215. application that accompanies this port. */
  216. vApplicationSetupTimerInterrupt();
  217. /* Reuse the stack from main() as the stack for the interrupts/exceptions. */
  218. pulISRStack = ( uint32_t * ) _stack;
  219. /* Ensure there is enough space for the functions called from the interrupt
  220. service routines to write back into the stack frame of the caller. */
  221. pulISRStack -= 2;
  222. /* Restore the context of the first task that is going to run. From here
  223. on, the created tasks will be executing. */
  224. vPortStartFirstTask();
  225. /* Should not get here as the tasks are now running! */
  226. return pdFALSE;
  227. }
  228. /*-----------------------------------------------------------*/
  229. void vPortEndScheduler( void )
  230. {
  231. /* Not implemented in ports where there is nothing to return to.
  232. Artificially force an assert. */
  233. configASSERT( uxCriticalNesting == 1000UL );
  234. }
  235. /*-----------------------------------------------------------*/
  236. /*
  237. * Manual context switch called by portYIELD or taskYIELD.
  238. */
  239. void vPortYield( void )
  240. {
  241. extern void VPortYieldASM( void );
  242. /* Perform the context switch in a critical section to assure it is
  243. not interrupted by the tick ISR. It is not a problem to do this as
  244. each task maintains its own interrupt status. */
  245. portENTER_CRITICAL();
  246. {
  247. /* Jump directly to the yield function to ensure there is no
  248. compiler generated prologue code. */
  249. asm volatile ( "bralid r14, VPortYieldASM \n\t" \
  250. "or r0, r0, r0 \n\t" );
  251. }
  252. portEXIT_CRITICAL();
  253. }
  254. /*-----------------------------------------------------------*/
  255. void vPortEnableInterrupt( uint8_t ucInterruptID )
  256. {
  257. int32_t lReturn;
  258. /* An API function is provided to enable an interrupt in the interrupt
  259. controller because the interrupt controller instance variable is private
  260. to this file. */
  261. lReturn = prvEnsureInterruptControllerIsInitialised();
  262. if( lReturn == pdPASS )
  263. {
  264. XIntc_Enable( &xInterruptControllerInstance, ucInterruptID );
  265. }
  266. configASSERT( lReturn );
  267. }
  268. /*-----------------------------------------------------------*/
  269. void vPortDisableInterrupt( uint8_t ucInterruptID )
  270. {
  271. int32_t lReturn;
  272. /* An API function is provided to disable an interrupt in the interrupt
  273. controller because the interrupt controller instance variable is private
  274. to this file. */
  275. lReturn = prvEnsureInterruptControllerIsInitialised();
  276. if( lReturn == pdPASS )
  277. {
  278. XIntc_Disable( &xInterruptControllerInstance, ucInterruptID );
  279. }
  280. configASSERT( lReturn );
  281. }
  282. /*-----------------------------------------------------------*/
  283. BaseType_t xPortInstallInterruptHandler( uint8_t ucInterruptID, XInterruptHandler pxHandler, void *pvCallBackRef )
  284. {
  285. int32_t lReturn;
  286. /* An API function is provided to install an interrupt handler because the
  287. interrupt controller instance variable is private to this file. */
  288. lReturn = prvEnsureInterruptControllerIsInitialised();
  289. if( lReturn == pdPASS )
  290. {
  291. lReturn = XIntc_Connect( &xInterruptControllerInstance, ucInterruptID, pxHandler, pvCallBackRef );
  292. }
  293. if( lReturn == XST_SUCCESS )
  294. {
  295. lReturn = pdPASS;
  296. }
  297. configASSERT( lReturn == pdPASS );
  298. return lReturn;
  299. }
  300. /*-----------------------------------------------------------*/
  301. static int32_t prvEnsureInterruptControllerIsInitialised( void )
  302. {
  303. static int32_t lInterruptControllerInitialised = pdFALSE;
  304. int32_t lReturn;
  305. /* Ensure the interrupt controller instance variable is initialised before
  306. it is used, and that the initialisation only happens once. */
  307. if( lInterruptControllerInitialised != pdTRUE )
  308. {
  309. lReturn = prvInitialiseInterruptController();
  310. if( lReturn == pdPASS )
  311. {
  312. lInterruptControllerInitialised = pdTRUE;
  313. }
  314. }
  315. else
  316. {
  317. lReturn = pdPASS;
  318. }
  319. return lReturn;
  320. }
  321. /*-----------------------------------------------------------*/
  322. /*
  323. * Handler for the timer interrupt. This is the handler that the application
  324. * defined callback function vApplicationSetupTimerInterrupt() should install.
  325. */
  326. void vPortTickISR( void *pvUnused )
  327. {
  328. extern void vApplicationClearTimerInterrupt( void );
  329. /* Ensure the unused parameter does not generate a compiler warning. */
  330. ( void ) pvUnused;
  331. /* This port uses an application defined callback function to clear the tick
  332. interrupt because the kernel will run on lots of different MicroBlaze and
  333. FPGA configurations - not all of which will have the same timer peripherals
  334. defined or available. An example definition of
  335. vApplicationClearTimerInterrupt() is provided in the official demo
  336. application that accompanies this port. */
  337. vApplicationClearTimerInterrupt();
  338. /* Increment the RTOS tick - this might cause a task to unblock. */
  339. if( xTaskIncrementTick() != pdFALSE )
  340. {
  341. /* Force vTaskSwitchContext() to be called as the interrupt exits. */
  342. ulTaskSwitchRequested = 1;
  343. }
  344. }
  345. /*-----------------------------------------------------------*/
  346. static int32_t prvInitialiseInterruptController( void )
  347. {
  348. int32_t lStatus;
  349. lStatus = XIntc_Initialize( &xInterruptControllerInstance, configINTERRUPT_CONTROLLER_TO_USE );
  350. if( lStatus == XST_SUCCESS )
  351. {
  352. /* Initialise the exception table. */
  353. Xil_ExceptionInit();
  354. /* Service all pending interrupts each time the handler is entered. */
  355. XIntc_SetIntrSvcOption( xInterruptControllerInstance.BaseAddress, XIN_SVC_ALL_ISRS_OPTION );
  356. /* Install exception handlers if the MicroBlaze is configured to handle
  357. exceptions, and the application defined constant
  358. configINSTALL_EXCEPTION_HANDLERS is set to 1. */
  359. #if ( MICROBLAZE_EXCEPTIONS_ENABLED == 1 ) && ( configINSTALL_EXCEPTION_HANDLERS == 1 )
  360. {
  361. vPortExceptionsInstallHandlers();
  362. }
  363. #endif /* MICROBLAZE_EXCEPTIONS_ENABLED */
  364. /* Start the interrupt controller. Interrupts are enabled when the
  365. scheduler starts. */
  366. lStatus = XIntc_Start( &xInterruptControllerInstance, XIN_REAL_MODE );
  367. if( lStatus == XST_SUCCESS )
  368. {
  369. lStatus = pdPASS;
  370. }
  371. else
  372. {
  373. lStatus = pdFAIL;
  374. }
  375. }
  376. configASSERT( lStatus == pdPASS );
  377. return lStatus;
  378. }
  379. /*-----------------------------------------------------------*/