Nonblocking SPI

Implementing a fast Serial DAC write on a MBED module

The DAC used was the Microchip MCP4921 mounted on a small evaluation board. The board was compatible with the 10 way connector fitted to our existing MBED breadboard. For the purposes of the experiment pin 1 of the connector has been rerouted to pin P8 of the MBED. The aim was twofold:

  1. Successfully write values to the DAC
  2. Implement nonblocking SPI Nonblocking SPI is particularly desirable with a DAC as there is no return data from the DAC to be read, but the standard library insists on waiting for the transfer to finish.

Writing to the DAC

The issues encountered were as follows:

  • The SPI port needs configuring to 16 bits for compatibility with the DAC
  • The 12 bit word (12 bit DAC) needs a prefix binary b0011 to enable it
  • The correct expression is 0x3000|(value&0xfff) not 0x3|(value&0xfff)
  • The DAC works better when supplied with 5v
  • dirty IC pins can present a high resistance

Nonblocking SPI

For this test I had a preference for not messing around at hardware level. I have used functions in the “spi_api.h” header which is accessible but not promoted.

To attempt to maintain portability I have derived a class “mySPI” from the MBED class “SPI”. This gives me access to the private “_spi” variable which allows access to a lower level API without having to “bake in” references to specific hardware.

To allow my derived class to accept the parameters that SPI would I must declare a constructor: mySPI(PinName mosi, PinName miso, PinName sclk, PinName ssel):SPI(mosi,miso,sclk,ssel = NC) {}

This serves no purpose other than to pass the parameters on to the proper set-up code in the SPI “constructor”. The “=NC” is a default value making this parameter optional.

The added function “stuff” is able to call the lower level “spi_slave_write” function which writes data the same way “write” does but does not read it back.

The result is to leave one word of data in the “pipeline” so from now on all calls to write will return the result of the previous operation.


Please log in to post comments.