]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/IAR/ATMega323/port.c
Update version number in readiness for V10.3.0 release. Sync SVN with reviewed releas...
[freertos] / FreeRTOS / Source / portable / IAR / ATMega323 / port.c
1 /*\r
2  * FreeRTOS Kernel V10.3.0\r
3  * Copyright (C) 2020 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software.\r
14  *\r
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 \r
28 #include <stdlib.h>\r
29 \r
30 #include "FreeRTOS.h"\r
31 #include "task.h"\r
32 \r
33 /*-----------------------------------------------------------\r
34  * Implementation of functions defined in portable.h for the AVR/IAR port.\r
35  *----------------------------------------------------------*/\r
36 \r
37 /* Start tasks with interrupts enables. */\r
38 #define portFLAGS_INT_ENABLED                                   ( ( StackType_t ) 0x80 )\r
39 \r
40 /* Hardware constants for timer 1. */\r
41 #define portCLEAR_COUNTER_ON_MATCH                              ( ( uint8_t ) 0x08 )\r
42 #define portPRESCALE_64                                                 ( ( uint8_t ) 0x03 )\r
43 #define portCLOCK_PRESCALER                                             ( ( uint32_t ) 64 )\r
44 #define portCOMPARE_MATCH_A_INTERRUPT_ENABLE    ( ( uint8_t ) 0x10 )\r
45 \r
46 /* The number of bytes used on the hardware stack by the task start address. */\r
47 #define portBYTES_USED_BY_RETURN_ADDRESS                ( 2 )\r
48 /*-----------------------------------------------------------*/\r
49 \r
50 /* Stores the critical section nesting.  This must not be initialised to 0.\r
51 It will be initialised when a task starts. */\r
52 #define portNO_CRITICAL_NESTING                                 ( ( UBaseType_t ) 0 )\r
53 UBaseType_t uxCriticalNesting = 0x50;\r
54 \r
55 \r
56 /*\r
57  * Perform hardware setup to enable ticks from timer 1, compare match A.\r
58  */\r
59 static void prvSetupTimerInterrupt( void );\r
60 \r
61 /*\r
62  * The IAR compiler does not have full support for inline assembler, so\r
63  * these are defined in the portmacro assembler file.\r
64  */\r
65 extern void vPortYieldFromTick( void );\r
66 extern void vPortStart( void );\r
67 \r
68 /*-----------------------------------------------------------*/\r
69 \r
70 /*\r
71  * See header file for description.\r
72  */\r
73 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )\r
74 {\r
75 uint16_t usAddress;\r
76 StackType_t *pxTopOfHardwareStack;\r
77 \r
78         /* Place a few bytes of known values on the bottom of the stack.\r
79         This is just useful for debugging. */\r
80 \r
81         *pxTopOfStack = 0x11;\r
82         pxTopOfStack--;\r
83         *pxTopOfStack = 0x22;\r
84         pxTopOfStack--;\r
85         *pxTopOfStack = 0x33;\r
86         pxTopOfStack--;\r
87 \r
88         /* Remember where the top of the hardware stack is - this is required\r
89         below. */\r
90         pxTopOfHardwareStack = pxTopOfStack;\r
91 \r
92 \r
93         /* Simulate how the stack would look after a call to vPortYield(). */\r
94 \r
95         /*lint -e950 -e611 -e923 Lint doesn't like this much - but nothing I can do about it. */\r
96 \r
97 \r
98 \r
99         /* The IAR compiler requires two stacks per task.  First there is the\r
100         hardware call stack which uses the AVR stack pointer.  Second there is the\r
101         software stack (local variables, parameter passing, etc.) which uses the\r
102         AVR Y register.\r
103 \r
104         This function places both stacks within the memory block passed in as the\r
105         first parameter.  The hardware stack is placed at the bottom of the memory\r
106         block.  A gap is then left for the hardware stack to grow.  Next the software\r
107         stack is placed.  The amount of space between the software and hardware\r
108         stacks is defined by configCALL_STACK_SIZE.\r
109 \r
110 \r
111 \r
112         The first part of the stack is the hardware stack.  Place the start\r
113         address of the task on the hardware stack. */\r
114         usAddress = ( uint16_t ) pxCode;\r
115         *pxTopOfStack = ( StackType_t ) ( usAddress & ( uint16_t ) 0x00ff );\r
116         pxTopOfStack--;\r
117 \r
118         usAddress >>= 8;\r
119         *pxTopOfStack = ( StackType_t ) ( usAddress & ( uint16_t ) 0x00ff );\r
120         pxTopOfStack--;\r
121 \r
122 \r
123         /* Leave enough space for the hardware stack before starting the software\r
124         stack.  The '- 2' is because we have already used two spaces for the\r
125         address of the start of the task. */\r
126         pxTopOfStack -= ( configCALL_STACK_SIZE - 2 );\r
127 \r
128 \r
129 \r
130         /* Next simulate the stack as if after a call to portSAVE_CONTEXT().\r
131         portSAVE_CONTEXT places the flags on the stack immediately after r0\r
132         to ensure the interrupts get disabled as soon as possible, and so ensuring\r
133         the stack use is minimal should a context switch interrupt occur. */\r
134         *pxTopOfStack = ( StackType_t ) 0x00;   /* R0 */\r
135         pxTopOfStack--;\r
136         *pxTopOfStack = portFLAGS_INT_ENABLED;\r
137         pxTopOfStack--;\r
138 \r
139         /* Next place the address of the hardware stack.  This is required so\r
140         the AVR stack pointer can be restored to point to the hardware stack. */\r
141         pxTopOfHardwareStack -= portBYTES_USED_BY_RETURN_ADDRESS;\r
142         usAddress = ( uint16_t ) pxTopOfHardwareStack;\r
143 \r
144         /* SPL */\r
145         *pxTopOfStack = ( StackType_t ) ( usAddress & ( uint16_t ) 0x00ff );\r
146         pxTopOfStack--;\r
147 \r
148         /* SPH */\r
149         usAddress >>= 8;\r
150         *pxTopOfStack = ( StackType_t ) ( usAddress & ( uint16_t ) 0x00ff );\r
151         pxTopOfStack--;\r
152 \r
153 \r
154 \r
155 \r
156         /* Now the remaining registers. */\r
157         *pxTopOfStack = ( StackType_t ) 0x01;   /* R1 */\r
158         pxTopOfStack--;\r
159         *pxTopOfStack = ( StackType_t ) 0x02;   /* R2 */\r
160         pxTopOfStack--;\r
161         *pxTopOfStack = ( StackType_t ) 0x03;   /* R3 */\r
162         pxTopOfStack--;\r
163         *pxTopOfStack = ( StackType_t ) 0x04;   /* R4 */\r
164         pxTopOfStack--;\r
165         *pxTopOfStack = ( StackType_t ) 0x05;   /* R5 */\r
166         pxTopOfStack--;\r
167         *pxTopOfStack = ( StackType_t ) 0x06;   /* R6 */\r
168         pxTopOfStack--;\r
169         *pxTopOfStack = ( StackType_t ) 0x07;   /* R7 */\r
170         pxTopOfStack--;\r
171         *pxTopOfStack = ( StackType_t ) 0x08;   /* R8 */\r
172         pxTopOfStack--;\r
173         *pxTopOfStack = ( StackType_t ) 0x09;   /* R9 */\r
174         pxTopOfStack--;\r
175         *pxTopOfStack = ( StackType_t ) 0x10;   /* R10 */\r
176         pxTopOfStack--;\r
177         *pxTopOfStack = ( StackType_t ) 0x11;   /* R11 */\r
178         pxTopOfStack--;\r
179         *pxTopOfStack = ( StackType_t ) 0x12;   /* R12 */\r
180         pxTopOfStack--;\r
181         *pxTopOfStack = ( StackType_t ) 0x13;   /* R13 */\r
182         pxTopOfStack--;\r
183         *pxTopOfStack = ( StackType_t ) 0x14;   /* R14 */\r
184         pxTopOfStack--;\r
185         *pxTopOfStack = ( StackType_t ) 0x15;   /* R15 */\r
186         pxTopOfStack--;\r
187 \r
188         /* Place the parameter on the stack in the expected location. */\r
189         usAddress = ( uint16_t ) pvParameters;\r
190         *pxTopOfStack = ( StackType_t ) ( usAddress & ( uint16_t ) 0x00ff );\r
191         pxTopOfStack--;\r
192 \r
193         usAddress >>= 8;\r
194         *pxTopOfStack = ( StackType_t ) ( usAddress & ( uint16_t ) 0x00ff );\r
195         pxTopOfStack--;\r
196 \r
197         *pxTopOfStack = ( StackType_t ) 0x18;   /* R18 */\r
198         pxTopOfStack--;\r
199         *pxTopOfStack = ( StackType_t ) 0x19;   /* R19 */\r
200         pxTopOfStack--;\r
201         *pxTopOfStack = ( StackType_t ) 0x20;   /* R20 */\r
202         pxTopOfStack--;\r
203         *pxTopOfStack = ( StackType_t ) 0x21;   /* R21 */\r
204         pxTopOfStack--;\r
205         *pxTopOfStack = ( StackType_t ) 0x22;   /* R22 */\r
206         pxTopOfStack--;\r
207         *pxTopOfStack = ( StackType_t ) 0x23;   /* R23 */\r
208         pxTopOfStack--;\r
209         *pxTopOfStack = ( StackType_t ) 0x24;   /* R24 */\r
210         pxTopOfStack--;\r
211         *pxTopOfStack = ( StackType_t ) 0x25;   /* R25 */\r
212         pxTopOfStack--;\r
213         *pxTopOfStack = ( StackType_t ) 0x26;   /* R26 X */\r
214         pxTopOfStack--;\r
215         *pxTopOfStack = ( StackType_t ) 0x27;   /* R27 */\r
216         pxTopOfStack--;\r
217 \r
218         /* The Y register is not stored as it is used as the software stack and\r
219         gets saved into the task control block. */\r
220 \r
221         *pxTopOfStack = ( StackType_t ) 0x30;   /* R30 Z */\r
222         pxTopOfStack--;\r
223         *pxTopOfStack = ( StackType_t ) 0x031;  /* R31 */\r
224 \r
225         pxTopOfStack--;\r
226         *pxTopOfStack = portNO_CRITICAL_NESTING;        /* Critical nesting is zero when the task starts. */\r
227 \r
228         /*lint +e950 +e611 +e923 */\r
229 \r
230         return pxTopOfStack;\r
231 }\r
232 /*-----------------------------------------------------------*/\r
233 \r
234 BaseType_t xPortStartScheduler( void )\r
235 {\r
236         /* Setup the hardware to generate the tick. */\r
237         prvSetupTimerInterrupt();\r
238 \r
239         /* Restore the context of the first task that is going to run.\r
240         Normally we would just call portRESTORE_CONTEXT() here, but as the IAR\r
241         compiler does not fully support inline assembler we have to make a call.*/\r
242         vPortStart();\r
243 \r
244         /* Should not get here! */\r
245         return pdTRUE;\r
246 }\r
247 /*-----------------------------------------------------------*/\r
248 \r
249 void vPortEndScheduler( void )\r
250 {\r
251         /* It is unlikely that the AVR port will get stopped.  If required simply\r
252         disable the tick interrupt here. */\r
253 }\r
254 /*-----------------------------------------------------------*/\r
255 \r
256 /*\r
257  * Setup timer 1 compare match A to generate a tick interrupt.\r
258  */\r
259 static void prvSetupTimerInterrupt( void )\r
260 {\r
261 uint32_t ulCompareMatch;\r
262 uint8_t ucHighByte, ucLowByte;\r
263 \r
264         /* Using 16bit timer 1 to generate the tick.  Correct fuses must be\r
265         selected for the configCPU_CLOCK_HZ clock. */\r
266 \r
267         ulCompareMatch = configCPU_CLOCK_HZ / configTICK_RATE_HZ;\r
268 \r
269         /* We only have 16 bits so have to scale to get our required tick rate. */\r
270         ulCompareMatch /= portCLOCK_PRESCALER;\r
271 \r
272         /* Adjust for correct value. */\r
273         ulCompareMatch -= ( uint32_t ) 1;\r
274 \r
275         /* Setup compare match value for compare match A.  Interrupts are disabled\r
276         before this is called so we need not worry here. */\r
277         ucLowByte = ( uint8_t ) ( ulCompareMatch & ( uint32_t ) 0xff );\r
278         ulCompareMatch >>= 8;\r
279         ucHighByte = ( uint8_t ) ( ulCompareMatch & ( uint32_t ) 0xff );\r
280         OCR1AH = ucHighByte;\r
281         OCR1AL = ucLowByte;\r
282 \r
283         /* Setup clock source and compare match behaviour. */\r
284         ucLowByte = portCLEAR_COUNTER_ON_MATCH | portPRESCALE_64;\r
285         TCCR1B = ucLowByte;\r
286 \r
287         /* Enable the interrupt - this is okay as interrupt are currently globally\r
288         disabled. */\r
289         TIMSK |= portCOMPARE_MATCH_A_INTERRUPT_ENABLE;\r
290 }\r
291 /*-----------------------------------------------------------*/\r
292 \r
293 #if configUSE_PREEMPTION == 1\r
294 \r
295         /*\r
296          * Tick ISR for preemptive scheduler.  We can use a __task attribute as\r
297          * the context is saved at the start of vPortYieldFromTick().  The tick\r
298          * count is incremented after the context is saved.\r
299          */\r
300         __task void SIG_OUTPUT_COMPARE1A( void )\r
301         {\r
302                 vPortYieldFromTick();\r
303                 asm( "reti" );\r
304         }\r
305 \r
306 #else\r
307 \r
308         /*\r
309          * Tick ISR for the cooperative scheduler.  All this does is increment the\r
310          * tick count.  We don't need to switch context, this can only be done by\r
311          * manual calls to taskYIELD();\r
312          *\r
313          * THE INTERRUPT VECTOR IS POPULATED IN portmacro.s90.  DO NOT INSTALL\r
314          * IT HERE USING THE USUAL PRAGMA.\r
315          */\r
316         __interrupt void SIG_OUTPUT_COMPARE1A( void )\r
317         {\r
318                 xTaskIncrementTick();\r
319         }\r
320 #endif\r
321 /*-----------------------------------------------------------*/\r
322 \r
323 void vPortEnterCritical( void )\r
324 {\r
325         portDISABLE_INTERRUPTS();\r
326         uxCriticalNesting++;\r
327 }\r
328 /*-----------------------------------------------------------*/\r
329 \r
330 void vPortExitCritical( void )\r
331 {\r
332         uxCriticalNesting--;\r
333         if( uxCriticalNesting == portNO_CRITICAL_NESTING )\r
334         {\r
335                 portENABLE_INTERRUPTS();\r
336         }\r
337 }\r
338 \r
339 \r