3 #ifdef PIOS_INCLUDE_SPISLAVE
5 struct pios_spislave_dev {
10 static void setup_spi_dma(
spislave_t slave_dev, uint32_t tx_len)
12 DMA_Cmd(slave_dev->cfg->rx_dma.channel, DISABLE);
13 DMA_Cmd(slave_dev->cfg->tx_dma.channel, DISABLE);
15 SPI_Cmd(slave_dev->cfg->regs, DISABLE);
17 SPI_I2S_DMACmd(slave_dev->cfg->regs, SPI_I2S_DMAReq_Tx, DISABLE);
18 SPI_I2S_DMACmd(slave_dev->cfg->regs, SPI_I2S_DMAReq_Rx, DISABLE);
20 DMA_SetCurrDataCounter(slave_dev->cfg->tx_dma.channel, tx_len);
21 DMA_SetCurrDataCounter(slave_dev->cfg->rx_dma.channel,
22 slave_dev->cfg->max_rx_len);
24 SPI_I2S_DMACmd(slave_dev->cfg->regs, SPI_I2S_DMAReq_Rx, ENABLE);
25 DMA_Cmd(slave_dev->cfg->rx_dma.channel, ENABLE);
26 DMA_Cmd(slave_dev->cfg->tx_dma.channel, ENABLE);
27 SPI_I2S_DMACmd(slave_dev->cfg->regs, SPI_I2S_DMAReq_Tx, ENABLE);
29 SPI_Cmd(slave_dev->cfg->regs, ENABLE);
34 if (DMA_GetCurrDataCounter(slave_dev->cfg->rx_dma.channel) ==
35 slave_dev->cfg->max_rx_len) {
42 bool pin_status = GPIO_ReadInputDataBit(slave_dev->cfg->ssel.gpio,
43 slave_dev->cfg->ssel.init.GPIO_Pin) == Bit_SET;
45 if (slave_dev->last_ssel != pin_status) {
46 slave_dev->last_ssel = pin_status;
53 slave_dev->cfg->process_message(
54 slave_dev->cfg->ctx, 0, &resp_len);
56 setup_spi_dma(slave_dev, resp_len);
71 if (!slave_dev)
goto out_fail;
74 slave_dev->last_ssel =
true;
79 if (slave_dev->cfg->remap) {
80 GPIO_PinAFConfig(slave_dev->cfg->sclk.gpio,
81 __builtin_ctz(slave_dev->cfg->sclk.init.GPIO_Pin),
82 slave_dev->cfg->remap);
83 GPIO_PinAFConfig(slave_dev->cfg->mosi.gpio,
84 __builtin_ctz(slave_dev->cfg->mosi.init.GPIO_Pin),
85 slave_dev->cfg->remap);
86 GPIO_PinAFConfig(slave_dev->cfg->miso.gpio,
87 __builtin_ctz(slave_dev->cfg->miso.init.GPIO_Pin),
88 slave_dev->cfg->remap);
89 GPIO_PinAFConfig(slave_dev->cfg->ssel.gpio,
90 __builtin_ctz(slave_dev->cfg->ssel.init.GPIO_Pin),
91 slave_dev->cfg->remap);
95 GPIO_Init(slave_dev->cfg->sclk.gpio,
96 (GPIO_InitTypeDef *) & (slave_dev->cfg->sclk.init));
97 GPIO_Init(slave_dev->cfg->mosi.gpio,
98 (GPIO_InitTypeDef *) & (slave_dev->cfg->mosi.init));
99 GPIO_Init(slave_dev->cfg->miso.gpio,
100 (GPIO_InitTypeDef *) & (slave_dev->cfg->miso.init));
101 GPIO_Init(slave_dev->cfg->ssel.gpio,
102 (GPIO_InitTypeDef *) & (slave_dev->cfg->miso.init));
104 SPI_I2S_DeInit(slave_dev->cfg->regs);
105 SPI_Init(slave_dev->cfg->regs,
106 (SPI_InitTypeDef *) & (slave_dev->cfg->init));
107 SPI_CalculateCRC(slave_dev->cfg->regs, DISABLE);
108 SPI_RxFIFOThresholdConfig(slave_dev->cfg->regs,
109 SPI_RxFIFOThreshold_QF);
111 DMA_DeInit(slave_dev->cfg->rx_dma.channel);
112 DMA_DeInit(slave_dev->cfg->tx_dma.channel);
114 DMA_Init(slave_dev->cfg->rx_dma.channel,
115 (DMA_InitTypeDef *) &slave_dev->cfg->rx_dma.init);
116 DMA_Init(slave_dev->cfg->tx_dma.channel,
117 (DMA_InitTypeDef *) &slave_dev->cfg->tx_dma.init);
119 setup_spi_dma(slave_dev, initial_tx_size);
121 *spislave_id = slave_dev;
int32_t PIOS_SPISLAVE_Init(spislave_t *spislave_id, const struct pios_spislave_cfg *cfg, int initial_tx_size)
int32_t PIOS_SPISLAVE_PollSS(spislave_t spislave_dev)
void * PIOS_malloc(size_t size)
static struct flyingpicmd_cfg_fa cfg
struct pios_spislave_dev * spislave_t
#define PIOS_Assert(test)