Driving an SPI LED module with the OSHChip.
How easy is SPI with the OSHChip? I thought I'd find out by first controlling a module that has a very basic SPI-ish slave interface.
SPI Driver Options
It seems there's at least three options:
- use the SPI class that is part of the mbed standard library
- go direct to registers for SPI control
- pick one of the other SPI libraries out there, for example SPI_Demo_Nano that implements an SPI class very similar to the Arduino SPI library.
I'm going to code this one with the mbed online compiler, so I'll start with the first option.
Configuring and Using SPI
It sets SPI for an 8 bit data frame, spi mode 0, 1MHz clock rate:
// Setup spi: 8 bit data frame, spi mode 0, 1MHz clock rate spi.format(8, 0); spi.frequency(1000000);
And 16-bits worth of data (for the two shift registers) is sent in two write operations:
led_module_cs = 0; spi.write(data); spi.write(data >> 8); led_module_cs = 1;
Two quirks that I still don't fully understand:
- I thought IO should be able to set a 16-bit frame with
format(16, 0)but I couldn't get it to work.
- even though SPI.write takes an "int", it still only sends the 8 lower bits, hence the transfer in two calls
Also note I haven't resorted to bulk transfers using the
transfer method in this case,
since I'm not sending large quantities of data, and not expecting any reply.
There's not much to the build. Basically hook together three modules: