Ultra fast and direct SPI library for some STM32 micros.

Dependents:   DirectSPI-test wave_player_super_lite

Revision:
2:1faa28f1a5bb
Parent:
1:576dbeb04958
Child:
4:f63f07d07d5a
--- a/DirectSPI.cpp	Thu Feb 23 00:15:12 2017 +0900
+++ b/DirectSPI.cpp	Thu Feb 23 22:25:39 2017 +0900
@@ -1,7 +1,12 @@
 #include "DirectSPI.h"
 
-#define isTXE(spi)     (spi->SR & SPI_SR_TXE)
-#define isRXNE(spi)    (spi->SR & SPI_SR_RXNE)
+#if defined(SPI_SR_TXE) && defined(SPI_SR_RXNE)
+#   define isTXE(spi)     (spi->SR & SPI_SR_TXE)
+#   define isRXNE(spi)    (spi->SR & SPI_SR_RXNE)
+#else
+#   error Unsupported MCU at this moment !
+#endif
+
 #define SPIBUF8(spi)   *(__IO uint8_t  *)&spi->DR
 #define SPIBUF16(spi)  spi->DR
 
@@ -13,18 +18,14 @@
 #endif
 
 DirectSPI::DirectSPI(PinName mosi, PinName miso, PinName sclk) : SPI(mosi, miso, sclk) {
-    spi = spi_get_id(mosi, miso, sclk, NC);
+    spi = _spi.spi.handle.Instance;
 #if TODO
-    struct spi_s *spiobj = SPI_S(spi);
-    SPI_HandleTypeDef *handle = &(spiobj->handle);
-    is16bit = (handle->Init.DataSize == SPI_DATASIZE_16BIT);
     spiSend = is16bit ? &DirectSPI::spiSend16 : &DirectSPI::spiSend8;
 #endif
 }
 
-#if TODO
 uint16_t DirectSPI::directWrite(uint16_t data) {
-    if(is16bit){
+    if(_bits == 16){
         while(!isTXE(spi));
         SPIBUF16(spi) = data;
         while(!isRXNE(spi));
@@ -36,7 +37,6 @@
         return SPIBUF8(spi);
     }
 }
-#endif
 
 uint16_t DirectSPI::directWrite8(uint16_t data) {
     while(!isTXE(spi));
@@ -52,16 +52,3 @@
     return SPIBUF16(spi);
 }
 
-SPI_TypeDef *DirectSPI::spi_get_id( PinName mosi, PinName miso, PinName sclk, PinName ssel) {
-    // Determine the SPI to use
-    SPIName spi_mosi = (SPIName)pinmap_peripheral(mosi, PinMap_SPI_MOSI);
-    SPIName spi_miso = (SPIName)pinmap_peripheral(miso, PinMap_SPI_MISO);
-    SPIName spi_sclk = (SPIName)pinmap_peripheral(sclk, PinMap_SPI_SCLK);
-    SPIName spi_ssel = (SPIName)pinmap_peripheral(ssel, PinMap_SPI_SSEL);
-
-    SPIName spi_data = (SPIName)pinmap_merge(spi_mosi, spi_miso);
-    SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel);
-
-    return (SPI_TypeDef *) ( (SPIName)pinmap_merge(spi_data, spi_cntl) );
-}
-