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