Difference between revisions of "Component: SPI Master (Comms: Interface)"

From Flowcode Help
Jump to navigationJump to search
Line 56: Line 56:
  
 
==Examples==
 
==Examples==
''<span style="color:red;">No additional examples</span>''
+
 
 +
More information on SPI can be found here,
 +
 
 +
[http://www.matrixtsl.com/blog/simplified-communications-i%c2%b2c-and-spi/ Matrix Flowcode Blog: Simplified communications I2C and SPI]
  
  

Revision as of 10:44, 28 July 2016


Author Matrix Ltd
Version 1.3 (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


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


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

GetConsoleHandle

Gets the handle to the console allowing data displaying on the panel etc.

Parameters

This macro has no parameters


Return value

HANDLE



Property reference

SPI Channel

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

SPI peripher onboard the target microcontroller to use.

Prescale

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

Rate by which the master oscillator is reduced before clocking the SPI peripheral.

Clock Polarity

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

The polarity of the clock signal

Clock Phase

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

The phase of the clock signal related to the data signal.

Sample Point

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

Point in the clock period to sample the data signal.

MOSI (SDO)

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

Data Out Pin - Micro Out Serial In

MISO (SDI)

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

Data In Pin - Micro In Serial Out

CLK

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

Clock Pin

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

Injector component to allow the data in simulation to be passed to something meaningful.