Component: SPI Master (Comms: Interface)

From Flowcode Help
Jump to navigationJump to search


Author Matrix Ltd
Version 2.0 (Release)
Category Comms: Interface


Image 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.

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

Downloadable macro reference

SendString

Sends a string of bytes to the SPI bus

Parameters

<- STRING Str
This parameter may be returned back to the caller


Return value

This call does not return a value


GetString

Attempts to read a string of bytes from the SPI bus.

Parameters

BYTE NumBytes
BYTE Ch


Return value

STRING


SendChar

General purpose SPI send byte macro

Parameters

BYTE Char


Return value

This call does not return a value


Transaction

Allows simultaneous two way communications, send and receive,

Parameters

BYTE DataOut


Return value

BYTE


Disable_CS

Outputs a high signal to the chip select pin to deselect the remote slave device.

Parameters

This macro has no parameters


Return value

This call does not return a value


GetChar

General purpose SPI get byte macro

Parameters

This macro has no parameters


Return value

BYTE


UnInitialise

Deactivates the SPI peripheral leaving the I/O pins in a state where they can be used for general purpose I/O.

Parameters

This macro has no parameters


Return value

This call does not return a value


Enable_CS

Outputs a low signal to the chip select pin to select the remote slave device.

Parameters

This macro has no parameters


Return value

This call does not return a value


Initialise

Activates the SPI peripheral and claims control over the I/O pins.

Parameters

This macro has no parameters


Return value

This call does not return a value


Simulation macro reference

This component does not contain any simulation macros


Property reference

Channel

This property is of type Fixed list of ints and can be referenced with the variable name cal_spi::CHANNEL.

SPI Channel selector

MOSI

This property is of type Single digital pin and can be referenced with the variable name cal_spi::MOSI.

SPI Data Out Pin SDO - Also Known as Master Out Slave In (MOSI) when used in Master mode.

MISO

This property is of type Single digital pin and can be referenced with the variable name cal_spi::MISO.

SPI Data In Pin SDI - Also Known as Master In Slave Out (MISO) when used in Master mode.

CLK

This property is of type Single digital pin and can be referenced with the variable name cal_spi::CLK.

SPI Clock Pin CLK - The Clock signal is driven by the SPI master.

Use Auto CS

This property is of type True or false and can be referenced with the variable name cal_spi::UseAutoCS.

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.

CS / SS

This property is of type Single digital pin and can be referenced with the variable name cal_spi::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.

Prescale

This property is of type Fixed list of ints and can be referenced with the variable name cal_spi::PR_SCALE.

Prescale option selector

Clock Phase

This property is of type Fixed list of ints and can be referenced with the variable name cal_spi::CKE.

Clock Phase (data change edge) selection

Clock Polarity

This property is of type Fixed list of ints and can be referenced with the variable name cal_spi::CKP.

Clock Polarity setting, inactive (idle) state

Sample Point

This property is of type Fixed list of ints and can be referenced with the variable name cal_spi::SMP.

Data bit read sample point

Label

This property is of type Line of text and can be referenced with the variable name label.

Label used to identify the component on the panel.

Scope Traces

This property is of type True or false and can be referenced with the variable name cal_spi::ScopeTraces.

Selects if the scope traces are automatically generated or not

Console Data

This property is of type True or false and can be referenced with the variable name cal_spi::ConsoleData.

Selects if the console data is automatically generated or not

Injector

This property is of type Fixed list of ints and can be referenced with the variable name cal_spi::Injector.

No additional information