Sunday, November 27, 2016

/* Read the angle register */angle = spi_write(A_READ | A_PARITY_1 | A_ANGLECOMREG);
Now, you don't have to understand exactly what's going on here (and the actual process of reading an angle from the AS5147 rotary position sensor is a little bit more complicated), but I am trying to make a point.

I would argue that the second code snippet is a waste of time. (Some presumptions here: You had to define A_READ, A_PARITY_1 and A_ANGLECOMREG).

But, why would I advocate the hard coded version?

The chip (AS5147) isn't going to change SPI command structure anytime during its production cycle.

If you choose a new chip, you are going to have to study it and figure out the hard coded values anyway.

The abstraction in the 2nd code snippet is going to only work for this exact chip.

You have to work out the bit twiddling anyway (to come up with A_READBIT, etc)

0xFFFF is what you will see in your debugger

It is *always* 0xFFFF

Ease of modification doesn't work here. If you go "oh, that should be A_PARITY_0" and change it (without verifying the actual resulting write value, it is going to cause a lot of debugging woes).

This is sort of an argument between "ease of writing" vs "ease of debugging" and in embedded development, "ease of debugging" is what you are really shooting for (or maybe call it "ease of validation").

I see the same thing happening with STM32's HAL or StdPeriph. There is a focus on making it easier to load up registers (often taking 1 line of register loading code and stretching it into multiple lines of structure loading using verbose enums/types you have to look up).

To be honest, when I work in Forth on the STM32, I don't have such luxuries and my program is full of well documented hex and binary register banging.

Monday, November 14, 2016

This is a painting on display at The Walters Museum in Baltimore Maryland. (http://art.thewalters.org/detail/4911/the-attack-at-dawn/). It is by "Attack At Dawn" by Alphonse de Neuville. I am not particularly a fan of military battle scenes, but there is something about this painting that I can't get the image out of my head. This photo does it no justice. The somber, winter setting, the lighting, the mastery of painting techniques. It is not a famous painting, but it does so many things that resonate with me: The distant foggy morning sky, the snow, the dim light sources (e.g. dawn, the lamp post, the doorway on the left, the rifle fire), the sparse brush strokes, the sense of desolation.Go see it, if you can.

I first discovered Literate Programming via looking at TeX sources in the 1980s. Then I became somewhat obsessed with the approach around 1992 with the publication of Literate Programming by Knuth. This is still one of my favorite books on programming.

I still like to thumb through TeX: The Program (likely the largest Literate Program ever written).

Perhaps I am starting to revisit Literate Programming because of my full-time-deep-dive into embedded programming (again). Still stuck with C and code that is supposed to work (I deal more with "firmware" than software these days -- code that can't just be frequently updated, is hard to unit test and must be very, very reliable).

Literate Programming doesn't fit well in today's rapid development, big team, fastest-time-to-market culture. But, when I am writing control systems with little instructional software/literature available (e.g. learning how to spin a BLDC motor using sinusoidal waves), I start thinking about approaching it in a literate style.

Shhh... don't tell my partners, but I am definitely thinking about using cweb or perhaps cwebx for this ;)

Somewhere, in a box, at my last job, is an "over the air" (GSM/SMS) remote control device that I developed using the Propeller chip and Tachyon Forth. It was a prototype I put together in less than 2 months and "field tested". It was an interesting (and fun) experience. I would have love to have used this logic analyzer during development.