Unit SPI

From Ultibo.org
Jump to: navigation, search

Return to Unit Reference


Description


Ultibo SPI Interface unit

SPI (Serial Peripheral Interface) is a synchronous serial bus for communication with peripheral components.

The SPI protocol is not defined by any actual standard but some pseudo standards exist with all of the available devices. SPI is a master-slave protocol where the master asserts the chip select line (CS) to select the slave device before sending data one byte at a time. For every byte written to the bus by the master the selected slave returns a byte as well so for every write there is an equivalent read. SPI can also operate in either 4 wire (standard) or 3 wire(bidirectional) modes.

Due to the lack of formal standards and the range of devices that exist various options are provided to allow setting clock phase and polarity as well chip select polarity.

For the purpose of this interface a device is the SPI controller attached to the local system and may be either a master or a slave. Since the protocol does not include any form of enumeration or identification the interface does not attempt to represent the devices connected to the bus, any driver written to communicate with a connected SPI device should know (or allow configuration of) the chip select for the for the device and the specific message format required for that device.

Constants



[Expand]
SPI specific constants SPI_*


[Expand]
SPI device type SPI_TYPE_*


[Expand]
SPI device state SPI_STATE_*


[Expand]
SPI device flag SPI_FLAG_*


[Expand]
SPI transfer flag SPI_TRANSFER_*


[Expand]
SPI logging SPI_LOG_*


Type definitions



SPI properties

[Expand]

PSPIProperties = ^TSPIProperties;

TSPIProperties = record

SPI chip select

[Expand]

PSPIChipSelect = ^TSPIChipSelect;

TSPIChipSelect = record

SPI enumeration callback

TSPIEnumerate = function(SPI:PSPIDevice; Data:Pointer):LongWord;

SPI notification callback

TSPINotification = function(Device:PDevice; Data:Pointer; Notification:LongWord):LongWord;

SPI device start

TSPIDeviceStart = function(SPI:PSPIDevice; Mode,ClockRate,ClockPhase,ClockPolarity:LongWord):LongWord;

SPI device stop

TSPIDeviceStop = function(SPI:PSPIDevice):LongWord;

SPI device read

TSPIDeviceRead = function(SPI:PSPIDevice; ChipSelect:Word; Dest:Pointer; Size,Flags:LongWord; var Count:LongWord):LongWord;

SPI device write

TSPIDeviceWrite = function(SPI:PSPIDevice; ChipSelect:Word; Source:Pointer; Size,Flags:LongWord; var Count:LongWord):LongWord;

SPI device write read

TSPIDeviceWriteRead = function(SPI:PSPIDevice; ChipSelect:Word; Source,Dest:Pointer; Size,Flags:LongWord; var Count:LongWord):LongWord;

SPI device get mode

TSPIDeviceGetMode = function(SPI:PSPIDevice):LongWord;

SPI device set mode

TSPIDeviceSetMode = function(SPI:PSPIDevice; Mode:LongWord):LongWord;

SPI device get clock rate

TSPIDeviceGetClockRate = function(SPI:PSPIDevice; ChipSelect:Word):LongWord;

SPI device set clock rate

TSPIDeviceSetClockRate = function(SPI:PSPIDevice; ChipSelect:Word; ClockRate:LongWord):LongWord;

SPI device get clock phase

TSPIDeviceGetClockPhase = function(SPI:PSPIDevice):LongWord;

SPI device set clock phase

TSPIDeviceSetClockPhase = function(SPI:PSPIDevice; ClockPhase:LongWord):LongWord;

SPI device get clock polarity

TSPIDeviceGetClockPolarity = function(SPI:PSPIDevice):LongWord;

SPI device set clock polarity

TSPIDeviceSetClockPolarity = function(SPI:PSPIDevice; ClockPolarity:LongWord):LongWord;

SPI device get select polarity

TSPIDeviceGetSelectPolarity = function(SPI:PSPIDevice; ChipSelect:Word):LongWord;

SPI device set select polarity

TSPIDeviceSetSelectPolarity = function(SPI:PSPIDevice; ChipSelect:Word; SelectPolarity:LongWord):LongWord;

SPI device get byte delay

TSPIDeviceGetByteDelay = function(SPI:PSPIDevice):LongWord;

SPI device set byte delay

TSPIDeviceSetByteDelay = function(SPI:PSPIDevice; Delay:LongWord):LongWord;

SPI device get properties

TSPIDeviceGetProperties = function(SPI:PSPIDevice; Properties:PSPIProperties):LongWord;

SPI device

[Expand]

PSPIDevice = ^TSPIDevice;

TSPIDevice = record


Public variables



SPI logging

SPI_DEFAULT_LOG_LEVEL:LongWord = SPI_LOG_LEVEL_DEBUG; Minimum level for SPI messages. Only messages with level greater than or equal to this will be printed.
SPI_LOG_ENABLED:Boolean;


Function declarations



Initialization functions

[Expand]
procedure SPIInit;
Description: Initialize the SPI unit and SPI device table


SPI functions

[Expand]
function SPIDeviceStart(SPI:PSPIDevice; Mode,ClockRate,ClockPhase,ClockPolarity:LongWord):LongWord;
Description: Start the specified SPI device ready for writing and reading


[Expand]
function SPIDeviceStop(SPI:PSPIDevice):LongWord;
Description: Stop the specified SPI device and terminate writing and reading


[Expand]
function SPIDeviceRead(SPI:PSPIDevice; ChipSelect:Word; Dest:Pointer; Size,Flags:LongWord; var Count:LongWord):LongWord;
Description: Read data from the specified SPI device


[Expand]
function SPIDeviceWrite(SPI:PSPIDevice; ChipSelect:Word; Source:Pointer; Size,Flags:LongWord; var Count:LongWord):LongWord;
Description: Write data to the specified SPI device


[Expand]
function SPIDeviceWriteRead(SPI:PSPIDevice; ChipSelect:Word; Source,Dest:Pointer; Size,Flags:LongWord; var Count:LongWord):LongWord;
Description: Write data to and Read data from the specified SPI device in one operation


[Expand]
function SPIDeviceGetMode(SPI:PSPIDevice):LongWord;
Description: Get the device mode of the specified SPI device


[Expand]
function SPIDeviceSetMode(SPI:PSPIDevice; Mode:LongWord):LongWord;
Description: Set the device mode for the specified SPI device


[Expand]
function SPIDeviceGetClockRate(SPI:PSPIDevice; ChipSelect:Word):LongWord;
Description: Get the clock rate of the specified SPI device


[Expand]
function SPIDeviceSetClockRate(SPI:PSPIDevice; ChipSelect:Word; ClockRate:LongWord):LongWord;
Description: Set the clock rate for the specified SPI device


[Expand]
function SPIDeviceGetClockPhase(SPI:PSPIDevice):LongWord;
Description: Get the clock phase of the specified SPI device


[Expand]
function SPIDeviceSetClockPhase(SPI:PSPIDevice; ClockPhase:LongWord):LongWord;
Description: Set the clock phase for the specified SPI device


[Expand]
function SPIDeviceGetClockPolarity(SPI:PSPIDevice):LongWord;
Description: Get the clock polarity of the specified SPI device


[Expand]
function SPIDeviceSetClockPolarity(SPI:PSPIDevice; ClockPolarity:LongWord):LongWord;
Description: Set the clock polarity for the specified SPI device


[Expand]
function SPIDeviceGetSelectPolarity(SPI:PSPIDevice; ChipSelect:Word):LongWord;
Description: Get the chip select polarity of the specified SPI device


[Expand]
function SPIDeviceSetSelectPolarity(SPI:PSPIDevice; ChipSelect:Word; SelectPolarity:LongWord):LongWord;
Description: Set the chip select polarity for the specified SPI device


[Expand]
function SPIDeviceGetByteDelay(SPI:PSPIDevice):LongWord;
Description: Get the delay between bytes written for the specified SPI device


[Expand]
function SPIDeviceSetByteDelay(SPI:PSPIDevice; Delay:LongWord):LongWord;
Description: Set the delay between bytes written for the specified SPI device


[Expand]
function SPIDeviceProperties(SPI:PSPIDevice; Properties:PSPIProperties):LongWord; inline;
Description: Get the properties for the specified SPI device


[Expand]
function SPIDeviceGetProperties(SPI:PSPIDevice;Properties:PSPIProperties):LongWord;
Description: Get the properties for the specified SPI device


[Expand]
function SPIDeviceCreate:PSPIDevice;
Description: Create a new SPI entry


[Expand]
function SPIDeviceCreateEx(Size:LongWord):PSPIDevice;
Description: Create a new SPI entry


[Expand]
function SPIDeviceDestroy(SPI:PSPIDevice):LongWord;
Description: Destroy an existing SPI entry


[Expand]
function SPIDeviceRegister(SPI:PSPIDevice):LongWord;
Description: Register a new SPI in the SPI table


[Expand]
function SPIDeviceDeregister(SPI:PSPIDevice):LongWord;
Description: Deregister an SPI from the SPI table


[Expand]
function SPIDeviceFind(SPIId:LongWord):PSPIDevice;
Description: ERROR_SUCCESS if completed or another error code on failure


[Expand]
function SPIDeviceFindByName(const Name:String):PSPIDevice; inline;
Description: Find an SPI device by name in the device table


[Expand]
function SPIDeviceFindByDescription(const Description:String):PSPIDevice; inline;
Description: Find an SPI device by description in the device table


[Expand]
function SPIDeviceEnumerate(Callback:TSPIEnumerate; Data:Pointer):LongWord;
Description: Enumerate all SPI devices in the SPI table


[Expand]
function SPIDeviceNotification(SPI:PSPIDevice; Callback:TSPINotification; Data:Pointer; Notification,Flags:LongWord):LongWord;
Description: Register a notification for SPI device changes


RTL SPI functions

[Expand]
function SysSPIAvailable:Boolean; 
Description: Check if an SPI device is available


[Expand]
function SysSPIStart(Mode,ClockRate,ClockPhase,ClockPolarity:LongWord):LongWord;
Description: Start the default SPI device ready for writing and reading


[Expand]
function SysSPIStop:LongWord;
Description: Stop the default SPI device and terminate writing and reading


[Expand]
function SysSPIRead(ChipSelect:Word; Dest:Pointer; Size:LongWord; var Count:LongWord):LongWord;
Description: Read data from the default SPI device


[Expand]
function SysSPIWrite(ChipSelect:Word; Source:Pointer; Size:LongWord; var Count:LongWord):LongWord;
Description: Write data to the default SPI device


[Expand]
function SysSPIWriteRead(ChipSelect:Word; Source,Dest:Pointer; Size:LongWord; var Count:LongWord):LongWord;
Description: Write data to and Read data from the default SPI device in one operation


[Expand]
function SysSPIGetMode:LongWord;
Description: Get the device mode of the default SPI device


[Expand]
function SysSPISetMode(Mode:LongWord):LongWord;
Description: Set the device mode for the default SPI device


[Expand]
function SysSPIGetClockRate(ChipSelect:Word):LongWord;
Description: Get the clock rate of the default SPI device


[Expand]
function SysSPISetClockRate(ChipSelect:Word; ClockRate:LongWord):LongWord;
Description: Set the clock rate for the default SPI device


[Expand]
function SysSPIGetClockPhase:LongWord;
Description: Get the clock phase of the default SPI device


[Expand]
function SysSPISetClockPhase(ClockPhase:LongWord):LongWord;
Description: Set the clock phase for the default SPI device


[Expand]
function SysSPIGetClockPolarity:LongWord;
Description: Get the clock polarity of the default SPI device


[Expand]
function SysSPISetClockPolarity(ClockPolarity:LongWord):LongWord;
Description: Set the clock polarity for the default SPI device


[Expand]
function SysSPIGetSelectPolarity(ChipSelect:Word):LongWord;
Description: Get the chip select polarity of the default SPI device


[Expand]
function SysSPISetSelectPolarity(ChipSelect:Word; SelectPolarity:LongWord):LongWord;
Description: Set the chip select polarity for the default SPI device


SPI helper functions

[Expand]
function SPIGetCount:LongWord;
Description: Get the current SPI count


[Expand]
function SPIDeviceGetDefault:PSPIDevice;
Description: Get the current default SPI device


[Expand]
function SPIDeviceSetDefault(SPI:PSPIDevice):LongWord;
Description: Set the current default SPI device


[Expand]
function SPIDeviceCheck(SPI:PSPIDevice):PSPIDevice;
Description: Check if the supplied SPI is in the SPI table


[Expand]
function SPITypeToString(SPIType:LongWord):String;
Description: Convert an SPI type value to a string


[Expand]
function SPIStateToString(SPIState:LongWord):String;
Description: Convert an SPI state value to a string


[Expand]
procedure SPILog(Level:LongWord; SPI:PSPIDevice; const AText:String);
Description: To be documented


[Expand]
procedure SPILogInfo(SPI:PSPIDevice; const AText:String); inline;
Description: To be documented


[Expand]
procedure SPILogWarn(SPI:PSPIDevice; const AText:String); inline;
Description: To be documented


[Expand]
procedure SPILogError(SPI:PSPIDevice; const AText:String); inline;
Description: To be documented


[Expand]
procedure SPILogDebug(SPI:PSPIDevice; const AText:String); inline;
Description: To be documented


[Expand]
function SPIChipSelectToString(ChipSelect:Word):String;
Description: To be documented


[Expand]
function SPIModeToString(Mode:LongWord):String;
Description: To be documented


[Expand]
function SPIClockPhaseToString(Phase:LongWord):String;
Description: To be documented


[Expand]
function SPIClockPolarityToString(Polarity:LongWord):String;
Description: To be documented


[Expand]
function SPISelectPolarityToString(Polarity:LongWord):String;
Description: To be documented


Return to Unit Reference