]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/GCC/ATMega323/port.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Source / portable / GCC / 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 /* \r
30 \r
31 Changes from V2.6.0\r
32 \r
33         + AVR port - Replaced the inb() and outb() functions with direct memory\r
34           access.  This allows the port to be built with the 20050414 build of\r
35           WinAVR.\r
36 */\r
37 \r
38 #include <stdlib.h>\r
39 #include <avr/interrupt.h>\r
40 \r
41 #include "FreeRTOS.h"\r
42 #include "task.h"\r
43 \r
44 /*-----------------------------------------------------------\r
45  * Implementation of functions defined in portable.h for the AVR port.\r
46  *----------------------------------------------------------*/\r
47 \r
48 /* Start tasks with interrupts enables. */\r
49 #define portFLAGS_INT_ENABLED                                   ( ( StackType_t ) 0x80 )\r
50 \r
51 /* Hardware constants for timer 1. */\r
52 #define portCLEAR_COUNTER_ON_MATCH                              ( ( uint8_t ) 0x08 )\r
53 #define portPRESCALE_64                                                 ( ( uint8_t ) 0x03 )\r
54 #define portCLOCK_PRESCALER                                             ( ( uint32_t ) 64 )\r
55 #define portCOMPARE_MATCH_A_INTERRUPT_ENABLE    ( ( uint8_t ) 0x10 )\r
56 \r
57 /*-----------------------------------------------------------*/\r
58 \r
59 /* We require the address of the pxCurrentTCB variable, but don't want to know\r
60 any details of its type. */\r
61 typedef void TCB_t;\r
62 extern volatile TCB_t * volatile pxCurrentTCB;\r
63 \r
64 /*-----------------------------------------------------------*/\r
65 \r
66 /* \r
67  * Macro to save all the general purpose registers, the save the stack pointer\r
68  * into the TCB.  \r
69  * \r
70  * The first thing we do is save the flags then disable interrupts.  This is to \r
71  * guard our stack against having a context switch interrupt after we have already \r
72  * pushed the registers onto the stack - causing the 32 registers to be on the \r
73  * stack twice. \r
74  * \r
75  * r1 is set to zero as the compiler expects it to be thus, however some\r
76  * of the math routines make use of R1. \r
77  * \r
78  * The interrupts will have been disabled during the call to portSAVE_CONTEXT()\r
79  * so we need not worry about reading/writing to the stack pointer. \r
80  */\r
81 \r
82 #define portSAVE_CONTEXT()                                                                      \\r
83         asm volatile (  "push   r0                                              \n\t"   \\r
84                                         "in             r0, __SREG__                    \n\t"   \\r
85                                         "cli                                                    \n\t"   \\r
86                                         "push   r0                                              \n\t"   \\r
87                                         "push   r1                                              \n\t"   \\r
88                                         "clr    r1                                              \n\t"   \\r
89                                         "push   r2                                              \n\t"   \\r
90                                         "push   r3                                              \n\t"   \\r
91                                         "push   r4                                              \n\t"   \\r
92                                         "push   r5                                              \n\t"   \\r
93                                         "push   r6                                              \n\t"   \\r
94                                         "push   r7                                              \n\t"   \\r
95                                         "push   r8                                              \n\t"   \\r
96                                         "push   r9                                              \n\t"   \\r
97                                         "push   r10                                             \n\t"   \\r
98                                         "push   r11                                             \n\t"   \\r
99                                         "push   r12                                             \n\t"   \\r
100                                         "push   r13                                             \n\t"   \\r
101                                         "push   r14                                             \n\t"   \\r
102                                         "push   r15                                             \n\t"   \\r
103                                         "push   r16                                             \n\t"   \\r
104                                         "push   r17                                             \n\t"   \\r
105                                         "push   r18                                             \n\t"   \\r
106                                         "push   r19                                             \n\t"   \\r
107                                         "push   r20                                             \n\t"   \\r
108                                         "push   r21                                             \n\t"   \\r
109                                         "push   r22                                             \n\t"   \\r
110                                         "push   r23                                             \n\t"   \\r
111                                         "push   r24                                             \n\t"   \\r
112                                         "push   r25                                             \n\t"   \\r
113                                         "push   r26                                             \n\t"   \\r
114                                         "push   r27                                             \n\t"   \\r
115                                         "push   r28                                             \n\t"   \\r
116                                         "push   r29                                             \n\t"   \\r
117                                         "push   r30                                             \n\t"   \\r
118                                         "push   r31                                             \n\t"   \\r
119                                         "lds    r26, pxCurrentTCB               \n\t"   \\r
120                                         "lds    r27, pxCurrentTCB + 1   \n\t"   \\r
121                                         "in             r0, 0x3d                                \n\t"   \\r
122                                         "st             x+, r0                                  \n\t"   \\r
123                                         "in             r0, 0x3e                                \n\t"   \\r
124                                         "st             x+, r0                                  \n\t"   \\r
125                                 );\r
126 \r
127 /* \r
128  * Opposite to portSAVE_CONTEXT().  Interrupts will have been disabled during\r
129  * the context save so we can write to the stack pointer. \r
130  */\r
131 \r
132 #define portRESTORE_CONTEXT()                                                           \\r
133         asm volatile (  "lds    r26, pxCurrentTCB               \n\t"   \\r
134                                         "lds    r27, pxCurrentTCB + 1   \n\t"   \\r
135                                         "ld             r28, x+                                 \n\t"   \\r
136                                         "out    __SP_L__, r28                   \n\t"   \\r
137                                         "ld             r29, x+                                 \n\t"   \\r
138                                         "out    __SP_H__, r29                   \n\t"   \\r
139                                         "pop    r31                                             \n\t"   \\r
140                                         "pop    r30                                             \n\t"   \\r
141                                         "pop    r29                                             \n\t"   \\r
142                                         "pop    r28                                             \n\t"   \\r
143                                         "pop    r27                                             \n\t"   \\r
144                                         "pop    r26                                             \n\t"   \\r
145                                         "pop    r25                                             \n\t"   \\r
146                                         "pop    r24                                             \n\t"   \\r
147                                         "pop    r23                                             \n\t"   \\r
148                                         "pop    r22                                             \n\t"   \\r
149                                         "pop    r21                                             \n\t"   \\r
150                                         "pop    r20                                             \n\t"   \\r
151                                         "pop    r19                                             \n\t"   \\r
152                                         "pop    r18                                             \n\t"   \\r
153                                         "pop    r17                                             \n\t"   \\r
154                                         "pop    r16                                             \n\t"   \\r
155                                         "pop    r15                                             \n\t"   \\r
156                                         "pop    r14                                             \n\t"   \\r
157                                         "pop    r13                                             \n\t"   \\r
158                                         "pop    r12                                             \n\t"   \\r
159                                         "pop    r11                                             \n\t"   \\r
160                                         "pop    r10                                             \n\t"   \\r
161                                         "pop    r9                                              \n\t"   \\r
162                                         "pop    r8                                              \n\t"   \\r
163                                         "pop    r7                                              \n\t"   \\r
164                                         "pop    r6                                              \n\t"   \\r
165                                         "pop    r5                                              \n\t"   \\r
166                                         "pop    r4                                              \n\t"   \\r
167                                         "pop    r3                                              \n\t"   \\r
168                                         "pop    r2                                              \n\t"   \\r
169                                         "pop    r1                                              \n\t"   \\r
170                                         "pop    r0                                              \n\t"   \\r
171                                         "out    __SREG__, r0                    \n\t"   \\r
172                                         "pop    r0                                              \n\t"   \\r
173                                 );\r
174 \r
175 /*-----------------------------------------------------------*/\r
176 \r
177 /*\r
178  * Perform hardware setup to enable ticks from timer 1, compare match A.\r
179  */\r
180 static void prvSetupTimerInterrupt( void );\r
181 /*-----------------------------------------------------------*/\r
182 \r
183 /* \r
184  * See header file for description. \r
185  */\r
186 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )\r
187 {\r
188 uint16_t usAddress;\r
189 \r
190         /* Place a few bytes of known values on the bottom of the stack. \r
191         This is just useful for debugging. */\r
192 \r
193         *pxTopOfStack = 0x11;\r
194         pxTopOfStack--;\r
195         *pxTopOfStack = 0x22;\r
196         pxTopOfStack--;\r
197         *pxTopOfStack = 0x33;\r
198         pxTopOfStack--;\r
199 \r
200         /* Simulate how the stack would look after a call to vPortYield() generated by \r
201         the compiler. */\r
202 \r
203         /*lint -e950 -e611 -e923 Lint doesn't like this much - but nothing I can do about it. */\r
204 \r
205         /* The start of the task code will be popped off the stack last, so place\r
206         it on first. */\r
207         usAddress = ( uint16_t ) pxCode;\r
208         *pxTopOfStack = ( StackType_t ) ( usAddress & ( uint16_t ) 0x00ff );\r
209         pxTopOfStack--;\r
210 \r
211         usAddress >>= 8;\r
212         *pxTopOfStack = ( StackType_t ) ( usAddress & ( uint16_t ) 0x00ff );\r
213         pxTopOfStack--;\r
214 \r
215         /* Next simulate the stack as if after a call to portSAVE_CONTEXT().  \r
216         portSAVE_CONTEXT places the flags on the stack immediately after r0\r
217         to ensure the interrupts get disabled as soon as possible, and so ensuring\r
218         the stack use is minimal should a context switch interrupt occur. */\r
219         *pxTopOfStack = ( StackType_t ) 0x00;   /* R0 */\r
220         pxTopOfStack--;\r
221         *pxTopOfStack = portFLAGS_INT_ENABLED;\r
222         pxTopOfStack--;\r
223 \r
224 \r
225         /* Now the remaining registers.   The compiler expects R1 to be 0. */\r
226         *pxTopOfStack = ( StackType_t ) 0x00;   /* R1 */\r
227         pxTopOfStack--;\r
228         *pxTopOfStack = ( StackType_t ) 0x02;   /* R2 */\r
229         pxTopOfStack--;\r
230         *pxTopOfStack = ( StackType_t ) 0x03;   /* R3 */\r
231         pxTopOfStack--;\r
232         *pxTopOfStack = ( StackType_t ) 0x04;   /* R4 */\r
233         pxTopOfStack--;\r
234         *pxTopOfStack = ( StackType_t ) 0x05;   /* R5 */\r
235         pxTopOfStack--;\r
236         *pxTopOfStack = ( StackType_t ) 0x06;   /* R6 */\r
237         pxTopOfStack--;\r
238         *pxTopOfStack = ( StackType_t ) 0x07;   /* R7 */\r
239         pxTopOfStack--;\r
240         *pxTopOfStack = ( StackType_t ) 0x08;   /* R8 */\r
241         pxTopOfStack--;\r
242         *pxTopOfStack = ( StackType_t ) 0x09;   /* R9 */\r
243         pxTopOfStack--;\r
244         *pxTopOfStack = ( StackType_t ) 0x10;   /* R10 */\r
245         pxTopOfStack--;\r
246         *pxTopOfStack = ( StackType_t ) 0x11;   /* R11 */\r
247         pxTopOfStack--;\r
248         *pxTopOfStack = ( StackType_t ) 0x12;   /* R12 */\r
249         pxTopOfStack--;\r
250         *pxTopOfStack = ( StackType_t ) 0x13;   /* R13 */\r
251         pxTopOfStack--;\r
252         *pxTopOfStack = ( StackType_t ) 0x14;   /* R14 */\r
253         pxTopOfStack--;\r
254         *pxTopOfStack = ( StackType_t ) 0x15;   /* R15 */\r
255         pxTopOfStack--;\r
256         *pxTopOfStack = ( StackType_t ) 0x16;   /* R16 */\r
257         pxTopOfStack--;\r
258         *pxTopOfStack = ( StackType_t ) 0x17;   /* R17 */\r
259         pxTopOfStack--;\r
260         *pxTopOfStack = ( StackType_t ) 0x18;   /* R18 */\r
261         pxTopOfStack--;\r
262         *pxTopOfStack = ( StackType_t ) 0x19;   /* R19 */\r
263         pxTopOfStack--;\r
264         *pxTopOfStack = ( StackType_t ) 0x20;   /* R20 */\r
265         pxTopOfStack--;\r
266         *pxTopOfStack = ( StackType_t ) 0x21;   /* R21 */\r
267         pxTopOfStack--;\r
268         *pxTopOfStack = ( StackType_t ) 0x22;   /* R22 */\r
269         pxTopOfStack--;\r
270         *pxTopOfStack = ( StackType_t ) 0x23;   /* R23 */\r
271         pxTopOfStack--;\r
272 \r
273         /* Place the parameter on the stack in the expected location. */\r
274         usAddress = ( uint16_t ) pvParameters;\r
275         *pxTopOfStack = ( StackType_t ) ( usAddress & ( uint16_t ) 0x00ff );\r
276         pxTopOfStack--;\r
277 \r
278         usAddress >>= 8;\r
279         *pxTopOfStack = ( StackType_t ) ( usAddress & ( uint16_t ) 0x00ff );\r
280         pxTopOfStack--;\r
281 \r
282         *pxTopOfStack = ( StackType_t ) 0x26;   /* R26 X */\r
283         pxTopOfStack--;\r
284         *pxTopOfStack = ( StackType_t ) 0x27;   /* R27 */\r
285         pxTopOfStack--;\r
286         *pxTopOfStack = ( StackType_t ) 0x28;   /* R28 Y */\r
287         pxTopOfStack--;\r
288         *pxTopOfStack = ( StackType_t ) 0x29;   /* R29 */\r
289         pxTopOfStack--;\r
290         *pxTopOfStack = ( StackType_t ) 0x30;   /* R30 Z */\r
291         pxTopOfStack--;\r
292         *pxTopOfStack = ( StackType_t ) 0x031;  /* R31 */\r
293         pxTopOfStack--;\r
294 \r
295         /*lint +e950 +e611 +e923 */\r
296 \r
297         return pxTopOfStack;\r
298 }\r
299 /*-----------------------------------------------------------*/\r
300 \r
301 BaseType_t xPortStartScheduler( void )\r
302 {\r
303         /* Setup the hardware to generate the tick. */\r
304         prvSetupTimerInterrupt();\r
305 \r
306         /* Restore the context of the first task that is going to run. */\r
307         portRESTORE_CONTEXT();\r
308 \r
309         /* Simulate a function call end as generated by the compiler.  We will now\r
310         jump to the start of the task the context of which we have just restored. */\r
311         asm volatile ( "ret" );\r
312 \r
313         /* Should not get here. */\r
314         return pdTRUE;\r
315 }\r
316 /*-----------------------------------------------------------*/\r
317 \r
318 void vPortEndScheduler( void )\r
319 {\r
320         /* It is unlikely that the AVR port will get stopped.  If required simply\r
321         disable the tick interrupt here. */\r
322 }\r
323 /*-----------------------------------------------------------*/\r
324 \r
325 /*\r
326  * Manual context switch.  The first thing we do is save the registers so we\r
327  * can use a naked attribute.\r
328  */\r
329 void vPortYield( void ) __attribute__ ( ( naked ) );\r
330 void vPortYield( void )\r
331 {\r
332         portSAVE_CONTEXT();\r
333         vTaskSwitchContext();\r
334         portRESTORE_CONTEXT();\r
335 \r
336         asm volatile ( "ret" );\r
337 }\r
338 /*-----------------------------------------------------------*/\r
339 \r
340 /*\r
341  * Context switch function used by the tick.  This must be identical to \r
342  * vPortYield() from the call to vTaskSwitchContext() onwards.  The only\r
343  * difference from vPortYield() is the tick count is incremented as the\r
344  * call comes from the tick ISR.\r
345  */\r
346 void vPortYieldFromTick( void ) __attribute__ ( ( naked ) );\r
347 void vPortYieldFromTick( void )\r
348 {\r
349         portSAVE_CONTEXT();\r
350         if( xTaskIncrementTick() != pdFALSE )\r
351         {\r
352                 vTaskSwitchContext();\r
353         }\r
354         portRESTORE_CONTEXT();\r
355 \r
356         asm volatile ( "ret" );\r
357 }\r
358 /*-----------------------------------------------------------*/\r
359 \r
360 /*\r
361  * Setup timer 1 compare match A to generate a tick interrupt.\r
362  */\r
363 static void prvSetupTimerInterrupt( void )\r
364 {\r
365 uint32_t ulCompareMatch;\r
366 uint8_t ucHighByte, ucLowByte;\r
367 \r
368         /* Using 16bit timer 1 to generate the tick.  Correct fuses must be\r
369         selected for the configCPU_CLOCK_HZ clock. */\r
370 \r
371         ulCompareMatch = configCPU_CLOCK_HZ / configTICK_RATE_HZ;\r
372 \r
373         /* We only have 16 bits so have to scale to get our required tick rate. */\r
374         ulCompareMatch /= portCLOCK_PRESCALER;\r
375 \r
376         /* Adjust for correct value. */\r
377         ulCompareMatch -= ( uint32_t ) 1;\r
378 \r
379         /* Setup compare match value for compare match A.  Interrupts are disabled \r
380         before this is called so we need not worry here. */\r
381         ucLowByte = ( uint8_t ) ( ulCompareMatch & ( uint32_t ) 0xff );\r
382         ulCompareMatch >>= 8;\r
383         ucHighByte = ( uint8_t ) ( ulCompareMatch & ( uint32_t ) 0xff );\r
384         OCR1AH = ucHighByte;\r
385         OCR1AL = ucLowByte;\r
386 \r
387         /* Setup clock source and compare match behaviour. */\r
388         ucLowByte = portCLEAR_COUNTER_ON_MATCH | portPRESCALE_64;\r
389         TCCR1B = ucLowByte;\r
390 \r
391         /* Enable the interrupt - this is okay as interrupt are currently globally\r
392         disabled. */\r
393         ucLowByte = TIMSK;\r
394         ucLowByte |= portCOMPARE_MATCH_A_INTERRUPT_ENABLE;\r
395         TIMSK = ucLowByte;\r
396 }\r
397 /*-----------------------------------------------------------*/\r
398 \r
399 #if configUSE_PREEMPTION == 1\r
400 \r
401         /*\r
402          * Tick ISR for preemptive scheduler.  We can use a naked attribute as\r
403          * the context is saved at the start of vPortYieldFromTick().  The tick\r
404          * count is incremented after the context is saved.\r
405          */\r
406         void SIG_OUTPUT_COMPARE1A( void ) __attribute__ ( ( signal, naked ) );\r
407         void SIG_OUTPUT_COMPARE1A( void )\r
408         {\r
409                 vPortYieldFromTick();\r
410                 asm volatile ( "reti" );\r
411         }\r
412 #else\r
413 \r
414         /*\r
415          * Tick ISR for the cooperative scheduler.  All this does is increment the\r
416          * tick count.  We don't need to switch context, this can only be done by\r
417          * manual calls to taskYIELD();\r
418          */\r
419         void SIG_OUTPUT_COMPARE1A( void ) __attribute__ ( ( signal ) );\r
420         void SIG_OUTPUT_COMPARE1A( void )\r
421         {\r
422                 xTaskIncrementTick();\r
423         }\r
424 #endif\r
425 \r
426 \r
427         \r