MSP430 launchpad development with linux


Introduction:

     MSP-EXP430G2 is an MSP430 microcontroller development tool released by TEXAS INSTRUMENTS ( http://ti.com ). It is available for a cheap price ie only $ 4.30 and this results in the popularity of this development tool. The LaunchPad development kit is a part of the MSP430 Value Line series.
LaunchPad has an integrated DIP target socket that supports up to 20 pins, allowing MSP430 Value Line devices to be dropped into the LaunchPad board. Also, an on-board flash emulation tool allows direct interface to a PC for easy programming, debugging, and evaluation. Included are free and downloadable software development environments for writing and debugging softwares. The LaunchPad is an easy-to-use, affordable, and scalable introduction to the world of microcontrollers and the MSP430 family.

MSP430 launchpad development in linux:
  •   C - There is a port of the GNU C Compiler (GCC) and GNU Binutils (as, ld) for the embedded processor MSP430. Tools for debugging and download are also available (GDB, JTAG and BSL) More over, nowadays, .deb packages for the development are also available for most of the linux distro. ie no need to compile the tool chain, instead we will get the binary.
  • ASM - For assembly language programming, the best one is naken430asm. I really likes this assembler, it is simpler and easier to do asm programs for naken430asm compared to the msp430-gcc assembler.
MSP430G2231 microcontroller (an example):

MSP430G2231 is one of the value line device of the MSP430 series. (MSP - Mixed Signal Processor) To familiarize with the launchpad and the MSP430 in general, I just selected the above microcontroller which comes with the launchpad kit.

Introduction:
    MSP30G2231 is a microcontroller with 16 bit processor and having von-neumann architecture. It is having a RISC processor with a 16 bit address bus and a 16 bit data bus. One of the most important feature of msp430 is the low power consumption, best for battery powered devices. We can select the power modes, I will show it with examples.

Features as per the datasheet of MSP430G2231:
  • Low Supply-Voltage Range: 1.8 V to 3.6 V
  • Ultra-Low Power Consumption
           – Active Mode: 220 μA at 1 MHz, 2.2 V
           – Standby Mode: 0.5 μA
           – Off Mode (RAM Retention): 0.1 μAFive Power-Saving Modes
  • Ultra-Fast Wake-Up From Standby Mode in Less Than 1 μs
  • 16-Bit RISC Architecture, 62.5-ns Instruction Cycle Time Basic Clock Module Configurations
         –Internal Frequencies up to 16 MHz With One Calibrated Frequency
         – Internal Very Low Power Low-Frequency (LF) Oscillator
         –32-kHz Crystal
         – External Digital Clock Source
  • 16-Bit Timer_A With Two Capture/Compare Registers.
  • Universal Serial Interface (USI) Supporting SPI and I2C.
  • Brownout Detector.
  • 10-Bit 200-ksps A/D Converter With Internal Reference, Sample-and-Hold, and Autoscan.
  • Serial Onboard Programming, No External Programming Voltage Needed, Programmable Code Protection by Security Fuse.
  • On-Chip Emulation Logic With Spy-Bi-Wire Interface.

Example:

1 second accurate timer using 32768 Hz external crystal: (low power mode 3)
  I am showing how to use the external 32768 Hz crystal for generating 1 Hz accurate clock pulse from an msp430 launchapad. Also, the mcu takes very very low power while the timer in incrementing upto 32767. Then it enters interrupt service routine and do the job written there and again enters the power down mode. There  I have written an LED flashing code which will flash the RED led for a fraction of second. We are not using the DCO clock for the timer, instead we use ACLK clock which is the 32768Hz crystal oscillator. DCO is not much accurate.

Assembly code: (double click on the code to copy it)

;---------------------------------------------------------
;author                  :vinod s (vinodstanur@gmail.com)
;date                    : sat June 9, 11:42:51PM
;PROCESSOR               : MSP430G2231
;assembler               : naken430asm
;development platform    : linux
;NOTE: 32768Hz crystal must be soldered to the launchpad
;      before doing this test program
;--------------------------------------------------------
.include "msp430g2x31.inc"

org 0xf800             ;flash begins 
start:                 ;startup code which sets the stack pointer and disable WDT
    mov.w #(WDTPW|WDTHOLD), &WDTCTL ;disabling watch dog timer, otherwise it will reset on regular interval
    mov.w #0x27f, SP     ;SETTING TOP OF THE STACK ON STACK POINTER
    call #main           ; NOT NEEDED ANY WAY :-)
  
main:
    mov.b #0, &P1OUT
    mov.b #1,&P1DIR       ;setting port1 bit 0 as output
    mov.b #(XCAP_3), BCSCTL3 ;selecting internal capacitor value for crystal
    mov.w #32767, &TACCR0 ;seting the timer compare value for interrupt
    mov.w #CCIE, &TACCTL0 ;compare interrupt enable
    mov.w #(TASSEL_1|MC_1|TACLR), &TACTL ;setting the timer with ACLK clock
    eint                                 ;general interrupt enable
    bis.b #(11011000b),SR                ;entering LOW POWER MODE 3
  
;WILL NEVER REACH HERE (CPU OFF , SMCLK OFF)

isr: ;LED flashing routine which happens on every second
    mov.b #1, &P1OUT
    mov.w #10000, R10
    small_delay_loop: dec.w R10
    jnz small_delay_loop
    mov.b #0, &P1OUT
    reti  

;VECTORS
org 0xfffe                  ;reset vector
    dw start             ;to write start address to 0xfffe on programming
org 0xfff2                  ;timer interrupt vector (CC)
    dw isr               ;to write isr address to 0xfff2 on programming
HEX CODE
:10F80000B240805A200131407F02B0120EF8C2434C
:10F810002100D2432200F0400C003908B240FF7FA3
:10F820007201B24010006201B2401401600132D294
:10F8300072D0D800D24321003A4010271A83FE2309
:06F84000C2432100001389
:02FFF20034F8E1
:02FFFE0000F809
:00000001FF

 C program for the same purpose: (double click on code to copy it)
/*
author:                 vinod s (vinodstanur@gmail.com)
date:                   sun jan 10, 12:14:53 AM
compiler:               MSP-GCC
command line:           msp430-gcc -mmcu=msp430g2231 timer.c
development platform:   LINUX

NOTE: 32768Hz crystal should be soldered before
testing this code
*/

#include <msp430g2231.h>
#include <legacymsp430.h>

void main(void) {
    WDTCTL = WDTPW + WDTHOLD; // turn WDT off
    P1OUT = 0;
    P1DIR = 1;
    BCSCTL3 |= XCAP_3;
    CCTL0 = CCIE;
    CCR0 = 32767;
    TACTL = TASSEL_1 + MC_1 + TACLR +  TAIE;
    _BIS_SR(GIE + LPM3_bits);
    
    for (;;) {
        //WILL NOT REACH HERE
    }
    
}

interrupt (TIMERA0_VECTOR) isr(void)
{
    volatile int i = 10000;
    P1OUT = 1;
    while(i--);
    P1OUT = 0;
}

1 comment :