SPI library

From openPicus Wiki
Jump to: navigation, search

Contents

Description

The SPIHelper library permits to manage the SPI communication module.

Write and Read functions are all MSB first. For LSB functions it is possible use this function:

BYTE reverse(BYTE data)
{
	BYTE p = data;
	BYTE a = (p&0b10000000)>>7;
	BYTE b = (p&0b01000000)>>5;
	BYTE c = (p&0b00100000)>>3;
	BYTE d = (p&0b00010000)>>1;
	BYTE e = (p&0b00001000)<<1;
	BYTE f = (p&0b00000100)<<3;
	BYTE g = (p&0b00000010)<<5;
	BYTE h = (p&0b00000001)<<7;
	return a|b|c|d|e|f|g|h;
}

Example with LSB receiver and 0x0F to send

without reverse:
SPIWriteByte(0x0F)
the receiver receives 0xF0

with reverse:
SPIWriteByte(reverse(0x0F))
the receiver receives 0x0F

Content

  • SPIHelper.c
  • SPIHelper.h

Download

SPI library

Connections

Importing into OpenPicus IDE

How To import external Lib Tutorial

Commands - SPIHelper


SPIOpen

Fuction to enable the SPI communication module
return - 0: the open operation is failed
return - 1: the SPI module is enabled

BOOL SPIOpen()


SPIClose

Function to disable the SPI communication module return - 0: the close operation is successful

BOOL SPIClose ()


SPIReadByte

Function to read a byte.
return - 0 the operation is successful
return - 1 the operation is failed. Check internal error for more details

BOOL SPIReadByte (BYTE * res)
Argument Description
BYTE * res pointer in which to store the result


SPIReadWord

Function to read a word
return - 0 the operation is successful
return - 1 the operation is failed. Check internal error for more details

BOOL SPIReadWord(unsigned int * res)
Argument Description
unsigned int * res pointer in which to store the result


SPIWriteByte

Function to write a byte
return - 0 the operation is successful
return - 1 the operation is failed. Check internal error for more details

BOOL SPIWriteByte(BYTE data)
Argument Description
BYTE data byte data to be sent


SPIWriteWord

Function to write a word
return - 0 the operation is successful
return - 1 the operation is failed. Check internal error for more details

BOOL SPIWriteWord(unsigned int data)
Argument Description
unsigned int data unsigned int data to be sent


SPIWriteReadByte

Function to perform a contemporary write and read of a BYTE.
This is used when the slave device supports full-duplex.
In this case while clocking data out, data in is considered valid and saved.
return - 0 the operation is successful
return - 1 the operation is failed. Check internal error for more details

BOOL SPIWriteReadByte(BYTE data, BYTE * res)
Argument Description
BYTE data byte data to be sent
BYTE * res pointer to store the received data


SPIWriteReadWord

Function to perform a contemporary write and read of a word.
This is used when the slave device supports full-duplex.
In this case while clocking data out, data in is considered valid and saved.
return - 0 the operation is successful
return - 1 the operation is failed. Check internal error for more details

BOOL SPIWriteReadWord(unsigned int data, unsigned int * res)
Argument Description
unsigned int data unsigned int data to be sent
unsigned int * res pointer to store the received data


SPIConfig

Function to set the SPI module
return - 0 the operation is successful
return - 1 the operation is failed. Check internal error for more details returns how many chars were successfully read

BOOL SPIConfig(SPIContext * obj, int options, int pin, long speed)
Argument Description
SPIContext * obj SPIContext pointer
int options values to set
  • SPI mode:
    • SPI_OPT_MODE16
    • SPI_OPT_MODE_0
    • SPI_OPT_MODE_1
    • SPI_OPT_MODE_2
    • SPI_OPT_MODE_3
  • master/slave mode:
    • SPI_OPT_MASTER
      • SPI_OPT_DI_SAMPLE_END
    • SPI_OPT_SLAVE
      • SPI_OPT_SLAVE_SELECT
int pin SlaveSelect pin, use SPI_OPT_NO_SS if not necessary
long speed desired connection speed. System will automatically calculate the nearest possible speed


SPIContextSave

Function to save the SPI peripheral status to a SPI Context object for further retrieval.
This can be used to manage multiple devices attached to the same SPI.
By having multiple contexts it is possible to switch from one to the other including the eventual SlaveSelect pin.
This function DOES NOT save the SS pin, this must be done with BOOL SPIConfig(SPIContext * obj, int options, int pin, long speed).

VOID SPIContextSave(SPIContext * obj)
Argument Description
SPIContext * obj SPIContext pointer


SPIContextRestore

Function to restore the SPI peripheral status to a SPI Context object.
This can be used to manage multiple devices attached to the same SPI.
By having multiple contexts it is possible to switch from one to the other including the eventual SlaveSelect pin..

VOID SPIContextRestore (SPIContext * obj)
Argument Description
SPIContext * obj SPIContext pointer


SPIStart

Function to start a SPI transaction on the given device.
This is used to set the SlaveSelect to active before clocking out data.

VOID SPIStart(SPIContext * obj)
Argument Description
SPIContext * obj SPIContext pointer


SPIStop

Function to stop a SPI transaction on the given device.
This is used to set the SlaveSelect to idle after a transaction occurred thus releasing the bus.

VOID SPIStop(SPIContext * obj)
Argument Description
SPIContext * obj SPIContext pointer


SPIGetErrorString

Function to get an extended description pertinent to the most recent SPI library error.

CHAR * SPIGetErrorString()


Related Resources

Credits

Personal tools
Namespaces

Variants
Actions
START HERE
DEVELOPMENT
HARDWARE INFO
RESOURCES
PHASED OUT
Toolbox