Donnerstag, 29. Oktober 2015

Programming the MSP430 Launchpad (MSP-EXP430G2) with the GNU assembler

Launchpad

The MSP-EXP430G2 Launchpad from Texas Instruments is one of a series of launchpads for TI's MSP430 line of controllers, it is probably the one most likely found in an electronics store.



It has a DIL-20 socket that makes it possible to work with a range of MSP430 controllers in DIP-20 and DIP-14 (for a list, see slau318f.pdf on ti.com).

The launchpad box includes an MSP430G2553 and an MSP4302452 controller in both DIP-20, Arduino style SIL headers for shields, and a USB cable for programming and serial connections. The external crystal and capacitors are not placed, a separate 32768 watch crystal comes with the box.

Programming tools

For developing, programming and debugging launchpad software, there are some free tools:

  • Compiler: search for MSP-430-OPENSOURCE on ti.com or a sufficiently up to date msp430 gcc from a distribution's package manager (e.g. the package gcc-msp430 on Ubuntu), that includes gcc, g++, binutils, linker, objcopy, objdump and so on
  • Programmer and debugger: mspdebug is being used here (it has also a package on Ubuntu and is also at Github)
Common to all MSP430x2xx chips is the location of the 16-entry vector table, which is at the end of the 64k memory map (0xffe0-0xffff). The memory containing the vector table is flash memory. Flash memory always extends from 0xffff downwards - how far depends on the flash size of the controller. Controller series with more than 64k memory also have flash above 0xffff.

The chips coming with the launchpad have 16k (MSP430G2553) and 8k (MSP430G2452) of flash.

Interesting for the first programming steps is the last entry in the vector table, which is the reset vector (i.e. the 16-bit value at 0xfffe, this value gets loaded into the program counter on startup and on each reset). This reset 'interrupt' has the highest priority.

Changing the state of output pins

Using the GNU assembler (msp430-as), the following program can be assembled if it is in a file like test.s by executing

msp430-as testasm.s -o testasm.o

The contents of testasm.s would be:

P1OUT = 0x0021
P1DIR = 0x0022

.text
.global entry_point

entry_point:
        mov   #0x5a80, &0x0120    ; disable watchdog
        mov.b #0x41, &P1DIR       ; P1.1 = 1 and P1.6 = 1

blink_loop:
        mov   #0x8000, r4
        mov.b #0x01, &P1OUT
p11_loop:
        dec   r4
        jnz   p11_loop

        mov   #0x8000, r4
        mov.b #0x40, &P1OUT
p16_loop:
        dec   r4
        jnz   p16_loop
        jmp   blink_loop

.end

Port pins P1.1 and P1.6 are set as outputs here and then turned on and off alternatively with a busy counter for timing.

To locate the 'entry_point' to an address in flash (e.g. 0xc000 for a 16k flash chip), set the reset vector accordingly and generate an ELF file, the GNU linker can be used with the following script:

MEMORY
{
    FLASH        (rx) : ORIGIN = 0xc000, LENGTH = 16k - 32
    RESET_VECTOR (r)  : ORIGIN = 0xfffe, LENGTH = 2
}

ENTRY (entry_point)

SECTIONS
{
    .text :
    {
        KEEP(*(.text))
    } > FLASH

    reset_vector :
    {
        SHORT(entry_point)
    } > RESET_VECTOR
}

If the linker script file is named msp430g2553.ld (for the 16k chip), the LD can be called like:

msp430-ld -Tmsp430g2553.ld testasm.o -o testasm.elf

To get srec, hex or bin files:


msp430-objcopy -Osrec   testasm.elf testasm.srec
msp430-objcopy -Oihex   testasm.elf testasm.ihex
msp430-objcopy -Obinary testasm.elf testasm.bin

MSPDebug

Now, let's test one of the generated files and start MSPDebug (tested here with version 0.23) in simulator mode:

mspdebug sim

Using the command:

prog testasm.elf

should erase the memory and load the ELF file:


To set up messages for the port 1 GPIO that we want to toggle before starting the simulator, use the following simio commands:

simio add gpio P1
simio config P1 base 0x0020
simio config P1 verbose



Then simulate with run and break with CTRL-C:


MSPDebug also has a stepping command that takes the number of steps as a parameter, break- and watchpoints, disassembly, syntax coloring and many more features.

Flashing onto the launchpad

To put the program onto the real launchpad and run it, just start MSPDebug with:

mspdebug rf2500

instead of starting it in simulator mode and then repeat the procedure above (i.e. use the command prog testasm.elf or prog testasm.srec or prog testasm.ihex)

1 Kommentar:

  1. I think `p11_loop` should have been called `p10_loop` since LED1 is P1.0, not P1.1.

    AntwortenLöschen