Here is a good starting point, for someone who needs the same sort of thing.
I finished trying this pass through that can sleep.
Over the air updates are not possible. at this point.
But could easily be added under another ISR.
Then using Over The Air Commands from Zigbee, an IO line could be changed. And then that ISR could call back into the Bootloader for an update.
And one added thing. you must set the keyboard interrupt vector to the new Interrupt routine. (ISR)
The rest of the code is in the previous post.
#define IO_DIO5_ASSOCIATE_INTERRUPT2 KBI2PE_KBIPE4_MASK
#define IO_DIO5_ASSOCIATE_RISING_EDGE() KBI2ES_KBEDG4=1
#define IO_DIO5_ASSOCIATE_FALLING_EDGE() KBI2ES_KBEDG4=0
#define IO_DIO5_ASSOCIATE_TOGGLE_EDGE() KBI2ES_KBEDG4=!KBI2ES_KBEDG4
#define IO_DIO9_ADC9_ON_SLEEP_INTERRUPT2 KBI1PE_KBIPE1_MASK
#define IO_DIO9_ADC9_ON_SLEEP_RISING_EDGE() KBI1ES_KBEDG1=1
#define IO_DIO9_ADC9_ON_SLEEP_FALLING_EDGE() KBI1ES_KBEDG1=0
#define IO_DIO9_ADC9_ON_SLEEP_TOGGLE_EDGE() KBI1ES_KBEDG1=!KBI1ES_KBEDG1
#define KB_INTERRUPT2_FLAG() KBI2SC_KBF
#define KB_INTERRUPT2_CLEAR() KBI2SC_KBACK=1
#define KB_INTERRUPT2_DETECT_EDGE_AND_LEVEL() KBI2SC_KBIMOD=1
#define KB_INTERRUPT2_DETECT_EDGE() KBI2SC_KBIMOD=0
#define KB_INTERRUPT2_ENABLE(pins) KBI2PE |= pins;
KBI2SC_KBIE=1
#define KB_INTERRUPT2_DISABLE_ALL() KBI2PE=0;
KBI2SC_KBIE=0
#define KB_INTERRUPT2_DISABLE(pins) KBI2PE &= ~pins;
if(!KBI2PE)
{
KBI2SC_KBIE=0;
}
#define KB_INTERRUPT1_FLAG() KBI1SC_KBF
#define KB_INTERRUPT1_CLEAR() KBI1SC_KBACK=1
#define KB_INTERRUPT1_DISABLE() KBI1SC_KBIE=0
#define KB_INTERRUPT1_DETECT_EDGE_AND_LEVEL() KBI1SC_KBIMOD=1
#define KB_INTERRUPT1_DETECT_EDGE() KBI1SC_KBIMOD=0
#define KB_INTERRUPT1_ENABLE_PINS(pins) KBI1PE |= pins;
KBI1SC_KBIE=1
#define KB_INTERRUPT1_ENABLE() KBI1SC_KBIE=1
#define STOP_MODE() __asm STOP;
#define WAIT_MODE() __asm WAIT;
void initGPIO(void)
{
PTADD = 0;//set all as inputs
PTBDD = 0;//set all as inputs
PTCDD = 0;//set all as inputs
PTDDD = 0;//set all as inputs
PTEPE = 0x70;//turn on Hardware detect pull-ups
PTEDD = 0;//set all as inputs
portE = PTED & HARDWARE_MASK_PORTE;//read hardware detect lines
PTAPE = 1<<6 | 1<<5 | 1<<4;//disable pullups except for no connects
PTBPE = 0xC1;//3<<6+1;//disable pullups except for no connects and DIN
PTCPE = 1<<6 | 7<<2;//disable pullups except for no connects and RESET_XBEE
PTDPE = 0xF;//disable pullups except for no connects
PTEPE = portE | 0x8F;//disable pullups except for no connects
IO_DIO7_CTS_HOST = 1; //De-Assert CTS Line
IO_DIO7_CTS_HOST_D = 1; //Set CTS as output to computer
IO_DIO7_CTS_HOST_PE = 0;
}
void enableSleepDetectionInterrupt(void)
{
KB_INTERRUPT1_DISABLE();//disable all
KB_INTERRUPT1_ENABLE_PINS(IO_DIO9_ADC9_ON_SLEEP_INTERRUPT2);//
KB_INTERRUPT1_DETECT_EDGE_AND_LEVEL();
IO_DIO9_ADC9_ON_SLEEP_RISING_EDGE();//wait for the EMBER to turn on after a reset
KB_INTERRUPT1_ENABLE();
EnableInterrupts;
}
#pragma TRAP_PROC
void SleepModuleISR(void){//keyboard ISR
if (KB_INTERRUPT2_FLAG()){
KB_INTERRUPT2_DISABLE_ALL();//this one should be disabled
KB_INTERRUPT2_CLEAR();
}
if (KB_INTERRUPT1_FLAG()){
if (IO_DIO9_ADC9_ON_SLEEP){//Is the EMBER ON? set interrupt for when it transitions to low
IO_DIO9_ADC9_ON_SLEEP_FALLING_EDGE(); //this code should only execute once
KB_INTERRUPT1_CLEAR();
}else{
IO_DIO7_CTS_HOST = 1; //De-Assert CTS Line
IO_DIO9_ADC9_ON_SLEEP_RISING_EDGE();
KB_INTERRUPT1_CLEAR();
SPMSC1 = 0;//turn off low voltage detection for lower sleep current
//KB_INTERRUPT1_ENABLE(); //WARNING!!! never enable interrupt before sleeping. ISR might happen between this line and the next.
STOP_MODE(); //Module will wake after Interrupt happens even though interrupts are not enabled.
SPMSC1 = 1;//turn on low voltage detection
IO_DIO9_ADC9_ON_SLEEP_FALLING_EDGE();//module is now awake
KB_INTERRUPT1_CLEAR();
}
}
}