Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.71 KB | None | 0 0
  1. void SPIBLOCKING_write(Spi *spid, const uint8_t *txbuf, uint16_t len)
  2. {
  3.   SPI_TypeDef* spi = spid->spi;
  4.  
  5.   while (len--) {
  6.     spi->DR = *txbuf++;             // write a byte
  7.    
  8.     while (!(spi->SR & SPI_SR_RXNE));
  9.     (void)spi->DR; // dummy read
  10.    
  11.     while (!(spi->SR & SPI_SR_TXE)); // make sure previous TX is done
  12.   }
  13. }
  14.  
  15. void SPIBLOCKING_read(Spi *spid, uint8_t *rxbuf, uint16_t len)
  16. {
  17.   SPI_TypeDef* spi = spid->spi;
  18.  
  19.   while (len--) {
  20.     spid->spi->DR = 0xFF;             // write a dummy byte
  21.    
  22.     while(!(spi->SR & SPI_SR_RXNE));  // wait for data
  23.     *rxbuf++ = spi->DR;               // read it
  24.    
  25.     while(!(spi->SR & SPI_SR_TXE));   // make sure previous TX is done
  26.   }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement