GlobLib
HAL and API libraries for MCUs and hardware.

This module contains low level functions for the SPI bus. More...

Files

file  stm32f103cb_spi.h
 Header file for stm32f103cb SPI.
 

Macros

#define SPI_DEFAULT_SPEED   SPI_SPEED_8
 Serial clock rate.
 
#define SPI_DEFAULT_CLOCK   SPI_CLOCK_HIGH
 Clock reset state.
 
#define SPI_DEFAULT_PHASE   SPI_PHASE_SECOND
 Clock capture edge relative to clock.
 
#define SPI_DEFAULT_DATA   SPI_DATA_8
 Data frame size.
 
#define SPI_DEFAULT_ENDIAN   SPI_MSB
 Data frame order.
 

Typedefs

typedef uint16_t(* spi_u16_fp_v) (void)
 Funtion point typedef for void(*fp)(void)
 
typedef void(* spi_v_fp_u16) (uint16_t)
 Funtion point typedef for void(*fp)(uint8_t)
 

Enumerations

Functions

mcu_error SPI_setup (spi_periph periph)
 Sets up the SPI peripheral with standard setting doneted with macro defines. More...
 
mcu_error SPI_setSpeed (spi_periph periph, spi_speed speed)
 Sets the speed of the serial clock. More...
 
mcu_error SPI_setClockPolarity (spi_periph periph, spi_clock polarity)
 Sets the polarity of the clock during the idle state. More...
 
mcu_error SPI_setDataFrame (spi_periph periph, spi_data data)
 Sets the size of the data frame for one transmission. More...
 
mcu_error SPI_setEndian (spi_periph periph, spi_endian endian)
 Sets order of the transmitted data frame. More...
 
mcu_error SPI_setPhase (spi_periph periph, spi_phase phase)
 Sets the phase of the clock. More...
 
spi_u16_fp_v SPI_getGetAdd (spi_periph periph)
 Get the function address of the get function for the port. More...
 
spi_v_fp_u16 SPI_getPutAdd (spi_periph periph)
 Get the function address of the put function for the port. More...
 
mcu_error SPI_put (spi_periph periph, uint16_t data)
 Send one data frame on the data bus. (blocking) More...
 
uint16_t SPI_get (spi_periph periph)
 Recieve a byte from the recieve buffer. (blocking) More...
 
mcu_error SPI_enableTxISR (spi_periph periph, void(*handler)(void))
 Enable the peripheral's transmit interrupt. More...
 
mcu_error SPI_enableRxISR (spi_periph periph, void(*handler)(uint16_t))
 Enable the peripheral's receive interrupt. More...
 
mcu_error SPI_disableTxISR (spi_periph periph)
 Disable the peripheral's transmit interrupt. More...
 
mcu_error SPI_disableRxISR (spi_periph periph)
 Disable the peripheral's receive interrupt. More...
 
void SPI_write (spi_periph periph, gpio_port port, gpio_pin pin, spi_csPol polarity, uint16_t address, uint16_t data)
 Write a data frame to a slave device using standard protocole. More...
 
void SPI_repeatWrite (spi_periph periph, gpio_port port, gpio_pin pin, spi_csPol polarity, uint16_t address, uint16_t *data, uint16_t count)
 Write multiple data frames to a slave device using standard protocole. More...
 
uint16_t SPI_read (spi_periph periph, gpio_port port, gpio_pin pin, spi_csPol polarity, uint16_t address)
 Read a data frame from the slave device using standard protocole. More...
 
void SPI_repeatRead (spi_periph periph, gpio_port port, gpio_pin pin, spi_csPol polarity, uint16_t address, uint16_t *data, uint16_t count)
 Read multiple data frames from the slave device using standard protocole. More...
 

Detailed Description

This module contains low level functions for the SPI bus.

Author
Stuart Ianna
Version
0.1
Date
July 2018
Warning
None
Bug:
Todo:
Compilers
  • arm-none-eabi-gcc (15:4.9.3+svn231177-1) 4.9.3 20150529 (prerelease)
Example Usage

Enumeration Type Documentation

enum spi_clock

The idle state the clock line is left with no activity.

Enumerator
SPI_CLOCK_LOW 

Clock is low when idle.

SPI_CLOCK_HIGH 

Clock is high when idle.

Definition at line 87 of file stm32f103cb_spi.h.

enum spi_csPol

The polarity of the chip select pin.

This is used by SPI_write(), SPI_read(), SPI_repeatWrite() and SPI_repeatRead()

Enumerator
SPI_CS_LOW 

Chip select pin is active low.

SPI_CS_HIGH 

Chip select pin is active high.

Definition at line 129 of file stm32f103cb_spi.h.

enum spi_data

The number of data bits send in one transmission.

Enumerator
SPI_DATA_8 

One data frame is 8 bits.

SPI_DATA_16 

One data frame is 16 bits.

Definition at line 96 of file stm32f103cb_spi.h.

enum spi_endian

Byte order for send data.

Enumerator
SPI_LSB 

Data is sent LSB first.

SPI_MSB 

Data is sent MSB first.

Definition at line 106 of file stm32f103cb_spi.h.

enum spi_periph

Peripheral SPI ports available.

Enumerator
SPI_1 

The first SPI peripheral.

SPI_2 

The second SPI peripheral.

Definition at line 60 of file stm32f103cb_spi.h.

enum spi_phase

The phase at which the data is captured on the clock line.

This value is relative to the clock idle line, so SPI_CLOCK_HIGH with SPI_PHASE_FIRST will capture data on the falling edge.

Enumerator
SPI_PHASE_FIRST 

Captured on first transition.

SPI_PHASE_SECOND 

Captured on second transition.

Definition at line 118 of file stm32f103cb_spi.h.

enum spi_speed

Clock rates for the serial clock.

These rates control the clock divisor applied between the main clock and the serial clock. These rates are tried to met as accurately as possible, but will not be exceeded.

Enumerator
SPI_SPEED_025 

Maximum 250KHz serial clock.

SPI_SPEED_05 

Maximum 500KHz serial clock.

SPI_SPEED_1 

Maximum 1MHz serial clock.

SPI_SPEED_4 

Maximum 4MHz serial clock.

SPI_SPEED_8 

Maximum 8MHz serial clock.

SPI_SPEED_16 

Maximum 16MHz serial clock.

SPI_SPEED_32 

Maximum 32MHz serial clock.

Definition at line 72 of file stm32f103cb_spi.h.

Function Documentation

mcu_error SPI_disableRxISR ( spi_periph  periph)

Disable the peripheral's receive interrupt.

Parameters
periphThe port to modify
Returns
  • E_SPI_PORT if port doesn't exist.
  • E_SPI_NONE if no error.

Definition at line 198 of file stm32f103cb_spi.c.

mcu_error SPI_disableTxISR ( spi_periph  periph)

Disable the peripheral's transmit interrupt.

Parameters
periphThe port to modify
Warning
The Rx ISR flag can only be cleared by reading the data frame in the send buffer (with SPI_get()). This is not implemented in the library. Code execution will lock up if this is not done externally.
Returns
  • E_SPI_PORT if port doesn't exist.
  • E_SPI_NONE if no error.

Definition at line 159 of file stm32f103cb_spi.c.

mcu_error SPI_enableRxISR ( spi_periph  periph,
void(*)(uint16_t)  handler 
)

Enable the peripheral's receive interrupt.

The given function is called when a data frame has been received.

Warning
The Tx ISR flag can only be cleared by placing another data frame into the send buffer (with SPI_put()). This is not implemented in the library. Code execution will lock up if this is not done externally.
Parameters
periphThe port to modify
handlerThe function to call on ISR event.
Returns
  • E_SPI_PORT if port doesn't exist.
  • E_SPI_NONE if no error.

Definition at line 126 of file stm32f103cb_spi.c.

mcu_error SPI_enableTxISR ( spi_periph  periph,
void(*)(void)  handler 
)

Enable the peripheral's transmit interrupt.

The given function is called when a data frame has finished being transfered along the serial line.

Parameters
periphThe port to modify
handlerThe function to call on ISR event.
Returns
  • E_SPI_PORT if port doesn't exist.
  • E_SPI_NONE if no error.

Definition at line 94 of file stm32f103cb_spi.c.

uint16_t SPI_get ( spi_periph  periph)

Recieve a byte from the recieve buffer. (blocking)

The size of the data fram is determined by SPI_setDataFrame(). Data is not returned until a full frame is received.

Parameters
periphThe port to modify.
Returns
The data contined in the buffer.

Definition at line 577 of file stm32f103cb_spi.c.

spi_u16_fp_v SPI_getGetAdd ( spi_periph  periph)

Get the function address of the get function for the port.

Returns
Function address.

Definition at line 512 of file stm32f103cb_spi.c.

spi_v_fp_u16 SPI_getPutAdd ( spi_periph  periph)

Get the function address of the put function for the port.

Returns
Function address.

Definition at line 531 of file stm32f103cb_spi.c.

mcu_error SPI_put ( spi_periph  periph,
uint16_t  data 
)

Send one data frame on the data bus. (blocking)

The size of the data fram is determined by SPI_setDataFrame(). Data is not transmitted until the previous byte is sent.

Parameters
periphThe port to modify
dataThe data to send
Returns
  • E_SPI_PORT if port doesn't exist.
  • E_SPI_NONE if no error.

Definition at line 549 of file stm32f103cb_spi.c.

uint16_t SPI_read ( spi_periph  periph,
gpio_port  port,
gpio_pin  pin,
spi_csPol  polarity,
uint16_t  address 
)

Read a data frame from the slave device using standard protocole.

Parameters
periphThe port to read from.
portThe gpio port the chip select pin is attached to.
pinThe gpio pin on the port for chip select.
polarityThe polarity of the chip select pin given by spi_csPol.
addressThe address to read the data from.
Returns
The data send from the slave device.

Definition at line 705 of file stm32f103cb_spi.c.

void SPI_repeatRead ( spi_periph  periph,
gpio_port  port,
gpio_pin  pin,
spi_csPol  polarity,
uint16_t  address,
uint16_t *  data,
uint16_t  count 
)

Read multiple data frames from the slave device using standard protocole.

Data is returned in data

Parameters
periphThe port to read from.
portThe gpio port the chip select pin is attached to.
pinThe gpio pin on the port for chip select.
polarityThe polarity of the chip select pin given by spi_csPol.
addressThe address to read the data from.
dataPointer to the container to store the data.
countThe number of data frames to read.

Definition at line 758 of file stm32f103cb_spi.c.

void SPI_repeatWrite ( spi_periph  periph,
gpio_port  port,
gpio_pin  pin,
spi_csPol  polarity,
uint16_t  address,
uint16_t *  data,
uint16_t  count 
)

Write multiple data frames to a slave device using standard protocole.

Parameters
periphThe port to write to.
portThe gpio port the chip select pin is attached to.
pinThe gpio pin on the port for chip select.
polarityThe polarity of the chip select pin given by spi_csPol.
addressThe address to send the data to.
dataPointer to the data to write to the bus.
countThe number of data frames to send.

Definition at line 655 of file stm32f103cb_spi.c.

mcu_error SPI_setClockPolarity ( spi_periph  periph,
spi_clock  polarity 
)

Sets the polarity of the clock during the idle state.

Parameters
periphThe port to modify
polarityThe clock polarity defined by spi_clock
Returns
  • E_SPI_PORT if port doesn't exist.
  • E_SPI_NONE if no error.

Definition at line 344 of file stm32f103cb_spi.c.

mcu_error SPI_setDataFrame ( spi_periph  periph,
spi_data  data 
)

Sets the size of the data frame for one transmission.

Parameters
periphThe port to modify
dataThe frame size defined by spi_data
Returns
  • E_SPI_PORT if port doesn't exist.
  • E_SPI_NONE if no error.

Definition at line 388 of file stm32f103cb_spi.c.

mcu_error SPI_setEndian ( spi_periph  periph,
spi_endian  endian 
)

Sets order of the transmitted data frame.

Parameters
periphThe port to modify
endianThe frame order define by spi_endian
Returns
  • E_SPI_PORT if port doesn't exist.
  • E_SPI_NONE if no error.

Definition at line 431 of file stm32f103cb_spi.c.

mcu_error SPI_setPhase ( spi_periph  periph,
spi_phase  phase 
)

Sets the phase of the clock.

The works in conjuction with SPI_setClockPolarity(). Example: SPI_CLOCK_HIGH in combination with SPI_PHASE_SECOND will result on data being captured on the rising edge.

Parameters
periphThe port to modify
phaseThe phase of the clock define by spi_phase
Returns
  • E_SPI_PORT if port doesn't exist.
  • E_SPI_NONE if no error.

Definition at line 472 of file stm32f103cb_spi.c.

mcu_error SPI_setSpeed ( spi_periph  periph,
spi_speed  speed 
)

Sets the speed of the serial clock.

This function attempts to match the requested speed as close as possible, without exceeding the rate specified.

Parameters
periphThe port to modify
speedThe speed desired, defined by spi_speed
Returns
  • E_SPI_PORT if port doesn't exist.
  • E_SPI_NONE if no error.

Definition at line 262 of file stm32f103cb_spi.c.

mcu_error SPI_setup ( spi_periph  periph)

Sets up the SPI peripheral with standard setting doneted with macro defines.

Parameters
periphThe port to set up.
Returns
  • E_SPI_PORT if port doesn't exist.
  • E_SPI_NONE if no error.

Definition at line 33 of file stm32f103cb_spi.c.

void SPI_write ( spi_periph  periph,
gpio_port  port,
gpio_pin  pin,
spi_csPol  polarity,
uint16_t  address,
uint16_t  data 
)

Write a data frame to a slave device using standard protocole.

Parameters
periphThe port to write to.
portThe gpio port the chip select pin is attached to.
pinThe gpio pin on the port for chip select.
polarityThe polarity of the chip select pin given by spi_csPol.
addressThe address to send the data to.
dataThe data to write to the bus.

Definition at line 627 of file stm32f103cb_spi.c.