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