port.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  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. // See function prvCheckMaxSysCallPrio and prvCalcMaxSysCallMTH
  48. #define configMAX_SYSCALL_INTERRUPT_PRIORITY 255
  49. #endif
  50. /* Constants required to check the validity of an interrupt priority. */
  51. #define portFIRST_USER_INTERRUPT_NUMBER ( 18 )
  52. #define SYSTICK_TICK_CONST (configSYSTICK_CLOCK_HZ / configTICK_RATE_HZ)
  53. /* Masks off all bits but the ECLIC MTH bits in the MTH register. */
  54. #define portMTH_MASK ( 0xFFUL )
  55. /* Constants required to set up the initial stack. */
  56. #define portINITIAL_MSTATUS ( MSTATUS_MPP | MSTATUS_MPIE | MSTATUS_FS_INITIAL | MSTATUS_VS_INITIAL)
  57. #define portINITIAL_EXC_RETURN ( 0xfffffffd )
  58. /* The systick is a 64-bit counter. */
  59. #define portMAX_BIT_NUMBER ( SysTimer_MTIMER_Msk )
  60. /* A fiddle factor to estimate the number of SysTick counts that would have
  61. occurred while the SysTick counter is stopped during tickless idle
  62. calculations. */
  63. #define portMISSED_COUNTS_FACTOR ( 45UL )
  64. /* Let the user override the pre-loading of the initial LR with the address of
  65. prvTaskExitError() in case it messes up unwinding of the stack in the
  66. debugger. */
  67. #ifdef configTASK_RETURN_ADDRESS
  68. #define portTASK_RETURN_ADDRESS configTASK_RETURN_ADDRESS
  69. #else
  70. #define portTASK_RETURN_ADDRESS prvTaskExitError
  71. #endif
  72. /*
  73. * Setup the timer to generate the tick interrupts. The implementation in this
  74. * file is weak to allow application writers to change the timer used to
  75. * generate the tick interrupt.
  76. */
  77. void vPortSetupTimerInterrupt(void);
  78. /*
  79. * Exception handlers.
  80. */
  81. void xPortSysTickHandler(void);
  82. /*
  83. * Start first task is a separate function so it can be tested in isolation.
  84. */
  85. extern void prvPortStartFirstTask(void) __attribute__((naked));
  86. /*
  87. * Used to catch tasks that attempt to return from their implementing function.
  88. */
  89. static void prvTaskExitError(void);
  90. #define xPortSysTickHandler eclic_mtip_handler
  91. /*-----------------------------------------------------------*/
  92. /* Each task maintains its own interrupt status in the critical nesting
  93. variable. */
  94. #if ( configNUMBER_OF_CORES == 1 )
  95. UBaseType_t uxCriticalNesting = 0xaaaaaaaa;
  96. #else /* #if ( configNUMBER_OF_CORES == 1 ) */
  97. UBaseType_t uxCriticalNestings[ configNUMBER_OF_CORES ] = { 0 };
  98. #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
  99. /*
  100. * Record the real MTH calculated by the configMAX_SYSCALL_INTERRUPT_PRIORITY
  101. * The configMAX_SYSCALL_INTERRUPT_PRIORITY is not the left-aligned level value,
  102. * See equations below:
  103. * Level Bits number: lvlbits = min(nlbits, CLICINTCTLBITS)
  104. * Left align Bits number: lfabits = 8-lvlbits
  105. * 0 < configMAX_SYSCALL_INTERRUPT_PRIORITY <= (2^lvlbits-1)
  106. * uxMaxSysCallMTH = (configMAX_SYSCALL_INTERRUPT_PRIORITY << lfabits) | ((2^lfabits)-1)
  107. * If nlbits = 3, CLICINTCTLBITS=3, then lvlbits = 3, lfabits = 5
  108. * Set configMAX_SYSCALL_INTERRUPT_PRIORITY to 6
  109. * Then uxMaxSysCallMTH = (6<<5) | (2^5 - 1) = 223
  110. *
  111. * See function prvCheckMaxSysCallPrio and prvCalcMaxSysCallMTH
  112. */
  113. uint8_t uxMaxSysCallMTH = 255;
  114. /*
  115. * The number of SysTick increments that make up one tick period.
  116. */
  117. #if( configUSE_TICKLESS_IDLE == 1 )
  118. static TickType_t ulTimerCountsForOneTick = 0;
  119. #endif /* configUSE_TICKLESS_IDLE */
  120. /*
  121. * The maximum number of tick periods that can be suppressed is limited by the
  122. * 24 bit resolution of the SysTick timer.
  123. */
  124. #if( configUSE_TICKLESS_IDLE == 1 )
  125. static TickType_t xMaximumPossibleSuppressedTicks = 0;
  126. #endif /* configUSE_TICKLESS_IDLE */
  127. /*
  128. * Compensate for the CPU cycles that pass while the SysTick is stopped (low
  129. * power functionality only.
  130. */
  131. #if( configUSE_TICKLESS_IDLE == 1 )
  132. static TickType_t ulStoppedTimerCompensation = 0;
  133. #endif /* configUSE_TICKLESS_IDLE */
  134. /*
  135. * Used by the portASSERT_IF_INTERRUPT_PRIORITY_INVALID() macro to ensure
  136. * FreeRTOS API functions are not called from interrupts that have been assigned
  137. * a priority above configMAX_SYSCALL_INTERRUPT_PRIORITY.
  138. */
  139. #if( configASSERT_DEFINED == 1 )
  140. static uint8_t ucMaxSysCallPriority = 0;
  141. #endif /* configASSERT_DEFINED */
  142. #if ( configNUMBER_OF_CORES > 1 )
  143. spin_lock_t hw_sync_locks[portRTOS_SPINLOCK_COUNT] = {0, 0};
  144. /* Note this is a single method with uxAcquire parameter since we have
  145. * static vars, the method is always called with a compile time constant for
  146. * uxAcquire, and the compiler should do the right thing! */
  147. void vPortRecursiveLock(unsigned long ulLockNum, spin_lock_t *pxSpinLock, BaseType_t uxAcquire)
  148. {
  149. static uint8_t ucOwnedByCore[portMAX_CORE_COUNT];
  150. static uint8_t ucRecursionCountByLock[portRTOS_SPINLOCK_COUNT];
  151. configASSERT(ulLockNum < portRTOS_SPINLOCK_COUNT);
  152. unsigned long ulCoreNum = __get_hart_index();
  153. unsigned long ulLockBit = 1u << ulLockNum;
  154. configASSERT(ulLockBit < 256u);
  155. if (uxAcquire) {
  156. if ((!*pxSpinLock == 0)) {
  157. if (ucOwnedByCore[ulCoreNum] & ulLockBit) {
  158. configASSERT(ucRecursionCountByLock[ulLockNum] != 255u);
  159. ucRecursionCountByLock[ulLockNum]++;
  160. return;
  161. }
  162. while ((!*pxSpinLock == 0)) {
  163. }
  164. }
  165. do {
  166. if (__AMOSWAP_W(pxSpinLock, 1) == 0) {
  167. break;
  168. }
  169. } while (1);
  170. configASSERT(ucRecursionCountByLock[ulLockNum] == 0);
  171. ucRecursionCountByLock[ulLockNum] = 1;
  172. ucOwnedByCore[ulCoreNum] |= ulLockBit;
  173. } else {
  174. configASSERT((ucOwnedByCore[ulCoreNum] & ulLockBit) != 0);
  175. configASSERT(ucRecursionCountByLock[ulLockNum] != 0);
  176. if (!--ucRecursionCountByLock[ulLockNum]) {
  177. ucOwnedByCore[ulCoreNum] &= ~ulLockBit;
  178. *pxSpinLock = 0;
  179. }
  180. }
  181. }
  182. #endif
  183. static unsigned long ulSchedulerReady = 0;
  184. /*-----------------------------------------------------------*/
  185. /*
  186. * See header file for description.
  187. * As per the standard RISC-V ABI pxTopcOfStack is passed in in a0, pxCode in
  188. * a1, and pvParameters in a2. The new top of stack is passed out in a0.
  189. *
  190. * RISC-V maps registers to ABI names as follows (X1 to X31 integer registers
  191. * for the 'I' profile, X1 to X15 for the 'E' profile, currently I assumed).
  192. *
  193. * Register ABI Name Description Saver
  194. * x0 zero Hard-wired zero -
  195. * x1 ra Return address Caller
  196. * x2 sp Stack pointer Callee
  197. * x3 gp Global pointer -
  198. * x4 tp Thread pointer -
  199. * x5-7 t0-2 Temporaries Caller
  200. * x8 s0/fp Saved register/Frame pointer Callee
  201. * x9 s1 Saved register Callee
  202. * x10-11 a0-1 Function Arguments/return values Caller
  203. * x12-17 a2-7 Function arguments Caller
  204. * x18-27 s2-11 Saved registers Callee
  205. * x28-31 t3-6 Temporaries Caller
  206. *
  207. * The RISC-V context is saved RTOS tasks in the following stack frame,
  208. * where the global and thread pointers are currently assumed to be constant so
  209. * are not saved:
  210. *
  211. * mstatus
  212. * #ifndef __riscv_32e
  213. * x31
  214. * x30
  215. * x29
  216. * x28
  217. * x27
  218. * x26
  219. * x25
  220. * x24
  221. * x23
  222. * x22
  223. * x21
  224. * x20
  225. * x19
  226. * x18
  227. * x17
  228. * x16
  229. * #endif
  230. * x15
  231. * x14
  232. * x13
  233. * x12
  234. * x11
  235. * pvParameters
  236. * x9
  237. * x8
  238. * x7
  239. * x6
  240. * x5
  241. * portTASK_RETURN_ADDRESS
  242. * pxCode
  243. */
  244. StackType_t* pxPortInitialiseStack(StackType_t* pxTopOfStack, TaskFunction_t pxCode, void* pvParameters)
  245. {
  246. /* Simulate the stack frame as it would be created by a context switch
  247. interrupt. */
  248. /* Offset added to account for the way the MCU uses the stack on entry/exit
  249. of interrupts, and to ensure alignment. */
  250. pxTopOfStack--;
  251. *pxTopOfStack = portINITIAL_MSTATUS; /* MSTATUS */
  252. /* Save code space by skipping register initialisation. */
  253. #ifndef __riscv_32e
  254. pxTopOfStack -= 22; /* X11 - X31. */
  255. #else
  256. pxTopOfStack -= 6; /* X11 - X15. */
  257. #endif
  258. *pxTopOfStack = (StackType_t) pvParameters; /* X10/A0 */
  259. pxTopOfStack -= 6; /* X5 - X9 */
  260. *pxTopOfStack = (StackType_t) portTASK_RETURN_ADDRESS; /* RA, X1 */
  261. pxTopOfStack --;
  262. *pxTopOfStack = ((StackType_t) pxCode) ; /* PC */
  263. return pxTopOfStack;
  264. }
  265. /*-----------------------------------------------------------*/
  266. static void prvTaskExitError(void)
  267. {
  268. volatile uint32_t ulDummy = 0;
  269. /* A function that implements a task must not exit or attempt to return to
  270. its caller as there is nothing to return to. If a task wants to exit it
  271. should instead call vTaskDelete( NULL ).
  272. Artificially force an assert() to be triggered if configASSERT() is
  273. defined, then stop here so application writers can catch the error. */
  274. configASSERT(portGET_CRITICAL_NESTING_COUNT() == ~0UL);
  275. portDISABLE_INTERRUPTS();
  276. while (ulDummy == 0) {
  277. /* This file calls prvTaskExitError() after the scheduler has been
  278. started to remove a compiler warning about the function being defined
  279. but never called. ulDummy is used purely to quieten other warnings
  280. about code appearing after this function is called - making ulDummy
  281. volatile makes the compiler think the function could return and
  282. therefore not output an 'unreachable code' warning for code that appears
  283. after it. */
  284. /* Sleep and wait for interrupt */
  285. __WFI();
  286. }
  287. }
  288. /*-----------------------------------------------------------*/
  289. static uint8_t prvCheckMaxSysCallPrio(uint8_t max_syscall_prio)
  290. {
  291. uint8_t nlbits = __ECLIC_GetCfgNlbits();
  292. uint8_t intctlbits = __ECLIC_INTCTLBITS;
  293. uint8_t lvlbits, temp;
  294. if (nlbits <= intctlbits) {
  295. lvlbits = nlbits;
  296. } else {
  297. lvlbits = intctlbits;
  298. }
  299. temp = ((1 << lvlbits) - 1);
  300. if (max_syscall_prio > temp) {
  301. max_syscall_prio = temp;
  302. }
  303. return max_syscall_prio;
  304. }
  305. static uint8_t prvCalcMaxSysCallMTH(uint8_t max_syscall_prio)
  306. {
  307. uint8_t nlbits = __ECLIC_GetCfgNlbits();
  308. uint8_t intctlbits = __ECLIC_INTCTLBITS;
  309. uint8_t lvlbits, lfabits;
  310. uint8_t maxsyscallmth = 0;
  311. uint8_t temp;
  312. if (nlbits <= intctlbits) {
  313. lvlbits = nlbits;
  314. } else {
  315. lvlbits = intctlbits;
  316. }
  317. lfabits = 8 - lvlbits;
  318. temp = ((1 << lvlbits) - 1);
  319. if (max_syscall_prio > temp) {
  320. max_syscall_prio = temp;
  321. }
  322. maxsyscallmth = (max_syscall_prio << lfabits) | ((1 << lfabits) - 1);
  323. return maxsyscallmth;
  324. }
  325. /*
  326. * See header file for description.
  327. */
  328. BaseType_t xPortStartScheduler(void)
  329. {
  330. /* configMAX_SYSCALL_INTERRUPT_PRIORITY must not be set to 0. */
  331. configASSERT(configMAX_SYSCALL_INTERRUPT_PRIORITY);
  332. /* Get the real MTH should be set to ECLIC MTH register */
  333. uxMaxSysCallMTH = prvCalcMaxSysCallMTH(configMAX_SYSCALL_INTERRUPT_PRIORITY);
  334. FREERTOS_PORT_DEBUG("Max SysCall MTH is set to 0x%x\n", uxMaxSysCallMTH);
  335. #if( configASSERT_DEFINED == 1 )
  336. {
  337. /* Use the same mask on the maximum system call priority. */
  338. ucMaxSysCallPriority = prvCheckMaxSysCallPrio(configMAX_SYSCALL_INTERRUPT_PRIORITY);
  339. FREERTOS_PORT_DEBUG("Max SysCall Priority is set to %d\n", ucMaxSysCallPriority);
  340. }
  341. #endif /* conifgASSERT_DEFINED */
  342. __disable_irq();
  343. #if ( configNUMBER_OF_CORES > 1 )
  344. if (__get_hart_index() == BOOT_HARTID) {
  345. ulSchedulerReady = 1;
  346. } else {
  347. // other cores wait for scheduler ready signal
  348. while (ulSchedulerReady == 0);
  349. }
  350. #endif
  351. /* Start the timer that generates the tick ISR. Interrupts are disabled
  352. here already. */
  353. vPortSetupTimerInterrupt();
  354. /* Initialise the critical nesting count ready for the first task. */
  355. portSET_CRITICAL_NESTING_COUNT(0);
  356. /* Initialise base priority to zero. */
  357. vPortSetBASEPRI(0);
  358. /* Start the first task. */
  359. prvPortStartFirstTask();
  360. /* Should never get here as the tasks will now be executing! Call the task
  361. exit error function to prevent compiler warnings about a static function
  362. not being called in the case that the application writer overrides this
  363. functionality by defining configTASK_RETURN_ADDRESS. Call
  364. vTaskSwitchContext() so link time optimisation does not remove the
  365. symbol. */
  366. #if ( configNUMBER_OF_CORES > 1 )
  367. vTaskSwitchContext( portGET_CORE_ID() );
  368. #else
  369. vTaskSwitchContext();
  370. #endif
  371. prvTaskExitError();
  372. /* Should not get here! */
  373. return 0;
  374. }
  375. /*-----------------------------------------------------------*/
  376. void vPortEndScheduler(void)
  377. {
  378. /* Not implemented in ports where there is nothing to return to.
  379. Artificially force an assert. */
  380. configASSERT(portGET_CRITICAL_NESTING_COUNT() == 1000UL);
  381. }
  382. /*-----------------------------------------------------------*/
  383. void vPortEnterCritical(void)
  384. {
  385. portDISABLE_INTERRUPTS();
  386. portINCREMENT_CRITICAL_NESTING_COUNT();
  387. /* This is not the interrupt safe version of the enter critical function so
  388. assert() if it is being called from an interrupt context. Only API
  389. functions that end in "FromISR" can be used in an interrupt. Only assert if
  390. the critical nesting count is 1 to protect against recursive calls if the
  391. assert function also uses a critical section. */
  392. if (portGET_CRITICAL_NESTING_COUNT() == 1) {
  393. configASSERT((__ECLIC_GetMth() & portMTH_MASK) == uxMaxSysCallMTH);
  394. }
  395. }
  396. /*-----------------------------------------------------------*/
  397. void vPortExitCritical(void)
  398. {
  399. configASSERT(portGET_CRITICAL_NESTING_COUNT());
  400. portDECREMENT_CRITICAL_NESTING_COUNT();
  401. if (portGET_CRITICAL_NESTING_COUNT() == 0) {
  402. portENABLE_INTERRUPTS();
  403. }
  404. }
  405. /*-----------------------------------------------------------*/
  406. void vPortAssert(int32_t x)
  407. {
  408. TaskHandle_t th;
  409. if ((x) == 0) {
  410. taskDISABLE_INTERRUPTS();
  411. #if (INCLUDE_xTaskGetCurrentTaskHandle == 1)
  412. th = xTaskGetCurrentTaskHandle();
  413. if (th) {
  414. printf("Assert in task %s\n", pcTaskGetName(th));
  415. }
  416. #endif
  417. while (1) {
  418. /* Sleep and wait for interrupt */
  419. __WFI();
  420. };
  421. }
  422. }
  423. /*-----------------------------------------------------------*/
  424. void xPortTaskSwitch(void)
  425. {
  426. portDISABLE_INTERRUPTS();
  427. /* Clear Software IRQ, A MUST */
  428. SysTimer_ClearSWIRQ();
  429. #if ( configNUMBER_OF_CORES > 1 )
  430. vTaskSwitchContext( portGET_CORE_ID() );
  431. #else
  432. vTaskSwitchContext();
  433. #endif
  434. portENABLE_INTERRUPTS();
  435. }
  436. /*-----------------------------------------------------------*/
  437. void xPortSysTickHandler(void)
  438. {
  439. /* The SysTick runs at the lowest interrupt priority, so when this interrupt
  440. executes all interrupts must be unmasked. There is therefore no need to
  441. save and then restore the interrupt mask value as its value is already
  442. known. */
  443. #if ( configNUMBER_OF_CORES == 1 )
  444. portDISABLE_INTERRUPTS();
  445. {
  446. SysTick_Reload(SYSTICK_TICK_CONST);
  447. /* Increment the RTOS tick. */
  448. if (xTaskIncrementTick() != pdFALSE) {
  449. /* A context switch is required. Context switching is performed in
  450. the SWI interrupt. Pend the SWI interrupt. */
  451. portYIELD();
  452. }
  453. }
  454. portENABLE_INTERRUPTS();
  455. #else
  456. UBaseType_t ulPreviousMask;
  457. /* Tasks or ISRs running on other cores may still in critical section in
  458. * multiple cores environment. Incrementing tick needs to performed in
  459. * critical section. */
  460. ulPreviousMask = taskENTER_CRITICAL_FROM_ISR();
  461. {
  462. SysTick_Reload(SYSTICK_TICK_CONST);
  463. /* Increment the RTOS tick. */
  464. if (xTaskIncrementTick() != pdFALSE) {
  465. /* A context switch is required. Context switching is performed in
  466. the SWI interrupt. Pend the SWI interrupt. */
  467. portYIELD();
  468. }
  469. }
  470. taskEXIT_CRITICAL_FROM_ISR( ulPreviousMask );
  471. #endif
  472. }
  473. /*-----------------------------------------------------------*/
  474. #if( configUSE_TICKLESS_IDLE == 1 )
  475. __attribute__((weak)) void vPortSuppressTicksAndSleep(TickType_t xExpectedIdleTime)
  476. {
  477. uint32_t ulReloadValue, ulCompleteTickPeriods, ulCompletedSysTickDecrements;
  478. volatile TickType_t xModifiableIdleTime, xTickCountBeforeSleep, XLastLoadValue;
  479. FREERTOS_PORT_DEBUG("Enter TickLess %d\n", (uint32_t)xExpectedIdleTime);
  480. /* Make sure the SysTick reload value does not overflow the counter. */
  481. if (xExpectedIdleTime > xMaximumPossibleSuppressedTicks) {
  482. xExpectedIdleTime = xMaximumPossibleSuppressedTicks;
  483. }
  484. /* Stop the SysTick momentarily. The time the SysTick is stopped for
  485. is accounted for as best it can be, but using the tickless mode will
  486. inevitably result in some tiny drift of the time maintained by the
  487. kernel with respect to calendar time. */
  488. SysTimer_Stop();
  489. /* Calculate the reload value required to wait xExpectedIdleTime
  490. tick periods. -1 is used because this code will execute part way
  491. through one of the tick periods. */
  492. ulReloadValue = (ulTimerCountsForOneTick * (xExpectedIdleTime - 1UL));
  493. if (ulReloadValue > ulStoppedTimerCompensation) {
  494. ulReloadValue -= ulStoppedTimerCompensation;
  495. }
  496. /* Enter a critical section but don't use the taskENTER_CRITICAL()
  497. method as that will mask interrupts that should exit sleep mode. */
  498. __disable_irq();
  499. /* If a context switch is pending or a task is waiting for the scheduler
  500. to be unsuspended then abandon the low power entry. */
  501. if (eTaskConfirmSleepModeStatus() == eAbortSleep) {
  502. /* Restart from whatever is left in the count register to complete
  503. this tick period. */
  504. /* Restart SysTick. */
  505. SysTimer_Start();
  506. /* Reset the reload register to the value required for normal tick
  507. periods. */
  508. SysTick_Reload(ulTimerCountsForOneTick);
  509. /* Re-enable interrupts - see comments above the cpsid instruction()
  510. above. */
  511. __enable_irq();
  512. } else {
  513. xTickCountBeforeSleep = xTaskGetTickCount();
  514. /* Set the new reload value. */
  515. SysTick_Reload(ulReloadValue);
  516. /* Get System timer load value before sleep */
  517. XLastLoadValue = SysTimer_GetLoadValue();
  518. /* Restart SysTick. */
  519. SysTimer_Start();
  520. ECLIC_EnableIRQ(SysTimer_IRQn);
  521. __RWMB();
  522. /* Sleep until something happens. configPRE_SLEEP_PROCESSING() can
  523. set its parameter to 0 to indicate that its implementation contains
  524. its own wait for interrupt or wait for event instruction, and so wfi
  525. should not be executed again. However, the original expected idle
  526. time variable must remain unmodified, so a copy is taken. */
  527. xModifiableIdleTime = xExpectedIdleTime;
  528. configPRE_SLEEP_PROCESSING(xModifiableIdleTime);
  529. if (xModifiableIdleTime > 0) {
  530. __WFI();
  531. }
  532. configPOST_SLEEP_PROCESSING(xExpectedIdleTime);
  533. /* Re-enable interrupts to allow the interrupt that brought the MCU
  534. out of sleep mode to execute immediately. */
  535. __enable_irq();
  536. /* Make sure interrupt enable is executed */
  537. __RWMB();
  538. __FENCE_I();
  539. __NOP();
  540. /* Disable interrupts again because the clock is about to be stopped
  541. and interrupts that execute while the clock is stopped will increase
  542. any slippage between the time maintained by the RTOS and calendar
  543. time. */
  544. __disable_irq();
  545. /* Disable the SysTick clock. Again,
  546. the time the SysTick is stopped for is accounted for as best it can
  547. be, but using the tickless mode will inevitably result in some tiny
  548. drift of the time maintained by the kernel with respect to calendar
  549. time*/
  550. ECLIC_DisableIRQ(SysTimer_IRQn);
  551. /* Determine if SysTimer Interrupt is not yet happened,
  552. (in which case an interrupt other than the SysTick
  553. must have brought the system out of sleep mode). */
  554. if (SysTimer_GetLoadValue() >= (XLastLoadValue + ulReloadValue)) {
  555. /* As the pending tick will be processed as soon as this
  556. function exits, the tick value maintained by the tick is stepped
  557. forward by one less than the time spent waiting. */
  558. ulCompleteTickPeriods = xExpectedIdleTime - 1UL;
  559. FREERTOS_PORT_DEBUG("TickLess - SysTimer Interrupt Entered!\n");
  560. } else {
  561. /* Something other than the tick interrupt ended the sleep.
  562. Work out how long the sleep lasted rounded to complete tick
  563. periods (not the ulReload value which accounted for part
  564. ticks). */
  565. xModifiableIdleTime = SysTimer_GetLoadValue();
  566. if (xModifiableIdleTime > XLastLoadValue) {
  567. ulCompletedSysTickDecrements = (xModifiableIdleTime - XLastLoadValue);
  568. } else {
  569. ulCompletedSysTickDecrements = (xModifiableIdleTime + portMAX_BIT_NUMBER - XLastLoadValue);
  570. }
  571. /* How many complete tick periods passed while the processor
  572. was waiting? */
  573. ulCompleteTickPeriods = ulCompletedSysTickDecrements / ulTimerCountsForOneTick;
  574. /* The reload value is set to whatever fraction of a single tick
  575. period remains. */
  576. SysTick_Reload(ulTimerCountsForOneTick);
  577. FREERTOS_PORT_DEBUG("TickLess - External Interrupt Happened!\n");
  578. }
  579. FREERTOS_PORT_DEBUG("End TickLess %d\n", (uint32_t)ulCompleteTickPeriods);
  580. /* Restart SysTick */
  581. vTaskStepTick(ulCompleteTickPeriods);
  582. /* Exit with interrupts enabled. */
  583. ECLIC_EnableIRQ(SysTimer_IRQn);
  584. __enable_irq();
  585. }
  586. }
  587. #endif /* #if configUSE_TICKLESS_IDLE */
  588. /*-----------------------------------------------------------*/
  589. /*
  590. * Setup the systick timer to generate the tick interrupts at the required
  591. * frequency.
  592. */
  593. __attribute__((weak)) void vPortSetupTimerInterrupt(void)
  594. {
  595. /* Calculate the constants required to configure the tick interrupt. */
  596. #if( configUSE_TICKLESS_IDLE == 1 )
  597. {
  598. ulTimerCountsForOneTick = (SYSTICK_TICK_CONST);
  599. xMaximumPossibleSuppressedTicks = portMAX_BIT_NUMBER / ulTimerCountsForOneTick;
  600. ulStoppedTimerCompensation = portMISSED_COUNTS_FACTOR / (configCPU_CLOCK_HZ / configSYSTICK_CLOCK_HZ);
  601. FREERTOS_PORT_DEBUG("CountsForOneTick, SuppressedTicks and TimerCompensation: %u, %u, %u\n", \
  602. (uint32_t)ulTimerCountsForOneTick, (uint32_t)xMaximumPossibleSuppressedTicks, (uint32_t)ulStoppedTimerCompensation);
  603. }
  604. #endif /* configUSE_TICKLESS_IDLE */
  605. TickType_t ticks = SYSTICK_TICK_CONST;
  606. /* Make SWI and SysTick the lowest priority interrupts. */
  607. /* Stop and clear the SysTimer. SysTimer as Non-Vector Interrupt */
  608. #if ( configNUMBER_OF_CORES > 1 )
  609. if (__get_hart_index() == BOOT_HARTID) {
  610. #else
  611. if (1) {
  612. #endif
  613. SysTick_Config(ticks);
  614. ECLIC_DisableIRQ(SysTimer_IRQn);
  615. ECLIC_SetLevelIRQ(SysTimer_IRQn, configKERNEL_INTERRUPT_PRIORITY);
  616. ECLIC_SetShvIRQ(SysTimer_IRQn, ECLIC_NON_VECTOR_INTERRUPT);
  617. ECLIC_EnableIRQ(SysTimer_IRQn);
  618. }
  619. /* Set SWI interrupt level to lowest level/priority, SysTimerSW as Vector Interrupt */
  620. ECLIC_SetShvIRQ(SysTimerSW_IRQn, ECLIC_VECTOR_INTERRUPT);
  621. ECLIC_SetLevelIRQ(SysTimerSW_IRQn, configKERNEL_INTERRUPT_PRIORITY);
  622. ECLIC_EnableIRQ(SysTimerSW_IRQn);
  623. }
  624. /*-----------------------------------------------------------*/
  625. /*-----------------------------------------------------------*/
  626. #if( configASSERT_DEFINED == 1 )
  627. void vPortValidateInterruptPriority(void)
  628. {
  629. uint32_t ulCurrentInterrupt;
  630. uint8_t ucCurrentPriority;
  631. /* Obtain the number of the currently executing interrupt. */
  632. CSR_MCAUSE_Type mcause;
  633. mcause.d = __RV_CSR_READ(CSR_MCAUSE);
  634. /* Make sure current trap type is interrupt */
  635. configASSERT(mcause.b.interrupt == 1);
  636. if (mcause.b.interrupt) {
  637. ulCurrentInterrupt = mcause.b.exccode;
  638. /* Is the interrupt number a user defined interrupt? */
  639. if (ulCurrentInterrupt >= portFIRST_USER_INTERRUPT_NUMBER) {
  640. /* Look up the interrupt's priority. */
  641. ucCurrentPriority = __ECLIC_GetLevelIRQ(ulCurrentInterrupt);
  642. /* The following assertion will fail if a service routine (ISR) for
  643. an interrupt that has been assigned a priority above
  644. ucMaxSysCallPriority calls an ISR safe FreeRTOS API
  645. function. ISR safe FreeRTOS API functions must *only* be called
  646. from interrupts that have been assigned a priority at or below
  647. ucMaxSysCallPriority.
  648. Numerically low interrupt priority numbers represent logically high
  649. interrupt priorities, therefore the priority of the interrupt must
  650. be set to a value equal to or numerically *higher* than
  651. ucMaxSysCallPriority.
  652. Interrupts that use the FreeRTOS API must not be left at their
  653. default priority of zero as that is the highest possible priority,
  654. which is guaranteed to be above ucMaxSysCallPriority,
  655. and therefore also guaranteed to be invalid.
  656. FreeRTOS maintains separate thread and ISR API functions to ensure
  657. interrupt entry is as fast and simple as possible.
  658. The following links provide detailed information:
  659. http://www.freertos.org/FAQHelp.html */
  660. configASSERT(ucCurrentPriority <= ucMaxSysCallPriority);
  661. }
  662. }
  663. }
  664. #endif /* configASSERT_DEFINED */