]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/IAR/ATMega323/port.c
06e4b7c1f6e9526c26c01791680f03cb10d80951
[freertos] / FreeRTOS / Source / portable / IAR / ATMega323 / port.c
1 /*\r
2     FreeRTOS V9.0.1 - Copyright (C) 2017 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     This file is part of the FreeRTOS distribution.\r
8 \r
9     FreeRTOS is free software; you can redistribute it and/or modify it under\r
10     the terms of the GNU General Public License (version 2) as published by the\r
11     Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.\r
12 \r
13     ***************************************************************************\r
14     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
15     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
16     >>!   obliged to provide the source code for proprietary components     !<<\r
17     >>!   outside of the FreeRTOS kernel.                                   !<<\r
18     ***************************************************************************\r
19 \r
20     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
21     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
22     FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
23     link: http://www.freertos.org/a00114.html\r
24 \r
25     ***************************************************************************\r
26      *                                                                       *\r
27      *    FreeRTOS provides completely free yet professionally developed,    *\r
28      *    robust, strictly quality controlled, supported, and cross          *\r
29      *    platform software that is more than just the market leader, it     *\r
30      *    is the industry's de facto standard.                               *\r
31      *                                                                       *\r
32      *    Help yourself get started quickly while simultaneously helping     *\r
33      *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
34      *    tutorial book, reference manual, or both:                          *\r
35      *    http://www.FreeRTOS.org/Documentation                              *\r
36      *                                                                       *\r
37     ***************************************************************************\r
38 \r
39     http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading\r
40     the FAQ page "My application does not run, what could be wrong?".  Have you\r
41     defined configASSERT()?\r
42 \r
43     http://www.FreeRTOS.org/support - In return for receiving this top quality\r
44     embedded software for free we request you assist our global community by\r
45     participating in the support forum.\r
46 \r
47     http://www.FreeRTOS.org/training - Investing in training allows your team to\r
48     be as productive as possible as early as possible.  Now you can receive\r
49     FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers\r
50     Ltd, and the world's leading authority on the world's leading RTOS.\r
51 \r
52     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
53     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
54     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
55 \r
56     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
57     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
58 \r
59     http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High\r
60     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
61     licenses offer ticketed support, indemnification and commercial middleware.\r
62 \r
63     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
64     engineered and independently SIL3 certified version for use in safety and\r
65     mission critical applications that require provable dependability.\r
66 \r
67     1 tab == 4 spaces!\r
68 */\r
69 \r
70 #include <stdlib.h>\r
71 \r
72 #include "FreeRTOS.h"\r
73 #include "task.h"\r
74 \r
75 /*-----------------------------------------------------------\r
76  * Implementation of functions defined in portable.h for the AVR/IAR port.\r
77  *----------------------------------------------------------*/\r
78 \r
79 /* Start tasks with interrupts enables. */\r
80 #define portFLAGS_INT_ENABLED                                   ( ( StackType_t ) 0x80 )\r
81 \r
82 /* Hardware constants for timer 1. */\r
83 #define portCLEAR_COUNTER_ON_MATCH                              ( ( uint8_t ) 0x08 )\r
84 #define portPRESCALE_64                                                 ( ( uint8_t ) 0x03 )\r
85 #define portCLOCK_PRESCALER                                             ( ( uint32_t ) 64 )\r
86 #define portCOMPARE_MATCH_A_INTERRUPT_ENABLE    ( ( uint8_t ) 0x10 )\r
87 \r
88 /* The number of bytes used on the hardware stack by the task start address. */\r
89 #define portBYTES_USED_BY_RETURN_ADDRESS                ( 2 )\r
90 /*-----------------------------------------------------------*/\r
91 \r
92 /* Stores the critical section nesting.  This must not be initialised to 0.\r
93 It will be initialised when a task starts. */\r
94 #define portNO_CRITICAL_NESTING                                 ( ( UBaseType_t ) 0 )\r
95 UBaseType_t uxCriticalNesting = 0x50;\r
96 \r
97 \r
98 /*\r
99  * Perform hardware setup to enable ticks from timer 1, compare match A.\r
100  */\r
101 static void prvSetupTimerInterrupt( void );\r
102 \r
103 /*\r
104  * The IAR compiler does not have full support for inline assembler, so\r
105  * these are defined in the portmacro assembler file.\r
106  */\r
107 extern void vPortYieldFromTick( void );\r
108 extern void vPortStart( void );\r
109 \r
110 /*-----------------------------------------------------------*/\r
111 \r
112 /*\r
113  * See header file for description.\r
114  */\r
115 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )\r
116 {\r
117 uint16_t usAddress;\r
118 StackType_t *pxTopOfHardwareStack;\r
119 \r
120         /* Place a few bytes of known values on the bottom of the stack.\r
121         This is just useful for debugging. */\r
122 \r
123         *pxTopOfStack = 0x11;\r
124         pxTopOfStack--;\r
125         *pxTopOfStack = 0x22;\r
126         pxTopOfStack--;\r
127         *pxTopOfStack = 0x33;\r
128         pxTopOfStack--;\r
129 \r
130         /* Remember where the top of the hardware stack is - this is required\r
131         below. */\r
132         pxTopOfHardwareStack = pxTopOfStack;\r
133 \r
134 \r
135         /* Simulate how the stack would look after a call to vPortYield(). */\r
136 \r
137         /*lint -e950 -e611 -e923 Lint doesn't like this much - but nothing I can do about it. */\r
138 \r
139 \r
140 \r
141         /* The IAR compiler requires two stacks per task.  First there is the\r
142         hardware call stack which uses the AVR stack pointer.  Second there is the\r
143         software stack (local variables, parameter passing, etc.) which uses the\r
144         AVR Y register.\r
145 \r
146         This function places both stacks within the memory block passed in as the\r
147         first parameter.  The hardware stack is placed at the bottom of the memory\r
148         block.  A gap is then left for the hardware stack to grow.  Next the software\r
149         stack is placed.  The amount of space between the software and hardware\r
150         stacks is defined by configCALL_STACK_SIZE.\r
151 \r
152 \r
153 \r
154         The first part of the stack is the hardware stack.  Place the start\r
155         address of the task on the hardware stack. */\r
156         usAddress = ( uint16_t ) pxCode;\r
157         *pxTopOfStack = ( StackType_t ) ( usAddress & ( uint16_t ) 0x00ff );\r
158         pxTopOfStack--;\r
159 \r
160         usAddress >>= 8;\r
161         *pxTopOfStack = ( StackType_t ) ( usAddress & ( uint16_t ) 0x00ff );\r
162         pxTopOfStack--;\r
163 \r
164 \r
165         /* Leave enough space for the hardware stack before starting the software\r
166         stack.  The '- 2' is because we have already used two spaces for the\r
167         address of the start of the task. */\r
168         pxTopOfStack -= ( configCALL_STACK_SIZE - 2 );\r
169 \r
170 \r
171 \r
172         /* Next simulate the stack as if after a call to portSAVE_CONTEXT().\r
173         portSAVE_CONTEXT places the flags on the stack immediately after r0\r
174         to ensure the interrupts get disabled as soon as possible, and so ensuring\r
175         the stack use is minimal should a context switch interrupt occur. */\r
176         *pxTopOfStack = ( StackType_t ) 0x00;   /* R0 */\r
177         pxTopOfStack--;\r
178         *pxTopOfStack = portFLAGS_INT_ENABLED;\r
179         pxTopOfStack--;\r
180 \r
181         /* Next place the address of the hardware stack.  This is required so\r
182         the AVR stack pointer can be restored to point to the hardware stack. */\r
183         pxTopOfHardwareStack -= portBYTES_USED_BY_RETURN_ADDRESS;\r
184         usAddress = ( uint16_t ) pxTopOfHardwareStack;\r
185 \r
186         /* SPL */\r
187         *pxTopOfStack = ( StackType_t ) ( usAddress & ( uint16_t ) 0x00ff );\r
188         pxTopOfStack--;\r
189 \r
190         /* SPH */\r
191         usAddress >>= 8;\r
192         *pxTopOfStack = ( StackType_t ) ( usAddress & ( uint16_t ) 0x00ff );\r
193         pxTopOfStack--;\r
194 \r
195 \r
196 \r
197 \r
198         /* Now the remaining registers. */\r
199         *pxTopOfStack = ( StackType_t ) 0x01;   /* R1 */\r
200         pxTopOfStack--;\r
201         *pxTopOfStack = ( StackType_t ) 0x02;   /* R2 */\r
202         pxTopOfStack--;\r
203         *pxTopOfStack = ( StackType_t ) 0x03;   /* R3 */\r
204         pxTopOfStack--;\r
205         *pxTopOfStack = ( StackType_t ) 0x04;   /* R4 */\r
206         pxTopOfStack--;\r
207         *pxTopOfStack = ( StackType_t ) 0x05;   /* R5 */\r
208         pxTopOfStack--;\r
209         *pxTopOfStack = ( StackType_t ) 0x06;   /* R6 */\r
210         pxTopOfStack--;\r
211         *pxTopOfStack = ( StackType_t ) 0x07;   /* R7 */\r
212         pxTopOfStack--;\r
213         *pxTopOfStack = ( StackType_t ) 0x08;   /* R8 */\r
214         pxTopOfStack--;\r
215         *pxTopOfStack = ( StackType_t ) 0x09;   /* R9 */\r
216         pxTopOfStack--;\r
217         *pxTopOfStack = ( StackType_t ) 0x10;   /* R10 */\r
218         pxTopOfStack--;\r
219         *pxTopOfStack = ( StackType_t ) 0x11;   /* R11 */\r
220         pxTopOfStack--;\r
221         *pxTopOfStack = ( StackType_t ) 0x12;   /* R12 */\r
222         pxTopOfStack--;\r
223         *pxTopOfStack = ( StackType_t ) 0x13;   /* R13 */\r
224         pxTopOfStack--;\r
225         *pxTopOfStack = ( StackType_t ) 0x14;   /* R14 */\r
226         pxTopOfStack--;\r
227         *pxTopOfStack = ( StackType_t ) 0x15;   /* R15 */\r
228         pxTopOfStack--;\r
229 \r
230         /* Place the parameter on the stack in the expected location. */\r
231         usAddress = ( uint16_t ) pvParameters;\r
232         *pxTopOfStack = ( StackType_t ) ( usAddress & ( uint16_t ) 0x00ff );\r
233         pxTopOfStack--;\r
234 \r
235         usAddress >>= 8;\r
236         *pxTopOfStack = ( StackType_t ) ( usAddress & ( uint16_t ) 0x00ff );\r
237         pxTopOfStack--;\r
238 \r
239         *pxTopOfStack = ( StackType_t ) 0x18;   /* R18 */\r
240         pxTopOfStack--;\r
241         *pxTopOfStack = ( StackType_t ) 0x19;   /* R19 */\r
242         pxTopOfStack--;\r
243         *pxTopOfStack = ( StackType_t ) 0x20;   /* R20 */\r
244         pxTopOfStack--;\r
245         *pxTopOfStack = ( StackType_t ) 0x21;   /* R21 */\r
246         pxTopOfStack--;\r
247         *pxTopOfStack = ( StackType_t ) 0x22;   /* R22 */\r
248         pxTopOfStack--;\r
249         *pxTopOfStack = ( StackType_t ) 0x23;   /* R23 */\r
250         pxTopOfStack--;\r
251         *pxTopOfStack = ( StackType_t ) 0x24;   /* R24 */\r
252         pxTopOfStack--;\r
253         *pxTopOfStack = ( StackType_t ) 0x25;   /* R25 */\r
254         pxTopOfStack--;\r
255         *pxTopOfStack = ( StackType_t ) 0x26;   /* R26 X */\r
256         pxTopOfStack--;\r
257         *pxTopOfStack = ( StackType_t ) 0x27;   /* R27 */\r
258         pxTopOfStack--;\r
259 \r
260         /* The Y register is not stored as it is used as the software stack and\r
261         gets saved into the task control block. */\r
262 \r
263         *pxTopOfStack = ( StackType_t ) 0x30;   /* R30 Z */\r
264         pxTopOfStack--;\r
265         *pxTopOfStack = ( StackType_t ) 0x031;  /* R31 */\r
266 \r
267         pxTopOfStack--;\r
268         *pxTopOfStack = portNO_CRITICAL_NESTING;        /* Critical nesting is zero when the task starts. */\r
269 \r
270         /*lint +e950 +e611 +e923 */\r
271 \r
272         return pxTopOfStack;\r
273 }\r
274 /*-----------------------------------------------------------*/\r
275 \r
276 BaseType_t xPortStartScheduler( void )\r
277 {\r
278         /* Setup the hardware to generate the tick. */\r
279         prvSetupTimerInterrupt();\r
280 \r
281         /* Restore the context of the first task that is going to run.\r
282         Normally we would just call portRESTORE_CONTEXT() here, but as the IAR\r
283         compiler does not fully support inline assembler we have to make a call.*/\r
284         vPortStart();\r
285 \r
286         /* Should not get here! */\r
287         return pdTRUE;\r
288 }\r
289 /*-----------------------------------------------------------*/\r
290 \r
291 void vPortEndScheduler( void )\r
292 {\r
293         /* It is unlikely that the AVR port will get stopped.  If required simply\r
294         disable the tick interrupt here. */\r
295 }\r
296 /*-----------------------------------------------------------*/\r
297 \r
298 /*\r
299  * Setup timer 1 compare match A to generate a tick interrupt.\r
300  */\r
301 static void prvSetupTimerInterrupt( void )\r
302 {\r
303 uint32_t ulCompareMatch;\r
304 uint8_t ucHighByte, ucLowByte;\r
305 \r
306         /* Using 16bit timer 1 to generate the tick.  Correct fuses must be\r
307         selected for the configCPU_CLOCK_HZ clock. */\r
308 \r
309         ulCompareMatch = configCPU_CLOCK_HZ / configTICK_RATE_HZ;\r
310 \r
311         /* We only have 16 bits so have to scale to get our required tick rate. */\r
312         ulCompareMatch /= portCLOCK_PRESCALER;\r
313 \r
314         /* Adjust for correct value. */\r
315         ulCompareMatch -= ( uint32_t ) 1;\r
316 \r
317         /* Setup compare match value for compare match A.  Interrupts are disabled\r
318         before this is called so we need not worry here. */\r
319         ucLowByte = ( uint8_t ) ( ulCompareMatch & ( uint32_t ) 0xff );\r
320         ulCompareMatch >>= 8;\r
321         ucHighByte = ( uint8_t ) ( ulCompareMatch & ( uint32_t ) 0xff );\r
322         OCR1AH = ucHighByte;\r
323         OCR1AL = ucLowByte;\r
324 \r
325         /* Setup clock source and compare match behaviour. */\r
326         ucLowByte = portCLEAR_COUNTER_ON_MATCH | portPRESCALE_64;\r
327         TCCR1B = ucLowByte;\r
328 \r
329         /* Enable the interrupt - this is okay as interrupt are currently globally\r
330         disabled. */\r
331         TIMSK |= portCOMPARE_MATCH_A_INTERRUPT_ENABLE;\r
332 }\r
333 /*-----------------------------------------------------------*/\r
334 \r
335 #if configUSE_PREEMPTION == 1\r
336 \r
337         /*\r
338          * Tick ISR for preemptive scheduler.  We can use a __task attribute as\r
339          * the context is saved at the start of vPortYieldFromTick().  The tick\r
340          * count is incremented after the context is saved.\r
341          */\r
342         __task void SIG_OUTPUT_COMPARE1A( void )\r
343         {\r
344                 vPortYieldFromTick();\r
345                 asm( "reti" );\r
346         }\r
347 \r
348 #else\r
349 \r
350         /*\r
351          * Tick ISR for the cooperative scheduler.  All this does is increment the\r
352          * tick count.  We don't need to switch context, this can only be done by\r
353          * manual calls to taskYIELD();\r
354          *\r
355          * THE INTERRUPT VECTOR IS POPULATED IN portmacro.s90.  DO NOT INSTALL\r
356          * IT HERE USING THE USUAL PRAGMA.\r
357          */\r
358         __interrupt void SIG_OUTPUT_COMPARE1A( void )\r
359         {\r
360                 xTaskIncrementTick();\r
361         }\r
362 #endif\r
363 /*-----------------------------------------------------------*/\r
364 \r
365 void vPortEnterCritical( void )\r
366 {\r
367         portDISABLE_INTERRUPTS();\r
368         uxCriticalNesting++;\r
369 }\r
370 /*-----------------------------------------------------------*/\r
371 \r
372 void vPortExitCritical( void )\r
373 {\r
374         uxCriticalNesting--;\r
375         if( uxCriticalNesting == portNO_CRITICAL_NESTING )\r
376         {\r
377                 portENABLE_INTERRUPTS();\r
378         }\r
379 }\r
380 \r
381 \r