Skip to content

Instantly share code, notes, and snippets.

@benpicco-tmp
Created January 12, 2016 19:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benpicco-tmp/a2ce0cdc1aac3dfd0e31 to your computer and use it in GitHub Desktop.
Save benpicco-tmp/a2ce0cdc1aac3dfd0e31 to your computer and use it in GitHub Desktop.
static inline uint8_t spi_send(SPI_TypeDef* SPI, uint8_t data) {
SPI->DR = data;
while (!(SPI->SR & SPI_FLAG_TXE));
while (!(SPI->SR & SPI_FLAG_RXNE));
return SPI->DR;
}
// responses to cmd_buffer are ignored, responses to data_buffer are stored in data_buffer
void spi_tx(const spi_cfg_t cfg, const uint8_t *cmd_buffer, int cmd_buffer_len, uint8_t *data_buffer, int data_buffer_len, uint8_t flags) {
// just to be safe
master_int_disable();
// CS low
((GPIO_TypeDef *) cfg.port_cs)->ODR &= !cfg.pin_cs;
for (int i = 0; i < cmd_buffer_len; ++i) {
spi_send((SPI_TypeDef*) cfg.base, cmd_buffer[i]);
}
uint8_t data;
for (int i = 0; i < data_buffer_len; ++i) {
data = spi_send((SPI_TypeDef*) cfg.base, data_buffer[i]);
if (!(flags & SPI_DATA_IMMUTABLE))
data_buffer[i] = data;
}
while (((SPI_TypeDef*) cfg.base)->SR & SPI_FLAG_BSY);
// CS high
((GPIO_TypeDef *) cfg.port_cs)->ODR |= cfg.pin_cs;
master_int_enable();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment