88 *---------------------------------------------------------------------------------
99 */
1010
11+ // Implementation of the SPI communication class of the library.
12+
1113#include " sfDevFPC2534SPI.h"
1214
1315// --------------------------------------------------------------------------------------------
@@ -17,12 +19,12 @@ sfDevFPC2534SPI::sfDevFPC2534SPI() : _inWrite{false}, _inRead{false}, _spiPort{n
1719}
1820
1921// --------------------------------------------------------------------------------------------
22+ // Initialize the SPI comms interface.
2023bool sfDevFPC2534SPI::initialize (SPIClass &spiPort, SPISettings &busSPISettings, uint8_t csPin, uint32_t interruptPin,
2124 bool bInit)
2225
2326{
2427 _spiPort = &spiPort;
25-
2628 _spiSettings = busSPISettings;
2729 _csPin = csPin;
2830
@@ -39,12 +41,14 @@ bool sfDevFPC2534SPI::initialize(SPIClass &spiPort, SPISettings &busSPISettings,
3941 return true ;
4042}
4143// --------------------------------------------------------------------------------------------
44+ // Simple initialize with default SPI settings
4245bool sfDevFPC2534SPI::initialize (uint8_t csPin, uint32_t interruptPin, bool bInit)
4346{
4447 // If the transaction settings are not provided by the user they are built here.
4548 SPISettings spiSettings = SPISettings (3000000 , MSBFIRST, SPI_MODE0);
4649 return initialize (SPI, spiSettings, csPin, interruptPin, bInit);
4750}
51+
4852// --------------------------------------------------------------------------------------------
4953// Is data available to read - either the device is indicating it via an interrupt, or we have
5054// data in our internal buffer
@@ -67,6 +71,7 @@ void sfDevFPC2534SPI::clearData()
6771 clearISRDataAvailable ();
6872}
6973
74+ // --------------------------------------------------------------------------------------------
7075void sfDevFPC2534SPI::beginWrite (void )
7176{
7277
@@ -80,7 +85,6 @@ void sfDevFPC2534SPI::beginWrite(void)
8085 // the datasheet specifiies a delay greater than 500us after CS goes low
8186 delayMicroseconds (600 );
8287 _inWrite = true ;
83- // Serial.println("Begin SPI Write");
8488}
8589
8690void sfDevFPC2534SPI ::endWrite (void )
@@ -91,7 +95,6 @@ void sfDevFPC2534SPI ::endWrite(void)
9195 digitalWrite (_csPin, HIGH);
9296 _spiPort->endTransaction ();
9397 _inWrite = false ;
94- // Serial.println("End SPI Write");
9598}
9699// --------------------------------------------------------------------------------------------
97100// Write data to the device
@@ -101,7 +104,6 @@ uint16_t sfDevFPC2534SPI::write(const uint8_t *data, size_t len)
101104 if (_spiPort == nullptr )
102105 return FPC_RESULT_IO_RUNTIME_FAILURE; // I2C bus not initialized
103106
104- // Serial.printf("Writing %d bytes to SPI\r\n", len);
105107 // now send the data
106108
107109 for (size_t i = 0 ; i < len; i++)
@@ -127,14 +129,6 @@ uint16_t sfDevFPC2534SPI::read(uint8_t *data, size_t len)
127129 if (_inWrite)
128130 endWrite ();
129131
130- // _spiPort->beginTransaction(_spiSettings);
131-
132- // // Signal communication start
133- // digitalWrite(_csPin, LOW);
134-
135- // // the datasheet specifiies a delay greater than 500us after CS goes low
136- // delayMicroseconds(600);
137-
138132 // if we are not in a read transaction, not okay.
139133 if (_inRead == false )
140134 return FPC_RESULT_IO_RUNTIME_FAILURE;
@@ -151,12 +145,12 @@ uint16_t sfDevFPC2534SPI::read(uint8_t *data, size_t len)
151145 for (size_t i = 0 ; i < len; i++)
152146 *data++ = _spiPort->transfer (0x00 );
153147
154- // // End transaction
155- // digitalWrite(_csPin, HIGH);
156- // _spiPort->endTransaction();
157-
158148 return FPC_RESULT_OK;
159149}
150+
151+ // We need to bracket the multiple reads with the SPI transaction calls and drive the CS pin low
152+ // This is counter to the FPC2534 datasheet, but is what the FPC2534 examples implement. More importantly,
153+ // this works with Adruino.
160154void sfDevFPC2534SPI::beginRead (void )
161155{
162156 if (_spiPort == nullptr )
@@ -172,6 +166,7 @@ void sfDevFPC2534SPI::beginRead(void)
172166 _inRead = true ;
173167}
174168
169+ // End the read transaction
175170void sfDevFPC2534SPI ::endRead (void )
176171{
177172 if (_spiPort == nullptr || !_inRead)
0 commit comments