Category Archives: Embedded Systems Programming

Two useful links

Have been working the last weeks with a very hard to find bug and during my work I’ve stumbled upon a number of very useful web resources.

Two of the most useful ones I’d like to keep for the future:

Time.h and epoch

I recently stumbled upon a time.h implementation from a hardware/software supplier that did not have its epoch time starting at midnight, Coordinated Universal Time (UTC), 1 January 1970 as expected.

Having always believed that this was standardized as a part of the C-standard I was surprised that this part was in fact not the case. A brief scan online showed that most, but not all, time.h implementations does in fact start it “time” in 1970.

This particular hardware/software supplier had instead chosen to have its epoch start at midnight UTC-6 Jan 1, 1900.

As I was working with a trace log implementation at the time, where I had logs on different types of processors being gathered on one processor it was important for me to have each trace entry’s time stamp be correct at least down to the second, to be able to match these trace logs between the processors correctly.

Some more searching on the matter showed that RFC 868 has a part defining the difference, in seconds, between these two times, giving to hand that 2,208,988,800 seconds have passed between these dates. Now of course, this would not be too hard to calculate by hand, but it’s always nice to be able to give a good reference to magical values like this that you leave behind you in your code.

On a side note, this particular implementation would wrap somewhere around 2036. While it is unlikely that any code we deliver today would still be running in some 23 years time – it’s still not that far away. Also, choosing a different epoch that most time.h implementations out there is just begging for problems – and error reports from your customers.

The Ethernet header

Ethernet (vlan) header

Having spent quite a lot of time in the last year or so writing Ethernet driver software, I find myself looking at this picture every now and then, when trying to decipher a tcpdump file or just random traces of Ethernet packages in an error log. To save some time trying to find this picture or a similar one – I thought I might as well add it here.

Courtesy of Wikipedia.

Memory Access through a structure

One of the more common things you do as an Embedded Systems programmer is accessing registers of different peripherals.

Rather than hardcoding the address to each and every register in a peripheral, I find it convenient to typedef a C-struct for easy access of these registers. Setup the struct with a layout identical to that of your hardware registers, assign a pointer to the structure to point at the (base)address of the hardware registers in question, and voila! Access each register as a regular element of the structure for read/write operations.

typedef struct {
volatile uint8_t STATUS;
volatile uint8_t TX_REG;
volatile uint8_t RX_REG;
} hw_device_t;

hw_device_t * const ptr = (hw_device_t *) 0xFF000000;

hw_device->RX_REG = 0x10;
hw_device->TX_REF = 0x03;
uint8_t current_status = hw_device->STATUS;

Note: all elements of structure should be made volatile to avoid compiler optimizations.