port.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. /*
  2. * FreeRTOS Kernel V10.3.1
  3. * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  6. * this software and associated documentation files (the "Software"), to deal in
  7. * the Software without restriction, including without limitation the rights to
  8. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  9. * the Software, and to permit persons to whom the Software is furnished to do so,
  10. * subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in all
  13. * copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  17. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  18. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  19. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  20. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * http://www.FreeRTOS.org
  23. * http://aws.amazon.com/freertos
  24. *
  25. * 1 tab == 4 spaces!
  26. */
  27. /*-----------------------------------------------------------
  28. * Implementation of functions defined in portable.h for the Nuclei N/NX Processor port.
  29. *----------------------------------------------------------*/
  30. /* Scheduler includes. */
  31. #include <stdio.h>
  32. #include "FreeRTOS.h"
  33. #include "task.h"
  34. //#define ENABLE_KERNEL_DEBUG
  35. #ifdef ENABLE_KERNEL_DEBUG
  36. #define FREERTOS_PORT_DEBUG(...) printf(__VA_ARGS__)
  37. #else
  38. #define FREERTOS_PORT_DEBUG(...)
  39. #endif
  40. #ifndef configSYSTICK_CLOCK_HZ
  41. #define configSYSTICK_CLOCK_HZ SOC_TIMER_FREQ
  42. #endif
  43. #ifndef configKERNEL_INTERRUPT_PRIORITY
  44. #define configKERNEL_INTERRUPT_PRIORITY 0
  45. #endif
  46. #ifndef configMAX_SYSCALL_INTERRUPT_PRIORITY
  47. #define configMAX_SYSCALL_INTERRUPT_PRIORITY 255
  48. #endif
  49. /* Constants required to check the validity of an interrupt priority. */
  50. #define portFIRST_USER_INTERRUPT_NUMBER ( 18 )
  51. #define SYSTICK_TICK_CONST (configSYSTICK_CLOCK_HZ / configTICK_RATE_HZ)
  52. /* Constants required to set up the initial stack. */
  53. #define portINITIAL_MSTATUS ( MSTATUS_MPP | MSTATUS_MPIE | MSTATUS_FS_INITIAL | MSTATUS_VS_INITIAL)
  54. #define portINITIAL_EXC_RETURN ( 0xfffffffd )
  55. /* The systick is a 24-bit counter. */
  56. #define portMAX_BIT_NUMBER ( SysTimer_MTIME_Msk )
  57. /* A fiddle factor to estimate the number of SysTick counts that would have
  58. occurred while the SysTick counter is stopped during tickless idle
  59. calculations. */
  60. #define portMISSED_COUNTS_FACTOR ( 45UL )
  61. /* Let the user override the pre-loading of the initial LR with the address of
  62. prvTaskExitError() in case it messes up unwinding of the stack in the
  63. debugger. */
  64. #ifdef configTASK_RETURN_ADDRESS
  65. #define portTASK_RETURN_ADDRESS configTASK_RETURN_ADDRESS
  66. #else
  67. #define portTASK_RETURN_ADDRESS prvTaskExitError
  68. #endif
  69. /*
  70. * Setup the timer to generate the tick interrupts. The implementation in this
  71. * file is weak to allow application writers to change the timer used to
  72. * generate the tick interrupt.
  73. */
  74. void vPortSetupTimerInterrupt(void);
  75. /*
  76. * Exception handlers.
  77. */
  78. void xPortSysTickHandler(void);
  79. /*
  80. * Start first task is a separate function so it can be tested in isolation.
  81. */
  82. extern void prvPortStartFirstTask(void) __attribute__((naked));
  83. /*
  84. * Used to catch tasks that attempt to return from their implementing function.
  85. */
  86. static void prvTaskExitError(void);
  87. #define xPortSysTickHandler irqc_mtip_handler
  88. /*-----------------------------------------------------------*/
  89. /* Each task maintains its own interrupt status in the critical nesting
  90. variable. */
  91. static UBaseType_t uxCriticalNesting = 0xaaaaaaaa;
  92. /*
  93. * The number of SysTick increments that make up one tick period.
  94. */
  95. #if( configUSE_TICKLESS_IDLE == 1 )
  96. static TickType_t ulTimerCountsForOneTick = 0;
  97. #endif /* configUSE_TICKLESS_IDLE */
  98. /*
  99. * The maximum number of tick periods that can be suppressed is limited by the
  100. * 24 bit resolution of the SysTick timer.
  101. */
  102. #if( configUSE_TICKLESS_IDLE == 1 )
  103. static TickType_t xMaximumPossibleSuppressedTicks = 0;
  104. #endif /* configUSE_TICKLESS_IDLE */
  105. /*
  106. * Compensate for the CPU cycles that pass while the SysTick is stopped (low
  107. * power functionality only.
  108. */
  109. #if( configUSE_TICKLESS_IDLE == 1 )
  110. static TickType_t ulStoppedTimerCompensation = 0;
  111. #endif /* configUSE_TICKLESS_IDLE */
  112. /*
  113. * Used by the portASSERT_IF_INTERRUPT_PRIORITY_INVALID() macro to ensure
  114. * FreeRTOS API functions are not called from interrupts that have been assigned
  115. * a priority above configMAX_SYSCALL_INTERRUPT_PRIORITY.
  116. */
  117. #if( configASSERT_DEFINED == 1 )
  118. #endif /* configASSERT_DEFINED */
  119. /*-----------------------------------------------------------*/
  120. /*
  121. * See header file for description.
  122. * As per the standard RISC-V ABI pxTopcOfStack is passed in in a0, pxCode in
  123. * a1, and pvParameters in a2. The new top of stack is passed out in a0.
  124. *
  125. * RISC-V maps registers to ABI names as follows (X1 to X31 integer registers
  126. * for the 'I' profile, X1 to X15 for the 'E' profile, currently I assumed).
  127. *
  128. * Register ABI Name Description Saver
  129. * x0 zero Hard-wired zero -
  130. * x1 ra Return address Caller
  131. * x2 sp Stack pointer Callee
  132. * x3 gp Global pointer -
  133. * x4 tp Thread pointer -
  134. * x5-7 t0-2 Temporaries Caller
  135. * x8 s0/fp Saved register/Frame pointer Callee
  136. * x9 s1 Saved register Callee
  137. * x10-11 a0-1 Function Arguments/return values Caller
  138. * x12-17 a2-7 Function arguments Caller
  139. * x18-27 s2-11 Saved registers Callee
  140. * x28-31 t3-6 Temporaries Caller
  141. *
  142. * The RISC-V context is saved RTOS tasks in the following stack frame,
  143. * where the global and thread pointers are currently assumed to be constant so
  144. * are not saved:
  145. *
  146. * mstatus
  147. * #ifndef __riscv_32e
  148. * rsv1
  149. * rsv0
  150. * x31
  151. * x30
  152. * x29
  153. * x28
  154. * x27
  155. * x26
  156. * x25
  157. * x24
  158. * x23
  159. * x22
  160. * x21
  161. * x20
  162. * x19
  163. * x18
  164. * x17
  165. * x16
  166. * #endif
  167. * x15
  168. * x14
  169. * x13
  170. * x12
  171. * x11
  172. * pvParameters
  173. * x9
  174. * x8
  175. * x7
  176. * x6
  177. * x5
  178. * portTASK_RETURN_ADDRESS
  179. * pxCode
  180. */
  181. StackType_t* pxPortInitialiseStack(StackType_t* pxTopOfStack, TaskFunction_t pxCode, void* pvParameters)
  182. {
  183. /* Simulate the stack frame as it would be created by a context switch
  184. interrupt. */
  185. /* Offset added to account for the way the MCU uses the stack on entry/exit
  186. of interrupts, and to ensure alignment. */
  187. pxTopOfStack--;
  188. *pxTopOfStack = portINITIAL_MSTATUS; /* MSTATUS */
  189. /* Save code space by skipping register initialisation. */
  190. #ifndef __riscv_32e
  191. pxTopOfStack -= 24; /* X11 - X31 + 2 reserved. */
  192. #else
  193. pxTopOfStack -= 6; /* X11 - X15. */
  194. #endif
  195. *pxTopOfStack = (StackType_t) pvParameters; /* X10/A0 */
  196. pxTopOfStack -= 6; /* X5 - X9 */
  197. *pxTopOfStack = (StackType_t) portTASK_RETURN_ADDRESS; /* RA, X1 */
  198. pxTopOfStack --;
  199. *pxTopOfStack = ((StackType_t) pxCode) ; /* PC */
  200. return pxTopOfStack;
  201. }
  202. /*-----------------------------------------------------------*/
  203. static void prvTaskExitError(void)
  204. {
  205. volatile uint32_t ulDummy = 0;
  206. /* A function that implements a task must not exit or attempt to return to
  207. its caller as there is nothing to return to. If a task wants to exit it
  208. should instead call vTaskDelete( NULL ).
  209. Artificially force an assert() to be triggered if configASSERT() is
  210. defined, then stop here so application writers can catch the error. */
  211. configASSERT(uxCriticalNesting == ~0UL);
  212. portDISABLE_INTERRUPTS();
  213. while (ulDummy == 0) {
  214. /* This file calls prvTaskExitError() after the scheduler has been
  215. started to remove a compiler warning about the function being defined
  216. but never called. ulDummy is used purely to quieten other warnings
  217. about code appearing after this function is called - making ulDummy
  218. volatile makes the compiler think the function could return and
  219. therefore not output an 'unreachable code' warning for code that appears
  220. after it. */
  221. /* Sleep and wait for interrupt */
  222. __WFI();
  223. }
  224. }
  225. /*-----------------------------------------------------------*/
  226. /*
  227. * See header file for description.
  228. */
  229. BaseType_t xPortStartScheduler(void)
  230. {
  231. __disable_irq();
  232. /* Start the timer that generates the tick ISR. Interrupts are disabled
  233. here already. */
  234. vPortSetupTimerInterrupt();
  235. /* Initialise the critical nesting count ready for the first task. */
  236. uxCriticalNesting = 0;
  237. /* Start the first task. */
  238. prvPortStartFirstTask();
  239. /* Should never get here as the tasks will now be executing! Call the task
  240. exit error function to prevent compiler warnings about a static function
  241. not being called in the case that the application writer overrides this
  242. functionality by defining configTASK_RETURN_ADDRESS. Call
  243. vTaskSwitchContext() so link time optimisation does not remove the
  244. symbol. */
  245. vTaskSwitchContext();
  246. prvTaskExitError();
  247. /* Should not get here! */
  248. return 0;
  249. }
  250. /*-----------------------------------------------------------*/
  251. void vPortEndScheduler(void)
  252. {
  253. /* Not implemented in ports where there is nothing to return to.
  254. Artificially force an assert. */
  255. configASSERT(uxCriticalNesting == 1000UL);
  256. }
  257. /*-----------------------------------------------------------*/
  258. void vPortEnterCritical(void)
  259. {
  260. portDISABLE_INTERRUPTS();
  261. uxCriticalNesting++;
  262. /* This is not the interrupt safe version of the enter critical function so
  263. assert() if it is being called from an interrupt context. Only API
  264. functions that end in "FromISR" can be used in an interrupt. Only assert if
  265. the critical nesting count is 1 to protect against recursive calls if the
  266. assert function also uses a critical section. */
  267. if (uxCriticalNesting == 1) {
  268. }
  269. }
  270. /*-----------------------------------------------------------*/
  271. void vPortExitCritical(void)
  272. {
  273. configASSERT(uxCriticalNesting);
  274. uxCriticalNesting--;
  275. if (uxCriticalNesting == 0) {
  276. portENABLE_INTERRUPTS();
  277. }
  278. }
  279. /*-----------------------------------------------------------*/
  280. void vPortAssert(int32_t x)
  281. {
  282. TaskHandle_t th;
  283. if ((x) == 0) {
  284. taskDISABLE_INTERRUPTS();
  285. #if (INCLUDE_xTaskGetCurrentTaskHandle == 1)
  286. th = xTaskGetCurrentTaskHandle();
  287. if (th) {
  288. printf("Assert in task %s\n", pcTaskGetName(th));
  289. }
  290. #endif
  291. while (1) {
  292. /* Sleep and wait for interrupt */
  293. __WFI();
  294. };
  295. }
  296. }
  297. /*-----------------------------------------------------------*/
  298. void xPortTaskSwitch(void)
  299. {
  300. portDISABLE_INTERRUPTS();
  301. /* Clear Software IRQ, A MUST */
  302. SysTimer_ClearSWIRQ();
  303. vTaskSwitchContext();
  304. portENABLE_INTERRUPTS();
  305. }
  306. /*-----------------------------------------------------------*/
  307. __INTERRUPT void xPortSysTickHandler(void)
  308. {
  309. /* The SysTick runs at the lowest interrupt priority, so when this interrupt
  310. executes all interrupts must be unmasked. There is therefore no need to
  311. save and then restore the interrupt mask value as its value is already
  312. known. */
  313. portDISABLE_INTERRUPTS();
  314. {
  315. SysTick_Reload(SYSTICK_TICK_CONST);
  316. /* Increment the RTOS tick. */
  317. if (xTaskIncrementTick() != pdFALSE) {
  318. /* A context switch is required. Context switching is performed in
  319. the SWI interrupt. Pend the SWI interrupt. */
  320. portYIELD();
  321. }
  322. }
  323. portENABLE_INTERRUPTS();
  324. }
  325. /*-----------------------------------------------------------*/
  326. #if( configUSE_TICKLESS_IDLE == 1 )
  327. __attribute__((weak)) void vPortSuppressTicksAndSleep(TickType_t xExpectedIdleTime)
  328. {
  329. uint32_t ulReloadValue, ulCompleteTickPeriods, ulCompletedSysTickDecrements;
  330. volatile TickType_t xModifiableIdleTime, xTickCountBeforeSleep, XLastLoadValue;
  331. FREERTOS_PORT_DEBUG("Enter TickLess %d\n", (uint32_t)xExpectedIdleTime);
  332. /* Make sure the SysTick reload value does not overflow the counter. */
  333. if (xExpectedIdleTime > xMaximumPossibleSuppressedTicks) {
  334. xExpectedIdleTime = xMaximumPossibleSuppressedTicks;
  335. }
  336. /* Stop the SysTick momentarily. The time the SysTick is stopped for
  337. is accounted for as best it can be, but using the tickless mode will
  338. inevitably result in some tiny drift of the time maintained by the
  339. kernel with respect to calendar time. */
  340. SysTimer_Stop();
  341. /* Calculate the reload value required to wait xExpectedIdleTime
  342. tick periods. -1 is used because this code will execute part way
  343. through one of the tick periods. */
  344. ulReloadValue = (ulTimerCountsForOneTick * (xExpectedIdleTime - 1UL));
  345. if (ulReloadValue > ulStoppedTimerCompensation) {
  346. ulReloadValue -= ulStoppedTimerCompensation;
  347. }
  348. /* Enter a critical section but don't use the taskENTER_CRITICAL()
  349. method as that will mask interrupts that should exit sleep mode. */
  350. __disable_irq();
  351. /* If a context switch is pending or a task is waiting for the scheduler
  352. to be unsuspended then abandon the low power entry. */
  353. if (eTaskConfirmSleepModeStatus() == eAbortSleep) {
  354. /* Restart from whatever is left in the count register to complete
  355. this tick period. */
  356. /* Restart SysTick. */
  357. SysTimer_Start();
  358. /* Reset the reload register to the value required for normal tick
  359. periods. */
  360. SysTick_Reload(ulTimerCountsForOneTick);
  361. /* Re-enable interrupts - see comments above the cpsid instruction()
  362. above. */
  363. __enable_irq();
  364. } else {
  365. xTickCountBeforeSleep = xTaskGetTickCount();
  366. /* Set the new reload value. */
  367. SysTick_Reload(ulReloadValue);
  368. /* Get System timer load value before sleep */
  369. XLastLoadValue = SysTimer_GetLoadValue();
  370. /* Restart SysTick. */
  371. SysTimer_Start();
  372. IRQC_EnableIRQ(SysTimer_IRQn);
  373. __RWMB();
  374. /* Sleep until something happens. configPRE_SLEEP_PROCESSING() can
  375. set its parameter to 0 to indicate that its implementation contains
  376. its own wait for interrupt or wait for event instruction, and so wfi
  377. should not be executed again. However, the original expected idle
  378. time variable must remain unmodified, so a copy is taken. */
  379. xModifiableIdleTime = xExpectedIdleTime;
  380. configPRE_SLEEP_PROCESSING(xModifiableIdleTime);
  381. if (xModifiableIdleTime > 0) {
  382. __WFI();
  383. }
  384. configPOST_SLEEP_PROCESSING(xExpectedIdleTime);
  385. /* Re-enable interrupts to allow the interrupt that brought the MCU
  386. out of sleep mode to execute immediately. */
  387. __enable_irq();
  388. /* Make sure interrupt enable is executed */
  389. __RWMB();
  390. __FENCE_I();
  391. __NOP();
  392. /* Disable interrupts again because the clock is about to be stopped
  393. and interrupts that execute while the clock is stopped will increase
  394. any slippage between the time maintained by the RTOS and calendar
  395. time. */
  396. __disable_irq();
  397. /* Disable the SysTick clock. Again,
  398. the time the SysTick is stopped for is accounted for as best it can
  399. be, but using the tickless mode will inevitably result in some tiny
  400. drift of the time maintained by the kernel with respect to calendar
  401. time*/
  402. IRQC_DisableIRQ(SysTimer_IRQn);
  403. /* Determine if SysTimer Interrupt is not yet happened,
  404. (in which case an interrupt other than the SysTick
  405. must have brought the system out of sleep mode). */
  406. if (SysTimer_GetLoadValue() >= (XLastLoadValue + ulReloadValue)) {
  407. /* As the pending tick will be processed as soon as this
  408. function exits, the tick value maintained by the tick is stepped
  409. forward by one less than the time spent waiting. */
  410. ulCompleteTickPeriods = xExpectedIdleTime - 1UL;
  411. FREERTOS_PORT_DEBUG("TickLess - SysTimer Interrupt Entered!\n");
  412. } else {
  413. /* Something other than the tick interrupt ended the sleep.
  414. Work out how long the sleep lasted rounded to complete tick
  415. periods (not the ulReload value which accounted for part
  416. ticks). */
  417. xModifiableIdleTime = SysTimer_GetLoadValue();
  418. if (xModifiableIdleTime > XLastLoadValue) {
  419. ulCompletedSysTickDecrements = (xModifiableIdleTime - XLastLoadValue);
  420. } else {
  421. ulCompletedSysTickDecrements = (xModifiableIdleTime + portMAX_BIT_NUMBER - XLastLoadValue);
  422. }
  423. /* How many complete tick periods passed while the processor
  424. was waiting? */
  425. ulCompleteTickPeriods = ulCompletedSysTickDecrements / ulTimerCountsForOneTick;
  426. /* The reload value is set to whatever fraction of a single tick
  427. period remains. */
  428. SysTick_Reload(ulTimerCountsForOneTick);
  429. FREERTOS_PORT_DEBUG("TickLess - External Interrupt Happened!\n");
  430. }
  431. FREERTOS_PORT_DEBUG("End TickLess %d\n", (uint32_t)ulCompleteTickPeriods);
  432. /* Restart SysTick */
  433. vTaskStepTick(ulCompleteTickPeriods);
  434. /* Exit with interrupts enabled. */
  435. IRQC_EnableIRQ(SysTimer_IRQn);
  436. __enable_irq();
  437. }
  438. }
  439. #endif /* #if configUSE_TICKLESS_IDLE */
  440. /*-----------------------------------------------------------*/
  441. /*
  442. * Setup the systick timer to generate the tick interrupts at the required
  443. * frequency.
  444. */
  445. __attribute__((weak)) void vPortSetupTimerInterrupt(void)
  446. {
  447. /* Calculate the constants required to configure the tick interrupt. */
  448. #if( configUSE_TICKLESS_IDLE == 1 )
  449. {
  450. ulTimerCountsForOneTick = (SYSTICK_TICK_CONST);
  451. xMaximumPossibleSuppressedTicks = portMAX_BIT_NUMBER / ulTimerCountsForOneTick;
  452. ulStoppedTimerCompensation = portMISSED_COUNTS_FACTOR / (configCPU_CLOCK_HZ / configSYSTICK_CLOCK_HZ);
  453. FREERTOS_PORT_DEBUG("CountsForOneTick, SuppressedTicks and TimerCompensation: %u, %u, %u\n", \
  454. (uint32_t)ulTimerCountsForOneTick, (uint32_t)xMaximumPossibleSuppressedTicks, (uint32_t)ulStoppedTimerCompensation);
  455. }
  456. #endif /* configUSE_TICKLESS_IDLE */
  457. TickType_t ticks = SYSTICK_TICK_CONST;
  458. /* Make SWI and SysTick the lowest priority interrupts. */
  459. /* Stop and clear the SysTimer. SysTimer as Vector Interrupt */
  460. SysTick_Config(ticks);
  461. IRQC_EnableIRQ(SysTimer_IRQn);
  462. /* Set as Vector Interrupt */
  463. IRQC_EnableIRQ(SysTimerSW_IRQn);
  464. }
  465. /*-----------------------------------------------------------*/
  466. /*-----------------------------------------------------------*/
  467. #if( configASSERT_DEFINED == 1 )
  468. void vPortValidateInterruptPriority(void)
  469. {
  470. }
  471. #endif /* configASSERT_DEFINED */