Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Info

This section refers to the following sample application(s):

  • 02-spi
  • 02-spi_net

Serial Peripheral Interface Bus (SPI) is a digital communication bus commonly used for interconnecting chips on a printed circuit board.

An SPI bus interconnects a master with one or more slaves. Communication is triggered by the master, and is full duplex, i.e. for each byte the master sends to the slave, the slave sends one to the master, at the same time.

An SPI bus consists of the following lines:

  • MISO (Master-In-Slave-Out) is used for the slave to send data bits to the master.
  • MOSI (Master-Out-Slave-In) is used for the master to send data bits to the slave.
  • CLK (clock) is used by the master to output a clock signal to time the transmission of the data bits.
  • CS (chip select) or SS (slave select) is used by the master to select which slave it wishes to talk to. There needs to be one independent CS line from the master to each of the slave.

In a single-master-single-slave scenario, an SPI transaction goes as follows:

  • The master lowers the CS line, indicating a transaction is starting
  • The master starts outputting a clock signal (a "square wave") on the CLK line
  • At each rising edge of the CLK line:
    • The master samples the MISO line. If it's high, that's a 1, if it's low, a 0.
    • The slave samples the MOSI line. If it's high, that's a 1, if it's low, a 0.
  • The transaction goes on until the master stops sending a CLK signal, and raises the CS line.

SPI on 
Include Page
_def_chip_eterna
_def_chip_eterna

The libraries provided with the 

Include Page
_def_ocsdk
_def_ocsdk
 allow you to use the SPI in master mode.

The following logic analyzer screen shots illustrate some of the configurations. These were obtained by running the 02-spi project from the 

Include Page
_def_ocsdk
_def_ocsdk
.

The screenshot below shows the SPI master (the 

Include Page
_def_chip_eterna
_def_chip_eterna
 platform) transfer 8 bytes of data (0x0001020304050607)

  • There is no slave attached. The MISO line stays high and the master therefore always receives 0xffffffffffffffff.
  • The CLK frequency is set to the 1/16th of the CPU clock frequency. With a CPU clock at 7.4MHz, the CLK period is 2.16us.
  • Sampling is set up to happen on a rising edge.
  • The SS0n pin is used for the CS line.
  • Data is sent most-significant-bit (msb) first, i.e. 5 is encoded as b00000101.

Image Added

The following shows some of the settings of the SPI master module.

Image AddedImage Added
the SS0n pin is used as CS line the SS1n pin is used as CS line
Image AddedImage Added
most-significant-bit (msb) firstleast-significant-bit (lsb) first
Image AddedImage Added
CLK line at 1/16th the CPU speedCLK line at 1/8th the CPU speed
Image AddedImage Added
sampling on the rising edge of CLKsampling on the falling edge of CLK
Image AddedImage Added
CLK is low when not in useCLK is high when not in use

 

The 02-spi Sample Application

The 02-spi sample application shows how to use SPI. Compile and load this application on your 

Include Page
_def_board_fraser
_def_board_fraser
 board.

When you run the application, each second:

  • The following bytes are sent over the SPI bus: 0x000102030405060708
  • The device prints on the CLI what bytes have been sent and received

    No Format
    spi app, ver 1.0.0.1
    SmartMeshIP stack, ver 1.2.1.3
    SPI sent:     00 01 02 03 04 05 06 07
    SPI received: 6f 70 71 72 73 74 75 76
    SPI sent:     00 01 02 03 04 05 06 07
    SPI received: 77 78 79 7a 7b 7c 7d 7e
    SPI sent:     00 01 02 03 04 05 06 07
    SPI received: 7f 80 81 82 83 84 85 86
    SPI sent:     00 01 02 03 04 05 06 07
    SPI received: 87 88 89 8a 8b 8c 8d 8e
    SPI sent:     00 01 02 03 04 05 06 07
    SPI received: 8f 90 91 92 93 94 95 96
Info

In this setup, the

Include Page
_def_board_fraser
_def_board_fraser
board is connected to an SPI slave device returns ever-incrementing bytes.

The 02-spi_net Sample Application

The 02-spi_net sample application periodically send the 4 bytes 0x00010203 over SPI, and sends the received bytes to the 

Include Page
_def_mgr
_def_mgr
. You can configure the period between samples.

The figure below is a typical CLI output:

No Format
spi_net app, ver 1.0.0.1
SmartMeshIP stack, ver 1.2.1.3
Current config:
 - period:  10000
 11037 : Joining
 11797 : Connected
 18084 : Active
SPI sent:     00 01 02 03
SPI received: 17 18 19 1a
SPI sent:     00 01 02 03
SPI received: 1b 1c 1d 1e
SPI sent:     00 01 02 03
SPI received: 1f 20 21 22
SPI sent:     00 01 02 03
SPI received: 23 24 25 26

in this case:

  • period is 10000ms, i.e. the application sends one packet to the 
    Include Page
    _def_mgr
    _def_mgr
     every 10s.

You can connect the ApiExplorer application to your 

Include Page
_def_mgr
_def_mgr
 to see the data received:

Image Added

At any time, you can change the configuration values through CLI:

  • type period 5000 to publish every 5s.
Info

How the 02-gpio_net sample application registers to extra CLI commands is covered in the CLI module section.

Include Page
_inc_refer_to_dox
_inc_refer_to_dox