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