KM3NeT CLB  2.0
KM3NeT CLB v2 Embedded Software
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
spi.h File Reference

This driver wraps the functions of the OpenCores SPI master. More...

#include <stdint.h>
#include <stdbool.h>
#include "cfg_soc.h"
#include "lm32soc/dev_spi.h"
#include "errorcode.h"

Go to the source code of this file.

Data Structures

struct  SpiInit
 SPI initialization structure. More...
 

Macros

#define SPI_MAX_BYTES   16
 The maximum bytes the SPI driver can transfer at a time.
 
#define SPI_MAX_SLAVE   8
 Maximum number of slaves (0 - SPI_MAX_SLAVE - 1)
 
#define SPI_MIN_BITRATE   ( WISHBONE_FREQ / ( ( SPI_DIV_MASK + 1 ) * 2 ) )
 Minimum SPI bitrate.
 
#define SPI_MAX_BITRATE   ( WISHBONE_FREQ / 2 )
 Maximum SPI bitrate.
 
#define E_SPI_TIMEOUT   ( E_SPI + 1 )
 SPI transmission timeout.
 
#define E_SPI_TIMEOUT_DESCR   "SPI transmission timeout"
 
#define SPI_DEFAULT_INIT
 Default initialization structure. More...
 

Functions

bool spiInit (SPI_Device *dev, SpiInit *init)
 Initializes the specified SPI device with the specified parameters. More...
 
bool spiTxRx (SPI_Device *dev, uint8_t *dataIn, uint8_t *dataOut, int len)
 Transfers a specific number of bytes in a synchronous way. More...
 
void spiAsyncTx (SPI_Device *dev, uint8_t *dataIn, int length)
 Start a asynchronous SPI transmission. More...
 
bool spiASyncBusy (SPI_Device *dev)
 Returns whether or not the SPI driver is busy tranceiving data. More...
 
int spiASyncLength (SPI_Device *dev)
 Last lenght of data transmitted. More...
 
void spiASyncRx (SPI_Device *dev, uint8_t *dataOut)
 Reads the data received in the previous transmission. More...
 
void spiSelect (SPI_Device *dev, uint32_t slaveNo)
 Selects a specific slave. More...
 
void spiDeselect (SPI_Device *dev)
 Deselects all slaves. More...
 

Detailed Description

This driver wraps the functions of the OpenCores SPI master.

The driver allows for multiple SPI devices in memory, thus the specific SPI device must be provided with every call.

See Also
dev_soc.h

The following code demonstrates how to use the SPI device, using a fictive SPI slave. So its not so much what it does, but more how it does it. This should of course be translated to your specific SPI slaves' need.

// Initialize default, no auto slave select, 1 MBit.
// initialize SPI device.
if (!spiInit(SPI, &init)) {
errPrint(true);
return;
}
// ! fictive SPI slave command build up.
const int cmdSize = 33;
uint8_t bufIn[cmdSize];
uint8_t bufOut[cmdSize];
bufIn[0] = 0x32; // fictive read packet command
spiSelect(3); // select slave no. 3 (fictive device).
if (!spiTxRx(SPI, bufIn, bufOut, packetSize)) {
errPrint(true);
return;
}
printf("Success! received a packet!");

Definition in file spi.h.

Macro Definition Documentation

#define SPI_DEFAULT_INIT
Value:
{ \
.bitrate = 1000000, \
.mode = 0, \
.lsbFirst = false, \
.intEnable = false, \
.autoSlaveSelect = false \
}

Default initialization structure.

Definition at line 104 of file spi.h.

Function Documentation

bool spiASyncBusy ( SPI_Device dev)

Returns whether or not the SPI driver is busy tranceiving data.

Parameters
devThe device to query.
Return values
trueIts busy
falseIts ready.

Definition at line 132 of file spi.c.

int spiASyncLength ( SPI_Device dev)

Last lenght of data transmitted.

Can be used for asynchronous communciation.

Parameters
devThe SPIdevice.
Returns
The no of bytes which can be read.

Definition at line 137 of file spi.c.

void spiASyncRx ( SPI_Device dev,
uint8_t *  dataOut 
)

Reads the data received in the previous transmission.

Parameters
devThe device to read.
dataOutThe buffer to write into
Return values
trueAll went ok.
falseFailed, see errCode() for error.

Definition at line 143 of file spi.c.

void spiAsyncTx ( SPI_Device dev,
uint8_t *  dataIn,
int  length 
)

Start a asynchronous SPI transmission.

Parameters
devThe SPI device.
dataInThe buffer to write
lenThe length of of the data to send. May not exceed SPI_MAX_BYTES.

Definition at line 89 of file spi.c.

void spiDeselect ( SPI_Device dev)

Deselects all slaves.

Parameters
devThe WB SPI device.

Definition at line 212 of file spi.c.

bool spiInit ( SPI_Device dev,
SpiInit init 
)

Initializes the specified SPI device with the specified parameters.

Parameters
devThe SPI device.
initThe initialization structure.
Return values
trueOperation was a success.
falseOperation failed, check errCode() for the error code.

Definition at line 39 of file spi.c.

void spiSelect ( SPI_Device dev,
uint32_t  slaveNo 
)

Selects a specific slave.

Parameters
devThe WB SPI device.
slaveNoThe selected slave number.

Definition at line 204 of file spi.c.

bool spiTxRx ( SPI_Device dev,
uint8_t *  dataIn,
uint8_t *  dataOut,
int  len 
)

Transfers a specific number of bytes in a synchronous way.

Note that the underlying SPI device can at most send SPI_MAX_BYTES. When the length of the data to transmit exceeds this value, multiple transmits will be issued.

Note
function will exit after all bytes have been send and received.
if auto slave select is enabled, each SPI_MAX_BYTES the SS will be selected and deselected.
If both dataIn and dataOut are both 0 (NULL), its assumed data from address 0 is going to be written to the SPI flash. This indeed happens on a core dump.
Parameters
devThe WB device.
dataInThe data to receive, may be NULL to send dummy data.
dataOutThe data to transmit, may be NULL to not receive data.
lenThe no of bytes to transmit and receive.
Return values
trueOperation was a success.
falseOperation failed, check errCode() for the error code.

Definition at line 192 of file spi.c.