ruuvi.drivers.c  ${PROJECT_VERSION}
Drivers for external sensors and peripherals on embedded systems.
ruuvi_interface_spi.h File Reference

Interface for SPI operations. More...

#include "ruuvi_driver_enabled_modules.h"
#include "ruuvi_driver_error.h"
#include "ruuvi_interface_gpio.h"
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>

Go to the source code of this file.

Data Structures

struct  ri_spi_init_config_t
 

Enumerations

enum  ri_spi_mode_t { RI_SPI_MODE_0 , RI_SPI_MODE_1 , RI_SPI_MODE_2 , RI_SPI_MODE_3 }
 
enum  ri_spi_frequency_t { RI_SPI_FREQUENCY_1M , RI_SPI_FREQUENCY_2M , RI_SPI_FREQUENCY_4M , RI_SPI_FREQUENCY_8M }
 

Functions

rd_status_t ri_spi_init (const ri_spi_init_config_t *const config)
 Initialize SPI driver with given settings. More...
 
bool ri_spi_is_init ()
 check if SPI interface is already initialized. More...
 
rd_status_t ri_spi_uninit ()
 Uninitialize SPI driver. More...
 
rd_status_t ri_spi_xfer_blocking (const uint8_t *const p_tx, const size_t tx_len, uint8_t *const p_rx, const size_t rx_len)
 SPI transfer function. More...
 

Detailed Description

Interface for SPI operations.

Author
Otso Jousimaa otso@.nosp@m.ojou.nosp@m.sima..nosp@m.net
Date
2019-05-30

Definition in file ruuvi_interface_spi.h.

Enumeration Type Documentation

◆ ri_spi_frequency_t

Clock speed

Enumerator
RI_SPI_FREQUENCY_1M 

1 Mbps

RI_SPI_FREQUENCY_2M 

2 Mbps

RI_SPI_FREQUENCY_4M 

4 Mbps

RI_SPI_FREQUENCY_8M 

8 Mbps

Definition at line 43 of file ruuvi_interface_spi.h.

◆ ri_spi_mode_t

SPI modes. Defines clock polarity and phase

Enumerator
RI_SPI_MODE_0 

CPOL = 0, CPHA = 0.

RI_SPI_MODE_1 

CPOL = 0, CPHA = 1.

RI_SPI_MODE_2 

CPOL = 1, CPHA = 0.

RI_SPI_MODE_3 

CPOL = 1, CPHA = 1.

Definition at line 32 of file ruuvi_interface_spi.h.

Function Documentation

◆ ri_spi_init()

rd_status_t ri_spi_init ( const ri_spi_init_config_t *const  config)

Initialize SPI driver with given settings.

This function also handles configuring the SS and SPI GPIO pins as outputs

Parameters
configConfiguration of the SPI peripheral. Will setup given slave select pins as outputs.
Returns
RD_SUCCESS if no error occurred
RD_ERROR_INVALID_STATE if SPI was already initialized

◆ ri_spi_is_init()

bool ri_spi_is_init ( )

check if SPI interface is already initialized.

Returns
true if SPI is initialized
false otherwise

◆ ri_spi_uninit()

rd_status_t ri_spi_uninit ( )

Uninitialize SPI driver.

This function might not uninitialize the SPI GPIO pins, only the underlying peripheral. Uninitialize GPIOs explicitly if that is required.

Returns
RD_SUCCESS
Warning
Uninitializes the SPI peripheral, may or may not uninitialize the associated gpio pins.

◆ ri_spi_xfer_blocking()

rd_status_t ri_spi_xfer_blocking ( const uint8_t *const  p_tx,
const size_t  tx_len,
uint8_t *const  p_rx,
const size_t  rx_len 
)

SPI transfer function.

Full-duplex SPI. Clocks out MAX(tx_len, rx_len) bytes. It is allowed to send different length transactions, tx will clock out 0xFF if there is less bytes in TX than RX. Does not use slave select pins. RX will start at the same time as TX, i.e. one byte address + read commands will generally have {0x00, data} in rx buffer. Function is blocking and will not sleep while transaction is ongoing.

Parameters
p_txpointer to data to be sent, can be NULL if tx_len is 0.
tx_lenlength of data to be sent
p_rxpointer to data to be received, can be NULL if rx_len is 0.
rx_lenlength of data to be received
Warning
First byte in RX is generally 0x00 if you're reading external sensor.