SPIC
SPI Controller driver.
The driver supports one instance of a 4-wire SPI controller, all four SPI modes and configuration of the bit order (MSB first or LSB first).
Any of the 11 GPIOs can be mapped to any of the four signals.
The Chip Select line is controlled automatically by the driver.
By setting riotee_spic_cfg_t.pin_cs to RIOTEE_SPIC_PIN_UNUSED
, this functionality is disabled, allowing manual control of the Chip Select.
Important
Always check the return code of riotee_spic_transfer(...)
to check for a potential reset/teardown during the transfer.
Example usage
1#include "riotee.h"
2#include "riotee_timing.h"
3#include "riotee_spic.h"
4#include "printf.h"
5
6uint8_t tx_data[] = {0xCA, 0xFE, 0xD0, 0x0D};
7uint8_t rx_data[sizeof(tx_data)];
8
9void lateinit(void) {
10 riotee_spic_cfg_t cfg = {.frequency = RIOTEE_SPIC_FREQUENCY_M8,
11 .mode = RIOTEE_SPIC_MODE0_CPOL0_CPHA0,
12 .order = RIOTEE_SPIC_ORDER_LSBFIRST,
13 .pin_cs = PIN_D7,
14 .pin_sck = PIN_D8,
15 .pin_copi = PIN_D10,
16 .pin_cipo = PIN_D9};
17 riotee_spic_init(&cfg);
18}
19
20int main(void) {
21 for (;;) {
22 riotee_spic_transfer(tx_data, sizeof(tx_data), rx_data, sizeof(rx_data));
23 for (int i = 0; i < 4; i++) {
24 printf("%02X", rx_data[i]);
25 }
26 printf("\r\n");
27 riotee_sleep_ms(1000);
28 }
29}
API Reference
-
enum riotee_spic_frequency_t
Values:
-
enumerator RIOTEE_SPIC_FREQUENCY_K125
125kHz Clock frequency.
-
enumerator RIOTEE_SPIC_FREQUENCY_K250
250kHz Clock frequency.
-
enumerator RIOTEE_SPIC_FREQUENCY_K500
500kHz Clock frequency.
-
enumerator RIOTEE_SPIC_FREQUENCY_M16
16MHz Clock frequency.
-
enumerator RIOTEE_SPIC_FREQUENCY_M1
1MHz Clock frequency.
-
enumerator RIOTEE_SPIC_FREQUENCY_M32
32MHz Clock frequency.
-
enumerator RIOTEE_SPIC_FREQUENCY_M2
2MHz Clock frequency.
-
enumerator RIOTEE_SPIC_FREQUENCY_M4
4MHz Clock frequency.
-
enumerator RIOTEE_SPIC_FREQUENCY_M8
8MHz Clock frequency.
-
enumerator RIOTEE_SPIC_FREQUENCY_K125
-
enum riotee_spic_mode_t
Values:
-
enumerator RIOTEE_SPIC_MODE0_CPOL0_CPHA0
-
enumerator RIOTEE_SPIC_MODE1_CPOL0_CPHA1
-
enumerator RIOTEE_SPIC_MODE2_CPOL1_CPHA0
-
enumerator RIOTEE_SPIC_MODE3_CPOL1_CPHA1
-
enumerator RIOTEE_SPIC_MODE0_CPOL0_CPHA0
-
enum riotee_spic_order_t
Values:
-
enumerator RIOTEE_SPIC_ORDER_MSBFIRST
-
enumerator RIOTEE_SPIC_ORDER_LSBFIRST
-
enumerator RIOTEE_SPIC_ORDER_MSBFIRST
-
riotee_spic_mode_t mode
SPI mode.
-
riotee_spic_frequency_t frequency
Clock frequency.
-
riotee_spic_order_t order
Bit order.
-
unsigned int pin_cs
Chip select pin.
-
unsigned int pin_sck
Clock pin.
-
unsigned int pin_copi
Controller Out Peripheral In pin.
-
unsigned int pin_cipo
Controller In Peripheral Out pin.
-
riotee_rc_t riotee_spic_init(riotee_spic_cfg_t *cfg)
Initializes the SPI peripheral. Must be called once after every reset before SPI can be used.
- Parameters:
cfg –
- Returns:
int
-
riotee_rc_t riotee_spic_transfer(uint8_t *data_tx, size_t n_tx, uint8_t *data_rx, size_t n_rx)
Transfers given number of bytes to and from the peripheral.
- Parameters:
data_tx – Pointer to TX data. Can be NULL, if n_tx=0.
n_tx – Number of bytes to transmit. Can be 0.
data_rx – Pointer to RX data buffer. Can be NULL if n_rx=0.
n_rx – Number of bytes to receive. Can be 0.
- Return values:
RIOTEE_SUCCESS – Transfer successful.
RIOTEE_ERR_RESET – Reset occured during transfer.
RIOTEE_ERR_TEARDOWN – Teardown occured during transfer.
-
void riotee_spic_def_cfg(riotee_spic_cfg_t *cfg)
Populates config struct with default configuration.
- Parameters:
cfg – Pointer to config struct that gets initialized.
-
RIOTEE_SPIC_PIN_UNUSED
-
struct riotee_spic_cfg_t