38 #error "ChibiOS operates only in SPI_MASTER mode"
42 #error "ChibiOS architectures don't have SPI0"
53 #define SPI_DMA_BUF_LEN 512 // it has to be big enough
70 #endif // USE_SPI_SLAVE0
75 #endif //USE_SPI_SLAVE1
80 #endif //USE_SPI_SLAVE2
85 #endif //USE_SPI_SLAVE3
90 #endif //USE_SPI_SLAVE4
95 #endif //USE_SPI_SLAVE5
117 #endif // USE_SPI_SLAVE0
122 #endif //USE_SPI_SLAVE1
127 #endif //USE_SPI_SLAVE2
132 #endif //USE_SPI_SLAVE3
137 #endif //USE_SPI_SLAVE4
142 #endif //USE_SPI_SLAVE5
164 #if defined(STM32F1) || defined(STM32F4) || defined(STM32F7)
169 CR1 |= SPI_CR1_LSBFIRST;
188 CR1 |= SPI_CR1_BR_1 | SPI_CR1_BR_0;
194 CR1 |= SPI_CR1_BR_2 | SPI_CR1_BR_0;
197 CR1 |= SPI_CR1_BR_2 | SPI_CR1_BR_1;
226 CR2 |= SPI_CR2_DS_0 | SPI_CR2_DS_1 | SPI_CR2_DS_2 | SPI_CR2_DS_3;
229 CR2 |= SPI_CR2_DS_0 | SPI_CR2_DS_1 | SPI_CR2_DS_2;
258 SPIConfig spi_cfg = {
267 static size_t t_length;
275 spiStart((SPIDriver *)p->
reg_addr, &spi_cfg);
276 spiSelect((SPIDriver *)p->
reg_addr);
288 spiExchange((SPIDriver *)p->
reg_addr, t_length, i->dma_buf_out, i->dma_buf_in);
295 spiUnselect((SPIDriver *)p->
reg_addr);
331 .dma_buf_out = spi1_dma_buf_out,
332 .dma_buf_in = spi1_dma_buf_in
340 static __attribute__((noreturn)) void
thd_spi1(
void *arg)
343 chRegSetThreadName(
"spi1");
354 spi1.reg_addr = &SPID1;
357 chThdCreateStatic(wa_thd_spi1,
sizeof(wa_thd_spi1),
370 .dma_buf_out = spi2_dma_buf_out,
371 .dma_buf_in = spi2_dma_buf_in
379 static __attribute__((noreturn)) void
thd_spi2(
void *arg)
382 chRegSetThreadName(
"spi2");
393 spi2.reg_addr = &SPID2;
396 chThdCreateStatic(wa_thd_spi2,
sizeof(wa_thd_spi2),
407 static struct spi_init spi3_init_s = {
409 .dma_buf_out = spi3_dma_buf_out,
410 .dma_buf_in = spi3_dma_buf_in
413 static struct spi_init spi3_init_s = {
418 static __attribute__((noreturn)) void thd_spi3(
void *arg)
421 chRegSetThreadName(
"spi3");
430 void spi3_arch_init(
void)
432 spi3.reg_addr = &SPID3;
433 spi3.init_struct = &spi3_init_s;
435 chThdCreateStatic(wa_thd_spi3,
sizeof(wa_thd_spi3),
436 NORMALPRIO+1, thd_spi3, NULL);
498 #endif // USE_SPI_SLAVE0
503 #endif //USE_SPI_SLAVE1
508 #endif //USE_SPI_SLAVE2
513 #endif //USE_SPI_SLAVE3
518 #endif //USE_SPI_SLAVE4
523 #endif //USE_SPI_SLAVE5
540 #endif // USE_SPI_SLAVE0
545 #endif //USE_SPI_SLAVE1
550 #endif //USE_SPI_SLAVE2
555 #endif //USE_SPI_SLAVE3
560 #endif //USE_SPI_SLAVE4
565 #endif //USE_SPI_SLAVE5
578 if (slave < 254 && p->suspend == 0) {
Specific RAM section for DMA usage on F7.
static uint16_t spi_resolve_slave_pin(uint8_t slave)
Resolve slave pin.
uint16_t output_length
number of data words to write
#define SPI_SELECT_SLAVE4_PORT
process_rx_dma_interrupt & spi2
receive transferred over DMA
static void gpio_clear(ioportid_t port, uint16_t pin)
Clear a gpio output to low level.
void spi1_arch_init(void)
Architecture dependent SPI1 initialization.
static uint16_t spi_resolve_CR1(struct spi_transaction *t)
Resolve CR1.
Some architecture independent helper functions for GPIOs.
SPI transaction structure.
bool spi_submit(struct spi_periph *p, struct spi_transaction *t)
Submit SPI transaction.
static void handle_spi_thd(struct spi_periph *p)
main thread function
void gpio_setup_output(ioportid_t port, uint16_t gpios)
Setup one or more pins of the given GPIO port as outputs.
volatile uint8_t * output_buf
pointer to transmit buffer for DMA
volatile uint8_t suspend
control for stop/resume of the fifo
static SEMAPHORE_DECL(spi1_sem, 0)
Configure SPI peripherals.
Architecture independent SPI (Serial Peripheral Interface) API.
#define SPI_SELECT_SLAVE1_PORT
static struct spi_init spi1_init_s
static struct spi_init spi2_init_s
bool spi_lock(struct spi_periph *p, uint8_t slave)
spi_lock() function
SPI peripheral structure.
#define SPI_SELECT_SLAVE0_PIN
void spi_slave_unselect(uint8_t slave)
spi_slave_unselect() function
#define SPI_SELECT_SLAVE2_PORT
SPICallback after_cb
NULL or function called after the transaction.
#define SPI_SELECT_SLAVE0_PORT
void spi2_arch_init(void)
Architecture dependent SPI2 initialization.
#define SPI_SELECT_SLAVE1_PIN
static ioportid_t spi_resolve_slave_port(uint8_t slave)
Resolve slave port.
#define SPI_SELECT_SLAVE2_PIN
uint16_t input_length
number of data words to read
enum SPIStatus status
internal state of the peripheral
#define SPI_SELECT_SLAVE3_PIN
#define IN_DMA_SECTION(var)
#define SPI_SELECT_SLAVE4_PIN
bool spi_resume(struct spi_periph *p, uint8_t slave)
spi_resume() function
#define SPI_SELECT_SLAVE5_PIN
uint8_t trans_extract_idx
uint8_t slave_idx
slave id: SPI_SLAVE0 to SPI_SLAVE4
static void thd_spi1(void *arg)
volatile uint8_t * input_buf
pointer to receive buffer for DMA
SPICallback before_cb
NULL or function called before the transaction.
#define SPI_TRANSACTION_QUEUE_LEN
SPI transaction queue length.
static void thd_spi2(void *arg)
void spi_init_slaves(void)
spi_init_slaves() function
void spi_slave_select(uint8_t slave)
spi_slave_select() function
#define SPI_SELECT_SLAVE3_PORT
static void gpio_set(ioportid_t port, uint16_t pin)
Set a gpio output to high level.
#define SPI_SELECT_SLAVE5_PORT
struct spi_transaction * trans[SPI_TRANSACTION_QUEUE_LEN]
circular buffer holding transactions
process_rx_dma_interrupt & spi1
receive transferred over DMA
static THD_WORKING_AREA(wa_thd_spi1, 1024)
enum SPITransactionStatus status
static uint16_t spi_resolve_CR2(struct spi_transaction *t)
Resolve CR2.