Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: DirectSPI-test wave_player_super_lite
DirectSPI.cpp
00001 #include "DirectSPI.h" 00002 00003 #if defined(SPI_SR_TXE) && defined(SPI_SR_RXNE) 00004 # define isTXE(spi) (spi->SR & SPI_SR_TXE) 00005 # define isRXNE(spi) (spi->SR & SPI_SR_RXNE) 00006 #else 00007 # error Unsupported MCU at this moment ! 00008 #endif 00009 00010 #define SPIBUF8(spi) *(__IO uint8_t *)&spi->DR 00011 #define SPIBUF16(spi) spi->DR 00012 00013 #if DEVICE_SPI_ASYNCH 00014 #define SPI_S(obj) (( struct spi_s *)(obj)) 00015 //#define SPI_S(obj) (( struct spi_s *)(&(obj->spi))) 00016 #else 00017 #define SPI_S(obj) (( struct spi_s *)(obj)) 00018 #endif 00019 00020 DirectSPI::DirectSPI(PinName mosi, PinName miso, PinName sclk) : SPI(mosi, miso, sclk) { 00021 spi = spi_get_id(mosi, miso, sclk, NC); 00022 #if TODO 00023 spiSend = is16bit ? &DirectSPI::spiSend16 : &DirectSPI::spiSend8; 00024 #endif 00025 } 00026 00027 uint16_t DirectSPI::directWrite(uint16_t data) { 00028 if(_bits == 16){ 00029 while(!isTXE(spi)); 00030 SPIBUF16(spi) = data; 00031 while(!isRXNE(spi)); 00032 return SPIBUF16(spi); 00033 } else { 00034 while(!isTXE(spi)); 00035 SPIBUF8(spi) = data; 00036 while(!isRXNE(spi)); 00037 return SPIBUF8(spi); 00038 } 00039 } 00040 00041 uint16_t DirectSPI::directWrite8(uint16_t data) { 00042 while(!isTXE(spi)); 00043 SPIBUF8(spi) = data; 00044 while(!isRXNE(spi)); 00045 return SPIBUF8(spi); 00046 } 00047 00048 uint16_t DirectSPI::directWrite16(uint16_t data) { 00049 while(!isTXE(spi)); 00050 SPIBUF16(spi) = data; 00051 while(!isRXNE(spi)); 00052 return SPIBUF16(spi); 00053 } 00054 00055 SPI_TypeDef *DirectSPI::spi_get_id( PinName mosi, PinName miso, PinName sclk, PinName ssel) { 00056 // Determine the SPI to use 00057 SPIName spi_mosi = (SPIName)pinmap_peripheral(mosi, PinMap_SPI_MOSI); 00058 SPIName spi_miso = (SPIName)pinmap_peripheral(miso, PinMap_SPI_MISO); 00059 SPIName spi_sclk = (SPIName)pinmap_peripheral(sclk, PinMap_SPI_SCLK); 00060 SPIName spi_ssel = (SPIName)pinmap_peripheral(ssel, PinMap_SPI_SSEL); 00061 00062 SPIName spi_data = (SPIName)pinmap_merge(spi_mosi, spi_miso); 00063 SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel); 00064 00065 return (SPI_TypeDef *) ( (SPIName)pinmap_merge(spi_data, spi_cntl) ); 00066 }
Generated on Tue Aug 9 2022 13:37:20 by
1.7.2