port.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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. #include "FreeRTOS.h"
  29. #include "task.h"
  30. /*-----------------------------------------------------------
  31. * Implementation of functions defined in portable.h for the 16FX port.
  32. *----------------------------------------------------------*/
  33. /*
  34. * Get current value of DPR and ADB registers
  35. */
  36. StackType_t xGet_DPR_ADB_bank( void );
  37. /*
  38. * Get current value of DTB and PCB registers
  39. */
  40. StackType_t xGet_DTB_PCB_bank( void );
  41. /*
  42. * Sets up the periodic ISR used for the RTOS tick. This uses RLT0, but
  43. * can be done using any given RLT.
  44. */
  45. static void prvSetupRLT0Interrupt( void );
  46. /*-----------------------------------------------------------*/
  47. /*
  48. * We require the address of the pxCurrentTCB variable, but don't want to know
  49. * any details of its type.
  50. */
  51. typedef void TCB_t;
  52. extern volatile TCB_t * volatile pxCurrentTCB;
  53. /*-----------------------------------------------------------*/
  54. /*
  55. * Macro to save a task context to the task stack. This macro copies the
  56. * saved context (AH:AL, DPR:ADB, DTB:PCB , PC and PS) from the system
  57. * stack to task stack pointed by user stack pointer ( USP for SMALL and
  58. * MEDIUM memory model amd USB:USP for COMPACT and LARGE memory model ),
  59. * then it pushes the general purpose registers RW0-RW7 on to the task
  60. * stack. Finally the resultant stack pointer value is saved into the
  61. * task control block so it can be retrieved the next time the task
  62. * executes.
  63. */
  64. #if( ( configMEMMODEL == portSMALL ) || ( configMEMMODEL == portMEDIUM ) )
  65. #define portSAVE_CONTEXT() \
  66. { __asm(" POPW A "); \
  67. __asm(" AND CCR,#H'DF "); \
  68. __asm(" PUSHW A "); \
  69. __asm(" OR CCR,#H'20 "); \
  70. __asm(" POPW A "); \
  71. __asm(" AND CCR,#H'DF "); \
  72. __asm(" PUSHW A "); \
  73. __asm(" OR CCR,#H'20 "); \
  74. __asm(" POPW A "); \
  75. __asm(" AND CCR,#H'DF "); \
  76. __asm(" PUSHW A "); \
  77. __asm(" OR CCR,#H'20 "); \
  78. __asm(" POPW A "); \
  79. __asm(" AND CCR,#H'DF "); \
  80. __asm(" PUSHW A "); \
  81. __asm(" OR CCR,#H'20 "); \
  82. __asm(" POPW A "); \
  83. __asm(" AND CCR,#H'DF "); \
  84. __asm(" PUSHW A "); \
  85. __asm(" OR CCR,#H'20 "); \
  86. __asm(" POPW A "); \
  87. __asm(" AND CCR,#H'DF "); \
  88. __asm(" PUSHW A "); \
  89. __asm(" PUSHW (RW0,RW1,RW2,RW3,RW4,RW5,RW6,RW7) "); \
  90. __asm(" MOVW A, _pxCurrentTCB "); \
  91. __asm(" MOVW A, SP "); \
  92. __asm(" SWAPW "); \
  93. __asm(" MOVW @AL, AH "); \
  94. __asm(" OR CCR,#H'20 "); \
  95. }
  96. /*
  97. * Macro to restore a task context from the task stack. This is effecti-
  98. * vely the reverse of SAVE_CONTEXT(). First the stack pointer value
  99. * (USP for SMALL and MEDIUM memory model amd USB:USP for COMPACT and
  100. * LARGE memory model ) is loaded from the task control block. Next the
  101. * value of all the general purpose registers RW0-RW7 is retrieved. Fina-
  102. * lly it copies of the context ( AH:AL, DPR:ADB, DTB:PCB, PC and PS) of
  103. * the task to be executed upon RETI from user stack to system stack.
  104. */
  105. #define portRESTORE_CONTEXT() \
  106. { __asm(" MOVW A, _pxCurrentTCB "); \
  107. __asm(" MOVW A, @A "); \
  108. __asm(" AND CCR,#H'DF "); \
  109. __asm(" MOVW SP, A "); \
  110. __asm(" POPW (RW0,RW1,RW2,RW3,RW4,RW5,RW6,RW7) "); \
  111. __asm(" POPW A "); \
  112. __asm(" OR CCR,#H'20 "); \
  113. __asm(" PUSHW A "); \
  114. __asm(" AND CCR,#H'DF "); \
  115. __asm(" POPW A "); \
  116. __asm(" OR CCR,#H'20 "); \
  117. __asm(" PUSHW A "); \
  118. __asm(" AND CCR,#H'DF "); \
  119. __asm(" POPW A "); \
  120. __asm(" OR CCR,#H'20 "); \
  121. __asm(" PUSHW A "); \
  122. __asm(" AND CCR,#H'DF "); \
  123. __asm(" POPW A "); \
  124. __asm(" OR CCR,#H'20 "); \
  125. __asm(" PUSHW A "); \
  126. __asm(" AND CCR,#H'DF "); \
  127. __asm(" POPW A "); \
  128. __asm(" OR CCR,#H'20 "); \
  129. __asm(" PUSHW A "); \
  130. __asm(" AND CCR,#H'DF "); \
  131. __asm(" POPW A "); \
  132. __asm(" OR CCR,#H'20 "); \
  133. __asm(" PUSHW A "); \
  134. }
  135. #elif( ( configMEMMODEL == portCOMPACT ) || ( configMEMMODEL == portLARGE ) )
  136. #define portSAVE_CONTEXT() \
  137. { __asm(" POPW A "); \
  138. __asm(" AND CCR,#H'DF "); \
  139. __asm(" PUSHW A "); \
  140. __asm(" OR CCR,#H'20 "); \
  141. __asm(" POPW A "); \
  142. __asm(" AND CCR,#H'DF "); \
  143. __asm(" PUSHW A "); \
  144. __asm(" OR CCR,#H'20 "); \
  145. __asm(" POPW A "); \
  146. __asm(" AND CCR,#H'DF "); \
  147. __asm(" PUSHW A "); \
  148. __asm(" OR CCR,#H'20 "); \
  149. __asm(" POPW A "); \
  150. __asm(" AND CCR,#H'DF "); \
  151. __asm(" PUSHW A "); \
  152. __asm(" OR CCR,#H'20 "); \
  153. __asm(" POPW A "); \
  154. __asm(" AND CCR,#H'DF "); \
  155. __asm(" PUSHW A "); \
  156. __asm(" OR CCR,#H'20 "); \
  157. __asm(" POPW A "); \
  158. __asm(" AND CCR,#H'DF "); \
  159. __asm(" PUSHW A "); \
  160. __asm(" PUSHW (RW0,RW1,RW2,RW3,RW4,RW5,RW6,RW7) "); \
  161. __asm(" MOVL A, _pxCurrentTCB "); \
  162. __asm(" MOVL RL2, A "); \
  163. __asm(" MOVW A, SP "); \
  164. __asm(" MOVW @RL2+0, A "); \
  165. __asm(" MOV A, USB "); \
  166. __asm(" MOV @RL2+2, A "); \
  167. }
  168. #define portRESTORE_CONTEXT() \
  169. { __asm(" MOVL A, _pxCurrentTCB "); \
  170. __asm(" MOVL RL2, A "); \
  171. __asm(" MOVW A, @RL2+0 "); \
  172. __asm(" AND CCR,#H'DF "); \
  173. __asm(" MOVW SP, A "); \
  174. __asm(" MOV A, @RL2+2 "); \
  175. __asm(" MOV USB, A "); \
  176. __asm(" POPW (RW0,RW1,RW2,RW3,RW4,RW5,RW6,RW7) "); \
  177. __asm(" POPW A "); \
  178. __asm(" OR CCR,#H'20 "); \
  179. __asm(" PUSHW A "); \
  180. __asm(" AND CCR,#H'DF "); \
  181. __asm(" POPW A "); \
  182. __asm(" OR CCR,#H'20 "); \
  183. __asm(" PUSHW A "); \
  184. __asm(" AND CCR,#H'DF "); \
  185. __asm(" POPW A "); \
  186. __asm(" OR CCR,#H'20 "); \
  187. __asm(" PUSHW A "); \
  188. __asm(" AND CCR,#H'DF "); \
  189. __asm(" POPW A "); \
  190. __asm(" OR CCR,#H'20 "); \
  191. __asm(" PUSHW A "); \
  192. __asm(" AND CCR,#H'DF "); \
  193. __asm(" POPW A "); \
  194. __asm(" OR CCR,#H'20 "); \
  195. __asm(" PUSHW A "); \
  196. __asm(" AND CCR,#H'DF "); \
  197. __asm(" POPW A "); \
  198. __asm(" OR CCR,#H'20 "); \
  199. __asm(" PUSHW A "); \
  200. }
  201. #endif
  202. /*-----------------------------------------------------------*/
  203. /*
  204. * Functions for obtaining the current value of DPR:ADB, DTB:PCB bank registers
  205. */
  206. #pragma asm
  207. .GLOBAL _xGet_DPR_ADB_bank
  208. .GLOBAL _xGet_DTB_PCB_bank
  209. .SECTION CODE, CODE, ALIGN=1
  210. _xGet_DPR_ADB_bank:
  211. MOV A, DPR
  212. SWAP
  213. MOV A, ADB
  214. ORW A
  215. #if configMEMMODEL == portMEDIUM || configMEMMODEL == portLARGE
  216. RETP
  217. #elif configMEMMODEL == portSMALL || configMEMMODEL == portCOMPACT
  218. RET
  219. #endif
  220. _xGet_DTB_PCB_bank:
  221. MOV A, DTB
  222. SWAP
  223. MOV A, PCB
  224. ORW A
  225. #if configMEMMODEL == portMEDIUM || configMEMMODEL == portLARGE
  226. RETP
  227. #elif configMEMMODEL == portSMALL || configMEMMODEL == portCOMPACT
  228. RET
  229. #endif
  230. #pragma endasm
  231. /*-----------------------------------------------------------*/
  232. /*
  233. * Initialise the stack of a task to look exactly as if a call to
  234. * portSAVE_CONTEXT had been called.
  235. *
  236. * See the header file portable.h.
  237. */
  238. StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
  239. {
  240. /* Place a few bytes of known values on the bottom of the stack.
  241. This is just useful for debugging. */
  242. *pxTopOfStack = 0x1111;
  243. pxTopOfStack--;
  244. *pxTopOfStack = 0x2222;
  245. pxTopOfStack--;
  246. *pxTopOfStack = 0x3333;
  247. pxTopOfStack--;
  248. /* Once the task is called the task would push the pointer to the
  249. parameter onto the stack. Hence here the pointer would be copied to the stack
  250. first. When using the COMPACT or LARGE memory model the pointer would be 24
  251. bits, and when using the SMALL or MEDIUM memory model the pointer would be 16
  252. bits. */
  253. #if( ( configMEMMODEL == portCOMPACT ) || ( configMEMMODEL == portLARGE ) )
  254. {
  255. *pxTopOfStack = ( StackType_t ) ( ( uint32_t ) ( pvParameters ) >> 16 );
  256. pxTopOfStack--;
  257. }
  258. #endif
  259. *pxTopOfStack = ( StackType_t ) ( pvParameters );
  260. pxTopOfStack--;
  261. /* This is redundant push to the stack. This is required in order to introduce
  262. an offset so that the task accesses a parameter correctly that is passed on to
  263. the task stack. */
  264. #if( ( configMEMMODEL == portMEDIUM ) || ( configMEMMODEL == portLARGE ) )
  265. {
  266. *pxTopOfStack = ( xGet_DTB_PCB_bank() & 0xff00 ) | ( ( ( int32_t ) ( pxCode ) >> 16 ) & 0xff );
  267. pxTopOfStack--;
  268. }
  269. #endif
  270. /* This is redundant push to the stack. This is required in order to introduce
  271. an offset so the task correctly accesses the parameter passed on the task stack. */
  272. *pxTopOfStack = ( StackType_t ) ( pxCode );
  273. pxTopOfStack--;
  274. /* PS - User Mode, ILM=7, RB=0, Interrupts enabled,USP */
  275. *pxTopOfStack = 0xE0C0;
  276. pxTopOfStack--;
  277. /* PC */
  278. *pxTopOfStack = ( StackType_t ) ( pxCode );
  279. pxTopOfStack--;
  280. /* DTB | PCB */
  281. #if configMEMMODEL == portSMALL || configMEMMODEL == portCOMPACT
  282. {
  283. *pxTopOfStack = xGet_DTB_PCB_bank();
  284. pxTopOfStack--;
  285. }
  286. #endif
  287. /* DTB | PCB, in case of MEDIUM and LARGE memory models, PCB would be used
  288. along with PC to indicate the start address of the function. */
  289. #if( ( configMEMMODEL == portMEDIUM ) || ( configMEMMODEL == portLARGE ) )
  290. {
  291. *pxTopOfStack = ( xGet_DTB_PCB_bank() & 0xff00 ) | ( ( ( int32_t ) ( pxCode ) >> 16 ) & 0xff );
  292. pxTopOfStack--;
  293. }
  294. #endif
  295. /* DPR | ADB */
  296. *pxTopOfStack = xGet_DPR_ADB_bank();
  297. pxTopOfStack--;
  298. /* AL */
  299. *pxTopOfStack = ( StackType_t ) 0x9999;
  300. pxTopOfStack--;
  301. /* AH */
  302. *pxTopOfStack = ( StackType_t ) 0xAAAA;
  303. pxTopOfStack--;
  304. /* Next the general purpose registers. */
  305. *pxTopOfStack = ( StackType_t ) 0x7777; /* RW7 */
  306. pxTopOfStack--;
  307. *pxTopOfStack = ( StackType_t ) 0x6666; /* RW6 */
  308. pxTopOfStack--;
  309. *pxTopOfStack = ( StackType_t ) 0x5555; /* RW5 */
  310. pxTopOfStack--;
  311. *pxTopOfStack = ( StackType_t ) 0x4444; /* RW4 */
  312. pxTopOfStack--;
  313. *pxTopOfStack = ( StackType_t ) 0x3333; /* RW3 */
  314. pxTopOfStack--;
  315. *pxTopOfStack = ( StackType_t ) 0x2222; /* RW2 */
  316. pxTopOfStack--;
  317. *pxTopOfStack = ( StackType_t ) 0x1111; /* RW1 */
  318. pxTopOfStack--;
  319. *pxTopOfStack = ( StackType_t ) 0x8888; /* RW0 */
  320. return pxTopOfStack;
  321. }
  322. /*-----------------------------------------------------------*/
  323. static void prvSetupRLT0Interrupt( void )
  324. {
  325. /* The peripheral clock divided by 16 is used by the timer. */
  326. const uint16_t usReloadValue = ( uint16_t ) ( ( ( configCLKP1_CLOCK_HZ / configTICK_RATE_HZ ) / 16UL ) - 1UL );
  327. /* set reload value = 34999+1, TICK Interrupt after 10 ms @ 56MHz of CLKP1 */
  328. TMRLR0 = usReloadValue;
  329. /* prescaler 1:16, reload, interrupt enable, count enable, trigger */
  330. TMCSR0 = 0x041B;
  331. }
  332. /*-----------------------------------------------------------*/
  333. BaseType_t xPortStartScheduler( void )
  334. {
  335. /* Setup the hardware to generate the tick. */
  336. prvSetupRLT0Interrupt();
  337. /* Restore the context of the first task that is going to run. */
  338. portRESTORE_CONTEXT();
  339. /* Simulate a function call end as generated by the compiler. We will now
  340. jump to the start of the task the context of which we have just restored. */
  341. __asm(" reti ");
  342. /* Should not get here. */
  343. return pdTRUE;
  344. }
  345. /*-----------------------------------------------------------*/
  346. void vPortEndScheduler( void )
  347. {
  348. /* Not implemented - unlikely to ever be required as there is nothing to
  349. return to. */
  350. }
  351. /*-----------------------------------------------------------*/
  352. /*
  353. * The interrupt service routine used depends on whether the pre-emptive
  354. * scheduler is being used or not.
  355. */
  356. #if configUSE_PREEMPTION == 1
  357. /*
  358. * Tick ISR for preemptive scheduler. We can use a __nosavereg attribute
  359. * as the context is to be saved by the portSAVE_CONTEXT() macro, not the
  360. * compiler generated code. The tick count is incremented after the context
  361. * is saved.
  362. */
  363. __nosavereg __interrupt void prvRLT0_TICKISR( void )
  364. {
  365. /* Disable interrupts so that portSAVE_CONTEXT() is not interrupted */
  366. __DI();
  367. /* Save the context of the interrupted task. */
  368. portSAVE_CONTEXT();
  369. /* Enable interrupts */
  370. __EI();
  371. /* Clear RLT0 interrupt flag */
  372. TMCSR0_UF = 0;
  373. /* Increment the tick count then switch to the highest priority task
  374. that is ready to run. */
  375. if( xTaskIncrementTick() != pdFALSE )
  376. {
  377. vTaskSwitchContext();
  378. }
  379. /* Disable interrupts so that portRESTORE_CONTEXT() is not interrupted */
  380. __DI();
  381. /* Restore the context of the new task. */
  382. portRESTORE_CONTEXT();
  383. /* Enable interrupts */
  384. __EI();
  385. }
  386. #else
  387. /*
  388. * Tick ISR for the cooperative scheduler. All this does is increment the
  389. * tick count. We don't need to switch context, this can only be done by
  390. * manual calls to taskYIELD();
  391. */
  392. __interrupt void prvRLT0_TICKISR( void )
  393. {
  394. /* Clear RLT0 interrupt flag */
  395. TMCSR0_UF = 0;
  396. xTaskIncrementTick();
  397. }
  398. #endif
  399. /*-----------------------------------------------------------*/
  400. /*
  401. * Manual context switch. We can use a __nosavereg attribute as the context
  402. * is to be saved by the portSAVE_CONTEXT() macro, not the compiler generated
  403. * code.
  404. */
  405. __nosavereg __interrupt void vPortYield( void )
  406. {
  407. /* Save the context of the interrupted task. */
  408. portSAVE_CONTEXT();
  409. /* Switch to the highest priority task that is ready to run. */
  410. vTaskSwitchContext();
  411. /* Restore the context of the new task. */
  412. portRESTORE_CONTEXT();
  413. }
  414. /*-----------------------------------------------------------*/
  415. __nosavereg __interrupt void vPortYieldDelayed( void )
  416. {
  417. /* Disable interrupts so that portSAVE_CONTEXT() is not interrupted */
  418. __DI();
  419. /* Save the context of the interrupted task. */
  420. portSAVE_CONTEXT();
  421. /* Enable interrupts */
  422. __EI();
  423. /* Clear delayed interrupt flag */
  424. __asm (" CLRB 03A4H:0 ");
  425. /* Switch to the highest priority task that is ready to run. */
  426. vTaskSwitchContext();
  427. /* Disable interrupts so that portSAVE_CONTEXT() is not interrupted */
  428. __DI();
  429. /* Restore the context of the new task. */
  430. portRESTORE_CONTEXT();
  431. /* Enable interrupts */
  432. __EI();
  433. }
  434. /*-----------------------------------------------------------*/