Skip to content
Gordon Williams edited this page Feb 23, 2017 · 2 revisions

THIS WIKI IS OUT OF DATE. check out the README.md on GitHub for docs on Espruino internals, or see www.espruino.com for searchable, up to date docs on using Espruino.

Much of the SPI implementation of Espruino is board specific. Within the jshardware.c source file which is to be implemented by each board, we will find a series of functions that have defined interfaces that must be honored.

Within the <board>.py file there is a definition entry within the chip data type called spi which defines how many hardware SPI devices the board supports. The hardware SPI interfaces are called SPI1, SPI2 and SPI3 and correspond to the spi definition being 1, 2 or 3. If 0, then none of these interfaces will be defined.

Within the file jswrap_spi_i2c.c are the definitions of the JavaScript exposed SPI functions. These can be found best documented in the Espruino reference.

A data type called struct JshSPIInfo provides a logical description of the SPI interface. It contains the following fields:

  • baudRate
  • baudRateSpec
  • pinSCK - Pin to use for clock.
  • pinMISO - Pin to use for MISO.
  • pinMOSI - Pin to use for MOSI.
  • spiMode - The value of SPI mode flags.
  • spiMSB - True if MSB first.

The SPI mode flags are:

Mode CPOL CPHA
0 0 0
1 0 1
2 1 0
3 1 1

####jshSPISetup Initialize the hardware SPI device.

void jshSPISetup(IOEventFlags device, JshSPIInfo *inf)

The identity of the device being initialized will be supplied in the device parameter. This is expected to be one of EV_SPI1, EV_SPI2 or EV_SPI3.

The inf parameter is a pointer to a struct JshSPIInfo structure that describes the nature of the hardware SPI configuration. Note that not all properties will be meaningful to all SPI devices. For example, on some boards, the SPI hardware pins are fixed so any specification of pins to be used will likely not be honored.


####jshSPISend Send data through the given SPI device (if data>=0), and return the result of the previous send (or -1). If data<0, no data is sent and the function waits for data to be returned.

int jshSPISend(IOEventFlags device, int data)

####jshSPISend16 Send 16 bit data through the given SPI device.

void jshSPISend16(IOEventFlags device, int data)

####jshSPISet16 Set whether to send 16 bits or 8 over SPI.

void jshSPISet16(IOEventFlags device, bool is16)

####jshSPISetReceive

Set whether to use the receive interrupt or not.

void jshSPISetReceive(IOEventFlags device, bool isReceive)

####jshSPIWait

Wait until SPI send has finished and flush all received data.

void jshSPIWait(IOEventFlags device)