]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/IAR/ATMega323/portmacro.s90
7fa1572b8548ad48b7b1b1c5b5b84a4b00943f29
[freertos] / FreeRTOS / Source / portable / IAR / ATMega323 / portmacro.s90
1 ;/*\r
2 ;    FreeRTOS V7.5.2 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3 ;\r
4 ;\r
5 ;    ***************************************************************************\r
6 ;     *                                                                       *\r
7 ;     *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
8 ;     *    Complete, revised, and edited pdf reference manuals are also       *\r
9 ;     *    available.                                                         *\r
10 ;     *                                                                       *\r
11 ;     *    Purchasing FreeRTOS documentation will not only help you, by       *\r
12 ;     *    ensuring you get running as quickly as possible and with an        *\r
13 ;     *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
14 ;     *    the FreeRTOS project to continue with its mission of providing     *\r
15 ;     *    professional grade, cross platform, de facto standard solutions    *\r
16 ;     *    for microcontrollers - completely free of charge!                  *\r
17 ;     *                                                                       *\r
18 ;     *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
19 ;     *                                                                       *\r
20 ;     *    Thank you for using FreeRTOS, and thank you for your support!      *\r
21 ;     *                                                                       *\r
22 ;    ***************************************************************************\r
23 ;\r
24 ;\r
25 ;    This file is part of the FreeRTOS distribution.\r
26 ;\r
27 ;    FreeRTOS is free software; you can redistribute it and/or modify it under\r
28 ;    the terms of the GNU General Public License (version 2) as published by the\r
29 ;    Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
30 ;    >>>NOTE<<< The modification to the GPL is included to allow you to\r
31 ;    distribute a combined work that includes FreeRTOS without being obliged to\r
32 ;    provide the source code for proprietary components outside of the FreeRTOS\r
33 ;    kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
34 ;    WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
35 ;    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
36 ;    more details. You should have received a copy of the GNU General Public\r
37 ;    License and the FreeRTOS license exception along with FreeRTOS; if not it\r
38 ;    can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
39 ;    by writing to Richard Barry, contact details for whom are available on the\r
40 ;    FreeRTOS WEB site.\r
41 ;\r
42 ;    1 tab == 4 spaces!\r
43 ;\r
44 ;    http://www.FreeRTOS.org - Documentation, latest information, license and\r
45 ;    contact details.\r
46 ;\r
47 ;    http://www.SafeRTOS.com - A version that is certified for use in safety\r
48 ;    critical systems.\r
49 ;\r
50 ;    http://www.OpenRTOS.com - Commercial support, development, porting,\r
51 ;    licensing and training services.\r
52 ;*/\r
53 \r
54 #include <iom323.h>\r
55 \r
56 ; Declare all extern symbols here - including any ISRs that are referenced in\r
57 ; the vector table.\r
58 \r
59 ; ISR functions\r
60 ; -------------\r
61 EXTERN SIG_OUTPUT_COMPARE1A\r
62 EXTERN SIG_UART_RECV\r
63 EXTERN SIG_UART_DATA\r
64 \r
65 \r
66 ; Functions used by scheduler\r
67 ; ---------------------------\r
68 EXTERN vTaskSwitchContext\r
69 EXTERN pxCurrentTCB\r
70 EXTERN xTaskIncrementTick\r
71 EXTERN uxCriticalNesting\r
72 \r
73 ; Functions implemented in this file\r
74 ; ----------------------------------\r
75 PUBLIC vPortYield\r
76 PUBLIC vPortYieldFromTick\r
77 PUBLIC vPortStart\r
78 \r
79 \r
80 ; Interrupt vector table.\r
81 ; -----------------------\r
82 ;\r
83 ; For simplicity the RTOS tick interrupt routine uses the __task keyword.\r
84 ; As the IAR compiler does not permit a function to be declared using both\r
85 ; __task and __interrupt, the use of __task necessitates that the interrupt\r
86 ; vector table be setup manually.\r
87 ;\r
88 ; To write an ISR, implement the ISR function using the __interrupt keyword\r
89 ; but do not install the interrupt using the "#pragma vector=ABC" method.\r
90 ; Instead manually place the name of the ISR in the vector table using an\r
91 ; ORG and jmp instruction as demonstrated below.\r
92 ; You will also have to add an EXTERN statement at the top of the file.\r
93 \r
94         ASEG\r
95 \r
96 \r
97         ORG TIMER1_COMPA_vect                           ; Vector address\r
98                 jmp SIG_OUTPUT_COMPARE1A                ; ISR\r
99 \r
100         ORG USART_RXC_vect                                      ; Vector address\r
101                 jmp SIG_UART_RECV                               ; ISR\r
102 \r
103         ORG USART_UDRE_vect                                     ; Vector address\r
104                 jmp SIG_UART_DATA                               ; ISR\r
105 \r
106 \r
107         RSEG CODE\r
108 \r
109 \r
110 \r
111 ; Saving and Restoring a Task Context and Task Switching\r
112 ; ------------------------------------------------------\r
113 ;\r
114 ; The IAR compiler does not fully support inline assembler, so saving and\r
115 ; restoring a task context has to be written in an asm file.\r
116 ;\r
117 ; vPortYield() and vPortYieldFromTick() are usually written in C.  Doing\r
118 ; so in this case would required calls to be made to portSAVE_CONTEXT() and\r
119 ; portRESTORE_CONTEXT().  This is dis-advantageous as the context switch\r
120 ; function would require two extra jump and return instructions over the\r
121 ; WinAVR equivalent.\r
122 ;\r
123 ; To avoid this I have opted to implement both vPortYield() and\r
124 ; vPortYieldFromTick() in this assembly file.  For convenience\r
125 ; portSAVE_CONTEXT and portRESTORE_CONTEXT are implemented as macros.\r
126 \r
127 portSAVE_CONTEXT MACRO\r
128         st      -y, r0                  ; First save the r0 register - we need to use this.\r
129         in      r0, SREG                ; Obtain the SREG value so we can disable interrupts...\r
130         cli                                     ; ... as soon as possible.\r
131         st      -y, r0                  ; Store the SREG as it was before we disabled interrupts.\r
132 \r
133         in      r0, SPL                 ; Next store the hardware stack pointer.  The IAR...\r
134         st      -y, r0                  ; ... compiler uses the hardware stack as a call stack ...\r
135         in      r0, SPH                 ; ...  only.\r
136         st      -y, r0\r
137 \r
138         st      -y, r1                  ; Now store the rest of the registers.  Dont store the ...\r
139         st      -y, r2                  ; ... the Y register here as it is used as the software\r
140         st      -y, r3                  ; stack pointer and will get saved into the TCB.\r
141         st      -y, r4\r
142         st      -y, r5\r
143         st      -y, r6\r
144         st      -y, r7\r
145         st      -y, r8\r
146         st      -y, r9\r
147         st      -y, r10\r
148         st      -y, r11\r
149         st      -y, r12\r
150         st      -y, r13\r
151         st      -y, r14\r
152         st      -y, r15\r
153         st      -y, r16\r
154         st      -y, r17\r
155         st      -y, r18\r
156         st      -y, r19\r
157         st      -y, r20\r
158         st      -y, r21\r
159         st      -y, r22\r
160         st      -y, r23\r
161         st      -y, r24\r
162         st      -y, r25\r
163         st      -y, r26\r
164         st      -y, r27\r
165         st      -y, r30\r
166         st      -y, r31\r
167         lds r0, uxCriticalNesting\r
168         st      -y, r0                                  ; Store the critical nesting counter.\r
169 \r
170         lds     r26, pxCurrentTCB               ; Finally save the software stack pointer (Y ...\r
171         lds     r27, pxCurrentTCB + 1   ; ... register) into the TCB.\r
172         st      x+, r28\r
173         st      x+, r29\r
174 \r
175         ENDM\r
176 \r
177 \r
178 portRESTORE_CONTEXT MACRO\r
179         lds     r26, pxCurrentTCB\r
180         lds     r27, pxCurrentTCB + 1   ; Restore the software stack pointer from ...\r
181         ld      r28, x+                                 ; the TCB into the software stack pointer (...\r
182         ld      r29, x+                                 ; ... the Y register).\r
183 \r
184         ld      r0, y+\r
185         sts     uxCriticalNesting, r0\r
186         ld      r31, y+                                 ; Restore the registers down to R0.  The Y\r
187         ld      r30, y+                                 ; register is missing from this list as it\r
188         ld      r27, y+                                 ; has already been restored.\r
189         ld      r26, y+\r
190         ld      r25, y+\r
191         ld      r24, y+\r
192         ld      r23, y+\r
193         ld      r22, y+\r
194         ld      r21, y+\r
195         ld      r20, y+\r
196         ld      r19, y+\r
197         ld      r18, y+\r
198         ld      r17, y+\r
199         ld      r16, y+\r
200         ld      r15, y+\r
201         ld      r14, y+\r
202         ld      r13, y+\r
203         ld      r12, y+\r
204         ld      r11, y+\r
205         ld      r10, y+\r
206         ld      r9, y+\r
207         ld      r8, y+\r
208         ld      r7, y+\r
209         ld      r6, y+\r
210         ld      r5, y+\r
211         ld      r4, y+\r
212         ld      r3, y+\r
213         ld      r2, y+\r
214         ld      r1, y+\r
215 \r
216         ld      r0, y+                                  ; The next thing on the stack is the ...\r
217         out     SPH, r0                                 ; ... hardware stack pointer.\r
218         ld      r0, y+\r
219         out     SPL, r0\r
220 \r
221         ld      r0, y+                                  ; Next there is the SREG register.\r
222         out SREG, r0\r
223 \r
224         ld      r0, y+                                  ; Finally we have finished with r0, so restore r0.\r
225 \r
226         ENDM\r
227 \r
228 \r
229 \r
230 ; vPortYield() and vPortYieldFromTick()\r
231 ; -------------------------------------\r
232 ;\r
233 ; Manual and preemptive context switch functions respectively.\r
234 ; The IAR compiler does not fully support inline assembler,\r
235 ; so these are implemented here rather than the more usually\r
236 ; place of within port.c.\r
237 \r
238 vPortYield:\r
239         portSAVE_CONTEXT                        ; Save the context of the current task.\r
240         call vTaskSwitchContext         ; Call the scheduler.\r
241         portRESTORE_CONTEXT                     ; Restore the context of whichever task the ...\r
242         ret                                                     ; ... scheduler decided should run.\r
243 \r
244 vPortYieldFromTick:\r
245         portSAVE_CONTEXT                        ; Save the context of the current task.\r
246         call xTaskIncrementTick         ; Call the timer tick function.\r
247         tst r16\r
248         breq SkipTaskSwitch\r
249         call vTaskSwitchContext         ; Call the scheduler.\r
250 SkipTaskSwitch:\r
251         portRESTORE_CONTEXT                     ; Restore the context of whichever task the ...\r
252         ret                                                     ; ... scheduler decided should run.\r
253 \r
254 ; vPortStart()\r
255 ; ------------\r
256 ;\r
257 ; Again due to the lack of inline assembler, this is required\r
258 ; to get access to the portRESTORE_CONTEXT macro.\r
259 \r
260 vPortStart:\r
261         portRESTORE_CONTEXT\r
262         ret\r
263 \r
264 \r
265 ; Just a filler for unused interrupt vectors.\r
266 vNoISR:\r
267         reti\r
268 \r
269 \r
270         END\r
271 \r