Skip to content

Instantly share code, notes, and snippets.

@spirilis
Last active August 29, 2015 14:05
Show Gist options
  • Save spirilis/8b7b115a4f598571ebc4 to your computer and use it in GitHub Desktop.
Save spirilis/8b7b115a4f598571ebc4 to your computer and use it in GitHub Desktop.
MSP430F5xxx infomem flasher
/* Simple F5xxx Flash Erase/Write primitives
* 2014 Eric Brundick <spirilis@linux.com>
*/
#include <msp430.h>
#include <stdint.h>
#include <stdbool.h>
#include "flash.h"
int flash_erase_info(void *loc)
{
WDTCTL = WDTPW | WDTHOLD;
while (FCTL3 & BUSY) ;
FCTL3 = FWPW; // Clear LOCK
FCTL4 = FWPW; // Clear LOCKINFO
if (FCTL3 & (KEYV|ACCVIFG))
return -1;
FCTL1 = FWPW | ERASE; // Enable Segment Erase
if (FCTL3 & (KEYV|ACCVIFG))
return -1;
*((uint8_t *)loc) = 0; // Dummy write to Flash segment
if (FCTL3 & (KEYV|ACCVIFG))
return -1;
while (FCTL3 & BUSY) ; // Wait for completion
FCTL3 = FWPW | LOCK; // Set LOCK
if (FCTL3 & (KEYV|ACCVIFG))
return -1;
return 0;
}
int flash_write_unlock()
{
WDTCTL = WDTPW | WDTHOLD;
while (FCTL3 & BUSY)
;
FCTL3 = FWPW; // Clear LOCK
FCTL4 = FWPW; // Clear LOCKINFO
if (FCTL3 & (KEYV|ACCVIFG))
return -1;
FCTL1 = FWPW | WRT; // Enable write
if (FCTL3 & (KEYV|ACCVIFG))
return -1;
return 0;
}
int flash_write_lock()
{
while (FCTL3 & BUSY)
;
FCTL1 = FWPW;
if (FCTL3 & (KEYV|ACCVIFG))
return -1;
FCTL3 = FWPW | LOCK;
FCTL4 = FWPW | LOCKINFO;
if (FCTL3 & (KEYV|ACCVIFG))
return -1;
return 0;
}
int flash_write_word(void *loc, uint16_t val)
{
if (!(FCTL4 & LOCKINFO)) {
*((uint16_t *)loc) = val;
while (!(FCTL3 & WAIT)) ;
} else {
return -2;
}
if (FCTL3 & (KEYV|ACCVIFG))
return -1;
return 0;
}
int flash_write_byte(void *loc, uint8_t val)
{
if (!(FCTL4 & LOCKINFO)) {
*((uint8_t *)loc) = val;
while (!(FCTL3 & WAIT)) ;
} else {
return -2;
}
if (FCTL3 & (KEYV|ACCVIFG))
return -1;
return 0;
}
/* Simple F5xxx Flash Erase/Write primitives
* 2014 Eric Brundick <spirilis@linux.com>
*/
#ifndef FLASH_H
#define FLASH_H
#include <msp430.h>
#include <stdint.h>
int flash_erase_info(void *loc);
int flash_write_unlock();
int flash_write_lock();
int flash_write_word(void *loc, uint16_t val);
int flash_write_byte(void *loc, uint8_t val);
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment