• AVR Freaks

PIC24-Declaring a new Stack in the Heap

Author
BenMichaud21
Junior Member
  • Total Posts : 101
  • Reward points : 0
  • Joined: 2008/11/21 19:27:57
  • Status: offline
2009/03/16 11:15:37 (permalink)
0

PIC24-Declaring a new Stack in the Heap

I'm trying to make room for a new stack in the heap on the PIC24FJ128GA101.  I have a declared heap size of 4000 bytes. All that I am trying to accomplish is just get the PIC to start executing the code in test(), by putting the address of test onto the newly initialized stack in the heap, then incrementing the stack pointer, then placeing the the address of the stack pointer into W15 and returning(hopefully popping the address of test into the PC).  This does not work, and I keep on getting the following error message:
 
CORE-E0004: Trap due to unimplemented FLASH memory access, occurred from instruction at 0x00045c
 
This is the code that I'm using:
 
#include <p24FJ128GA010.h>
#include <stdlib.h>
#include <stdio.h>
 
#define stackType unsigned int

void moveStackPTR(void (*t)(void));
 
stackType *stackPtr;
 
void tryMovingStack(void (*taskCode)(void), stackType stacksSize)
{
     stackPtr = (stackType)malloc(sizeof(stackType) * stacksSize);
     *stackPtr = taskCode;
     stackPtr++;
     moveStackPTR(stackPtr);
}
 
void test(void)
{
    int a = 40;
    int b = 20;
    AD1PCFG = 0xffff;
    TRISA = 0b0000000000000000;
    PORTA = 0b0000000000000000;
    for(;;)
    {
         PORTA = 0b1111111111111111;
    }
}

void main(void)
{
 stackType size = 50;
 tryMovingStack(&test, size);
}
 
Here is the assembly file that is referenced by moveStackPTR() in tryMovingStack();
 
.include "p24Fxxxx.inc"
.text
.global _moveStackPTR
_moveStackPTR:  
  
        mov  W0, W15
  return
        .end
 
I have a feeling it might have something to do with the PSVPAG or the TBLPAG registers, but I have no clue how to dismiss or verify that thought.  Thanks for your time.
 
Ben
#1

2 Replies Related Threads

    BenMichaud21
    Junior Member
    • Total Posts : 101
    • Reward points : 0
    • Joined: 2008/11/21 19:27:57
    • Status: offline
    RE: PIC24-Declaring a new Stack in the Heap 2009/03/16 13:41:23 (permalink)
    0
    Update:
     
    I put another stackPtr++; in after the previous one.  Thinking that the PC was looking for a 32 bit number, and I was only giving it 16 bits, plus whatever was before that in memory.  This seemed to fix the probelm, sort of.  The MPLABS Simulator says that it will work, sometimes.  Meaning sometimes the simulator shows the program going into the test routine, and sometimes it doesn't, even if I dont change anything.  I tested to program out on the PIC24, and the lights never turned on, so I'm assuming it never went into the test routine.  The simulator seems a little unpredictable at times, which is making things more difficult.  Any suggestions would be greatly appreciated.  Thanks for your time.
     
    Ben
    #2
    zardoz1
    Super Member
    • Total Posts : 1852
    • Reward points : 0
    • Joined: 2005/07/09 08:03:28
    • Location: 's-Hertogenbosch, The Netherlands
    • Status: offline
    RE: PIC24-Declaring a new Stack in the Heap 2009/03/16 14:23:07 (permalink)
    0
    I have not looked in detail what you are trying to do in your code but one thing is wrong for sure.
     
    The new stack is filled with the address of the function you want to activate. For PIC24 this is a 16 bit address. Function pointers for PIC24 are 16 which either is the correct address of in case the function is at an address not reachable with 16 bit, it is the address of a location containing a goto instruction to the actual address which is 24 bit.
     
    So here you are with a new stack containing a 16 bit address and next your assembly part contains a return which assumes a 24 bit address contained in two words on the stack.
     
    From what you are doing I see you are making the first steps in creating an RTOS. When doing so my advice is to first make a very detailed study of the hardware and make sure you understand details like the above. Only then start writing code.


    AVIX
    the PIC32 & dsPIC/PIC24 RTOS with:
    - Zero Latency Interrupts
    - The best performance!
    - Integrated Power Management
    Download here: http://www.avix-rt.com/
    #3
    Jump to:
    © 2019 APG vNext Commercial Version 4.5