Nut/OS  4.10.3
API Reference
spibus.h File Reference

SPI bus declarations. More...

#include <dev/irqreg.h>
#include <sys/device.h>
#include <sys/event.h>
Include dependency graph for spibus.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  _NUTSPIBUS
 SPI bus structure. More...
struct  _NUTSPINODE
 SPI node structure. More...

Defines

#define SPI_CURRENT_MODE   ((uint16_t)-1)
#define SPI_MODE_CPHA   0x0001
#define SPI_MODE_CPOL   0x0002
#define SPI_MODE_FAULT   0x0004
#define SPI_MODE_LOOPBACK   0x0400
#define SPI_MODE_SLAVE   0x0800
#define SPI_MODE_CSKEEP   0x1000
#define SPI_MODE_CSDEC   0x2000
#define SPI_MODE_CSHIGH   0x4000
#define SPI_MODE_UPDATE   0x8000
#define SPI_MODE_0   0x0000
#define SPI_MODE_1   SPI_MODE_CPHA
#define SPI_MODE_2   SPI_MODE_CPOL
#define SPI_MODE_3   (SPI_MODE_CPOL | SPI_MODE_CPHA)
#define SPI_CURRENT_RATE   ((uint32_t)-1)
#define SPI_CURRENT_BITS   ((uint8_t)-1)

Typedefs

typedef struct _NUTSPIBUS NUTSPIBUS
typedef struct _NUTSPINODE NUTSPINODE

Functions

int NutRegisterSpiDevice (NUTDEVICE *dev, NUTSPIBUS *bus, int cs)
 Register and initialize an SPI device attached to a specified bus.
uint_fast16_t NutSpiBusSetMode (NUTSPINODE *node, uint_fast16_t mode)
 Set SPI mode of a specified bus device.
uint_fast32_t NutSpiBusSetRate (NUTSPINODE *node, uint_fast32_t rate)
 Set clock rate of a specified SPI device.
uint_fast8_t NutSpiBusSetBits (NUTSPINODE *node, uint_fast8_t bits)
 Set clock rate of a specified SPI device.
int NutSpiBusWait (NUTSPINODE *node, uint32_t tmo)
 Wait until all SPI bus transfers are done.

Detailed Description

SPI bus declarations.

 * $Id: spibus.h 3653 2011-11-14 15:20:31Z haraldkipp $
 * 

Definition in file spibus.h.


Define Documentation

#define SPI_CURRENT_MODE   ((uint16_t)-1)

Definition at line 50 of file spibus.h.

Referenced by NutSpiBusSetMode().

#define SPI_MODE_CPHA   0x0001

Definition at line 52 of file spibus.h.

Referenced by At91SpiSetup(), and Avr32SpiSetup().

#define SPI_MODE_CPOL   0x0002

Definition at line 53 of file spibus.h.

Referenced by At91SpiSetup(), Avr32SpiSetup(), and GpioSpiBus0Select().

#define SPI_MODE_FAULT   0x0004

Definition at line 54 of file spibus.h.

Referenced by At91SpiSetup(), and Avr32SpiSetup().

#define SPI_MODE_LOOPBACK   0x0400

Definition at line 55 of file spibus.h.

Referenced by At91SpiSetup(), and Avr32SpiSetup().

#define SPI_MODE_SLAVE   0x0800

Definition at line 56 of file spibus.h.

#define SPI_MODE_CSKEEP   0x1000

Definition at line 57 of file spibus.h.

Referenced by At91SpiSetup(), and Avr32SpiSetup().

#define SPI_MODE_CSDEC   0x2000

Definition at line 58 of file spibus.h.

#define SPI_MODE_0   0x0000

Definition at line 62 of file spibus.h.

Referenced by GpioSpiBus0Transfer().

#define SPI_MODE_1   SPI_MODE_CPHA

Definition at line 63 of file spibus.h.

Referenced by GpioSpiBus0Transfer().

#define SPI_MODE_2   SPI_MODE_CPOL

Definition at line 64 of file spibus.h.

Referenced by GpioSpiBus0Transfer().

#define SPI_MODE_3   (SPI_MODE_CPOL | SPI_MODE_CPHA)

Definition at line 65 of file spibus.h.

Referenced by AvrSpiSetup(), and GpioSpiBus0Transfer().

#define SPI_CURRENT_RATE   ((uint32_t)-1)

Definition at line 68 of file spibus.h.

Referenced by NutSpiBusSetRate().

#define SPI_CURRENT_BITS   ((uint8_t)-1)

Definition at line 70 of file spibus.h.

Referenced by NutSpiBusSetBits().


Typedef Documentation

typedef struct _NUTSPIBUS NUTSPIBUS

Definition at line 73 of file spibus.h.

typedef struct _NUTSPINODE NUTSPINODE

Definition at line 74 of file spibus.h.


Function Documentation

int NutRegisterSpiDevice ( NUTDEVICE dev,
NUTSPIBUS bus,
int  cs 
)

Register and initialize an SPI device attached to a specified bus.

Calls the bus controller initialization and, if successful, initializes the SPI device and adds it to the system device list.

Applications should call this function during initialization for each SPI device they intend to use.

Parameters:
devPointer to the NUTDEVICE structure, which is provided by the device driver.
busPointer to the NUTSPIBUS structure, which is provided by the bus controller driver.
csZero based chip select number for this device. Must be set to 0, if only one device is attached to the bus and no chip select line is provided.
Returns:
0 if the device has been registered for the first time and the initialization was successful. The function returns -1, if any device with the same name had been registered previously, if the NUTDEVICE structure or the chip select is invalid or if the device or bus controller initialization failed.

Definition at line 72 of file spibus.c.

References _NUTSPIBUS::bus_initnode, _NUTSPIBUS::bus_mutex, _NUTDEVICE::dev_icb, _NUTSPINODE::node_bus, _NUTSPINODE::node_cs, NUTASSERT, NutEventPost(), and NutRegisterDevice().

Here is the call graph for this function:

uint_fast16_t NutSpiBusSetMode ( NUTSPINODE node,
uint_fast16_t  mode 
)

Set SPI mode of a specified bus device.

The new mode will be used during the next transfer. If its value is SPI_CURRENT_MODE, then the mode is not updated. This can be used to query the current mode.

Otherwise this parameter may be the or'ed combination of the following bits:

  • SPI_MODE_CPHA: Data updated on leading edge.
  • SPI_MODE_CPOL: Idle clock is high.
  • SPI_MODE_FAULT: Enables mode fault detection.
  • SPI_MODE_LOOPBACK: Loopback mode.
  • SPI_MODE_SLAVE: Slave mode.
  • SPI_MODE_CSKEEP: Chip select remains active after transfer.
  • SPI_MODE_CSDEC: Decoded chip selects.
  • SPI_MODE_CSHIGH: Chip select is high active.

Be aware, that SPI drivers may have implemented a subset only.

Instead of SPI_MODE_CPHA and SPI_MODE_CPOL one of the following mode numbers may be used:

  • SPI_MODE_0: Idle clock is low and data is captured on the rising edge.
  • SPI_MODE_1: Idle clock is low and data is captured on the falling edge.
  • SPI_MODE_2: Idle clock is high and data is captured on the falling edge.
  • SPI_MODE_3: Idle clock is high and data is captured on the rising edge.

The return value may additionally contain the bit SPI_MODE_UPDATE, which indicates that here had been no transfer since the last mode update.

Parameters:
nodeSpecifies the SPI bus node.
modeNew mode.
Returns:
Old mode.

Definition at line 154 of file spibus.c.

References _NUTSPINODE::node_mode, SPI_CURRENT_MODE, and SPI_MODE_UPDATE.

uint_fast32_t NutSpiBusSetRate ( NUTSPINODE node,
uint_fast32_t  rate 
)

Set clock rate of a specified SPI device.

The new clock rate will be used for the next transfer. If the given rate is beyond the capabilities of the bus controller, it will automatically adjusted before the transfer starts.

Parameters:
nodeSpecifies the SPI bus node.
rateNew clock rate, given in bits per second. If the value is SPI_CURRENT_RATE, then the current rate is kept.
Returns:
Previous rate.

Definition at line 177 of file spibus.c.

References _NUTSPINODE::node_mode, _NUTSPINODE::node_rate, SPI_CURRENT_RATE, and SPI_MODE_UPDATE.

uint_fast8_t NutSpiBusSetBits ( NUTSPINODE node,
uint_fast8_t  bits 
)

Set clock rate of a specified SPI device.

The new clock rate will be used for the next transfer. If the given rate is beyond the capabilities of the bus controller, it will automatically adjusted before the transfer starts.

Parameters:
nodeSpecifies the SPI bus node.
rateNew clock rate, given in bits per second. If the value is SPI_CURRENT_RATE, then the current rate is kept.
Returns:
Previous rate.

Definition at line 201 of file spibus.c.

References _NUTSPINODE::node_bits, _NUTSPINODE::node_mode, SPI_CURRENT_BITS, and SPI_MODE_UPDATE.

int NutSpiBusWait ( NUTSPINODE node,
uint32_t  tmo 
)

Wait until all SPI bus transfers are done.

This dummy function is used by SPI device drivers, which do not provide asynchronous transfers.

Parameters:
nodeSpecifies the SPI bus node.
tmoTimeout in milliseconds. To disable timeout, set this parameter to NUT_WAIT_INFINITE.
Returns:
Always 0.

Definition at line 111 of file spibus.c.