38 #if defined(PIOS_INCLUDE_SPI)
42 #if !defined(STM32F30X)
43 #define SPI_SendData8(regs,b) do { (regs)->DR = (b); } while (0)
44 #define SPI_ReceiveData8(regs) ((regs)->DR)
48 PIOS_SPI_PRESCALER_2 = 0,
49 PIOS_SPI_PRESCALER_4 = 1,
50 PIOS_SPI_PRESCALER_8 = 2,
51 PIOS_SPI_PRESCALER_16 = 3,
52 PIOS_SPI_PRESCALER_32 = 4,
53 PIOS_SPI_PRESCALER_64 = 5,
54 PIOS_SPI_PRESCALER_128 = 6,
55 PIOS_SPI_PRESCALER_256 = 7
56 } SPIPrescalerTypeDef;
59 static bool PIOS_SPI_validate(
struct pios_spi_dev *com_dev)
77 uint32_t init_ssel = 0;
86 if (!spi_dev)
goto out_fail;
93 switch (spi_dev->
cfg->
init.SPI_NSS) {
96 SPI_NSSInternalSoftwareConfig(spi_dev->
cfg->
regs, SPI_NSSInternalSoft_Set);
105 SPI_SSOutputCmd(spi_dev->
cfg->
regs, ENABLE);
128 if (spi_dev->
cfg->
init.SPI_NSS == SPI_NSS_Hard) {
129 for (uint32_t
i = 0;
i < init_ssel;
i++) {
141 for (uint32_t
i = 0;
i < init_ssel;
i++) {
148 SPI_I2S_DeInit(spi_dev->
cfg->
regs);
149 SPI_Init(spi_dev->
cfg->
regs, (SPI_InitTypeDef *) & (spi_dev->
cfg->
init));
150 SPI_CalculateCRC(spi_dev->
cfg->
regs, DISABLE);
152 #if defined(STM32F30X)
154 SPI_RxFIFOThresholdConfig(spi_dev->
cfg->
regs, SPI_RxFIFOThreshold_QF);
158 SPI_Cmd(spi_dev->
cfg->
regs, ENABLE);
170 bool valid = PIOS_SPI_validate(spi_dev);
173 SPI_InitTypeDef SPI_InitStructure;
175 SPIPrescalerTypeDef spi_prescaler;
180 #if defined(STM32F40_41xxx) || defined(STM32F446xx)
185 #elif defined(STM32F30X)
189 #elif !defined(STM32F10X_MD)
191 #error Unrecognized architecture
195 float desiredPrescaler=(float) spiBusClock / spi_speed;
198 if(desiredPrescaler <= 2)
199 spi_prescaler = PIOS_SPI_PRESCALER_2;
200 else if(desiredPrescaler <= 4)
201 spi_prescaler = PIOS_SPI_PRESCALER_4;
202 else if(desiredPrescaler <= 8)
203 spi_prescaler = PIOS_SPI_PRESCALER_8;
204 else if(desiredPrescaler <= 16)
205 spi_prescaler = PIOS_SPI_PRESCALER_16;
206 else if(desiredPrescaler <= 32)
207 spi_prescaler = PIOS_SPI_PRESCALER_32;
208 else if(desiredPrescaler <= 64)
209 spi_prescaler = PIOS_SPI_PRESCALER_64;
210 else if(desiredPrescaler <= 128)
211 spi_prescaler = PIOS_SPI_PRESCALER_128;
213 spi_prescaler = PIOS_SPI_PRESCALER_256;
216 SPI_InitStructure = spi_dev->
cfg->
init;
219 SPI_InitStructure.SPI_BaudRatePrescaler = ((uint16_t) spi_prescaler & 7) << 3;
222 SPI_Init(spi_dev->
cfg->
regs, &SPI_InitStructure);
225 return spiBusClock >> (1 + spi_prescaler);
230 bool valid = PIOS_SPI_validate(spi_dev);
241 bool valid = PIOS_SPI_validate(spi_dev);
251 bool valid = PIOS_SPI_validate(spi_dev);
266 bool valid = PIOS_SPI_validate(spi_dev);
276 SPI_ReceiveData8(spi_dev->
cfg->
regs);
279 while (SPI_I2S_GetFlagStatus(spi_dev->
cfg->
regs, SPI_I2S_FLAG_TXE) == RESET);
282 SPI_SendData8(spi_dev->
cfg->
regs, b);
285 while (SPI_I2S_GetFlagStatus(spi_dev->
cfg->
regs, SPI_I2S_FLAG_RXNE) == RESET);
288 rx_byte = SPI_ReceiveData8(spi_dev->
cfg->
regs);
291 while (!(spi_dev->
cfg->
regs->SR & SPI_I2S_FLAG_TXE));
294 while (spi_dev->
cfg->
regs->SR & SPI_I2S_FLAG_BSY);
312 static int32_t SPI_PIO_TransferBlock(
pios_spi_t spi_dev,
313 const uint8_t *send_buffer, uint8_t *receive_buffer, uint16_t len)
317 bool valid = PIOS_SPI_validate(spi_dev);
322 b = send_buffer ? *(send_buffer++) : 0xff;
325 while (SPI_I2S_GetFlagStatus(spi_dev->
cfg->
regs, SPI_I2S_FLAG_TXE) == RESET);
328 SPI_SendData8(spi_dev->
cfg->
regs, b);
331 while (SPI_I2S_GetFlagStatus(spi_dev->
cfg->
regs, SPI_I2S_FLAG_RXNE) == RESET);
334 b = SPI_ReceiveData8(spi_dev->
cfg->
regs);
338 *(receive_buffer++) = b;
342 while (spi_dev->
cfg->
regs->SR & SPI_I2S_FLAG_BSY);
348 uint8_t *receive_buffer, uint16_t len)
350 return SPI_PIO_TransferBlock(spi_dev, send_buffer, receive_buffer, len);
#define PIOS_SEMAPHORE_TIMEOUT_MAX
Main PiOS header to include all the compiled in PiOS options.
int32_t PIOS_SPI_RC_PinSet(pios_spi_t spi_dev, uint32_t slave_id, bool pin_value)
int32_t PIOS_SPI_ClaimBus(pios_spi_t spi_dev)
void * PIOS_malloc(size_t size)
static struct flyingpicmd_cfg_fa cfg
struct pios_semaphore * PIOS_Semaphore_Create(void)
Creates a binary semaphore.
uint8_t PIOS_SPI_TransferByte(pios_spi_t spi_dev, uint8_t b)
int32_t PIOS_SPI_SetClockSpeed(pios_spi_t spi_dev, uint32_t speed)
int32_t PIOS_SPI_Init(pios_spi_t *spi_dev, const struct pios_spi_cfg *cfg)
int32_t PIOS_SPI_TransferBlock(pios_spi_t spi_dev, const uint8_t *send_buffer, uint8_t *receive_buffer, uint16_t len)
bool PIOS_Semaphore_Give(struct pios_semaphore *sema)
Gives binary semaphore.
const struct pios_spi_cfg * cfg
bool PIOS_Semaphore_Take(struct pios_semaphore *sema, uint32_t timeout_ms)
Takes binary semaphore.
int32_t PIOS_SPI_ReleaseBus(pios_spi_t spi_dev)
#define PIOS_Assert(test)
struct pios_semaphore * busy