APM:Libraries
Classes | Macros | Typedefs | Enumerations | Functions | Variables
spi.h File Reference
#include "gpio_hal.h"
#include <stm32f4xx.h>
#include "dma.h"
#include "nvic.h"
Include dependency graph for spi.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  SPI_DMA
 
struct  SPI_state
 
struct  spi_dev
 

Macros

#define SPI_BIT_RXNE   ((uint16_t)0x0001)
 
#define SPI_BIT_TXE   ((uint16_t)0x0002)
 
#define SPI_BIT_CRCERR   ((uint16_t)0x0010)
 
#define SPI_BIT_MODF   ((uint16_t)0x0020)
 
#define SPI_BIT_OVR   ((uint16_t)0x0040)
 
#define SPI_BIT_BSY   ((uint16_t)0x0080)
 
#define SPI_BIT_TIFRFE   ((uint16_t)0x0100)
 
#define SPI_size_16b   ((uint16_t)0x0800)
 
#define SPI_size_8b   ((uint16_t)0x0000)
 
#define SPI_DMAreq_Tx   ((uint16_t)0x0002)
 
#define SPI_DMAreq_Rx   ((uint16_t)0x0001)
 
#define SPI_INTERRUPTS_ALL
 Mask for all spi_interrupt values. More...
 

Typedefs

typedef struct SPI_DMA Spi_DMA
 
typedef struct SPI_state spi_state
 
typedef struct spi_dev spi_dev
 
typedef enum spi_firstbit spi_firstbit
 
typedef enum spi_mode spi_mode
 SPI mode configuration. More...
 
typedef enum spi_baud_rate spi_baud_rate
 SPI baud rate configuration, as a divisor of f_PCLK, the PCLK clock frequency. More...
 
typedef enum spi_interrupt spi_interrupt
 

Enumerations

enum  spi_firstbit { LSBFIRST =0, MSBFIRST }
 
enum  spi_mode { SPI_MODE_0 =0, SPI_MODE_1, SPI_MODE_2, SPI_MODE_3 }
 SPI mode configuration. More...
 
enum  spi_baud_rate {
  SPI_BAUD_PCLK_DIV_2 = ((uint16_t)0x0000), SPI_BAUD_PCLK_DIV_4 = ((uint16_t)0x0008), SPI_BAUD_PCLK_DIV_8 = ((uint16_t)0x0010), SPI_BAUD_PCLK_DIV_16 = ((uint16_t)0x0018),
  SPI_BAUD_PCLK_DIV_32 = ((uint16_t)0x0020), SPI_BAUD_PCLK_DIV_64 = ((uint16_t)0x0028), SPI_BAUD_PCLK_DIV_128 = ((uint16_t)0x0030), SPI_BAUD_PCLK_DIV_256 = ((uint16_t)0x0038)
}
 SPI baud rate configuration, as a divisor of f_PCLK, the PCLK clock frequency. More...
 
enum  spi_interrupt {
  SPI_TXE_INTERRUPT = 1<<7, SPI_RXNE_INTERRUPT = 1<<6, SPI_ERR_INTERRUPT = 1<<5, SPI_RXNE_TXE_INTERRUPTS = SPI_RXNE_INTERRUPT | SPI_TXE_INTERRUPT,
  SPI_INTERRUPTS_ALL = SPI_TXE_INTERRUPT | SPI_RXNE_INTERRUPT | SPI_ERR_INTERRUPT
}
 

Functions

static void spi_peripheral_enable (const spi_dev *dev)
 Enable a SPI peripheral. More...
 
static void spi_peripheral_disable (const spi_dev *dev)
 Disable a SPI peripheral. More...
 
void spi_reconfigure (const spi_dev *dev, uint8_t ismaster, uint16_t baudPrescaler, uint16_t bitorder, uint8_t mode)
 
void spi_init (const spi_dev *dev)
 Initialize and reset a SPI device. More...
 
static void spi_master_enable (const spi_dev *dev, spi_baud_rate baudPrescaler, spi_mode mode, uint16_t bitorder)
 Configure GPIO bit modes for use as a SPI port's pins. More...
 
void spi_gpio_slave_cfg (const spi_dev *dev, const gpio_dev *comm_dev, uint8_t sck_bit, uint8_t miso_bit, uint8_t mosi_bit)
 
void spi_gpio_master_cfg (const spi_dev *dev, const gpio_dev *comm_dev, uint8_t sck_bit, uint8_t miso_bit, uint8_t mosi_bit)
 Configure and enable a SPI device as bus master. More...
 
void spi_slave_enable (const spi_dev *dev, spi_mode mode, uint16_t bitorder)
 Configure and enable a SPI device as a bus slave. More...
 
void spi_set_speed (const spi_dev *dev, uint16_t baudPrescaler)
 
void spi_foreach (void(*fn)(const spi_dev *))
 Call a function on each SPI port. More...
 
uint32_t spi_tx (const spi_dev *dev, const void *buf, uint16_t len)
 
int spimaster_transfer (const spi_dev *dev, const uint8_t *txbuf, uint16_t txcount, uint8_t *rxbuf, uint16_t rxcount)
 
static uint8_t spi_is_enabled (const spi_dev *dev)
 
static void spi_peripheral_disable_all (void)
 
static void spi_enable_irq (const spi_dev *dev, spi_interrupt interrupt_flags)
 
static void spi_disable_irq (const spi_dev *dev, spi_interrupt interrupt_flags)
 
static bool spi_is_irq_enabled (const spi_dev *dev, uint32_t interrupt_flags)
 
static uint16_t spi_dff (const spi_dev *dev)
 
static uint8_t spi_is_rx_nonempty (const spi_dev *dev)
 
static uint8_t spi_rx_reg (const spi_dev *dev)
 
static uint8_t spi_is_tx_empty (const spi_dev *dev)
 
static void spi_tx_reg (const spi_dev *dev, uint8_t val)
 
static uint8_t spi_is_busy (const spi_dev *dev)
 
static void spi_wait_busy (const spi_dev *dev)
 
static void spi_enable_dma_req (const spi_dev *dev, uint16_t SPI_DMAReq)
 
static void spi_disable_dma_req (const spi_dev *dev, uint16_t SPI_DMAReq)
 
static void spi_attach_interrupt (const spi_dev *dev, Handler handler)
 
static void spi_detach_interrupt (const spi_dev *dev)
 

Variables

const spi_dev *const _SPI1
 
const spi_dev *const _SPI2
 
const spi_dev *const _SPI3
 

Macro Definition Documentation

◆ SPI_BIT_BSY

#define SPI_BIT_BSY   ((uint16_t)0x0080)

Definition at line 87 of file spi.h.

Referenced by spi_is_busy(), spi_wait_busy(), and spimaster_transfer().

◆ SPI_BIT_CRCERR

#define SPI_BIT_CRCERR   ((uint16_t)0x0010)

Definition at line 84 of file spi.h.

◆ SPI_BIT_MODF

#define SPI_BIT_MODF   ((uint16_t)0x0020)

Definition at line 85 of file spi.h.

◆ SPI_BIT_OVR

#define SPI_BIT_OVR   ((uint16_t)0x0040)

Definition at line 86 of file spi.h.

◆ SPI_BIT_RXNE

#define SPI_BIT_RXNE   ((uint16_t)0x0001)

Definition at line 82 of file spi.h.

Referenced by spi_is_rx_nonempty(), and spimaster_transfer().

◆ SPI_BIT_TIFRFE

#define SPI_BIT_TIFRFE   ((uint16_t)0x0100)

Definition at line 88 of file spi.h.

◆ SPI_BIT_TXE

#define SPI_BIT_TXE   ((uint16_t)0x0002)

Definition at line 83 of file spi.h.

Referenced by spi_is_tx_empty(), spi_reconfigure(), and spimaster_transfer().

◆ SPI_DMAreq_Rx

#define SPI_DMAreq_Rx   ((uint16_t)0x0001)

◆ SPI_DMAreq_Tx

#define SPI_DMAreq_Tx   ((uint16_t)0x0002)

◆ SPI_INTERRUPTS_ALL

#define SPI_INTERRUPTS_ALL
Value:
SPI_RXNE_INTERRUPT | \
SPI_ERR_INTERRUPT)

Mask for all spi_interrupt values.

See also
spi_interrupt

Definition at line 113 of file spi.h.

◆ SPI_size_16b

#define SPI_size_16b   ((uint16_t)0x0800)

Definition at line 90 of file spi.h.

Referenced by spi_dff().

◆ SPI_size_8b

#define SPI_size_8b   ((uint16_t)0x0000)

Definition at line 91 of file spi.h.

Referenced by spi_dff().

Typedef Documentation

◆ spi_baud_rate

SPI baud rate configuration, as a divisor of f_PCLK, the PCLK clock frequency.

◆ spi_dev

typedef struct spi_dev spi_dev

SPI device type

◆ Spi_DMA

typedef struct SPI_DMA Spi_DMA

◆ spi_firstbit

typedef enum spi_firstbit spi_firstbit

◆ spi_interrupt

Available SPI interrupts - see SPI_I2S_GetITStatus

◆ spi_mode

typedef enum spi_mode spi_mode

SPI mode configuration.

Determines a combination of clock polarity (CPOL), which determines idle state of the clock line, and clock phase (CPHA), which determines which clock edge triggers data capture.

◆ spi_state

typedef struct SPI_state spi_state

Enumeration Type Documentation

◆ spi_baud_rate

SPI baud rate configuration, as a divisor of f_PCLK, the PCLK clock frequency.

Enumerator
SPI_BAUD_PCLK_DIV_2 

f_PCLK/2

SPI_BAUD_PCLK_DIV_4 

f_PCLK/4

SPI_BAUD_PCLK_DIV_8 

f_PCLK/8

SPI_BAUD_PCLK_DIV_16 

f_PCLK/16

SPI_BAUD_PCLK_DIV_32 

f_PCLK/32

SPI_BAUD_PCLK_DIV_64 

f_PCLK/64

SPI_BAUD_PCLK_DIV_128 

f_PCLK/128

SPI_BAUD_PCLK_DIV_256 

f_PCLK/256

Definition at line 70 of file spi.h.

◆ spi_firstbit

Enumerator
LSBFIRST 
MSBFIRST 

Definition at line 42 of file spi.h.

◆ spi_interrupt

Available SPI interrupts - see SPI_I2S_GetITStatus

Enumerator
SPI_TXE_INTERRUPT 

TX buffer empty interrupt

SPI_RXNE_INTERRUPT 

RX buffer not empty interrupt

SPI_ERR_INTERRUPT 
  • Error interrupt (CRC, overrun, and mode fault errors for SPI; underrun, overrun errors for I2S)
SPI_RXNE_TXE_INTERRUPTS 
SPI_INTERRUPTS_ALL 

Definition at line 97 of file spi.h.

◆ spi_mode

enum spi_mode

SPI mode configuration.

Determines a combination of clock polarity (CPOL), which determines idle state of the clock line, and clock phase (CPHA), which determines which clock edge triggers data capture.

Enumerator
SPI_MODE_0 

Clock line idles low (0), data capture on first clock transition.

SPI_MODE_1 

Clock line idles low (0), data capture on second clock transition

SPI_MODE_2 

Clock line idles high (1), data capture on first clock transition.

SPI_MODE_3 

Clock line idles high (1), data capture on second clock transition.

Definition at line 54 of file spi.h.

Function Documentation

◆ spi_attach_interrupt()

static void spi_attach_interrupt ( const spi_dev dev,
Handler  handler 
)
inlinestatic

Definition at line 295 of file spi.h.

Referenced by F4Light::SPIDevice::send_strobe(), F4Light::SPIDevice::setup_isr_transfer(), and F4Light::SPIDevice::wait_for().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ spi_detach_interrupt()

static void spi_detach_interrupt ( const spi_dev dev)
inlinestatic

Definition at line 306 of file spi.h.

Referenced by F4Light::SPIDevice::isr_transfer_finish().

Here is the caller graph for this function:

◆ spi_dff()

static uint16_t spi_dff ( const spi_dev dev)
inlinestatic

Definition at line 251 of file spi.h.

◆ spi_disable_dma_req()

static void spi_disable_dma_req ( const spi_dev dev,
uint16_t  SPI_DMAReq 
)
inlinestatic

Definition at line 289 of file spi.h.

Referenced by F4Light::SPIDevice::disable_dma().

Here is the caller graph for this function:

◆ spi_disable_irq()

static void spi_disable_irq ( const spi_dev dev,
spi_interrupt  interrupt_flags 
)
inlinestatic

Definition at line 241 of file spi.h.

Referenced by F4Light::SPIDevice::dma_isr(), F4Light::SPIDevice::get_dma_ready(), isr_handler(), F4Light::SPIDevice::isr_transfer_finish(), F4Light::SPIDevice::spi_isr(), and spi_reconfigure().

Here is the caller graph for this function:

◆ spi_enable_dma_req()

static void spi_enable_dma_req ( const spi_dev dev,
uint16_t  SPI_DMAReq 
)
inlinestatic

Definition at line 284 of file spi.h.

Referenced by F4Light::SPIDevice::start_dma_transfer().

Here is the caller graph for this function:

◆ spi_enable_irq()

static void spi_enable_irq ( const spi_dev dev,
spi_interrupt  interrupt_flags 
)
inlinestatic

Definition at line 237 of file spi.h.

Referenced by F4Light::SPIDevice::dma_isr(), F4Light::SPIDevice::do_transfer(), F4Light::SPIDevice::spi_isr(), and F4Light::SPIDevice::wait_for().

Here is the caller graph for this function:

◆ spi_foreach()

void spi_foreach ( void(*)(const spi_dev *)  fn)

Call a function on each SPI port.

Parameters
fnFunction to call.

Definition at line 84 of file spi.c.

Referenced by spi_master_enable(), and spi_peripheral_disable_all().

Here is the caller graph for this function:

◆ spi_gpio_master_cfg()

void spi_gpio_master_cfg ( const spi_dev dev,
const gpio_dev comm_dev,
uint8_t  sck_bit,
uint8_t  miso_bit,
uint8_t  mosi_bit 
)

Configure and enable a SPI device as bus master.

The device's peripheral will be disabled before being reconfigured.

Parameters
devDevice to configure as bus master
baudBus baud rate
modeSPI mode
flagsLogical OR of spi_cfg_flag values.
See also
spi_cfg_flag

Definition at line 92 of file spi.c.

Referenced by F4Light::SPIDevice::init(), and spi_master_enable().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ spi_gpio_slave_cfg()

void spi_gpio_slave_cfg ( const spi_dev dev,
const gpio_dev comm_dev,
uint8_t  sck_bit,
uint8_t  miso_bit,
uint8_t  mosi_bit 
)

Definition at line 115 of file spi.c.

Referenced by spi_master_enable().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ spi_init()

void spi_init ( const spi_dev dev)

Initialize and reset a SPI device.

Parameters
devDevice to initialize and reset.

Definition at line 57 of file spi.c.

Referenced by F4Light::SPIDevice::init(), setup(), spi_peripheral_disable(), and spi_reconfigure().

Here is the caller graph for this function:

◆ spi_is_busy()

static uint8_t spi_is_busy ( const spi_dev dev)
inlinestatic

Definition at line 271 of file spi.h.

Referenced by F4Light::SPIDevice::_transfer(), F4Light::SPIDevice::send(), and spimaster_transfer().

Here is the caller graph for this function:

◆ spi_is_enabled()

static uint8_t spi_is_enabled ( const spi_dev dev)
inlinestatic

Definition at line 227 of file spi.h.

◆ spi_is_irq_enabled()

static bool spi_is_irq_enabled ( const spi_dev dev,
uint32_t  interrupt_flags 
)
inlinestatic

Definition at line 246 of file spi.h.

Referenced by F4Light::SPIDevice::spi_isr().

Here is the caller graph for this function:

◆ spi_is_rx_nonempty()

static uint8_t spi_is_rx_nonempty ( const spi_dev dev)
inlinestatic

Definition at line 255 of file spi.h.

Referenced by F4Light::SPIDevice::_transfer(), F4Light::SPIDevice::isr_transfer_finish(), and F4Light::SPIDevice::spi_isr().

Here is the caller graph for this function:

◆ spi_is_tx_empty()

static uint8_t spi_is_tx_empty ( const spi_dev dev)
inlinestatic

Definition at line 263 of file spi.h.

Referenced by F4Light::SPIDevice::_transfer(), F4Light::SPIDevice::send(), and F4Light::SPIDevice::spi_isr().

Here is the caller graph for this function:

◆ spi_master_enable()

static void spi_master_enable ( const spi_dev dev,
spi_baud_rate  baudPrescaler,
spi_mode  mode,
uint16_t  bitorder 
)
inlinestatic

Configure GPIO bit modes for use as a SPI port's pins.

Parameters
as_masterIf true, configure bits for use as a bus master. Otherwise, configure bits for use as slave.
nss_devNSS pin's GPIO device
comm_devSCK, MISO, MOSI pins' GPIO device
nss_bitNSS pin's GPIO bit on nss_dev
sck_bitSCK pin's GPIO bit on comm_dev
miso_bitMISO pin's GPIO bit on comm_dev
mosi_bitMOSI pin's GPIO bit on comm_dev

Definition at line 163 of file spi.h.

Referenced by F4Light::SPIDevice::init().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ spi_peripheral_disable()

static void spi_peripheral_disable ( const spi_dev dev)
inlinestatic

Disable a SPI peripheral.

Parameters
devDevice to disable

Definition at line 138 of file spi.h.

Referenced by spi_peripheral_disable_all(), and spi_reconfigure().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ spi_peripheral_disable_all()

static void spi_peripheral_disable_all ( void  )
inlinestatic

Definition at line 231 of file spi.h.

Here is the call graph for this function:

◆ spi_peripheral_enable()

static void spi_peripheral_enable ( const spi_dev dev)
inlinestatic

Enable a SPI peripheral.

Parameters
devDevice to enable

Definition at line 128 of file spi.h.

◆ spi_reconfigure()

void spi_reconfigure ( const spi_dev dev,
uint8_t  ismaster,
uint16_t  baudPrescaler,
uint16_t  bitorder,
uint8_t  mode 
)

Definition at line 132 of file spi.c.

Referenced by spi_master_enable(), and spi_peripheral_disable().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ spi_rx_reg()

static uint8_t spi_rx_reg ( const spi_dev dev)
inlinestatic

Definition at line 259 of file spi.h.

Referenced by F4Light::SPIDevice::isr_transfer_finish().

Here is the caller graph for this function:

◆ spi_set_speed()

void spi_set_speed ( const spi_dev dev,
uint16_t  baudPrescaler 
)

Definition at line 205 of file spi.c.

Referenced by F4Light::SPIDevice::apply_speed(), spi_master_enable(), F4Light::SPIDevice::transfer(), and F4Light::SPIDevice::transfer_fullduplex().

Here is the caller graph for this function:

◆ spi_slave_enable()

void spi_slave_enable ( const spi_dev dev,
spi_mode  mode,
uint16_t  bitorder 
)

Configure and enable a SPI device as a bus slave.

The device's peripheral will be disabled before being reconfigured.

Parameters
devDevice to configure as a bus slave
modeSPI mode
flagsLogical OR of spi_cfg_flag values.
See also
spi_cfg_flag

Referenced by spi_master_enable().

Here is the caller graph for this function:

◆ spi_tx()

uint32_t spi_tx ( const spi_dev dev,
const void *  buf,
uint16_t  len 
)

Referenced by spi_master_enable().

Here is the caller graph for this function:

◆ spi_tx_reg()

static void spi_tx_reg ( const spi_dev dev,
uint8_t  val 
)
inlinestatic

Definition at line 267 of file spi.h.

Referenced by F4Light::SPIDevice::send().

Here is the caller graph for this function:

◆ spi_wait_busy()

static void spi_wait_busy ( const spi_dev dev)
inlinestatic

Definition at line 275 of file spi.h.

Referenced by F4Light::SPIDevice::_cs_release(), F4Light::SPIDevice::dma_isr(), F4Light::SPIDevice::isr_transfer_finish(), F4Light::SPIDevice::spi_isr(), and F4Light::SPIDevice::wait_busy().

Here is the caller graph for this function:

◆ spimaster_transfer()

int spimaster_transfer ( const spi_dev dev,
const uint8_t *  txbuf,
uint16_t  txcount,
uint8_t *  rxbuf,
uint16_t  rxcount 
)

Definition at line 219 of file spi.c.

Referenced by spi_master_enable(), and F4Light::SPIDevice::transfer().

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ _SPI1

const spi_dev* const _SPI1

SPI device 1

Definition at line 32 of file spi.c.

◆ _SPI2

const spi_dev* const _SPI2

SPI device 2

Definition at line 43 of file spi.c.

◆ _SPI3

const spi_dev* const _SPI3

SPI device 3

Definition at line 54 of file spi.c.