Ultra fast and direct SPI library for some STM32 micros.

Dependents:   DirectSPI-test wave_player_super_lite

DirectSPI.cpp

Committer:
mimi3
Date:
2017-02-23
Revision:
2:1faa28f1a5bb
Parent:
1:576dbeb04958
Child:
4:f63f07d07d5a

File content as of revision 2:1faa28f1a5bb:

#include "DirectSPI.h"

#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

#if DEVICE_SPI_ASYNCH
    #define SPI_S(obj)    (( struct spi_s *)(obj))
    //#define SPI_S(obj)    (( struct spi_s *)(&(obj->spi)))
#else
    #define SPI_S(obj)    (( struct spi_s *)(obj))
#endif

DirectSPI::DirectSPI(PinName mosi, PinName miso, PinName sclk) : SPI(mosi, miso, sclk) {
    spi = _spi.spi.handle.Instance;
#if TODO
    spiSend = is16bit ? &DirectSPI::spiSend16 : &DirectSPI::spiSend8;
#endif
}

uint16_t DirectSPI::directWrite(uint16_t data) {
    if(_bits == 16){
        while(!isTXE(spi));
        SPIBUF16(spi) = data;
        while(!isRXNE(spi));
        return SPIBUF16(spi);
    } else {
        while(!isTXE(spi));
        SPIBUF8(spi) = data;
        while(!isRXNE(spi));
        return SPIBUF8(spi);
    }
}

uint16_t DirectSPI::directWrite8(uint16_t data) {
    while(!isTXE(spi));
    SPIBUF8(spi) = data;
    while(!isRXNE(spi));
    return SPIBUF8(spi);
}

uint16_t DirectSPI::directWrite16(uint16_t data) {
    while(!isTXE(spi));
    SPIBUF16(spi) = data;
    while(!isRXNE(spi));
    return SPIBUF16(spi);
}