Searching \ for '[PIC] Copying the stack with PIC18?' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: www.piclist.com/techref/microchip/devices.htm?key=pic
Search entire site for: 'Copying the stack with PIC18?'.

Exact match. Not showing close matches.
PICList Thread
'[PIC] Copying the stack with PIC18?'
2006\12\20@195433 by Harold Hallikainen

face
flavicon
face
Is there some way to copy the stack to an array, then restore it later on
a PIC18? I see that we have access to TOS, the stack pointer, and have
push and pop instructions. I'd kinda expect pop to then place the contents
of the current stack location in a register, then decrement the stack
pointer. Instead, the POP discards the data and decrements the stack
pointer. I'd expect PUSH to increment the stack pointer, then put the
contents of a register on the stack. It sort of does that, but the
register it puts on the stack is the PC. Is there any way to put user data
on the stack and recover it? I'd like to have an array of arrays, each
holding a set of stack contents. I could then multitask by copying the
contents of the stack to an array, then loading the stack with the stack
contents for another task, then return. Everything would be as it was when
we left that task.

I almost thought I could do this with the 18 series, but as I look closer,
I'm thinking not. Is there a way to copy data on and off the stack?

THANKS!

Harold



--
FCC Rules Updated Daily at http://www.hallikainen.com - Advertising
opportunities available!

2006\12\20@203857 by peter green

flavicon
face

> I almost thought I could do this with the 18 series, but as I look closer,
> I'm thinking not. Is there a way to copy data on and off the stack?
looking at the 452 data sheet under memory organisation we see a section labeled return address stack, this contains the following.

"The stack space is not part of either program or data
space. The stack pointer is readable and writable, and
the address on the top of the stack is readable and writable
through SFR registers. Data can also be pushed
to, or popped from, the stack using the top-of-stack
SFRs. Status bits indicate if the stack pointer is at, or
beyond the 31 levels provided."

so now i head off to the special function registers section under data memory, this directs us to pages 37 and 38 where we find a description of the special function registers that relate to the stack.

there seems to be enough information here to write code to save/restore the stack.

The code to save the stack would be something like

short long [32] savedstack;
//since the first entry in the stack is in slot 1
//we may as well use slot zero in the array for the
//stack pointer
savedstack[0] = STKPTR&0x1f;
while ((STKPTR&0x1f) != 0) {
 savedstack[STKPTR&0x1f] = TOS;
 _asm POP _endasm
}

and the code to restore it would be something like

STKPTR=0
while ((STKPTR&0x1f) != savedstack[0]) {
 _asm PUSH _endasm
 TOS = savedstack[STKPTR&0x1f];
}



2006\12\20@223349 by Harold Hallikainen

face
flavicon
face

{Quote hidden}

I think this would work if push and pop did what I think they should. But,
as I read the manual, push pushes the current PC (or maybe PC+2) onto the
stack, not the TOS registers. Further, it appears POP does not put the
contents in the TOS register, but throws them out. Actually, it appears
TOS would be updated, so we could recover data off the stack from there.
But, how do we put data (other than the PC) on the stack?

Harold


--
FCC Rules Updated Daily at http://www.hallikainen.com - Advertising
opportunities available!

2006\12\21@013058 by Rastervec

picon face
The data sheet states:
Only the top of the return address stack (TOS) is readable and writable. A
set of three registers,
TOSU:TOSH:TOSL, hold the contents of the stack location pointed to by the
STKPTR register

I have implemented task switching by saving reg values in TOSU: TOSH: TOSL
then incrementing the STKPTR (equivilant to PUSH) and decrementing it (POP).
You can also save the value of the STKPTR using reg manipulation
instructions such as
movff STKPTR,RegX
and restore similarly
movff RegX,STKPTR

I hope this helps,
T. Uzri

{Original Message removed}

2006\12\21@032749 by Marcel van Lieshout

flavicon
face
Take a peek at the pic18 ports of FreeRTOS at http://www.freertos.org

Harold Hallikainen wrote:
{Quote hidden}

2006\12\23@163040 by peter green

flavicon
face


{Quote hidden}

my understanding (and i could be wrong here, i haven't tested it) of the data sheet was that TOS allowed you to directly read and write the location at the top of the stack. so my code does a PUSH and then overwrites the newly pushed program counter value with what i wan't to restore.


2006\12\23@165435 by Jan-Erik Söderholm

face picon face
>> But, how do we put data (other than the PC) on the stack?

Is *that* stack realy ment for that ?

Jan-Erik.

2006\12\23@205409 by Harold Hallikainen

face
flavicon
face

>>> But, how do we put data (other than the PC) on the stack?
>
> Is *that* stack realy ment for that ?
>
> Jan-Erik.

This week I've been messing with this some more. The datasheet talks about
a push putting the PC on the stack, but it really is putting the TOS data
on the stack (or incrementing the stack pointer to the next empty location
above TOS). So, it DOES allow arbitrary data to be put on the stack.

So, what's it good for? I've pretty much got an extremely simple
cooperative multitasking system running. Ive done this a lot before with
state machines, but it's a pain when you've got some depth of function
calls and you may want to go to the next task at any depth. So, what I'm
doing is keeping an array of stack copies. A typical task looks like this:

void Task0(void){
 InitTask(0);           // copies current stack to array, but returns here
 while(1){
   DoSomething();
   DoSomethingElse();
   NextRask();          // saves current stack, loads stack for next task
 }                      // then returns
}

The NextTask() is typically in a loop where we're waiting for input. It
can be several subroutines or function calls down. Local variables,
however, have to be static. Automatic variables get pretty confused when
you go through a task switch.

It's pretty much working, but I want to do some more testing. I'll post it
when I'm done.

Harold



--
FCC Rules Updated Daily at http://www.hallikainen.com - Advertising
opportunities available!

2006\12\23@223439 by James Newtons Massmind

face picon face
> So, what's it good for? I've pretty much got an extremely
> simple cooperative multitasking system running.

<SNIP>

> It's pretty much working, but I want to do some more testing.
> I'll post it when I'm done.
>
> Harold


Looking forward to that!

---
James.


2006\12\24@112718 by olin piclist

face picon face
Harold Hallikainen wrote:
> The NextTask() is typically in a loop where we're waiting for input.
> It can be several subroutines or function calls down. Local variables,
> however, have to be static. Automatic variables get pretty confused
> when you go through a task switch.
>
> It's pretty much working, but I want to do some more testing. I'll
> post it when I'm done.

Since there seems to be interest, I've added my version of this to the PIC
development tools release at http://www.embedinc.com/pic/dload.htm.  After
installation look at source\pic\qqq_task.aspic.

This version can be configured for normal and C18 FSR usage, and some
instructions are eliminated when built for PICs with 32Kbyte code space or
less.


********************************************************************
Embed Inc, Littleton Massachusetts, http://www.embedinc.com/products
(978) 742-9014.  Gold level PIC consultants since 2000.

More... (looser matching)
- Last day of these posts
- In 2006 , 2007 only
- Today
- New search...