Skip to content

Commit 77f939b

Browse files
committed
Merge pull request DuinOS#2 from niesteszeck/Mega2560
Added the ability to save RAMPZ and EIND registers (Atmega2560 has it)
2 parents c1dace9 + a7e5eb9 commit 77f939b

1 file changed

Lines changed: 124 additions & 8 deletions

File tree

‎arduino.DuinOS/DuinOS/port.c‎

Lines changed: 124 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,16 @@ extern volatile tskTCB * volatile pxCurrentTCB;
125125
* The interrupts will have been disabled during the call to portSAVE_CONTEXT()
126126
* so we need not worry about reading/writing to the stack pointer.
127127
*/
128-
128+
#if defined(__AVR_ATmega2560__)
129129
#define portSAVE_CONTEXT() \
130130
asm volatile ( "push r0 \n\t" \
131131
"in r0, __SREG__ \n\t" \
132132
"cli \n\t" \
133133
"push r0 \n\t" \
134+
"push r0 \n\t" \
135+
"in r0, 0x3b \n\t" \
136+
"push r0 \n\t" \
137+
"in r0, 0x3c \n\t" \
134138
"push r1 \n\t" \
135139
"clr r1 \n\t" \
136140
"push r2 \n\t" \
@@ -170,12 +174,106 @@ extern volatile tskTCB * volatile pxCurrentTCB;
170174
"in r0, 0x3e \n\t" \
171175
"st x+, r0 \n\t" \
172176
);
177+
#else
178+
#define portSAVE_CONTEXT() \
179+
asm volatile ( "push r0 \n\t" \
180+
"in r0, __SREG__ \n\t" \
181+
"cli \n\t" \
182+
"push r0 \n\t" \
183+
"push r1 \n\t" \
184+
"clr r1 \n\t" \
185+
"push r2 \n\t" \
186+
"push r3 \n\t" \
187+
"push r4 \n\t" \
188+
"push r5 \n\t" \
189+
"push r6 \n\t" \
190+
"push r7 \n\t" \
191+
"push r8 \n\t" \
192+
"push r9 \n\t" \
193+
"push r10 \n\t" \
194+
"push r11 \n\t" \
195+
"push r12 \n\t" \
196+
"push r13 \n\t" \
197+
"push r14 \n\t" \
198+
"push r15 \n\t" \
199+
"push r16 \n\t" \
200+
"push r17 \n\t" \
201+
"push r18 \n\t" \
202+
"push r19 \n\t" \
203+
"push r20 \n\t" \
204+
"push r21 \n\t" \
205+
"push r22 \n\t" \
206+
"push r23 \n\t" \
207+
"push r24 \n\t" \
208+
"push r25 \n\t" \
209+
"push r26 \n\t" \
210+
"push r27 \n\t" \
211+
"push r28 \n\t" \
212+
"push r29 \n\t" \
213+
"push r30 \n\t" \
214+
"push r31 \n\t" \
215+
"lds r26, pxCurrentTCB \n\t" \
216+
"lds r27, pxCurrentTCB + 1 \n\t" \
217+
"in r0, 0x3d \n\t" \
218+
"st x+, r0 \n\t" \
219+
"in r0, 0x3e \n\t" \
220+
"st x+, r0 \n\t" \
221+
);
222+
#endif
173223

174224
/*
175225
* Opposite to portSAVE_CONTEXT(). Interrupts will have been disabled during
176226
* the context save so we can write to the stack pointer.
177227
*/
178228

229+
#if defined(__AVR_ATmega2560__)
230+
#define portRESTORE_CONTEXT() \
231+
asm volatile ( "lds r26, pxCurrentTCB \n\t" \
232+
"lds r27, pxCurrentTCB + 1 \n\t" \
233+
"ld r28, x+ \n\t" \
234+
"out __SP_L__, r28 \n\t" \
235+
"ld r29, x+ \n\t" \
236+
"out __SP_H__, r29 \n\t" \
237+
"pop r31 \n\t" \
238+
"pop r30 \n\t" \
239+
"pop r29 \n\t" \
240+
"pop r28 \n\t" \
241+
"pop r27 \n\t" \
242+
"pop r26 \n\t" \
243+
"pop r25 \n\t" \
244+
"pop r24 \n\t" \
245+
"pop r23 \n\t" \
246+
"pop r22 \n\t" \
247+
"pop r21 \n\t" \
248+
"pop r20 \n\t" \
249+
"pop r19 \n\t" \
250+
"pop r18 \n\t" \
251+
"pop r17 \n\t" \
252+
"pop r16 \n\t" \
253+
"pop r15 \n\t" \
254+
"pop r14 \n\t" \
255+
"pop r13 \n\t" \
256+
"pop r12 \n\t" \
257+
"pop r11 \n\t" \
258+
"pop r10 \n\t" \
259+
"pop r9 \n\t" \
260+
"pop r8 \n\t" \
261+
"pop r7 \n\t" \
262+
"pop r6 \n\t" \
263+
"pop r5 \n\t" \
264+
"pop r4 \n\t" \
265+
"pop r3 \n\t" \
266+
"pop r2 \n\t" \
267+
"pop r1 \n\t" \
268+
"pop r0 \n\t" \
269+
"out 0x3c, r0 \n\t" \
270+
"pop r0 \n\t" \
271+
"out 0x3b, r0 \n\t" \
272+
"pop r0 \n\t" \
273+
"out __SREG__, r0 \n\t" \
274+
"pop r0 \n\t" \
275+
);
276+
#else
179277
#define portRESTORE_CONTEXT() \
180278
asm volatile ( "lds r26, pxCurrentTCB \n\t" \
181279
"lds r27, pxCurrentTCB + 1 \n\t" \
@@ -215,9 +313,14 @@ extern volatile tskTCB * volatile pxCurrentTCB;
215313
"pop r2 \n\t" \
216314
"pop r1 \n\t" \
217315
"pop r0 \n\t" \
316+
"out 0x3c, r0 \n\t" \
317+
"pop r0 \n\t" \
318+
"out 0x3b, r0 \n\t" \
319+
"pop r0 \n\t" \
218320
"out __SREG__, r0 \n\t" \
219321
"pop r0 \n\t" \
220322
);
323+
#endif
221324

222325
/*-----------------------------------------------------------*/
223326

@@ -232,13 +335,14 @@ static void prvSetupTimerInterrupt( void );
232335
*/
233336
portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )
234337
{
235-
/* ATmega2560 DuinOS port by SkyWodd */
236-
#if defined(__AVR_ATmega2560__)
237-
unsigned portLONG usAddress; // ATMega2560 have 22bit Program Counter register
238-
#else
239-
unsigned portSHORT usAddress; // over ATmega have 16bit Program Counter register
240-
#endif
241-
338+
#if defined(__AVR_ATmega2560__)
339+
/* ATmega2560 DuinOS port by SkyWodd
340+
* Corrected by NiesteSzeck*/
341+
unsigned portLONG usAddress; // ATMega2560 have 17bit Program Counter register
342+
#else
343+
unsigned portSHORT usAddress; // over ATmega have 16bit Program Counter register
344+
#endif
345+
242346

243347
/* Place a few bytes of known values on the bottom of the stack.
244348
This is just useful for debugging. */
@@ -292,6 +396,18 @@ portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE
292396
*pxTopOfStack = portFLAGS_INT_ENABLED;
293397
pxTopOfStack--;
294398

399+
#if defined(__AVR_ATmega2560__)
400+
401+
/* The Atmega2560 has two more register that we are saving
402+
* The EIND and AMPZ and wea re going to initialize
403+
* this registers to 0 (default initial values)
404+
*/
405+
*pxTopOfStack = ( portSTACK_TYPE ) 0x00; /* EIND */
406+
pxTopOfStack--;
407+
*pxTopOfStack = ( portSTACK_TYPE ) 0x00; /* RAMPZ */
408+
pxTopOfStack--;
409+
410+
#endif
295411

296412
/* Now the remaining registers. The compiler expects R1 to be 0. */
297413
*pxTopOfStack = ( portSTACK_TYPE ) 0x00; /* R1 */

0 commit comments

Comments
 (0)