CONTENTS

Home
Projects
  Electronics
  Graphics
  Java
  Java Mobile
  Android
  Other Stuff
Resume
Music
Pictures
Contact



MSP430 Assembler

More electronics projects on mikekohn.net here.

Posted: June 14, 2010
Updated: October 30, 2011

Texas Instruments Launchpad

The Texas Instruments Launchpad running code assembled with naken430asm and uploaded with MSPDebug. On the screen is the hex file disassembled. This program was assembled and tested using 100% open-source software on Linux.

My test program is here: launchpad_blink.asm

Contents

Introduction
Download
Progress
License
Helping Out
TODO
Usage
Assembler Syntax
Why

Introduction

This is an open-sourced assembler / disassembler / simulator for the MSP430 series of microcontrollers from Texas Instruments. The goal of this project is to have a completely cross-platform assembler with the exe (naken430asm) under 50k and to have a cross-platform disassembly utility (naken430util) along with it to help with debugging and a bit of profiling. The naken430util, when disassembling, shows how many CPU cycles each instruction will take and I have built in simulation to help show how the binary code will flow on the chip and how many cycles it will take to execute. I also wanted to add the ability to program and debug the chips from naken430util, but I may just leave that for the MSPDebug project.

Download (October 30, 2011)
naken430asm-2011-10-30.tar.gz (Source Code)
naken430asm-2011-10-30-macosx10.6-x86_64.tar.gz (MacOSX 10.6)
naken430asm-2011-10-30.zip (Windows XP/Vista/7)

Samples

launchpad_blink.asm - Make 2 LED's blink
spi.asm - Write 16 bits out of SPI to a MCP4921 DAC
ds1305_rtc.asm - Read time from a DS1305 real time clock chip.
ds18b20.asm - Read temperature from a DS18B20 temperature sensor.
guitar_proc.asm - Example of taking input on the A/D, changing the sound sample, and throwing it out a DAC.
clock_thermometer.asm - Talking alarm clock thermometer.
msp430_sirc.asm - Reading SIRC data from a Sony remote control.
uart.asm - UART example for msp430g2553.
Bluetooth Thermometer - Bluetooth module hooked up to an msp430g2553.
Syma S107 No Fly Zone - IR Jammer for Syma S107 Helicopters.

Older Releases

naken430asm-2011-10-24.tar.gz
naken430asm-2011-09-21.tar.gz
naken430asm-2011-09-19.tar.gz
naken430asm-2011-09-13.tar.gz
naken430asm-2011-09-05.tar.gz
naken430asm-2011-09-03.tar.gz
naken430asm-2011-08-02.tar.gz

Progress

January 16, 2012: Just wanted to post a message so this project doesn't look abandoned. The assembler appears to be quite stable. I've been using it for a while and haven't had a complaint in a while. Currently I've been working (slowly) on MSP430X support which is... eh... not as clean of an instruction set as straight 16 bit MSP430 is, so this hasn't been very fun.

So I got an email from Gregory Davill with some tips on include files with naken430asm. He writes:

The standard headers include a "#include <iomacros.h>" which defines
how the registers are defined. By replacing that line with these six
macros your assembler works perfectly with the new header.
 
#define sfrb(x,y) x equ y
#define sfrw(x,y) x equ y
#define sfra(x,y) x equ y

#define const_sfrb(x,y) sfrb(x,y)
#define const_sfrw(x,y) sfrw(x,y)
#define const_sfra(x,y) sfra(x,y)

October 24, 2011: Fixed issue reading in ELF files with naken430util (the file was being read in correctly, but the end address wasn't properly marked). Added an include file for msp430g2553 from Peter Johansson and an include file for msp430x14x from Alex B.

September 21, 2011: Fixed issue with macros not unrolling in the lst files. Fixed issue with comments between .macro / .endm. Changed .db and .dw so it can have expressions instead of constants and labels.

September 19, 2011: Changed db so it doesn't null terminate strings. Added .ascii and .asciiz for strings and null terminated strings.

September 13, 2011: Some bug fixes: A typo in the assembler / disassembler mixed up adc and addc, the CG alias was missing from the assembler, and .w and .b couldn't be uppercase. ELF now splits up data and text sections (although there needs to be more work on that I think).

September 5, 2011: I began adding support for ELF file output (and input into naken430util). It currently does something similar to what msp430-gcc does in that it dumps the binary block into a single .text section. This to me is kind of gross, so I'm going end up seeing what it takes to separate the binary block into multiple .text and .data sections (depending on if what is written in that location is a .db/.dw or instruction). I have a symbol table but it only has labels right now. I'm hoping to add other things later. I also plan on adding the DWARF support for debugging. The ELF option is configurable at compile time and since it currently adds about 4k of code to naken430asm. I believe some people are actually using naken430asm on embedded devices so I decided to make this optional. If ELF is not needed then a ./configure --disable-elf can remove the code. I still need to add a way to configure the ELF for which MSP430 device family the elf file is for. To see what symbols and sections are created in these ELF files, I have a program called magic_elf which can print out headers and such.

I also changed the behavior of .db so that it won't warn and pad if the datasize is not 2 byte divisible. Instead it will warn and pad if an instruction or .dw falls on an unaligned boundary.

Glen Worstell has contributed a makeInc.lua script that can take include files from TI's Code Composer and put it in a format the naken430asm can read. I included this script in the scripts dir of naken430asm.

August 2, 2011: Added .macro / .endm for replacing the #define (or .define). The .hex files are now deleted on an assembler failure. The assembler failure messages are more obvious now (with a ** failed type thing at the bottom of the screen). The code also checks for overflows in constants for .w and .b instructions.

June 27, 2011: Wrote a Python script (included in the tarball) that allows me to take the Texas Instruments MSP430 datasheets, open them with xpdf, highlight the peripheral file map section, middle-mousebutton it into a text file, and run this script to create an include file. Other than the weird error I found in the MSP430G2x31 guide, it should make pretty accurate include files. The only thing missing is the bit values.

May 30, 2011: Fixed problem with nested macros.

May 23, 2011: Fixed clr and tst to use mov #0 and cmp #0 instead of xor and and. Fixed /* comments */ in macros.

May 22, 2011: Last two releases fixes the following bugs: disassembler was adding .b to instructions that don't have a .b or .w size, simulator wasn't calculating symbolic addresses correctly, #define would include ; and // comments, and assembler didn't calculate hex numbers that end in h correctly. I also added ds, ds8, ds16 for reserving extra space and binary and octal constants using q and b.

May 5, 2011: Fixed a bug in the assembler which wasn't allowing rra to have a byte size on it. In the simluator I fixed issues with 8bit instructions not clearing the upper byte and an issue with the RRC instruction. More thanks to Dan Karmann, who's been doing a lot of work testing the simulator.

May 3, 2011: In naken430util I added the ability to set or clear status register flags by typing: set c or clear c for example to set or clear the carry flag. All flags should be settable/clearable.

April 30, 2011: In the simulator I added wwrite and bwrite for writing multiple bytes or writes to a memory location. I added the ability to use . instead of # for things like .define, .include, .org, etc. I also added a step command instead of having to set the speed to 0 and then typing run. Lastly I changed the way breakpoints work. In the assembler I fixed a bug that could cause the assembler to crash when having a db as the last byte in a file.

April 28, 2011: Thanks to Jasper Lievisse Adriaanse for reporting a crashing bug in the assembler in the case that org was set above the 64k limit (fixed in the April 28, 2011 version) and for maintaining an OpenBSD port for naken430asm. Also fixed an issue when 0x00(r0) is the the source in the simulator. Lastly I changed the breakpoint code around.

I'm tempted to add the 20 bit extension instructions to the entire project. Could end up being a lot of work so I'm only going to do it if I get requests for it. If anyone wants this feature please let me know.

License

naken430msp is distributed under the GPL license.

How To Help Out

Well, I'm not really looking for any patches right now since this is in heavily development and I prefer to do my own work, but the following would be really nice:

  • Include files (please give yourself credit at the top of the .inc file)
  • Bug reports
  • Suggestions
  • Emails of encouragement
  • The TI JTAG protocol specification (Anyone at TI reading reading this??)
  • Test boards and JTAG's
  • Non-poisoned candy (speciellt nånting från sverige... mmm svenska godis)
  • A nice looking girl
  • Money :)

TODO

  • ELF: Add support for multiple .text and .data sections
  • ELF: Add support DWARF debug sections
  • ELF: Add support for equ symbols
  • Add support for MSP430X instructions
  • naken430util: Debugger (with JTAG)
  • naken430util: Upload/download firmware (with JTAG)
  • naken430asm: Create include files

Assembling, Disassembling, Simulating

A program can be assembled from the command line with a simple:

naken430asm -d launchpad_blink.asm

The -d option will create an .ndbg file which helps naken430util when using the -list option. It can be left out. The -o option can be used to name the output file something other than out.hex. To disassemble the code, simply type:

./naken430util out.hex
for interactive mode or:
./naken430util -disasm out.hex
to disassemble the out.hex file or
./naken430util -list out.hex
to disassemble the out.hex program and print the source code to the right of it using the .ndbg file.

Simulation
The simulator allows you to show how your program will run. It gives a view of all the registers and how many CPU cycles have passed, along with the current instruction that executed and the next few in memory. To test the simulation, after assembling launchpad.asm type:

./naken430util out.hex
speed 1
run

This will load the out.hex file and set the CPU speed to 1Hz. The run command of course will start the simulation. To pause it just press Ctrl-C. To run in single step mode:

speed 0
run

One instruction is executed and the simulator breaks. Pressing enter or typing 'run' again will execute one more instruction. If there is a function needing to be profiled at location 0xf010, typing: call 0xf010 will execute it. The program will stop running at the RET of the function.

Three other useful commands are call, push, and set. To push something on the stack just do: push 0x1000 for example to push 0x1000 to the stack. To set the value of a register: set pc=1000 or set r5=0xffff. To call a function: call 0xf034 to call a function at location 0xf034. This pushes the return address 0xffff on the stack and sets PC to 0xf034. When the simulator sees PC==0xffff it returns back to you so the number of clock cycles it took to run the function.

There are 4 commands for reading and writing memory: bprint, wprint, bwrite, wwrite. So to write 5 bytes to location 0x1000, I could type:

bwrite 0x1000 100 102 143 0 5

and I can confirm it's written by typing: bprint 0x1000

For reading and writing word sized data, wprint and wwrite work the same way.

Assembler Syntax

Like other MSP430 assemblers, naken430asm accepts C style #define, #ifdef, #if defined(). An example would be:

#define BLAH 50
#define SOMETHING

#ifdef SOMETHING
mov.w r1, r12
#else
mov.w r3, r12
#endif

#if defined(SOMETHING) && !defined(ANOTHER) && BLAH>50
mov.w @r12+, r3
#endif


Standard assembler syntax "equ" is also accepted too:

BLAH equ 50

Also .macro / .endm works like the following:

.macro BLAH
mov.w #5, r10
mov.w #6, r11
.endm

or also:

.macro BLAH(a,b)
mov.w #a, r10
mov.w #b, r11
.endm

Data bytes and words can be done with db (or dc8) and dw (or dc16). Chunks of databytes that default to 0 can be set with ds, ds8, ds16. Strings in db sections are not null terminated. For example:

db "Copyright 2010 by Michael Kohn", 0, 5, -9
dw 100, 2000, 400, 9

Files can be included with a .include "filename.h".

Comments can be done with a standard assembler ; (semicolon) or with standard C's // and /* */.

As with other assemblers, org or .org is used to tell the assembler what address a piece of code starts on.

Hex number can be written with either 0xFFD2 or 0FFD2h. Octal numbers can either start with 0 or end with q: 070 or 70q. Binary numbers can just end with a b: 11110000b.

... To be completed ...

Why?

Incase anyone cares why I wrote this, I originally started programming MSP430 with IAR on Windows. I actually don't mind IAR so much, but I don't normally use Windows boxen and writing code on them. I find to be extremely irritating. So I tried to get MSP430-gcc to compile on Linux. That ended up being a lot of work and I could never get gdb to compile. On top of that I use 64bit Linux and the MSP430.so and HIL.so files that are closed source and 32 bit. After I wrote naken430asm I did notice there is a msp430-gcc4 which is a lot better, but I still prefer a small assembler to a big thing like this.



Copyright 1997-2012 - Michael Kohn

This page was designed to work with all standard HTML compatible web browsers including Firefox, IE, Safari, and Links.