portmacro.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  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 AND BSD-3-Clause
  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. /*This file has been prepared for Doxygen automatic documentation generation.*/
  29. /*! \file *********************************************************************
  30. *
  31. * \brief FreeRTOS port source for AVR32 UC3.
  32. *
  33. * - Compiler: GNU GCC for AVR32
  34. * - Supported devices: All AVR32 devices can be used.
  35. * - AppNote:
  36. *
  37. * \author Atmel Corporation (Now Microchip):
  38. * https://www.microchip.com \n
  39. * Support and FAQ: https://www.microchip.com/support/
  40. *
  41. *****************************************************************************/
  42. /*
  43. * Copyright (c) 2007, Atmel Corporation All rights reserved.
  44. *
  45. * Redistribution and use in source and binary forms, with or without
  46. * modification, are permitted provided that the following conditions are met:
  47. *
  48. * 1. Redistributions of source code must retain the above copyright notice,
  49. * this list of conditions and the following disclaimer.
  50. *
  51. * 2. Redistributions in binary form must reproduce the above copyright notice,
  52. * this list of conditions and the following disclaimer in the documentation
  53. * and/or other materials provided with the distribution.
  54. *
  55. * 3. The name of ATMEL may not be used to endorse or promote products derived
  56. * from this software without specific prior written permission.
  57. *
  58. * THIS SOFTWARE IS PROVIDED BY ATMEL ``AS IS'' AND ANY EXPRESS OR IMPLIED
  59. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  60. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND
  61. * SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT,
  62. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  63. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  64. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  65. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  66. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  67. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  68. */
  69. #ifndef PORTMACRO_H
  70. #define PORTMACRO_H
  71. /*-----------------------------------------------------------
  72. * Port specific definitions.
  73. *
  74. * The settings in this file configure FreeRTOS correctly for the
  75. * given hardware and compiler.
  76. *
  77. * These settings should not be altered.
  78. *-----------------------------------------------------------
  79. */
  80. #include <avr32/io.h>
  81. #include "intc.h"
  82. #include "compiler.h"
  83. #ifdef __cplusplus
  84. extern "C" {
  85. #endif
  86. /* Type definitions. */
  87. #define portCHAR char
  88. #define portFLOAT float
  89. #define portDOUBLE double
  90. #define portLONG long
  91. #define portSHORT short
  92. #define portSTACK_TYPE uint32_t
  93. #define portBASE_TYPE long
  94. typedef portSTACK_TYPE StackType_t;
  95. typedef long BaseType_t;
  96. typedef unsigned long UBaseType_t;
  97. #define TASK_DELAY_MS(x) ( (x) /portTICK_PERIOD_MS )
  98. #define TASK_DELAY_S(x) ( (x)*1000 /portTICK_PERIOD_MS )
  99. #define TASK_DELAY_MIN(x) ( (x)*60*1000/portTICK_PERIOD_MS )
  100. #define configTICK_TC_IRQ ATPASTE2(AVR32_TC_IRQ, configTICK_TC_CHANNEL)
  101. #if( configUSE_16_BIT_TICKS == 1 )
  102. typedef uint16_t TickType_t;
  103. #define portMAX_DELAY ( TickType_t ) 0xffff
  104. #else
  105. typedef uint32_t TickType_t;
  106. #define portMAX_DELAY ( TickType_t ) 0xffffffffUL
  107. #endif
  108. /*-----------------------------------------------------------*/
  109. /* Architecture specifics. */
  110. #define portSTACK_GROWTH ( -1 )
  111. #define portTICK_PERIOD_MS ( ( TickType_t ) 1000 / configTICK_RATE_HZ )
  112. #define portBYTE_ALIGNMENT 4
  113. #define portNOP() {__asm__ __volatile__ ("nop");}
  114. /*-----------------------------------------------------------*/
  115. /*-----------------------------------------------------------*/
  116. /* INTC-specific. */
  117. #define DISABLE_ALL_EXCEPTIONS() Disable_global_exception()
  118. #define ENABLE_ALL_EXCEPTIONS() Enable_global_exception()
  119. #define DISABLE_ALL_INTERRUPTS() Disable_global_interrupt()
  120. #define ENABLE_ALL_INTERRUPTS() Enable_global_interrupt()
  121. #define DISABLE_INT_LEVEL(int_lev) Disable_interrupt_level(int_lev)
  122. #define ENABLE_INT_LEVEL(int_lev) Enable_interrupt_level(int_lev)
  123. /*
  124. * Debug trace.
  125. * Activated if and only if configDBG is nonzero.
  126. * Prints a formatted string to stdout.
  127. * The current source file name and line number are output with a colon before
  128. * the formatted string.
  129. * A carriage return and a linefeed are appended to the output.
  130. * stdout is redirected to the USART configured by configDBG_USART.
  131. * The parameters are the same as for the standard printf function.
  132. * There is no return value.
  133. * SHALL NOT BE CALLED FROM WITHIN AN INTERRUPT as fputs and printf use malloc,
  134. * which is interrupt-unsafe with the current __malloc_lock and __malloc_unlock.
  135. */
  136. #if configDBG
  137. #define portDBG_TRACE(...) \
  138. {\
  139. fputs(__FILE__ ":" ASTRINGZ(__LINE__) ": ", stdout);\
  140. printf(__VA_ARGS__);\
  141. fputs("\r\n", stdout);\
  142. }
  143. #else
  144. #define portDBG_TRACE(...)
  145. #endif
  146. /* Critical section management. */
  147. #define portDISABLE_INTERRUPTS() DISABLE_ALL_INTERRUPTS()
  148. #define portENABLE_INTERRUPTS() ENABLE_ALL_INTERRUPTS()
  149. extern void vPortEnterCritical( void );
  150. extern void vPortExitCritical( void );
  151. #define portENTER_CRITICAL() vPortEnterCritical();
  152. #define portEXIT_CRITICAL() vPortExitCritical();
  153. /* Added as there is no such function in FreeRTOS. */
  154. extern void *pvPortRealloc( void *pv, size_t xSize );
  155. /*-----------------------------------------------------------*/
  156. /*=============================================================================================*/
  157. /*
  158. * Restore Context for cases other than INTi.
  159. */
  160. #define portRESTORE_CONTEXT() \
  161. { \
  162. extern volatile uint32_t ulCriticalNesting; \
  163. extern volatile void *volatile pxCurrentTCB; \
  164. \
  165. __asm__ __volatile__ ( \
  166. /* Set SP to point to new stack */ \
  167. "mov r8, LO(%[pxCurrentTCB]) \n\t"\
  168. "orh r8, HI(%[pxCurrentTCB]) \n\t"\
  169. "ld.w r0, r8[0] \n\t"\
  170. "ld.w sp, r0[0] \n\t"\
  171. \
  172. /* Restore ulCriticalNesting variable */ \
  173. "ld.w r0, sp++ \n\t"\
  174. "mov r8, LO(%[ulCriticalNesting]) \n\t"\
  175. "orh r8, HI(%[ulCriticalNesting]) \n\t"\
  176. "st.w r8[0], r0 \n\t"\
  177. \
  178. /* Restore R0..R7 */ \
  179. "ldm sp++, r0-r7 \n\t"\
  180. /* R0-R7 should not be used below this line */ \
  181. /* Skip PC and SR (will do it at the end) */ \
  182. "sub sp, -2*4 \n\t"\
  183. /* Restore R8..R12 and LR */ \
  184. "ldm sp++, r8-r12, lr \n\t"\
  185. /* Restore SR */ \
  186. "ld.w r0, sp[-8*4]\n\t" /* R0 is modified, is restored later. */ \
  187. "mtsr %[SR], r0 \n\t"\
  188. /* Restore r0 */ \
  189. "ld.w r0, sp[-9*4] \n\t"\
  190. /* Restore PC */ \
  191. "ld.w pc, sp[-7*4]" /* Get PC from stack - PC is the 7th register saved */ \
  192. : \
  193. : [ulCriticalNesting] "i" (&ulCriticalNesting), \
  194. [pxCurrentTCB] "i" (&pxCurrentTCB), \
  195. [SR] "i" (AVR32_SR) \
  196. ); \
  197. }
  198. /*
  199. * portSAVE_CONTEXT_INT() and portRESTORE_CONTEXT_INT(): for INT0..3 exceptions.
  200. * portSAVE_CONTEXT_SCALL() and portRESTORE_CONTEXT_SCALL(): for the scall exception.
  201. *
  202. * Had to make different versions because registers saved on the system stack
  203. * are not the same between INT0..3 exceptions and the scall exception.
  204. */
  205. // Task context stack layout:
  206. // R8 (*)
  207. // R9 (*)
  208. // R10 (*)
  209. // R11 (*)
  210. // R12 (*)
  211. // R14/LR (*)
  212. // R15/PC (*)
  213. // SR (*)
  214. // R0
  215. // R1
  216. // R2
  217. // R3
  218. // R4
  219. // R5
  220. // R6
  221. // R7
  222. // ulCriticalNesting
  223. // (*) automatically done for INT0..INT3, but not for SCALL
  224. /*
  225. * The ISR used for the scheduler tick depends on whether the cooperative or
  226. * the preemptive scheduler is being used.
  227. */
  228. #if configUSE_PREEMPTION == 0
  229. /*
  230. * portSAVE_CONTEXT_OS_INT() for OS Tick exception.
  231. */
  232. #define portSAVE_CONTEXT_OS_INT() \
  233. { \
  234. /* Save R0..R7 */ \
  235. __asm__ __volatile__ ("stm --sp, r0-r7"); \
  236. \
  237. /* With the cooperative scheduler, as there is no context switch by interrupt, */ \
  238. /* there is also no context save. */ \
  239. }
  240. /*
  241. * portRESTORE_CONTEXT_OS_INT() for Tick exception.
  242. */
  243. #define portRESTORE_CONTEXT_OS_INT() \
  244. { \
  245. __asm__ __volatile__ ( \
  246. /* Restore R0..R7 */ \
  247. "ldm sp++, r0-r7\n\t" \
  248. \
  249. /* With the cooperative scheduler, as there is no context switch by interrupt, */ \
  250. /* there is also no context restore. */ \
  251. "rete" \
  252. ); \
  253. }
  254. #else
  255. /*
  256. * portSAVE_CONTEXT_OS_INT() for OS Tick exception.
  257. */
  258. #define portSAVE_CONTEXT_OS_INT() \
  259. { \
  260. extern volatile uint32_t ulCriticalNesting; \
  261. extern volatile void *volatile pxCurrentTCB; \
  262. \
  263. /* When we come here */ \
  264. /* Registers R8..R12, LR, PC and SR had already been pushed to system stack */ \
  265. \
  266. __asm__ __volatile__ ( \
  267. /* Save R0..R7 */ \
  268. "stm --sp, r0-r7 \n\t"\
  269. \
  270. /* Save ulCriticalNesting variable - R0 is overwritten */ \
  271. "mov r8, LO(%[ulCriticalNesting])\n\t" \
  272. "orh r8, HI(%[ulCriticalNesting])\n\t" \
  273. "ld.w r0, r8[0] \n\t"\
  274. "st.w --sp, r0 \n\t"\
  275. \
  276. /* Check if INT0 or higher were being handled (case where the OS tick interrupted another */ \
  277. /* interrupt handler (which was of a higher priority level but decided to lower its priority */ \
  278. /* level and allow other lower interrupt level to occur). */ \
  279. /* In this case we don't want to do a task switch because we don't know what the stack */ \
  280. /* currently looks like (we don't know what the interrupted interrupt handler was doing). */ \
  281. /* Saving SP in pxCurrentTCB and then later restoring it (thinking restoring the task) */ \
  282. /* will just be restoring the interrupt handler, no way!!! */ \
  283. /* So, since we won't do a vTaskSwitchContext(), it's of no use to save SP. */ \
  284. "ld.w r0, sp[9*4]\n\t" /* Read SR in stack */ \
  285. "bfextu r0, r0, 22, 3\n\t" /* Extract the mode bits to R0. */ \
  286. "cp.w r0, 1\n\t" /* Compare the mode bits with supervisor mode(b'001) */ \
  287. "brhi LABEL_INT_SKIP_SAVE_CONTEXT_%[LINE] \n\t"\
  288. \
  289. /* Store SP in the first member of the structure pointed to by pxCurrentTCB */ \
  290. /* NOTE: we don't enter a critical section here because all interrupt handlers */ \
  291. /* MUST perform a SAVE_CONTEXT/RESTORE_CONTEXT in the same way as */ \
  292. /* portSAVE_CONTEXT_OS_INT/port_RESTORE_CONTEXT_OS_INT if they call OS functions. */ \
  293. /* => all interrupt handlers must use portENTER_SWITCHING_ISR/portEXIT_SWITCHING_ISR. */ \
  294. "mov r8, LO(%[pxCurrentTCB])\n\t" \
  295. "orh r8, HI(%[pxCurrentTCB])\n\t" \
  296. "ld.w r0, r8[0]\n\t" \
  297. "st.w r0[0], sp\n" \
  298. \
  299. "LABEL_INT_SKIP_SAVE_CONTEXT_%[LINE]:" \
  300. : \
  301. : [ulCriticalNesting] "i" (&ulCriticalNesting), \
  302. [pxCurrentTCB] "i" (&pxCurrentTCB), \
  303. [LINE] "i" (__LINE__) \
  304. ); \
  305. }
  306. /*
  307. * portRESTORE_CONTEXT_OS_INT() for Tick exception.
  308. */
  309. #define portRESTORE_CONTEXT_OS_INT() \
  310. { \
  311. extern volatile uint32_t ulCriticalNesting; \
  312. extern volatile void *volatile pxCurrentTCB; \
  313. \
  314. /* Check if INT0 or higher were being handled (case where the OS tick interrupted another */ \
  315. /* interrupt handler (which was of a higher priority level but decided to lower its priority */ \
  316. /* level and allow other lower interrupt level to occur). */ \
  317. /* In this case we don't want to do a task switch because we don't know what the stack */ \
  318. /* currently looks like (we don't know what the interrupted interrupt handler was doing). */ \
  319. /* Saving SP in pxCurrentTCB and then later restoring it (thinking restoring the task) */ \
  320. /* will just be restoring the interrupt handler, no way!!! */ \
  321. __asm__ __volatile__ ( \
  322. "ld.w r0, sp[9*4]\n\t" /* Read SR in stack */ \
  323. "bfextu r0, r0, 22, 3\n\t" /* Extract the mode bits to R0. */ \
  324. "cp.w r0, 1\n\t" /* Compare the mode bits with supervisor mode(b'001) */ \
  325. "brhi LABEL_INT_SKIP_RESTORE_CONTEXT_%[LINE]" \
  326. : \
  327. : [LINE] "i" (__LINE__) \
  328. ); \
  329. \
  330. /* Else */ \
  331. /* because it is here safe, always call vTaskSwitchContext() since an OS tick occurred. */ \
  332. /* A critical section has to be used here because vTaskSwitchContext handles FreeRTOS linked lists. */\
  333. portENTER_CRITICAL(); \
  334. vTaskSwitchContext(); \
  335. portEXIT_CRITICAL(); \
  336. \
  337. /* Restore all registers */ \
  338. \
  339. __asm__ __volatile__ ( \
  340. /* Set SP to point to new stack */ \
  341. "mov r8, LO(%[pxCurrentTCB]) \n\t"\
  342. "orh r8, HI(%[pxCurrentTCB]) \n\t"\
  343. "ld.w r0, r8[0] \n\t"\
  344. "ld.w sp, r0[0] \n"\
  345. \
  346. "LABEL_INT_SKIP_RESTORE_CONTEXT_%[LINE]: \n\t"\
  347. \
  348. /* Restore ulCriticalNesting variable */ \
  349. "ld.w r0, sp++ \n\t" \
  350. "mov r8, LO(%[ulCriticalNesting]) \n\t"\
  351. "orh r8, HI(%[ulCriticalNesting]) \n\t"\
  352. "st.w r8[0], r0 \n\t"\
  353. \
  354. /* Restore R0..R7 */ \
  355. "ldm sp++, r0-r7 \n\t"\
  356. \
  357. /* Now, the stack should be R8..R12, LR, PC and SR */ \
  358. "rete" \
  359. : \
  360. : [ulCriticalNesting] "i" (&ulCriticalNesting), \
  361. [pxCurrentTCB] "i" (&pxCurrentTCB), \
  362. [LINE] "i" (__LINE__) \
  363. ); \
  364. }
  365. #endif
  366. /*
  367. * portSAVE_CONTEXT_SCALL() for SupervisorCALL exception.
  368. *
  369. * NOTE: taskYIELD()(== SCALL) MUST NOT be called in a mode > supervisor mode.
  370. *
  371. */
  372. #define portSAVE_CONTEXT_SCALL() \
  373. { \
  374. extern volatile uint32_t ulCriticalNesting; \
  375. extern volatile void *volatile pxCurrentTCB; \
  376. \
  377. /* Warning: the stack layout after SCALL doesn't match the one after an interrupt. */ \
  378. /* If SR[M2:M0] == 001 */ \
  379. /* PC and SR are on the stack. */ \
  380. /* Else (other modes) */ \
  381. /* Nothing on the stack. */ \
  382. \
  383. /* WARNING NOTE: the else case cannot happen as it is strictly forbidden to call */ \
  384. /* vTaskDelay() and vTaskDelayUntil() OS functions (that result in a taskYield()) */ \
  385. /* in an interrupt|exception handler. */ \
  386. \
  387. __asm__ __volatile__ ( \
  388. /* in order to save R0-R7 */ \
  389. "sub sp, 6*4 \n\t"\
  390. /* Save R0..R7 */ \
  391. "stm --sp, r0-r7 \n\t"\
  392. \
  393. /* in order to save R8-R12 and LR */ \
  394. /* do not use SP if interrupts occurs, SP must be left at bottom of stack */ \
  395. "sub r7, sp,-16*4 \n\t"\
  396. /* Copy PC and SR in other places in the stack. */ \
  397. "ld.w r0, r7[-2*4] \n\t" /* Read SR */\
  398. "st.w r7[-8*4], r0 \n\t" /* Copy SR */\
  399. "ld.w r0, r7[-1*4] \n\t" /* Read PC */\
  400. "st.w r7[-7*4], r0 \n\t" /* Copy PC */\
  401. \
  402. /* Save R8..R12 and LR on the stack. */ \
  403. "stm --r7, r8-r12, lr \n\t"\
  404. \
  405. /* Arriving here we have the following stack organizations: */ \
  406. /* R8..R12, LR, PC, SR, R0..R7. */ \
  407. \
  408. /* Now we can finalize the save. */ \
  409. \
  410. /* Save ulCriticalNesting variable - R0 is overwritten */ \
  411. "mov r8, LO(%[ulCriticalNesting]) \n\t"\
  412. "orh r8, HI(%[ulCriticalNesting]) \n\t"\
  413. "ld.w r0, r8[0] \n\t"\
  414. "st.w --sp, r0" \
  415. : \
  416. : [ulCriticalNesting] "i" (&ulCriticalNesting) \
  417. ); \
  418. \
  419. /* Disable the its which may cause a context switch (i.e. cause a change of */ \
  420. /* pxCurrentTCB). */ \
  421. /* Basically, all accesses to the pxCurrentTCB structure should be put in a */ \
  422. /* critical section because it is a global structure. */ \
  423. portENTER_CRITICAL(); \
  424. \
  425. /* Store SP in the first member of the structure pointed to by pxCurrentTCB */ \
  426. __asm__ __volatile__ ( \
  427. "mov r8, LO(%[pxCurrentTCB]) \n\t"\
  428. "orh r8, HI(%[pxCurrentTCB]) \n\t"\
  429. "ld.w r0, r8[0] \n\t"\
  430. "st.w r0[0], sp" \
  431. : \
  432. : [pxCurrentTCB] "i" (&pxCurrentTCB) \
  433. ); \
  434. }
  435. /*
  436. * portRESTORE_CONTEXT() for SupervisorCALL exception.
  437. */
  438. #define portRESTORE_CONTEXT_SCALL() \
  439. { \
  440. extern volatile uint32_t ulCriticalNesting; \
  441. extern volatile void *volatile pxCurrentTCB; \
  442. \
  443. /* Restore all registers */ \
  444. \
  445. /* Set SP to point to new stack */ \
  446. __asm__ __volatile__ ( \
  447. "mov r8, LO(%[pxCurrentTCB]) \n\t"\
  448. "orh r8, HI(%[pxCurrentTCB]) \n\t"\
  449. "ld.w r0, r8[0] \n\t"\
  450. "ld.w sp, r0[0]" \
  451. : \
  452. : [pxCurrentTCB] "i" (&pxCurrentTCB) \
  453. ); \
  454. \
  455. /* Leave pxCurrentTCB variable access critical section */ \
  456. portEXIT_CRITICAL(); \
  457. \
  458. __asm__ __volatile__ ( \
  459. /* Restore ulCriticalNesting variable */ \
  460. "ld.w r0, sp++ \n\t"\
  461. "mov r8, LO(%[ulCriticalNesting]) \n\t"\
  462. "orh r8, HI(%[ulCriticalNesting]) \n\t"\
  463. "st.w r8[0], r0 \n\t"\
  464. \
  465. /* skip PC and SR */ \
  466. /* do not use SP if interrupts occurs, SP must be left at bottom of stack */ \
  467. "sub r7, sp, -10*4 \n\t"\
  468. /* Restore r8-r12 and LR */ \
  469. "ldm r7++, r8-r12, lr \n\t"\
  470. \
  471. /* RETS will take care of the extra PC and SR restore. */ \
  472. /* So, we have to prepare the stack for this. */ \
  473. "ld.w r0, r7[-8*4] \n\t" /* Read SR */\
  474. "st.w r7[-2*4], r0 \n\t" /* Copy SR */\
  475. "ld.w r0, r7[-7*4] \n\t" /* Read PC */\
  476. "st.w r7[-1*4], r0 \n\t" /* Copy PC */\
  477. \
  478. /* Restore R0..R7 */ \
  479. "ldm sp++, r0-r7 \n\t"\
  480. \
  481. "sub sp, -6*4 \n\t"\
  482. \
  483. "rets" \
  484. : \
  485. : [ulCriticalNesting] "i" (&ulCriticalNesting) \
  486. ); \
  487. }
  488. /*
  489. * The ISR used depends on whether the cooperative or
  490. * the preemptive scheduler is being used.
  491. */
  492. #if configUSE_PREEMPTION == 0
  493. /*
  494. * ISR entry and exit macros. These are only required if a task switch
  495. * is required from the ISR.
  496. */
  497. #define portENTER_SWITCHING_ISR() \
  498. { \
  499. /* Save R0..R7 */ \
  500. __asm__ __volatile__ ("stm --sp, r0-r7"); \
  501. \
  502. /* With the cooperative scheduler, as there is no context switch by interrupt, */ \
  503. /* there is also no context save. */ \
  504. }
  505. /*
  506. * Input parameter: in R12, boolean. Perform a vTaskSwitchContext() if 1
  507. */
  508. #define portEXIT_SWITCHING_ISR() \
  509. { \
  510. __asm__ __volatile__ ( \
  511. /* Restore R0..R7 */ \
  512. "ldm sp++, r0-r7 \n\t"\
  513. \
  514. /* With the cooperative scheduler, as there is no context switch by interrupt, */ \
  515. /* there is also no context restore. */ \
  516. "rete" \
  517. ); \
  518. }
  519. #else
  520. /*
  521. * ISR entry and exit macros. These are only required if a task switch
  522. * is required from the ISR.
  523. */
  524. #define portENTER_SWITCHING_ISR() \
  525. { \
  526. extern volatile uint32_t ulCriticalNesting; \
  527. extern volatile void *volatile pxCurrentTCB; \
  528. \
  529. /* When we come here */ \
  530. /* Registers R8..R12, LR, PC and SR had already been pushed to system stack */ \
  531. \
  532. __asm__ __volatile__ ( \
  533. /* Save R0..R7 */ \
  534. "stm --sp, r0-r7 \n\t"\
  535. \
  536. /* Save ulCriticalNesting variable - R0 is overwritten */ \
  537. "mov r8, LO(%[ulCriticalNesting]) \n\t"\
  538. "orh r8, HI(%[ulCriticalNesting]) \n\t"\
  539. "ld.w r0, r8[0] \n\t"\
  540. "st.w --sp, r0 \n\t"\
  541. \
  542. /* Check if INT0 or higher were being handled (case where the OS tick interrupted another */ \
  543. /* interrupt handler (which was of a higher priority level but decided to lower its priority */ \
  544. /* level and allow other lower interrupt level to occur). */ \
  545. /* In this case we don't want to do a task switch because we don't know what the stack */ \
  546. /* currently looks like (we don't know what the interrupted interrupt handler was doing). */ \
  547. /* Saving SP in pxCurrentTCB and then later restoring it (thinking restoring the task) */ \
  548. /* will just be restoring the interrupt handler, no way!!! */ \
  549. /* So, since we won't do a vTaskSwitchContext(), it's of no use to save SP. */ \
  550. "ld.w r0, sp[9*4] \n\t" /* Read SR in stack */\
  551. "bfextu r0, r0, 22, 3 \n\t" /* Extract the mode bits to R0. */\
  552. "cp.w r0, 1 \n\t" /* Compare the mode bits with supervisor mode(b'001) */\
  553. "brhi LABEL_ISR_SKIP_SAVE_CONTEXT_%[LINE] \n\t"\
  554. \
  555. /* Store SP in the first member of the structure pointed to by pxCurrentTCB */ \
  556. "mov r8, LO(%[pxCurrentTCB]) \n\t"\
  557. "orh r8, HI(%[pxCurrentTCB]) \n\t"\
  558. "ld.w r0, r8[0] \n\t"\
  559. "st.w r0[0], sp \n"\
  560. \
  561. "LABEL_ISR_SKIP_SAVE_CONTEXT_%[LINE]:" \
  562. : \
  563. : [ulCriticalNesting] "i" (&ulCriticalNesting), \
  564. [pxCurrentTCB] "i" (&pxCurrentTCB), \
  565. [LINE] "i" (__LINE__) \
  566. ); \
  567. }
  568. /*
  569. * Input parameter: in R12, boolean. Perform a vTaskSwitchContext() if 1
  570. */
  571. #define portEXIT_SWITCHING_ISR() \
  572. { \
  573. extern volatile uint32_t ulCriticalNesting; \
  574. extern volatile void *volatile pxCurrentTCB; \
  575. \
  576. __asm__ __volatile__ ( \
  577. /* Check if INT0 or higher were being handled (case where the OS tick interrupted another */ \
  578. /* interrupt handler (which was of a higher priority level but decided to lower its priority */ \
  579. /* level and allow other lower interrupt level to occur). */ \
  580. /* In this case it's of no use to switch context and restore a new SP because we purposedly */ \
  581. /* did not previously save SP in its TCB. */ \
  582. "ld.w r0, sp[9*4] \n\t" /* Read SR in stack */\
  583. "bfextu r0, r0, 22, 3 \n\t" /* Extract the mode bits to R0. */\
  584. "cp.w r0, 1 \n\t" /* Compare the mode bits with supervisor mode(b'001) */\
  585. "brhi LABEL_ISR_SKIP_RESTORE_CONTEXT_%[LINE] \n\t"\
  586. \
  587. /* If a switch is required then we just need to call */ \
  588. /* vTaskSwitchContext() as the context has already been */ \
  589. /* saved. */ \
  590. "cp.w r12, 1 \n\t" /* Check if Switch context is required. */\
  591. "brne LABEL_ISR_RESTORE_CONTEXT_%[LINE]" \
  592. : \
  593. : [LINE] "i" (__LINE__) \
  594. ); \
  595. \
  596. /* A critical section has to be used here because vTaskSwitchContext handles FreeRTOS linked lists. */ \
  597. portENTER_CRITICAL(); \
  598. vTaskSwitchContext(); \
  599. portEXIT_CRITICAL(); \
  600. \
  601. __asm__ __volatile__ ( \
  602. "LABEL_ISR_RESTORE_CONTEXT_%[LINE]: \n\t"\
  603. /* Restore the context of which ever task is now the highest */ \
  604. /* priority that is ready to run. */ \
  605. \
  606. /* Restore all registers */ \
  607. \
  608. /* Set SP to point to new stack */ \
  609. "mov r8, LO(%[pxCurrentTCB]) \n\t"\
  610. "orh r8, HI(%[pxCurrentTCB]) \n\t"\
  611. "ld.w r0, r8[0] \n\t"\
  612. "ld.w sp, r0[0] \n"\
  613. \
  614. "LABEL_ISR_SKIP_RESTORE_CONTEXT_%[LINE]: \n\t"\
  615. \
  616. /* Restore ulCriticalNesting variable */ \
  617. "ld.w r0, sp++ \n\t"\
  618. "mov r8, LO(%[ulCriticalNesting]) \n\t"\
  619. "orh r8, HI(%[ulCriticalNesting]) \n\t"\
  620. "st.w r8[0], r0 \n\t"\
  621. \
  622. /* Restore R0..R7 */ \
  623. "ldm sp++, r0-r7 \n\t"\
  624. \
  625. /* Now, the stack should be R8..R12, LR, PC and SR */ \
  626. "rete" \
  627. : \
  628. : [ulCriticalNesting] "i" (&ulCriticalNesting), \
  629. [pxCurrentTCB] "i" (&pxCurrentTCB), \
  630. [LINE] "i" (__LINE__) \
  631. ); \
  632. }
  633. #endif
  634. #define portYIELD() {__asm__ __volatile__ ("scall");}
  635. /* Task function macros as described on the FreeRTOS.org WEB site. */
  636. #define portTASK_FUNCTION_PROTO( vFunction, pvParameters ) void vFunction( void *pvParameters )
  637. #define portTASK_FUNCTION( vFunction, pvParameters ) void vFunction( void *pvParameters )
  638. #ifdef __cplusplus
  639. }
  640. #endif
  641. #endif /* PORTMACRO_H */