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