Ultra fast and direct SPI library for some STM32 micros.

Dependents:   DirectSPI-test wave_player_super_lite

Committer:
mimi3
Date:
Thu Feb 23 22:25:39 2017 +0900
Revision:
2:1faa28f1a5bb
Parent:
1:576dbeb04958
Child:
4:f63f07d07d5a
update

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mimi3 1:576dbeb04958 1 #include "DirectSPI.h"
mimi3 1:576dbeb04958 2
mimi3 2:1faa28f1a5bb 3 #if defined(SPI_SR_TXE) && defined(SPI_SR_RXNE)
mimi3 2:1faa28f1a5bb 4 # define isTXE(spi) (spi->SR & SPI_SR_TXE)
mimi3 2:1faa28f1a5bb 5 # define isRXNE(spi) (spi->SR & SPI_SR_RXNE)
mimi3 2:1faa28f1a5bb 6 #else
mimi3 2:1faa28f1a5bb 7 # error Unsupported MCU at this moment !
mimi3 2:1faa28f1a5bb 8 #endif
mimi3 2:1faa28f1a5bb 9
mimi3 1:576dbeb04958 10 #define SPIBUF8(spi) *(__IO uint8_t *)&spi->DR
mimi3 1:576dbeb04958 11 #define SPIBUF16(spi) spi->DR
mimi3 1:576dbeb04958 12
mimi3 1:576dbeb04958 13 #if DEVICE_SPI_ASYNCH
mimi3 1:576dbeb04958 14 #define SPI_S(obj) (( struct spi_s *)(obj))
mimi3 1:576dbeb04958 15 //#define SPI_S(obj) (( struct spi_s *)(&(obj->spi)))
mimi3 1:576dbeb04958 16 #else
mimi3 1:576dbeb04958 17 #define SPI_S(obj) (( struct spi_s *)(obj))
mimi3 1:576dbeb04958 18 #endif
mimi3 1:576dbeb04958 19
mimi3 1:576dbeb04958 20 DirectSPI::DirectSPI(PinName mosi, PinName miso, PinName sclk) : SPI(mosi, miso, sclk) {
mimi3 2:1faa28f1a5bb 21 spi = _spi.spi.handle.Instance;
mimi3 1:576dbeb04958 22 #if TODO
mimi3 1:576dbeb04958 23 spiSend = is16bit ? &DirectSPI::spiSend16 : &DirectSPI::spiSend8;
mimi3 1:576dbeb04958 24 #endif
mimi3 1:576dbeb04958 25 }
mimi3 1:576dbeb04958 26
mimi3 1:576dbeb04958 27 uint16_t DirectSPI::directWrite(uint16_t data) {
mimi3 2:1faa28f1a5bb 28 if(_bits == 16){
mimi3 1:576dbeb04958 29 while(!isTXE(spi));
mimi3 1:576dbeb04958 30 SPIBUF16(spi) = data;
mimi3 1:576dbeb04958 31 while(!isRXNE(spi));
mimi3 1:576dbeb04958 32 return SPIBUF16(spi);
mimi3 1:576dbeb04958 33 } else {
mimi3 1:576dbeb04958 34 while(!isTXE(spi));
mimi3 1:576dbeb04958 35 SPIBUF8(spi) = data;
mimi3 1:576dbeb04958 36 while(!isRXNE(spi));
mimi3 1:576dbeb04958 37 return SPIBUF8(spi);
mimi3 1:576dbeb04958 38 }
mimi3 1:576dbeb04958 39 }
mimi3 1:576dbeb04958 40
mimi3 1:576dbeb04958 41 uint16_t DirectSPI::directWrite8(uint16_t data) {
mimi3 1:576dbeb04958 42 while(!isTXE(spi));
mimi3 1:576dbeb04958 43 SPIBUF8(spi) = data;
mimi3 1:576dbeb04958 44 while(!isRXNE(spi));
mimi3 1:576dbeb04958 45 return SPIBUF8(spi);
mimi3 1:576dbeb04958 46 }
mimi3 1:576dbeb04958 47
mimi3 1:576dbeb04958 48 uint16_t DirectSPI::directWrite16(uint16_t data) {
mimi3 1:576dbeb04958 49 while(!isTXE(spi));
mimi3 1:576dbeb04958 50 SPIBUF16(spi) = data;
mimi3 1:576dbeb04958 51 while(!isRXNE(spi));
mimi3 1:576dbeb04958 52 return SPIBUF16(spi);
mimi3 1:576dbeb04958 53 }
mimi3 1:576dbeb04958 54