March 30, 2006

Unit Testing Embedded System Registers

How do you unit test embedded software that accesses hardware registers? For example, here is the code that runs on 68HC12 microprocessor:
unsigned char read_sensor(void)
{
    unsigned char sensor_value;
    
    SPI0CR1 = 0x50; // Enable SPI, master mode, CPOL = 0, CPHA = 0
    SPI0BR = 0x22;  // 1.024 MHz baud rate
    SPI0DR = 0; // write the "get flow" command to shift the data out of the DSP
    
    while( (SPI0SR & 0x80) == 0 ) {};
    sensor_value = SPI0DR; 
    
    return sensor_value;
}
The obvious problem is that the register SPI0DR is set to zero before being read, so we can NOT unit test it by running:
    unsigned char SPI0DR; // Simulate the register by a global variable
    ....
    SPI0DR=77;
    assert_equal(77, read_sensor(), "reading sensor");
For testing purposes, we need to set the register before reading, and read the register after writing. In order to splits reads and writes into separate variable, we use this definition:
extern unsigned char REG_read;
extern unsigned char REG_write;
 
#define REG          REG_read ; REG_write
 
//
//      REG = 7;      // resolves to:
//                    // REG_read ; REG_write = 7; 
//                    // ------- the first half is ignored
//
//      blood_flow = REG; // resolves to:
//                    // sensor_value = REG_read ; REG_write;
//                    // the 2nd half is ignored   ---------
//
Now we can test that correct register is read:
    SPI0DR_read=77;
    assert_equal(77, read_sensor(), "reading sensor");
Posted by laza at 12:21 PM | Comments (0) | TrackBack

March 27, 2006

Skiing Utah 2006

Our group of 8 skiers (Ziko, Maja, Daniel, Kati, Mladen, Keti, Saska, Laza) had great weather conditions and awsome skiing and enjoyment for 9 days. We stayed again in Salt Lake City. Skiing Alta was great as last year. For the first time we skied Solitude, Snowbird and Snowbasin. Snowbird and Snowbasin were so great so as to warrant returning to ski these mountains for an additional day. The best memories will be those of Snowbasin, pounded by fresh snow.

Anyway, here are the pictures:
http://lazax.com/photos/2006-02%20Ski%20Utah/

Posted by laza at 06:58 PM | Comments (0) | TrackBack