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