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