SPI¶
Registers¶
-
struct
spi_register_map
¶ This struct maps the registers of the SPI. The names of the struct members correspond to the registers from the Datasheet:
struct spi_register_map { uint32_t CS; uint32_t FIFO; uint32_t CLK; uint32_t DLEN; uint32_t LTOH; uint32_t DC; };
-
extern volatile struct spi_register_map *
SPI
¶ SPI = (volatile struct spi_register_map *)spi_base_ptr;
By using this variable, the registers of the SPI can be accessed like this
SPI->CS
.
Structs¶
-
spi_channel_config
¶ This struct is used to configure SPI:
typedef struct { union { struct { uint32_t: 2; uint32_t cpha: 1; uint32_t cpol: 1; uint32_t: 2; uint32_t cspol: 1; uint32_t: 14; uint32_t cspol0: 1; uint32_t cspol1: 1; uint32_t cspol2: 1; }; uint32_t cs_register; }; uint16_t divisor; } spi_channel_config;
-
uint32_t
cs_register
¶ This member can be directly edited by the anonymous struct inside this union. This register maps directly to the
CS
register. The settings of this register are described in the `Macros`_.
-
uint16_t
divisor
¶ The master clock divisor.
-
uint32_t
Functions¶
-
int
spi_map
(void)¶ This function maps the SPI registers. It calls
peripheral_map()
with the valuesSPI_OFFSET
andSPI_SIZE
. On error-1
is returned.
-
void
spi_unmap
(void)¶ This function unmaps the SPI registers.
-
void
spi_configure
(spi_channel_config *config)¶ This function configures SPI with a
spi_channel_config
pointed to byconfig
.
-
void
spi_set_ce
(uint8_t ce)¶ This function sets which chip enable line the SPI controller should use. This can be a 3 bit value.
-
void
spi_transfer_start
(void)¶ This function starts a SPI transfer.
-
void
spi_transfer_stop
(void)¶ This function stops the current SPI transfer.
-
uint8_t
spi_transfer_byte
(uint8_t data)¶ While there is a SPI transfer active you can call this function as often as needed by the slave, to send and receive. This function needs to be called between
spi_transfer_start()
andspi_transfer_stop()
, it sendsdata
over SPI and asynchronously receives data and returns it.
-
uint8_t
spi_send2_recv1
(uint8_t data0, uint8_t data1)¶ This function writes to bytes of data and than keeps the clock running to receive and return the third byte.
spi_transfer_start()
andspi_transfer_stop()
may not be called when using this function.
CS Register Bit Values¶
SPI_CS_CE0 |
Use chip enable 0 |
SPI_CS_CE1 |
Use chip enable 1 |
SPI_CS_CE2 |
Use chip enable 2 |
SPI_CPHA_CLK_BEGINNING |
Data on clock leading edge |
SPI_CPHA_CLK_MIDDLE |
Data on clock trailing edge |
SPI_CPOL_RESET_LOW |
Clock polarity: active low |
SPI_CPOL_RESET_HIGH |
Clock polarity: active high |
SPI_CSPOL_ACTIVE_LOW |
Chip enable: active low |
SPI_CSPOL_ACTIVE_HIGH |
Chip enable: active high |