A very simple software SPI library for the MAX32630FTHR board. It was designed to support the max31865 and RadioHead libraries I ported for my Rocket mbed project.
swspi.cpp
00001 // Software (bit-banged) SPI Module supporting two modes. 00002 // Each mode consists of a CS and CPHA mode 00003 // 00004 00005 #include <swspi.h> 00006 #include "mbed.h" 00007 00008 00009 swspi::swspi(PinName mosiPin, PinName misoPin, PinName sckPin, PinName ss1Pin, PinName ss2Pin) 00010 { 00011 mosiP = new DigitalOut(mosiPin); 00012 misoP = new DigitalIn(misoPin); 00013 sckP = new DigitalOut(sckPin); 00014 ss1P = new DigitalOut(ss1Pin); 00015 ss2P = new DigitalOut(ss2Pin); 00016 } 00017 00018 void swspi::init() 00019 { 00020 sckP->write(0); 00021 mosiP->write(0); 00022 ss1P->write(1); 00023 ss2P->write(1); 00024 } 00025 00026 uint8_t swspi::spiRead(uint8_t reg, int cpha, int ss) 00027 { 00028 uint8_t val; 00029 00030 _spi_mutex.lock(); 00031 _cpha = cpha; 00032 _ss = ss; 00033 _setSlaveSelect(0); 00034 _transfer(reg & ~SPI_WRITE_MASK); // Send the address with the write mask off 00035 val = _transfer(0); // The written value is ignored, reg value is read 00036 _setSlaveSelect(1); 00037 _spi_mutex.unlock(); 00038 00039 return val; 00040 } 00041 00042 uint8_t swspi::spiWrite(uint8_t reg, uint8_t val, int cpha, int ss) 00043 { 00044 uint8_t status = 0; 00045 00046 _spi_mutex.lock(); 00047 _cpha = cpha; 00048 _ss = ss; 00049 _setSlaveSelect(0); 00050 status = _transfer(reg | SPI_WRITE_MASK); // Send the address with the write mask on 00051 _transfer(val); // New value follows 00052 _setSlaveSelect(1); 00053 _spi_mutex.unlock(); 00054 00055 return status; 00056 } 00057 00058 uint8_t swspi::spiBurstRead(uint8_t reg, uint8_t* dest, uint8_t len, int cpha, int ss) 00059 { 00060 uint8_t status = 0; 00061 00062 _spi_mutex.lock(); 00063 _cpha = cpha; 00064 _ss = ss; 00065 _setSlaveSelect(0); 00066 status = _transfer(reg & ~SPI_WRITE_MASK); // Send the start address with the write mask off 00067 while (len--) 00068 *dest++ = _transfer(0); 00069 _setSlaveSelect(1); 00070 _spi_mutex.unlock(); 00071 00072 return status; 00073 } 00074 00075 uint8_t swspi::spiBurstWrite(uint8_t reg, const uint8_t* src, uint8_t len, int cpha, int ss) 00076 { 00077 uint8_t status = 0; 00078 00079 _spi_mutex.lock(); 00080 _cpha = cpha; 00081 _ss = ss; 00082 _setSlaveSelect(0); 00083 status = _transfer(reg | SPI_WRITE_MASK); // Send the start address with the write mask on 00084 while (len--) 00085 _transfer(*src++); 00086 _setSlaveSelect(1); 00087 _spi_mutex.unlock(); 00088 00089 return status; 00090 } 00091 00092 void swspi::_setSlaveSelect(int v) 00093 { 00094 if (_ss == 1) 00095 ss1P->write(v); 00096 else 00097 ss2P->write(v); 00098 } 00099 00100 uint8_t swspi::_transfer(uint8_t v) 00101 { 00102 uint8_t reply = 0; 00103 00104 if (_cpha == 0) { 00105 // Sample on rising clock edge 00106 for (int i=7; i>=0; i--) { 00107 reply <<= 1; 00108 mosiP->write( (v & (1<<i)) != 0 ? 1 : 0 ); 00109 sckP->write(1); 00110 if (misoP->read() == 1) reply |= 1; 00111 sckP->write(0); 00112 } 00113 } else { 00114 // Sample on falling clock edge 00115 for (int i=7; i>=0; i--) { 00116 reply <<= 1; 00117 sckP->write(1); 00118 mosiP->write( (v & (1<<i)) != 0 ? 1 : 0 ); 00119 sckP->write(0); 00120 if (misoP->read() == 1) reply |= 1; 00121 } 00122 } 00123 00124 return reply; 00125 }
Generated on Tue Jul 12 2022 13:56:05 by 1.7.2