Reputation: 195
Wonder if anybody knows the correct C syntax to store context (in my case just one int) before a PIC 24F sleeps and then recover it when woken.
The example code (Microchip app note), which is supposed to store context in DSGPRO, set the DSEN bit and call sleep is -
asm("MOV #0x8000, w2");
asm("MOV w2, DSCON");
asm("MOV w2, DSCON");
asm("PWRAV #0")
My (probably poor) equivalent in C (to store the int in led_step) is
int *a = (int*) 0x8000;
*a = led_step;
IFS1bits.INT2IF = 0; //clear interrupt flag.
INTCON2bits.INT2EP = 1; //Interrupt iNT2E is on the falling edge
IPC7bits.INT2IP = 7; //set highest interrupt priority to INT2
IEC1bits.INT2IE = 1; //Enable INT2IE interruption
CTMUCONbits.CTMUEN = 0; //Disable CTMU module
Sleep(); // sleep();
But this just results in an output toggling whilst in sleep. The sleep / wake functions do work, there's something wrong with variable storage / recovery.
Upvotes: 1
Views: 103
Reputation: 4288
You had to put the assembly part directly into your c-function by using inline assembly.
There is an example here for the xc16 compiler:
void main void
{
//do C-stuff
// fall into deep sleep
// Case 1: simplest delay scenario
//
asm("bset DSCON, #15");
asm("nop");
asm("nop");
asm("nop");
asm("pwrsav #0"
}
Upvotes: 0
Reputation: 195
The eventual issue was that the next command after sleep is never executed, despite what all the docs say. The wake caused random jumps to various parts of the code and I had to add a service routine.
Upvotes: 0
Reputation: 93564
The Microchip code is inline assembly embeddable in C code. It is likely that they have used it exactly because it cannot be done in C code - which will be the case when accessing non memory mapped registers.
If you are using a compiler other than XC16, your compiler's inline assembly syntax may differ, but will certainly support it one way or another. I think XC16 supports GCC style in-line assembly.
Si the answer is simply to use in-line assembly, either verbatim as provided or adapted for your specific compiler as necessary.
Upvotes: 0