port.c 18 KB

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