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 voltix_spic_cfg_t.pin_cs to VOLTIX_SPIC_PIN_UNUSED, this functionality is disabled, allowing manual control of the Chip Select.
Important
Always check the return code of voltix_spic_transfer(...) to check for a potential reset/teardown during the transfer.
Example usage
1#include "voltix.h"
2#include "voltix_timing.h"
3#include "voltix_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 voltix_spic_cfg_t cfg = {.frequency = VOLTIX_SPIC_FREQUENCY_M8,
11 .mode = VOLTIX_SPIC_MODE0_CPOL0_CPHA0,
12 .order = VOLTIX_SPIC_ORDER_LSBFIRST,
13 .pin_cs = PIN_D7,
14 .pin_sck = PIN_D8,
15 .pin_copi = PIN_D10,
16 .pin_cipo = PIN_D9};
17 voltix_spic_init(&cfg);
18}
19
20int main(void) {
21 for (;;) {
22 voltix_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 voltix_sleep_ms(1000);
28 }
29}
API Reference
-
enum voltix_spic_frequency_t
Values:
-
enumerator VOLTIX_SPIC_FREQUENCY_K125
125kHz Clock frequency.
-
enumerator VOLTIX_SPIC_FREQUENCY_K250
250kHz Clock frequency.
-
enumerator VOLTIX_SPIC_FREQUENCY_K500
500kHz Clock frequency.
-
enumerator VOLTIX_SPIC_FREQUENCY_M16
16MHz Clock frequency.
-
enumerator VOLTIX_SPIC_FREQUENCY_M1
1MHz Clock frequency.
-
enumerator VOLTIX_SPIC_FREQUENCY_M32
32MHz Clock frequency.
-
enumerator VOLTIX_SPIC_FREQUENCY_M2
2MHz Clock frequency.
-
enumerator VOLTIX_SPIC_FREQUENCY_M4
4MHz Clock frequency.
-
enumerator VOLTIX_SPIC_FREQUENCY_M8
8MHz Clock frequency.
-
enumerator VOLTIX_SPIC_FREQUENCY_K125
-
enum voltix_spic_mode_t
Values:
-
enumerator VOLTIX_SPIC_MODE0_CPOL0_CPHA0
-
enumerator VOLTIX_SPIC_MODE1_CPOL0_CPHA1
-
enumerator VOLTIX_SPIC_MODE2_CPOL1_CPHA0
-
enumerator VOLTIX_SPIC_MODE3_CPOL1_CPHA1
-
enumerator VOLTIX_SPIC_MODE0_CPOL0_CPHA0
-
enum voltix_spic_order_t
Values:
-
enumerator VOLTIX_SPIC_ORDER_MSBFIRST
-
enumerator VOLTIX_SPIC_ORDER_LSBFIRST
-
enumerator VOLTIX_SPIC_ORDER_MSBFIRST
-
voltix_spic_mode_t mode
SPI mode.
-
voltix_spic_frequency_t frequency
Clock frequency.
-
voltix_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.
-
voltix_rc_t voltix_spic_init(voltix_spic_cfg_t *cfg)
Initializes the SPI peripheral. Must be called once after every reset before SPI can be used.
- Parameters:
cfg –
- Returns:
int
-
voltix_rc_t voltix_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:
VOLTIX_SUCCESS – Transfer successful.
VOLTIX_ERR_RESET – Reset occured during transfer.
VOLTIX_ERR_TEARDOWN – Teardown occured during transfer.
-
void voltix_spic_def_cfg(voltix_spic_cfg_t *cfg)
Populates config struct with default configuration.
- Parameters:
cfg – Pointer to config struct that gets initialized.
-
VOLTIX_SPIC_PIN_UNUSED
-
struct voltix_spic_cfg_t