Porting AVR code for MSP430 chips

I’ve just finished my first port of code from AVR over to MSP430. I used the garage door code button source because it’s fresh on my mind and I can reproduce the hardware on the TI Launchpad board. This provided a few sets of challenges, and showed me what I can do from the start to make my code more portable. I’ll get into both of those subjects and share the ported code after the break.

My first step was to convert the function that starts the timer. I had been using a 10ms interrupt to run some code and increment a variable. It wasn’t hard to get the MSP430G2231 to do that. Its TimerA is a 16-bit timer (this is a 16-bit chip after all) and has the option to use a divider. But if the system clock is running at 1MHz, and I want 100 interrupts per second, I just needed to run the timer directly from the system clock and interrupt at 10,000.

Next, I made changes to the I/O setup for the buttons, LED, and the load. This is pedantic so I’m just going to light the green LED to simulate the load, the red LED for the status light, the onboard switch for the user button, and a piece of wire on P1.4 to short to ground as a programming button. The LEDs don’t require too much change. The AVR defines used just a pin number, and the MSP430 defines used a bit shifted value like: (1<<5), so I had to account for that.

There was just a bit of initialization rewriting required for the buttons. That’s because I had been using the AVR internal pull-up resistors. MSP430 chips have internal pull-up OR pull-down resistors, requiring one extra line of code for that selection.

The real problem issue was that I stored the entry code in EEPROM on the AVR chip. There’s no built-in EEPROM with the MSP430 line, so how can we store the code through power cycles? Flash memory.

The MSP430G2231 has 2kB of program memory but it also has 256 bytes of Info Flash. This memory works a bit differently than internal EEPROM, and made me realize that I should have used separate functions for the EEPROM reads and writes. When porting the code I broke out the save and retrieve operations by adding these functions:

  • void get_code_from_flash(void);
  • void store_code(void);
  • void clear_flash(void);

The info flash is a bit peculiar to me. I guess all flash memory works this way but I haven’t dealt with it before. Apparently bits can only be written when you are changing them from 1 to 0. If you need a 0 to become a 1 you must erase the entire 64 kB block first. It’s not a huge deal as long as you know.

After studying the datasheets, looking at the app notes, and testing out the sample code I did manage to get it to work. But I found that I could NEVER get the first byte of a block to write. I have no idea why but I’ve got a forum thread going about it. I worked around this by offsetting my pointer by 1 byte.

That’s about it. The code works quite well on the Launchpad. All-in-all I find porting between these two architectures quite simple. One of the problems I am facing is that the MSP430 chips have a max operating voltage of 3.3V and most of the parts I have laying around are 5V. I’ll have to get working on that issue.

/*--------------------------------------------------------------------------
Garage Entry
Copyright (c) 2010 Mike Szczys

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the &quot;Software&quot;), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED &quot;AS IS&quot;, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------
  This project uses an MSP430G2231 to take entry from a single button and
  display status with a single LED. A proper code entry will trigger a
  relay and open a garage door. There is functionality for changing the
  default code using a programming button. The original project use an
  AVR ATtiny13:

http://jumptuck.wordpress.com/2010/08/01/garage-door-code-button/

  This has been ported for use with the MSP-EXP430G2 Launchpad board using
  the value line G2231 chip. Unlike AVR chips, there is no onboard EEPROM
  with the TI chips. This required one portion of the code to be overhauled.
  The code is now saved to Section B of the Info memory. This memory when
  erased reads back 0xFF. Software can program this Info Flash, changing the
  appropriate bits from 1 to 0 but it's can't change 0 bits to 1. Because
  of this, then entire segment needs to be erased every time data is written
  to that segment.

--------------------------------------------------------------------------*/

#define F_CPU 1000000

#include &lt;io.h&gt;
#include &lt;signal.h&gt;
//#include &lt;avr/eeprom.h&gt;

//Set default security code here
unsigned char code1 = 1;
unsigned char code2 = 2;
unsigned char code3 = 3;
unsigned char code4 = 4;

#define KEY_DDR		P1DIR
#define KEY_PORT	P1OUT
#define KEY_REN		P1REN
#define KEY_PIN		P1IN
#define KEY0		BIT3	//User button
#define KEY1		BIT4	//Programming jumper

#define LED_DDR		P1DIR
#define LED_PORT	P1OUT
#define LED0		BIT0

#define LOAD_DDR	P1DIR
#define LOAD_PORT	P1OUT
#define LOAD0		BIT6

//State aliases
#define STATE_REST		0
#define STATE_ENTRY		1
#define STATE_PROGRAM_WAIT	2
#define STATE_PROGRAM 		3

//System
volatile unsigned char systick = 0;

unsigned char entry_index = 0;
unsigned char entry_code[4];

//Debounce
unsigned char debounce_cnt = 0;
volatile unsigned char key_press;
unsigned char key_state;

/*--------------------------------------------------------------------------
  Prototypes
--------------------------------------------------------------------------*/
unsigned char get_key_press( unsigned char key_mask );
void init_timers(void);
void init_io(void);
void delay_ms(unsigned int n);
void blink_LED(unsigned char times, unsigned int duration_ms);
void initialize_entry_code(void);
unsigned char system_reset(unsigned char state);
void readback(void);
void check_code(unsigned char state);
void get_code_from_flash(void);
void store_code(void);
void clear_flash(void);

/*--------------------------------------------------------------------------
  FUNC: 8/12/10 - Used to read debounced button presses
  PARAMS: A keymask corresponding to the pin for the button you with to poll
  RETURNS: A keymask where any high bits represent a button press
--------------------------------------------------------------------------*/
unsigned char get_key_press( unsigned char key_mask )
{
  dint();			// read and clear atomic !
  key_mask &amp;= key_press;	// read key(s)
  key_press ^= key_mask;	// clear key(s)
  eint();
  return key_mask;
}

/*--------------------------------------------------------------------------
  FUNC: 8/12/10 - Sets and starts a system timer
  PARAMS: NONE
  RETURNS: NONE
--------------------------------------------------------------------------*/
void init_timers(void)
{
  // If calibration constants have not been erased
  if (CALBC1_1MHZ != 0xFF &amp;&amp; CALDCO_1MHZ != 0xFF)
  {
    BCSCTL1 = CALBC1_1MHZ;                    // Set DCO to 1MHz
    DCOCTL = CALDCO_1MHZ;
  }

  dint();
  //TimerA for buttons and systick
  TACTL = TASSEL_2 | MC_1;		//SMCLK in UP mode
  TACCTL0 |= CCIE;			//enable timer interrupt
  TACCR0 = F_CPU / 100;  //set the TimerA match for 10ms interrupts
  eint();				//global interrupt enable
}

/*--------------------------------------------------------------------------
  FUNC: 8/12/10 - Initialize input and output registers
  PARAMS: NONE
  RETURNS: NONE
--------------------------------------------------------------------------*/
void init_io(void)
{
  //Setup Button
  KEY_DDR &amp;= ~(KEY0 | KEY1);	//set pins as inputs
  KEY_REN |= (KEY0 | KEY1);	//enable resistor
  KEY_PORT |= (KEY0 | KEY1);	//set resistor to pull-up

  //Setup LED
  LED_DDR |= LED0;
  LED_PORT &amp;= ~(LED0);

  //Setup Load (relay pin)
  LOAD_DDR |= LOAD0;
  LOAD_PORT &amp;= ~(LOAD0);
}

/*--------------------------------------------------------------------------
  FUNC: 7/23/10 - Wastes approximately n milliseconds
  PARAMS: Duration in milliseconds
  RETURNS: NONE
--------------------------------------------------------------------------*/
void delay_ms(unsigned int n)
{
  systick = 0;
  while (systick &lt; n/10) nop();
  systick = 0;
}

/*--------------------------------------------------------------------------
  FUNC: 7/23/10 - Blinks LED in the doorbell
  PARAMS
    1: # of times to blink
    2: Duration in milliseconds
  RETURNS: NONE
  NOTES: This is a blocking function.  When the LED blinks, the only other code
    that can execute is in interrupt handlers.
--------------------------------------------------------------------------*/
void blink_LED(unsigned char times, unsigned int duration_ms)
{
  //while (times--)
  for (unsigned char i=times; i&gt;0; i--)
  {
    LED_PORT |= LED0;
    delay_ms(duration_ms);
    LED_PORT &amp;= ~(LED0);
    if (i &gt;= 1) delay_ms(duration_ms);
    else systick=0;
  }
}

/*--------------------------------------------------------------------------
  FUNC: 7/23/10 - Inits the entry code variables
  PARAMS: NONE
  RETURNS: NONE
--------------------------------------------------------------------------*/
void initialize_entry_code(void)
{
  for (unsigned char i=0; i&lt;4; i++)
  {
    entry_code[i] = 0;
  }
  entry_index = 0;
}

/*--------------------------------------------------------------------------
  FUNC: 7/23/10 - Resets the system
  PARAMS: NONE
  RETURNS: NONE
  NOTES: Sets state to rest, sets code array to all 0, blinks the LED quickly
--------------------------------------------------------------------------*/
unsigned char system_reset(unsigned char state)
{
  initialize_entry_code();
  blink_LED(12,100);
  return STATE_REST;
}

/*--------------------------------------------------------------------------
  FUNC: 7/23/10 - Displays a 4 digit code in a series of LED blinks
  PARAMS: NONE
  RETURNS: NONE
--------------------------------------------------------------------------*/
void readback(void)
{
  for (unsigned char i=0; i&lt;4; i++)
  {
    blink_LED(entry_code[i],200);
    delay_ms(1500);
  }
}

/*--------------------------------------------------------------------------
  FUNC: 8/12/10 - Used to set new code entrys or check codes for access
  PARAMS: Program state - STATE_PROGRAM (used to set new code)
  RETURNS: NONE
  NOTE: Added a new function call (and new function) store_code() to make
    this more device independent.
--------------------------------------------------------------------------*/
void check_code(unsigned char state)
{
  if (state == STATE_PROGRAM)
  {
    store_code();		//Write the new code to Info Flash memory
    get_code_from_flash();	//Load the newly stored code
    readback();			//Display the new code
  }
  //Verify the code entered is correct
  else if (entry_code[0]==code1 &amp;&amp; entry_code[1]==code2 &amp;&amp; entry_code[2]==code3 &amp;&amp; entry_code[3]==code4)
  {
    //Cycle to load to open the door
    LOAD_PORT |= LOAD0;
    delay_ms(800);
    LOAD_PORT &amp;= ~(LOAD0);
  }
  else state = system_reset(state);
}

/*--------------------------------------------------------------------------
  FUNC: 8/12/10 - Loads code from flash memory into code(x) variables
  PARAMS: NONE
  RETURNS: NONE
--------------------------------------------------------------------------*/
void get_code_from_flash(void)
{
  char *p;
  p = (char *)0x1001;
  code1 = *p++;
  code2 = *p++;
  code3 = *p++;
  code4 = *p;
}

/*--------------------------------------------------------------------------
  FUNC: 8/12/10 - Saves newly entered code to info flash memory
  PARAMS: NONE
  RETURNS: NONE
--------------------------------------------------------------------------*/
void store_code(void)
{
  FCTL2 = FWKEY + FSSEL1 + FN1;		// MCLK/3 for Flash Timing Generator
  clear_flash();			// Block must always be cleared before writing

  char *Flash_ptr;
  Flash_ptr = (char *) 0x1001;		//Point to Info Flash segment D

  FCTL3 = FWKEY;                            // Clear Lock bit
  FCTL1 = FWKEY + WRT;                      // Set WRT bit for write operation

  *Flash_ptr++ = entry_code[0];
  *Flash_ptr++ = entry_code[1];
  *Flash_ptr++ = entry_code[2];
  *Flash_ptr++ = entry_code[3];

  FCTL1 = FWKEY;                            // Clear WRT bit
  FCTL3 = FWKEY + LOCK;                     // Set LOCK bit
}

/*--------------------------------------------------------------------------
  FUNC: 8/12/10 - Clears one block of info flash memory
  PARAMS: NONE
  RETURNS: NONE
  NOTE: Info flash must be cleared before writing new values
--------------------------------------------------------------------------*/
void clear_flash(void)
{
  int *Flash_ptr;
  Flash_ptr = (int *)0x1000;	//Point to Info Flash segment D

  FCTL3 = FWKEY;		// Clear Lock bit
  FCTL1 = FWKEY + ERASE;	// Set Erase bit
  *Flash_ptr = 0;		// Dummy write to erase Flash segment D
  FCTL1 = FWKEY;		// Clear WRT bit
  FCTL3 = FWKEY + LOCK;		// Set LOCK bit
}

/*--------------------------------------------------------------------------
  FUNC: 7/23/10 - Main
--------------------------------------------------------------------------*/
int main(void)
{
  WDTCTL = WDTPW + WDTHOLD;                 // Stop WDT
  init_timers();
  init_io();
  get_code_from_flash();

  unsigned char state = STATE_REST;
  unsigned char system_timeout = 0;

  system_reset(state);

  for (;;) {
    switch(state){
      case STATE_REST :
        if( get_key_press( KEY0 )) {
          //Set entry index and array data to zero
          initialize_entry_code();
          //Set state to STATE_ENTRY
          state = STATE_ENTRY;
          //blink LED once (also resets systick)
          blink_LED(1,600);
          systick=0;
        }
        else if (get_key_press( KEY1 ))
        {
          //Programming button has been pushed
          state = STATE_PROGRAM_WAIT;
          system_timeout = 0;
          initialize_entry_code();
        }
      break;

      case STATE_PROGRAM_WAIT:
        //Waiting for code entry.
        if( get_key_press( KEY0 )) {
	  state = STATE_PROGRAM;
	  blink_LED(1,600);	//signal that we're ready for first entry
	  systick=0;
	}
	//Flash until user button is pushed
	else if ( systick &gt; 50 ) {
	  LED_PORT ^= LED0;
	  systick = 0;
	  //We've been idle for 2 minutes so reset the system.
	  if (++system_timeout &gt; 240) state = system_reset(state);
	}
      break;

      case STATE_PROGRAM:
      case STATE_ENTRY:
	//Has entry timed out?
	if (systick &gt; 150) {
	  //Make sure a number was entered
	  if(entry_code[entry_index]) {
	    //check if that was the final digit
	    if(++entry_index&gt;3)	{
	      check_code(state);
	      state = STATE_REST;
	    }
	    //Confirm with a blink (resets systick)
	    else blink_LED(1,600);
	  }
	  //current code is 0 which is not allowed, reset the system
	  else {
	    state = system_reset(state);
	  }
      	}
      	else if( get_key_press( KEY0 )) {
	  //increment digit to array
	  ++entry_code[entry_index];
	  //reset systick because button was just pushed
	  systick = 0;
      	}
      break;
    }
  }
}

//--------------------------------------------------------------------------
interrupt(TIMERA0_VECTOR) Timer_A (void)	// every 10ms
{
  static unsigned char ct0, ct1;
  unsigned char i;

  //TAR0 = (unsigned char)(signed short)-((F_CPU * .01) + 0.5);   // preload for 10ms

  i = key_state ^ ~KEY_PIN;    // key changed ?
  ct0 = ~( ct0 &amp; i );          // reset or count ct0
  ct1 = ct0 ^ (ct1 &amp; i);       // reset or count ct1
  i &amp;= ct0 &amp; ct1;              // count until roll over ?
  key_state ^= i;              // then toggle debounced state
  key_press |= key_state &amp; i;  // 0-&gt;1: key press detect

  ++systick;
}
essential