Component: SPI Master (Comms: Interface)

From Flowcode Help
Jump to navigationJump to search
Author Matrix Ltd
Version 2.1
Category Comms: Interface


SPI Master component

Low level routines for controlling or interacting with an SPI interface. SPI or Serial Peripheral Interface is a bus used for board level communications between devices. A target microcontroller will usually have at least one hardware SPI peripheral built in. If the hardware SPI pins are in use or more SPI channels are required then there is also a software mode available.

Component Source Code

Please click here to download the component source project: FC_Comp_Source_SPI_Master.fcfx

Please click here to view the component source code (Beta): FC_Comp_Source_SPI_Master.fcfx

Detailed description

Overview

The SPI bus is a high speed communications bus which is usually best suited for talking between devices situated on the same circuit board. Due to the high frequency digital nature of the bus care should be taken to keep tracks as short as possible and as far away as possible from other sources of noise. A typical SPI bus consists of four signals, chip select, data out, data in and clock. The SPI bus usually consists of a single master device and then one or more slave devices.

As the data out and data in signals are subjective in terms of who is sending and receiving the data there are alternate names to help clarify the connection.

Data out (SDO) is also known as MOSI which stands for Master Out Slave In.

Data in (SDI) is also known as MISO which stands for Master In Slave Out.

Each SPI transaction consists of a byte made up of 8 clock cycles allowing the 8-bits of the byte to be transferred.

SPI Bytes.jpg


Chip Select CS / Slave Select SS

Each device connected to the SPI bus connects in parallel to the main three SPI pins: data out, data in and clock. The fourth pin the CS pin must be unique for every SPI device present on the bus allowing only one device to be addressed at any one time. As every slave device on the SPI bus requires a individual CS signal from the SPI Master a good way of adding more devices without consuming more of the pins of the SPI master is to use things like 3 to 8 pin multiplexer ICs. The CS signal activates the slave device when pulled low and disables the slave device when pulled high.

Adding a pull up resistor of around 10K - 100K between the CS signal and VCC allows the SPI slave device to be disabled whenever the Microcontroller is not ready to perform the functionality of the SPI master e.g. during reprogramming or when the device is held in reset.


Three Wire SPI

Three wire SPI is a version of SPI where the master can send data to the slave device but cannot read any data back for example a DAC or graphical LCD.


Four Wire SPI

Four wire SPI is the complete version of SPI where the master can sent data to the device and receive data back from the device. A single SPI operation simultaneously transfers a byte from the master to the slave via the MOSI signal and also a byte from the slave to the master via the MISO signal. Normally only one of these bytes is meaningful so it's down to the protocol of the slave device as to how to perform writes and reads.

Examples

More information on SPI can be found here,

Matrix Flowcode Blog: Simplified communications I2C and SPI







Macro reference

Disable_CS

Fc9-comp-macro.png Disable_CS
Outputs a high signal to the chip select pin to deselect the remote slave device. 
Fc9-void-icon.png - VOID Return


Enable_CS

Fc9-comp-macro.png Enable_CS
Outputs a low signal to the chip select pin to select the remote slave device. 
Fc9-void-icon.png - VOID Return


GetByteArray

Fc9-comp-macro.png GetByteArray
Attempts to read an array of bytes from the SPI bus. Similar to the GetString component macro but allows the value 0. 
Fc9-u16-icon.png - UINT NumBytes
Number of bytes to read 
Fc9-u8-icon.png - BYTE Data
Byte Array to Assign Data to. 
Fc9-void-icon.png - VOID Return


GetChar

Fc9-comp-macro.png GetChar
General purpose SPI get byte macro 
Fc9-u8-icon.png - BYTE Return


GetString

Fc9-comp-macro.png GetString
Attempts to read a string of bytes from the SPI bus. Not compatible with NULL values. String variable needs to be at least (NumBytes + 1) in length to store NULL terminator. 
Fc9-u8-icon.png - BYTE NumBytes
Number of bytes to read into the string, string array length must be at least +1 
Fc9-string-icon.png - STRING Return


Initialise

Fc9-comp-macro.png Initialise
Activates the SPI peripheral and claims control over the I/O pins. 
Fc9-void-icon.png - VOID Return


SendByteArray

Fc9-comp-macro.png SendByteArray
Sends a an array of bytes to the SPI bus. Similar to the SendString component macro but allows the value 0. 
Fc9-u16-icon.png - UINT NumBytes
Number of bytes to write 
Fc9-u8-icon.png - BYTE Data
Byte Array to Send Data from. 
Fc9-void-icon.png - VOID Return


SendChar

Fc9-comp-macro.png SendChar
General purpose SPI send byte macro 
Fc9-u8-icon.png - BYTE Char
Data byte to send 
Fc9-void-icon.png - VOID Return


SendString

Fc9-comp-macro.png SendString
Sends a string of bytes to the SPI bus. Not compatible with NULL values. 
Fc9-string-icon.png - STRING Str
Data string to send out. 
Fc9-void-icon.png - VOID Return


Transaction

Fc9-comp-macro.png Transaction
Allows simultaneous two way communications, send and receive, 
Fc9-u8-icon.png - BYTE DataOut
Data Byte to send out 
Fc9-u8-icon.png - BYTE Return


TransactionArray

Fc9-comp-macro.png TransactionArray
Allows simultaneous two way communications, send and receive multiple times. DataOut is used to store data to be transmitted. DataIn is used to store received data. 
Fc9-u8-icon.png - BYTE DataOut
Data Bytes to send out 
Fc9-u8-icon.png - BYTE DataIn
Data Bytes received 
Fc9-u8-icon.png - BYTE NumBytes
Number of bytes to send and receive 
Fc9-void-icon.png - VOID Return


UnInitialise

Fc9-comp-macro.png UnInitialise
Deactivates the SPI peripheral leaving the I/O pins in a state where they can be used for general purpose I/O. 
Fc9-void-icon.png - VOID Return


Property reference

Fc9-prop-icon.png Properties
Fc9-conn-icon.png Connections
Fc9-type-16-icon.png Channel
SPI Channel selector 
Fc9-type-5-icon.png MOSI
SPI Data Out Pin SDO - Also Known as Master Out Slave In (MOSI) when used in Master mode. 
Fc9-type-5-icon.png MISO
SPI Data In Pin SDI - Also Known as Master In Slave Out (MISO) when used in Master mode. 
Fc9-type-5-icon.png CLK
SPI Clock Pin CLK - The Clock signal is driven by the SPI master. 
Fc9-type-7-icon.png Use Auto CS
Allow the SPI component to manage the CS pin for us in Master mode. On initialise the CS pin is automatically set high to disable the remote SPI device.  
Fc9-type-5-icon.png CS / SS
Chip Select / Slave Select Pin Master Mode: General purpose output pin used to select the remote SPI device. Slave Mode: Hardware chip select pin input used to select the SPI device.  
Fc9-conn-icon.png Bus Settings
Fc9-type-16-icon.png Prescale
Prescale option selector 
Fc9-type-16-icon.png Clock Phase
Clock Phase (data change edge) selection 
Fc9-type-16-icon.png Clock Polarity
Clock Polarity setting, inactive (idle) state 
Fc9-type-16-icon.png Sample Point
Data bit read sample point 
Fc9-conn-icon.png Simulation
Fc9-type-10-icon.png Label
Label used to identify the component on the panel. 
Fc9-type-7-icon.png Scope Traces
Selects if the scope traces are automatically generated or not 
Fc9-type-7-icon.png Console Data
Selects if the console data is automatically generated or not 
Fc9-type-16-icon.png API