This coming week I’ll either do the USART or TWI. I haven’t decided which. If it goes well (no problems) I might get both finished. I finished the SPI. The SPI slave implementation was interesting. I ended up with three slave examples.
The slave examples do need a master. As I said in the last blog post, I implemented a SPI master in software. This code is identical for all three slave examples, and can be included via a conditional compilation. It sends and receives four bytes per transfer and then prints to the terminal the returned values. I wrote a separate program to use the hardware SPI of any second Arduino board. To be completely portable I used only the Arduino Library functions. If a second Arduino board is available, then it can be used with any of the slave examples, and provides a better test.
In the first slave example, it acts as two bytes in the serial shift path, so the first two bytes shifted in are passed through and the last two bytes are held and processed, to be sent out as the first bytes in the next transfer. This is basically the same slave example used in the Far Inside The Arduino book. Hardware buffering is disabled and the program suffers from the same problems the Far Inside one has because of the lack of buffering.
A second slave example turns the buffering on and is functionally the same as the first example. The problems of the first version appear to go away. However I’m not really happy with it as it seems to fail about once an hour in continuous operation. I’ll have to get back to this and either fix whatever the problem is or explain it. Making it work at all required some assumptions about the SPI hardware design, and the assumptions might be wrong.
The third example also has buffering on and appears 100% reliable. No data is passed through, so no other slave devices can be serially connected. The four received bytes are processed and the four processed bytes are sent out in the next transfer. Data coming in and data going out are handled separately. The first two examples handle the data in pairs in and out. I expect that the real fix to the second example is to do things the same way, but in that case it would make for an example that would be fairly complicated. I always feel that the best examples are simple ones with no distractions.