23#include "px_spi_defs.h"
28#warning "There is a caveat with the SS pin when using the SPI in master mode! See documentation in 'px_spi.h'"
34typedef struct px_spi_per_s
84 px_spi_init_per(spi_nr, spcr, use_spi2x);
100#if PX_SPI_CFG_SPI0_EN
101 px_spi_init_per_data(PX_SPI_NR_0, &px_spi_per_0);
129#if PX_SPI_CFG_SPI0_EN
131 spi_per = &px_spi_per_0;
135 PX_LOG_E(
"Invalid peripheral specified");
139 handle->
cs_id = cs_id;
163 uint8_t mo_dummy_byte)
187#if PX_SPI_CFG_SPI0_EN
189 spi_per = &px_spi_per_0;
193 PX_LOG_E(
"Invalid peripheral specified");
197 handle->
cs_id = cs_id;
199 spcr = (0 << SPIE) | (1 << SPE) | (0 << DORD) | (1 << MSTR)
200 | (0 << CPOL) | (0 << CPHA) | (0 << SPR1) | (0 << SPR0);
215 spcr |= (((uint8_t)mode) & 3) << CPHA;
264#if PX_SPI_CFG_SPI0_EN
285 const uint8_t * data_u8 = (
const uint8_t *)data;
335 uint8_t * data_u8 = (uint8_t *)data;
377 const void * data_wr,
383 const uint8_t * data_wr_u8 = (
const uint8_t *)data_wr;
384 uint8_t * data_rd_u8 = (uint8_t *)data_rd;
408 SPDR = *data_wr_u8++;
412 *data_rd_u8++ = SPDR;
441 spcr = handle->
spcr & (~((1 << SPR1) | (1 << SPR0)));
465 uint32_t actual_bit_rate_hz;
472 actual_bit_rate_hz =
F_CPU >> 1;
476 while(actual_bit_rate_hz > baud_hz)
479 actual_bit_rate_hz >>= 1;
492 return (((uint32_t)
F_CPU) >> (baud + 1));
#define PX_SPI_CFG_USE_SPI2X
Default value for "Use double clock rate" used in px_spi_open().
#define PX_SPI_CFG_SPCR
Default value for SPI Control Register used in px_spi_open().
@ PX_SPI_DATA_ORDER_LSB
Data order is Least Significant Bit first (D0, D1, ..., D7)
@ PX_SPI_BAUD_CLK_DIV_128
F_PCLK / 128.
@ PX_SPI_BAUD_CLK_DIV_8
F_PCLK / 8.
@ PX_SPI_BAUD_CLK_DIV_4
F_PCLK / 4.
@ PX_SPI_BAUD_CLK_DIV_64
F_PCLK / 64.
@ PX_SPI_BAUD_CLK_DIV_16
F_PCLK / 16.
@ PX_SPI_BAUD_CLK_DIV_2
F_PCLK / 2.
@ PX_SPI_BAUD_CLK_DIV_32
F_PCLK / 32.
#define F_CPU
Processor frequency in Hz.
#define NULL
NULL pointer.
#define PX_WAIT_UNTIL_BIT_IS_HI(var, bit)
Wait until a bit is set.
#define PX_BIT_SET_HI(var, bit)
Set a bit (1)
#define PX_BIT_SET_LO(var, bit)
Clear a bit (0)
#define PX_LOG_NAME(name)
Macro to declare a log name string once for each file to reduce code size.
#define PX_LOG_W(format,...)
Macro to display a formatted WARNING message.
#define PX_LOG_E(format,...)
Macro to display a formatted ERROR message.
#define PX_LOG_ASSERT(expression)
Macro that will test an expression, and block indefinitely if false.
bool use_spi2x
Use 2X clock rate.
uint8_t cs_id
Chip Select GPIO ID.
uint8_t mo_dummy_byte
Master Out dummy byte when data is read from Master In.
uint8_t spcr
SPI Control Register value.
struct px_spi_per_s * spi_per
SPI peripheral.
bool px_spi_close(px_spi_handle_t *handle)
Close specified device.
#define PX_SPI_CFG_CS_HI(cs_id)
Map PX_SPI_CFG_CS_HI() macro to px_board_spi_cs_hi() function.
px_spi_mode_t
Specify SPI Clock polarity / Clock phase.
void px_spi_init(void)
Initialise SPI driver.
bool px_spi_open2(px_spi_handle_t *handle, px_spi_nr_t spi_nr, uint8_t cs_id, px_spi_baud_t baud, px_spi_mode_t mode, px_spi_dord_t data_order, uint8_t mo_dummy_byte)
Open SPI peripheral using specified parameters.
#define PX_SPI_CFG_CS_LO(cs_id)
Map PX_SPI_CFG_CS_LO() macro to px_board_spi_cs_lo() function.
px_spi_dord_t
Specify SPI Data order.
#define PX_SPI_FLAG_START
Begin SPI transaction (take SPI slave's Chip Select line low)
void px_spi_xc(px_spi_handle_t *handle, const void *data_wr, void *data_rd, size_t nr_of_bytes, uint8_t flags)
Perform an SPI exchange (write and read) transaction with an SPI slave.
void px_spi_wr(px_spi_handle_t *handle, const void *data, size_t nr_of_bytes, uint8_t flags)
Perform an SPI write transaction with an SPI slave.
void px_spi_rd(px_spi_handle_t *handle, void *data, size_t nr_of_bytes, uint8_t flags)
Perform an SPI read transaction with an SPI slave.
px_spi_nr_t
Specify SPI peripheral number.
void px_spi_change_baud(px_spi_handle_t *handle, px_spi_baud_t baud)
Change SPI peripheral baud.
uint32_t px_spi_util_clk_div_to_baud_hz(px_spi_baud_t baud)
Calculate the actual BAUD (in Hz) from the specified division.
bool px_spi_open(px_spi_handle_t *handle, px_spi_nr_t spi_nr, uint8_t cs_id)
Open SPI peripheral using predefined (default) parameters.
#define PX_SPI_FLAG_STOP
Finish SPI transaction (take SPI slave's Chip Select line high)
px_spi_baud_t
Specify SPI baud rate as a ratio of the peripheral clock.
px_spi_baud_t px_spi_util_baud_hz_to_clk_div(uint32_t baud_hz)
Calculate clock divisor that will yield closest frequency equal to or less than specified baud rate (...
Internal data for each SPI peripheral.
uint8_t open_counter
Number of open handles referencing peripheral.
px_spi_nr_t spi_nr
Peripheral number.