Energia, MSP432 delays and serial data corruption.

Here is my Energia MSP432 Code:

// the setup routine runs once when you press reset:
void setup() {

// the loop routine runs over and over again forever:
void loop() {
  Serial.println("Hello World");

I expected this to produce a stream of “Hello World” strings. It didn’t! I got this instead:

Hello World��
Hello World��
Hello World��
Hello World��
Hello World��
Hello World��

I discovered that if I change the delay to 501, I got an output like this:

Hello World
Hello World
Hello World
Hello World
Hello World
Hello World

How could a millisecond make such a difference? The answer lies in the way delay is implemented in the MSP432 wiring library. Sections of this library are shown below (file is wiring.c). The short answer to my problem is this:
If the delay in milliseconds is a divisible by 250 the library puts the MSP432 into a deep sleep and lets the watchdog timer wake it up 250ms later. When in such a sleep, the state of the TX pin is high impedance so it is susceptible to noise (hence the junk after Hello World).
If the delay is not divisible by 250 then the CPU is not put into a deep sleep and the TX pin is maintained at a known state.
This “feature” was found in Energia 1.6.10E18 and on a Rev 1.0 MSP432 board. It could probably be fixed by using a pull-up resistor on the TX line.
So, if you see this effect in your program just add one to your delays 🙂

void delay(uint32_t milliseconds)
    if (milliseconds == 0) {

    switch (delayMode) {
        /* using Timer_A, check for opportunity to transition to WDT */
        case 0:
            if ( (milliseconds >= 250) && (milliseconds % 250) == 0) {
                delayMode = 1;
            else {
                delayMode = 2;
        /* using WDT, check for need to transition to Timer_A */
        case 1:
            if ( (milliseconds >= 250) && (milliseconds % 250) == 0) {
                /* stay in mode 1 */
            else {
                /* switch to Timer_A and never look back */
                delayMode = 2;
        /* always using Timer_A */
        case 2:

    /* timeout is always in milliseconds so that Clock_workFunc() behaves properly */

 *  ======== switchToWatchdogTimer ========
 *  Use 250ms watchdog timer interrupt to drive the Clock tick
 *  Stop the default Timer_A then start the watchdog timer.
static void switchToWatchdogTimer()
    Clock_TimerProxy_Handle clockTimer;
    static Hwi_Handle wdtHwi = NULL;

    /* Stop Timer_A currrently being used by Clock */
    clockTimer = Clock_getTimerHandle();


    if (wdtHwi == NULL) {
        /* Create watchdog Timer Hwi */
        wdtHwi = Hwi_create(19, clockTickFxn, NULL, NULL);
        /* set WDT to use 32KHz input, 250ms period */

    /* remove DEEPSLEEP0 constraint left from TimerA usage */

    /* don't allow deeper than DEEPSLEEP1 */

    /* Start watchdog Timer */

    /* hence, Clock_tick() will be called from 250ms watchdog timer interrupt */

 *  ======== switchToTimerA ========
 *  Use 1ms Timer_A interrupt to drive the Clock tick
 *  By default, the Timer_A Hwi object has already been
 *  statically created and configured to call Clock_tick().
 *  Simply stop the watchdog timer and restart the Timer_A.
static void switchToTimerA()
    Clock_TimerProxy_Handle clockTimer;

    /* Stop watchdog Timer */

    /* remove DEEPSLEEP1 constraint left from watchdog usage */

    /* don't all the power to be cut in deep sleep */

    /* Re-start Timer_A */
    clockTimer = Clock_getTimerHandle();

    /* hence, Clock_tick() will be called from 1ms Timer_A interrupt */


MCU Invaders Part 2


This post follows up on the previous one and includes a description of the software involved in MCU Invaders.  Initially this project started out as a C++ project however I found it difficult to get the initialization “glue” sorted out.  It seems that C++ initialization procedures have changes since GCC 4.7 and I didn’t want to get diverted from the main goal figuring this out.  It was easier instead to convert to a C project while retaining the object oriented nature in spirit.

The project is made up of the following layers:



The SPI Layer
Communication with the ILI9341 display is carried out over the SPI bus.  This consists of 4 signals:
MOSI (Master Out Slave In).  This carries data from the LPC1114 to the ILI9341
MISO (Master In Slave Out).  This carries data from the ILI9341 to the LPC1114
SCK (Serial ClocK).  This is driven by the master and sets the pace for SPI Communications.
CSn or SSEL.  This line is driven by the LPC1114 and can be used to wake up the ILI9341 by driving it low. The LPC1114 is capable of generating this signal automatically when transfers are taking place.  This proved to be a problem in this case.  The ILI9341 is slow to respond to changes to this signal and this limited data rates to less than 1 Mbit/s.  This was not sufficient for the game.  I noticed however that if the SSEL (CS) line is kept low all of the time the SPI bus could be run at about 24MHz.  This of course means that the SPI bus is dedicated to this display which is not a problem in this case.

The Pixel Layer
This layer “understands” how to control the ILI9341.  It is used to initialize the display and contains the following low level graphical functions:
It also contains the macro RGBToWord which converts RGB colour values (1 byte per colour) into a 16 bit number that can be used by the ILI9341.

The Sprite Layer

typedef struct {
// Location of top left corner. 
 uint16_t x;
 uint16_t y;
// Dimensions of sprite
 uint16_t width;
 uint16_t height;
// The pixels for the sprite are stored at this location
 const uint16_t *SpriteImage;
 uint8_t visible;
} Sprite;
// Construction based on 2 D pixel array 
void initSprite(Sprite * sp,const uint16_t * InitArray, uint8_t Width, uint8_t Height, uint16_t x, uint16_t y);
// Show/hide the sprite 
void showSprite(Sprite * sp); // Put a sprite on the display
void hideSprite(Sprite * sp); // hide the sprite
void drawSprite(Sprite * sp); // show the sprite
// Move to the specified position
void moveSprite(Sprite * sp,uint16_t x, uint16_t y); // move sprite
// Does the sprite contain the given point
uint8_t withinSprite(Sprite * sp,uint16_t x, uint16_t y);

This layer is all about Sprites : Movable pictures on the display.  A sprite is defined by a data structure that specifies location, dimensions and a pointer to an array of data that contains the actual image.  The remaining functions are used to show/hide and move the sprite.  An additional function ‘withinSprite’ is used to test whether a given point is within the sprite.  This is used to detect whether missiles or invaders have hit a target.

The game layer (main)

This layer contains all of the game logic.  It also defines the shape of the various sprites used.  This layer also makes calls out to  the serial Communications layer allowing the developer to debug game play.  Dynamic memory allocation (new/malloc etc) is not used in this project.  Fixed length arrays are used for the invaders and missiles and these are enabled/disabled as necessary.  One implication of this is that there can be up to 5 missiles in flight at any one time.  There is still sufficient RAM available to increase this number should a developer wish to.

The video below shows the game in operation.

Code is available here on Github


MCU Invaders Part 1

Back in the 1980’s computers were a good deal simpler than they are now. This of course limited their capabilities but it also allowed people to fully understand what exactly was going on inside. Today, this is less easy as computers are extremely complex from a software and a hardware perspective. One area that still retains some of the innocence of the early days is embedded systems. Today’s low-end microcontrollers (MCU’s) are similar in specification and complexity to the home computers of the 1980’s. This makes them very attractive for re-inventing some of the classic programs of that time.
In a previous blog post I discussed a simple RPG called “MicroRealms”.
Here you will find a “Simon” lookalike.
This series of posts will describe a game inspired by Space Invaders and implemented on the LPC1114FN28 microcontroller. I will start with a description of the hardware platform and then later delve into the software and any problems encountered.

MCU Invaders hardware


The hardware consists of an LPC1114 MCU, a 320×240 colour LCD display, some buttons, resistors and finally a USB to serial interface to allow the MCU to be programmed.

The display is driven over the SPI bus and runs at approx 25MHz (this is beyond the official specification but it seems to work fine).  The buttons allow you move a defender left and right at the bottom of the screen and a “fire” button allow you launch missiles at the invader.

The MCU is programmed using ISP or IAP – i.e. programs are downloaded via its UART after it is reset into “ISP mode” (hold down Reset and ISP buttons, release Reset and then release ISP).  The completed circuit is shown in the picture below.

I got the display from dx.com and I note that it is now out of stock.  The original supplier ElecFreaks still has plenty of them available.

The next post will describe the code in detail bit in case you can’t wait that long you can find it over on github.