portmacro.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  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 header for AVR32 UC3.
  32. *
  33. * - Compiler: IAR EWAVR32
  34. * - Supported devices: All AVR32 devices can be used.
  35. * - AppNote:
  36. *
  37. * \author Atmel Corporation (Now Microchip):
  38. * https://www.microchip.com
  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, LWRD("ASTRINGZ(pxCurrentTCB)") \n\t"\
  168. "orh r8, HWRD("ASTRINGZ(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, LWRD("ASTRINGZ(ulCriticalNesting)") \n\t"\
  175. "orh r8, HWRD("ASTRINGZ(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 "ASTRINGZ(AVR32_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. \
  194. /* Force import of global symbols from assembly */ \
  195. ulCriticalNesting; \
  196. pxCurrentTCB; \
  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, LWRD("ASTRINGZ(ulCriticalNesting)") \n\t"\
  272. "orh r8, HWRD("ASTRINGZ(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_"ASTRINGZ(__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, LWRD("ASTRINGZ(pxCurrentTCB)") \n\t"\
  295. "orh r8, HWRD("ASTRINGZ(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_"ASTRINGZ(__LINE__)":" \
  300. ); \
  301. }
  302. /*
  303. * portRESTORE_CONTEXT_OS_INT() for Tick exception.
  304. */
  305. #define portRESTORE_CONTEXT_OS_INT() \
  306. { \
  307. extern volatile uint32_t ulCriticalNesting; \
  308. extern volatile void *volatile pxCurrentTCB; \
  309. \
  310. /* Check if INT0 or higher were being handled (case where the OS tick interrupted another */ \
  311. /* interrupt handler (which was of a higher priority level but decided to lower its priority */ \
  312. /* level and allow other lower interrupt level to occur). */ \
  313. /* In this case we don't want to do a task switch because we don't know what the stack */ \
  314. /* currently looks like (we don't know what the interrupted interrupt handler was doing). */ \
  315. /* Saving SP in pxCurrentTCB and then later restoring it (thinking restoring the task) */ \
  316. /* will just be restoring the interrupt handler, no way!!! */ \
  317. __asm__ __volatile__ ( \
  318. "ld.w r0, sp[9*4] \n\t" /* Read SR in stack */\
  319. "bfextu r0, r0, 22, 3 \n\t" /* Extract the mode bits to R0. */\
  320. "cp.w r0, 1 \n\t" /* Compare the mode bits with supervisor mode(b'001) */\
  321. "brhi LABEL_INT_SKIP_RESTORE_CONTEXT_"ASTRINGZ(__LINE__) \
  322. ); \
  323. \
  324. /* Else */ \
  325. /* because it is here safe, always call vTaskSwitchContext() since an OS tick occurred. */ \
  326. /* A critical section has to be used here because vTaskSwitchContext handles FreeRTOS linked lists. */\
  327. portENTER_CRITICAL(); \
  328. vTaskSwitchContext(); \
  329. portEXIT_CRITICAL(); \
  330. \
  331. /* Restore all registers */ \
  332. \
  333. __asm__ __volatile__ ( \
  334. /* Set SP to point to new stack */ \
  335. "mov r8, LWRD("ASTRINGZ(pxCurrentTCB)") \n\t"\
  336. "orh r8, HWRD("ASTRINGZ(pxCurrentTCB)") \n\t"\
  337. "ld.w r0, r8[0] \n\t"\
  338. "ld.w sp, r0[0] \n"\
  339. \
  340. "LABEL_INT_SKIP_RESTORE_CONTEXT_"ASTRINGZ(__LINE__)": \n\t"\
  341. \
  342. /* Restore ulCriticalNesting variable */ \
  343. "ld.w r0, sp++ \n\t"\
  344. "mov r8, LWRD("ASTRINGZ(ulCriticalNesting)") \n\t"\
  345. "orh r8, HWRD("ASTRINGZ(ulCriticalNesting)") \n\t"\
  346. "st.w r8[0], r0 \n\t"\
  347. \
  348. /* Restore R0..R7 */ \
  349. "ldm sp++, r0-r7 \n\t"\
  350. \
  351. /* Now, the stack should be R8..R12, LR, PC and SR */ \
  352. "rete" \
  353. ); \
  354. \
  355. /* Force import of global symbols from assembly */ \
  356. ulCriticalNesting; \
  357. pxCurrentTCB; \
  358. }
  359. #endif
  360. /*
  361. * portSAVE_CONTEXT_SCALL() for SupervisorCALL exception.
  362. *
  363. * NOTE: taskYIELD()(== SCALL) MUST NOT be called in a mode > supervisor mode.
  364. *
  365. */
  366. #define portSAVE_CONTEXT_SCALL() \
  367. { \
  368. extern volatile uint32_t ulCriticalNesting; \
  369. extern volatile void *volatile pxCurrentTCB; \
  370. \
  371. /* Warning: the stack layout after SCALL doesn't match the one after an interrupt. */ \
  372. /* If SR[M2:M0] == 001 */ \
  373. /* PC and SR are on the stack. */ \
  374. /* Else (other modes) */ \
  375. /* Nothing on the stack. */ \
  376. \
  377. /* WARNING NOTE: the else case cannot happen as it is strictly forbidden to call */ \
  378. /* vTaskDelay() and vTaskDelayUntil() OS functions (that result in a taskYield()) */ \
  379. /* in an interrupt|exception handler. */ \
  380. \
  381. __asm__ __volatile__ ( \
  382. /* in order to save R0-R7 */ \
  383. "sub sp, 6*4 \n\t"\
  384. /* Save R0..R7 */ \
  385. "stm --sp, r0-r7 \n\t"\
  386. \
  387. /* in order to save R8-R12 and LR */ \
  388. /* do not use SP if interrupts occurs, SP must be left at bottom of stack */ \
  389. "sub r7, sp,-16*4 \n\t"\
  390. /* Copy PC and SR in other places in the stack. */ \
  391. "ld.w r0, r7[-2*4] \n\t" /* Read SR */\
  392. "st.w r7[-8*4], r0 \n\t" /* Copy SR */\
  393. "ld.w r0, r7[-1*4] \n\t" /* Read PC */\
  394. "st.w r7[-7*4], r0 \n\t" /* Copy PC */\
  395. \
  396. /* Save R8..R12 and LR on the stack. */ \
  397. "stm --r7, r8-r12, lr \n\t"\
  398. \
  399. /* Arriving here we have the following stack organizations: */ \
  400. /* R8..R12, LR, PC, SR, R0..R7. */ \
  401. \
  402. /* Now we can finalize the save. */ \
  403. \
  404. /* Save ulCriticalNesting variable - R0 is overwritten */ \
  405. "mov r8, LWRD("ASTRINGZ(ulCriticalNesting)") \n\t"\
  406. "orh r8, HWRD("ASTRINGZ(ulCriticalNesting)") \n\t"\
  407. "ld.w r0, r8[0] \n\t"\
  408. "st.w --sp, r0" \
  409. ); \
  410. \
  411. /* Disable the its which may cause a context switch (i.e. cause a change of */ \
  412. /* pxCurrentTCB). */ \
  413. /* Basically, all accesses to the pxCurrentTCB structure should be put in a */ \
  414. /* critical section because it is a global structure. */ \
  415. portENTER_CRITICAL(); \
  416. \
  417. /* Store SP in the first member of the structure pointed to by pxCurrentTCB */ \
  418. __asm__ __volatile__ ( \
  419. "mov r8, LWRD("ASTRINGZ(pxCurrentTCB)") \n\t"\
  420. "orh r8, HWRD("ASTRINGZ(pxCurrentTCB)") \n\t"\
  421. "ld.w r0, r8[0] \n\t"\
  422. "st.w r0[0], sp" \
  423. ); \
  424. }
  425. /*
  426. * portRESTORE_CONTEXT() for SupervisorCALL exception.
  427. */
  428. #define portRESTORE_CONTEXT_SCALL() \
  429. { \
  430. extern volatile uint32_t ulCriticalNesting; \
  431. extern volatile void *volatile pxCurrentTCB; \
  432. \
  433. /* Restore all registers */ \
  434. \
  435. /* Set SP to point to new stack */ \
  436. __asm__ __volatile__ ( \
  437. "mov r8, LWRD("ASTRINGZ(pxCurrentTCB)") \n\t"\
  438. "orh r8, HWRD("ASTRINGZ(pxCurrentTCB)") \n\t"\
  439. "ld.w r0, r8[0] \n\t"\
  440. "ld.w sp, r0[0]" \
  441. ); \
  442. \
  443. /* Leave pxCurrentTCB variable access critical section */ \
  444. portEXIT_CRITICAL(); \
  445. \
  446. __asm__ __volatile__ ( \
  447. /* Restore ulCriticalNesting variable */ \
  448. "ld.w r0, sp++ \n\t"\
  449. "mov r8, LWRD("ASTRINGZ(ulCriticalNesting)") \n\t"\
  450. "orh r8, HWRD("ASTRINGZ(ulCriticalNesting)") \n\t"\
  451. "st.w r8[0], r0 \n\t"\
  452. \
  453. /* skip PC and SR */ \
  454. /* do not use SP if interrupts occurs, SP must be left at bottom of stack */ \
  455. "sub r7, sp, -10*4 \n\t"\
  456. /* Restore r8-r12 and LR */ \
  457. "ldm r7++, r8-r12, lr \n\t"\
  458. \
  459. /* RETS will take care of the extra PC and SR restore. */ \
  460. /* So, we have to prepare the stack for this. */ \
  461. "ld.w r0, r7[-8*4] \n\t" /* Read SR */\
  462. "st.w r7[-2*4], r0 \n\t" /* Copy SR */\
  463. "ld.w r0, r7[-7*4] \n\t" /* Read PC */\
  464. "st.w r7[-1*4], r0 \n\t" /* Copy PC */\
  465. \
  466. /* Restore R0..R7 */ \
  467. "ldm sp++, r0-r7 \n\t"\
  468. \
  469. "sub sp, -6*4 \n\t"\
  470. \
  471. "rets" \
  472. ); \
  473. \
  474. /* Force import of global symbols from assembly */ \
  475. ulCriticalNesting; \
  476. pxCurrentTCB; \
  477. }
  478. /*
  479. * The ISR used depends on whether the cooperative or
  480. * the preemptive scheduler is being used.
  481. */
  482. #if configUSE_PREEMPTION == 0
  483. /*
  484. * ISR entry and exit macros. These are only required if a task switch
  485. * is required from the ISR.
  486. */
  487. #define portENTER_SWITCHING_ISR() \
  488. { \
  489. /* Save R0..R7 */ \
  490. __asm__ __volatile__ ("stm --sp, r0-r7"); \
  491. \
  492. /* With the cooperative scheduler, as there is no context switch by interrupt, */ \
  493. /* there is also no context save. */ \
  494. }
  495. /*
  496. * Input parameter: in R12, boolean. Perform a vTaskSwitchContext() if 1
  497. */
  498. #define portEXIT_SWITCHING_ISR() \
  499. { \
  500. __asm__ __volatile__ ( \
  501. /* Restore R0..R7 */ \
  502. "ldm sp++, r0-r7 \n\t"\
  503. \
  504. /* With the cooperative scheduler, as there is no context switch by interrupt, */ \
  505. /* there is also no context restore. */ \
  506. "rete" \
  507. ); \
  508. }
  509. #else
  510. /*
  511. * ISR entry and exit macros. These are only required if a task switch
  512. * is required from the ISR.
  513. */
  514. #define portENTER_SWITCHING_ISR() \
  515. { \
  516. extern volatile uint32_t ulCriticalNesting; \
  517. extern volatile void *volatile pxCurrentTCB; \
  518. \
  519. /* When we come here */ \
  520. /* Registers R8..R12, LR, PC and SR had already been pushed to system stack */ \
  521. \
  522. __asm__ __volatile__ ( \
  523. /* Save R0..R7 */ \
  524. "stm --sp, r0-r7 \n\t"\
  525. \
  526. /* Save ulCriticalNesting variable - R0 is overwritten */ \
  527. "mov r8, LWRD("ASTRINGZ(ulCriticalNesting)") \n\t"\
  528. "orh r8, HWRD("ASTRINGZ(ulCriticalNesting)") \n\t"\
  529. "ld.w r0, r8[0] \n\t"\
  530. "st.w --sp, r0 \n\t"\
  531. \
  532. /* Check if INT0 or higher were being handled (case where the OS tick interrupted another */ \
  533. /* interrupt handler (which was of a higher priority level but decided to lower its priority */ \
  534. /* level and allow other lower interrupt level to occur). */ \
  535. /* In this case we don't want to do a task switch because we don't know what the stack */ \
  536. /* currently looks like (we don't know what the interrupted interrupt handler was doing). */ \
  537. /* Saving SP in pxCurrentTCB and then later restoring it (thinking restoring the task) */ \
  538. /* will just be restoring the interrupt handler, no way!!! */ \
  539. /* So, since we won't do a vTaskSwitchContext(), it's of no use to save SP. */ \
  540. "ld.w r0, sp[9*4] \n\t" /* Read SR in stack */\
  541. "bfextu r0, r0, 22, 3 \n\t" /* Extract the mode bits to R0. */\
  542. "cp.w r0, 1 \n\t" /* Compare the mode bits with supervisor mode(b'001) */\
  543. "brhi LABEL_ISR_SKIP_SAVE_CONTEXT_"ASTRINGZ(__LINE__)" \n\t"\
  544. \
  545. /* Store SP in the first member of the structure pointed to by pxCurrentTCB */ \
  546. "mov r8, LWRD("ASTRINGZ(pxCurrentTCB)") \n\t"\
  547. "orh r8, HWRD("ASTRINGZ(pxCurrentTCB)") \n\t"\
  548. "ld.w r0, r8[0] \n\t"\
  549. "st.w r0[0], sp \n"\
  550. \
  551. "LABEL_ISR_SKIP_SAVE_CONTEXT_"ASTRINGZ(__LINE__)":" \
  552. ); \
  553. }
  554. /*
  555. * Input parameter: in R12, boolean. Perform a vTaskSwitchContext() if 1
  556. */
  557. #define portEXIT_SWITCHING_ISR() \
  558. { \
  559. extern volatile uint32_t ulCriticalNesting; \
  560. extern volatile void *volatile pxCurrentTCB; \
  561. \
  562. __asm__ __volatile__ ( \
  563. /* Check if INT0 or higher were being handled (case where the OS tick interrupted another */ \
  564. /* interrupt handler (which was of a higher priority level but decided to lower its priority */ \
  565. /* level and allow other lower interrupt level to occur). */ \
  566. /* In this case it's of no use to switch context and restore a new SP because we purposedly */ \
  567. /* did not previously save SP in its TCB. */ \
  568. "ld.w r0, sp[9*4] \n\t" /* Read SR in stack */\
  569. "bfextu r0, r0, 22, 3 \n\t" /* Extract the mode bits to R0. */\
  570. "cp.w r0, 1 \n\t" /* Compare the mode bits with supervisor mode(b'001) */\
  571. "brhi LABEL_ISR_SKIP_RESTORE_CONTEXT_"ASTRINGZ(__LINE__)" \n\t"\
  572. \
  573. /* If a switch is required then we just need to call */ \
  574. /* vTaskSwitchContext() as the context has already been */ \
  575. /* saved. */ \
  576. "cp.w r12, 1 \n\t" /* Check if Switch context is required. */\
  577. "brne LABEL_ISR_RESTORE_CONTEXT_"ASTRINGZ(__LINE__)":C" \
  578. ); \
  579. \
  580. /* A critical section has to be used here because vTaskSwitchContext handles FreeRTOS linked lists. */\
  581. portENTER_CRITICAL(); \
  582. vTaskSwitchContext(); \
  583. portEXIT_CRITICAL(); \
  584. \
  585. __asm__ __volatile__ ( \
  586. "LABEL_ISR_RESTORE_CONTEXT_"ASTRINGZ(__LINE__)": \n\t"\
  587. /* Restore the context of which ever task is now the highest */ \
  588. /* priority that is ready to run. */ \
  589. \
  590. /* Restore all registers */ \
  591. \
  592. /* Set SP to point to new stack */ \
  593. "mov r8, LWRD("ASTRINGZ(pxCurrentTCB)") \n\t"\
  594. "orh r8, HWRD("ASTRINGZ(pxCurrentTCB)") \n\t"\
  595. "ld.w r0, r8[0] \n\t"\
  596. "ld.w sp, r0[0] \n"\
  597. \
  598. "LABEL_ISR_SKIP_RESTORE_CONTEXT_"ASTRINGZ(__LINE__)": \n\t"\
  599. \
  600. /* Restore ulCriticalNesting variable */ \
  601. "ld.w r0, sp++ \n\t"\
  602. "mov r8, LWRD("ASTRINGZ(ulCriticalNesting)") \n\t"\
  603. "orh r8, HWRD("ASTRINGZ(ulCriticalNesting)") \n\t"\
  604. "st.w r8[0], r0 \n\t"\
  605. \
  606. /* Restore R0..R7 */ \
  607. "ldm sp++, r0-r7 \n\t"\
  608. \
  609. /* Now, the stack should be R8..R12, LR, PC and SR */ \
  610. "rete" \
  611. ); \
  612. \
  613. /* Force import of global symbols from assembly */ \
  614. ulCriticalNesting; \
  615. pxCurrentTCB; \
  616. }
  617. #endif
  618. #define portYIELD() {__asm__ __volatile__ ("scall");}
  619. /* Task function macros as described on the FreeRTOS.org WEB site. */
  620. #define portTASK_FUNCTION_PROTO( vFunction, pvParameters ) void vFunction( void *pvParameters )
  621. #define portTASK_FUNCTION( vFunction, pvParameters ) void vFunction( void *pvParameters )
  622. #ifdef __cplusplus
  623. }
  624. #endif
  625. #endif /* PORTMACRO_H */