Hi All,
I got some interesting behavior for the cc430f5137 flash operation here. I have a program runs in 0x8000~0x9FFF. Tried to write a test function to start segment of 0xA000 (0xA000~0xA1ff).
It works fine if the code for erase and write of the segment directly run from flash. But it fails to write the flash segment 0xA000~0xA1ff (it is still all 0xffff after the function calls).
But from the cc430 family guide, it should be only possible to erase and write the flash via RAM for segment operation. While what happened here i got a totally reverse behavior.
The code:
typedef void (*VFPTR)(void);
static uint16_t ramFunc[128];
#define RUN_FROM_RAM 1
void demo()
{
printf("test function\n");
}
void test()
{
uint16_t *code = (uint16_t*)0xA000;
uint16_t i;
uint16_t *demo_code = (uint16_t*)(uint16_t)demo;
__disable_interrupt();
WDTCTL = WDTPW | WDTHOLD;
FCTL1 = FWKEY + ERASE; //segment erase
FCTL3 = FWKEY;
while(FCTL3 & BUSY);
*code = 0;
FCTL1 = FWKEY + WRT;
for(i = 0;i < 0x200;i++)
{
while(FCTL3 & BUSY);
*code++ = *demo_code++;
}
FCTL1 = FWKEY;
FCTL3 = FWKEY + LOCK;
P2OUT |= BIT3; //led on
((void (*)(void))0xA000)();
while(1);
}
void run_from_ram(VFPTR func, uint16_t *ramAddr, uint16_t len)
{
VFPTR f = (VFPTR)(uint16_t)ramAddr;
uint16_t *code = (uint16_t *)(uint16_t)func;
uint16_t i = 0;
for(i = 0; i < len; i++)
{
ramAddr[i] = code[i];
}
(*f)();
}
int main(int argc, char **argv)
{
cpu_init();
P2SEL &= ~BIT3;
P2DIR |= BIT3;
P2OUT &= ~BIT3; //led off
#if RUN_FROM_RAM
run_from_ram(test, ramFunc, sizeof(ramFunc));
#else
test();
#endif
return 0;
}
Is there any suggestion or explanation on this?
thanks,