port.c 16 KB

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