Dale Cunningham wrote:
I thought I had read in the documentation that the code to write to
flash either should be or must be run out of RAM. Or, maybe I decided
to use the mass erase function to save time and power. I've
forgotten. Your scheme seems good if it works.
I know the TI supplied BSL copies itself to RAM before execution. Why?
Dale
You can erase and write to flash from code running in flash. The flash
timing control circuitry deals with this, and inserts the appropriate
delays for both erasing and writing. This is described in the
documentation, but you have to do some reading to figure that out. A
simple clear statement that it works would have been nice.
The following simple routines, running from flash, will deal with erase
and write operations:
/* ptr must point to an address within the flash page you wish to clear */
void flash_clr(int *ptr)
{
_DINT();
FCTL3 = FWKEY; /* Lock = 0 */
FCTL1 = FWKEY | ERASE;
*((int *) ptr) = 0; /* Erase flash
segment */
FCTL1 = FWKEY; /* Erase, write
= 0 */
FCTL3 = FWKEY | LOCK;
_EINT();
}
void flash_write_int16(int16_t *ptr, int16_t value)
{
_DINT();
FCTL3 = FWKEY; /* Lock = 0 */
FCTL1 = FWKEY | WRT;
*((int16_t *) ptr) = value; /* Program the flash */
FCTL1 = FWKEY; /* Erase, write
= 0 */
FCTL3 = FWKEY | LOCK;
_EINT();
}
Steve