Skip to content

Commit 8ed4762

Browse files
committed
SPI is almost 100% working (on ESP32 for now) - have not validated that I did not break I2c or UART, but will do that next
1 parent 3ad9068 commit 8ed4762

File tree

5 files changed

+81
-27
lines changed

5 files changed

+81
-27
lines changed

src/sfTk/sfDevFPC2534.cpp

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,8 @@ fpc_result_t sfDevFPC2534::parseStatusCommand(fpc_cmd_hdr_t *cmd_hdr, size_t siz
215215

216216
fpc_cmd_status_response_t *status = (fpc_cmd_status_response_t *)cmd_hdr;
217217

218-
Serial.printf("Parsing Status Command: Event: 0x%04X, State: 0x%04X, AppFail: 0x%04X\r\n", status->event,
219-
status->state, status->app_fail_code);
218+
// Serial.printf("Parsing Status Command: Event: 0x%04X, State: 0x%04X, AppFail: 0x%04X\r\n", status->event,
219+
// status->state, status->app_fail_code);
220220
// TODO: Implement secure interface handling
221221
// if (status->state & STATE_SECURE_INTERFACE)
222222
// {
@@ -544,11 +544,11 @@ fpc_result_t sfDevFPC2534::parseCommand(uint8_t *payload, size_t size)
544544
if (payload == nullptr || size == 0)
545545
return FPC_RESULT_INVALID_PARAM;
546546

547-
Serial.printf("Parsing command of size %d\n\r", size);
547+
// Serial.printf("Parsing command of size %d\n\r", size);
548548

549549
fpc_cmd_hdr_t *cmdHeader = (fpc_cmd_hdr_t *)payload;
550550

551-
Serial.printf("Command ID: 0x%02X, Type: 0x%02X\n\r", cmdHeader->cmd_id, cmdHeader->type);
551+
// Serial.printf("Command ID: 0x%02X, Type: 0x%02X\n\r", cmdHeader->cmd_id, cmdHeader->type);
552552
// look legit?
553553
if (cmdHeader->type != FPC_FRAME_TYPE_CMD_EVENT && cmdHeader->type != FPC_FRAME_TYPE_CMD_RESPONSE)
554554
return FPC_RESULT_INVALID_PARAM;
@@ -638,36 +638,46 @@ fpc_result_t sfDevFPC2534::processNextResponse(bool flushNone)
638638

639639
fpc_frame_hdr_t frameHeader;
640640

641+
_comm->beginRead();
641642
/* Step 1: Read Frame Header */
642643
fpc_result_t rc = _comm->read((uint8_t *)&frameHeader, sizeof(fpc_frame_hdr_t));
643644

644645
// No data? No problem
645646
if (rc == FPC_RESULT_IO_NO_DATA)
646647
{
647-
// response.type = SFE_FPC_RESP_NONE;
648+
_comm->endRead();
648649
return FPC_RESULT_OK; // No data to process, just return
649650
}
650651
else if (rc != FPC_RESULT_OK)
652+
{
653+
_comm->endRead();
651654
return rc;
655+
}
652656

653657
// Debug output - helpful when developing
654-
Serial.printf("Frame Header: ver 0x%04X, type 0x%02X, flags 0x%04X, payload size %d\n\r", frameHeader.version,
655-
frameHeader.type, frameHeader.flags, frameHeader.payload_size);
658+
// Serial.printf("Frame Header: ver 0x%04X, type 0x%02X, flags 0x%04X, payload size %d\n\r", frameHeader.version,
659+
// frameHeader.type, frameHeader.flags, frameHeader.payload_size);
656660

657661
// Sanity check of the header...
658662
if (frameHeader.version != FPC_FRAME_PROTOCOL_VERSION ||
659-
((frameHeader.flags & (FPC_FRAME_FLAG_SENDER_FW_APP | FPC_FRAME_FLAG_SENDER_FW_BL)) == 0) ||
663+
((frameHeader.flags & FPC_FRAME_FLAG_SENDER_FW_APP) == 0) ||
660664
(frameHeader.type != FPC_FRAME_TYPE_CMD_RESPONSE && frameHeader.type != FPC_FRAME_TYPE_CMD_EVENT))
665+
{
666+
// Serial.println("Bad frame header");
667+
_comm->endRead();
661668
return FPC_RESULT_IO_BAD_DATA;
669+
}
662670

663671
// okay, lets read the payload
664672
uint8_t framePayload[frameHeader.payload_size];
665673

666674
rc = _comm->read(framePayload, frameHeader.payload_size);
667-
675+
_comm->endRead();
668676
if (rc != FPC_RESULT_OK)
677+
{
678+
Serial.printf("Error reading payload: %d\n\r", rc);
669679
return rc;
670-
680+
}
671681
// if we are flushing NONE events, and this is one, just return
672682
if (flushNone)
673683
{
@@ -691,7 +701,7 @@ fpc_result_t sfDevFPC2534::setLED(bool ledOn)
691701
requestSetGPIO(1, GPIO_CONTROL_MODE_OUTPUT_PP, ledOn ? GPIO_CONTROL_STATE_SET : GPIO_CONTROL_STATE_RESET);
692702
if (rc == FPC_RESULT_OK)
693703
{
694-
delay(20);
704+
delay(50);
695705
flushNoneEvent();
696706
}
697707
return rc;

src/sfTk/sfDevFPC2534IComm.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ void sfDevFPC2534IComm::clearISRDataAvailable(void)
9191
// Data available ?
9292
bool sfDevFPC2534IComm::isISRDataAvailable(void)
9393
{
94+
// Serial.printf("isISRDataAvailable: usingISRParam=%d, _dataAvailable=%d\r\n", _usingISRParam, _dataAvailable);
9495
// Are we using the ISR param method?
9596
if (_usingISRParam)
9697
return _dataAvailable;

src/sfTk/sfDevFPC2534IComm.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ class sfDevFPC2534IComm
2828
virtual void beginWrite(void) {};
2929
virtual void endWrite(void) {};
3030

31+
virtual void beginRead(void) {};
32+
virtual void endRead(void) {};
33+
3134
// public method -- for the ISR handler to set the data available flag for the specific object
3235
// representing the IRS callback parameter.
3336
void setISRDataAvailable(void);

src/sfTk/sfDevFPC2534SPI.cpp

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
// --------------------------------------------------------------------------------------------
1414
// CTOR
15-
sfDevFPC2534SPI::sfDevFPC2534SPI() : _inWrite{false}, _spiPort{nullptr}, _csPin{0}
15+
sfDevFPC2534SPI::sfDevFPC2534SPI() : _inWrite{false}, _inRead{false}, _spiPort{nullptr}, _csPin{0}
1616
{
1717
}
1818

@@ -80,7 +80,7 @@ void sfDevFPC2534SPI::beginWrite(void)
8080
// the datasheet specifiies a delay greater than 500us after CS goes low
8181
delayMicroseconds(600);
8282
_inWrite = true;
83-
Serial.println("Begin SPI Write");
83+
// Serial.println("Begin SPI Write");
8484
}
8585

8686
void sfDevFPC2534SPI ::endWrite(void)
@@ -91,7 +91,7 @@ void sfDevFPC2534SPI ::endWrite(void)
9191
digitalWrite(_csPin, HIGH);
9292
_spiPort->endTransaction();
9393
_inWrite = false;
94-
Serial.println("End SPI Write");
94+
// Serial.println("End SPI Write");
9595
}
9696
//--------------------------------------------------------------------------------------------
9797
// Write data to the device
@@ -101,7 +101,7 @@ uint16_t sfDevFPC2534SPI::write(const uint8_t *data, size_t len)
101101
if (_spiPort == nullptr)
102102
return FPC_RESULT_IO_RUNTIME_FAILURE; // I2C bus not initialized
103103

104-
Serial.printf("Writing %d bytes to SPI\r\n", len);
104+
// Serial.printf("Writing %d bytes to SPI\r\n", len);
105105
// now send the data
106106

107107
for (size_t i = 0; i < len; i++)
@@ -127,22 +127,58 @@ uint16_t sfDevFPC2534SPI::read(uint8_t *data, size_t len)
127127
if (_inWrite)
128128
endWrite();
129129

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+
138+
// if we are not in a read transaction, not okay.
139+
if (_inRead == false)
140+
return FPC_RESULT_IO_RUNTIME_FAILURE;
141+
142+
// Data available?
143+
if (!dataAvailable() && !_inRead)
144+
return FPC_RESULT_IO_NO_DATA;
145+
146+
// clear the data available flag
147+
clearISRDataAvailable();
148+
149+
// Serial.printf("Reading %d bytes from SPI\r\n", len);
150+
// Lets read the data...
151+
for (size_t i = 0; i < len; i++)
152+
*data++ = _spiPort->transfer(0x00);
153+
154+
// // End transaction
155+
// digitalWrite(_csPin, HIGH);
156+
// _spiPort->endTransaction();
157+
158+
return FPC_RESULT_OK;
159+
}
160+
void sfDevFPC2534SPI::beginRead(void)
161+
{
162+
if (_spiPort == nullptr)
163+
return; // SPI bus not initialized
164+
130165
_spiPort->beginTransaction(_spiSettings);
131166

132167
// Signal communication start
133168
digitalWrite(_csPin, LOW);
134-
135169
// the datasheet specifiies a delay greater than 500us after CS goes low
136170
delayMicroseconds(600);
137171

138-
Serial.printf("Reading %d bytes from SPI\r\n", len);
139-
// Lets read the data...
140-
for (size_t i = 0; i < len; i++)
141-
*data++ = _spiPort->transfer(0x00);
172+
_inRead = true;
173+
}
142174

143-
// End transaction
175+
void sfDevFPC2534SPI ::endRead(void)
176+
{
177+
if (_spiPort == nullptr || !_inRead)
178+
return; // SPI bus not initialized
179+
// End comms
144180
digitalWrite(_csPin, HIGH);
145181
_spiPort->endTransaction();
146182

147-
return FPC_RESULT_OK;
148-
}
183+
_inRead = false;
184+
}

src/sfTk/sfDevFPC2534SPI.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,20 @@ class sfDevFPC2534SPI : public sfDevFPC2534IComm
2727
bool bInit = false);
2828
bool initialize(uint8_t csPin, uint32_t interruptPin, bool bInit = false);
2929

30-
bool dataAvailable();
31-
void clearData();
32-
uint16_t write(const uint8_t *data, size_t len);
33-
uint16_t read(uint8_t *data, size_t len);
30+
bool dataAvailable() override;
31+
void clearData() override;
32+
uint16_t write(const uint8_t *data, size_t len) override;
33+
uint16_t read(uint8_t *data, size_t len) override;
3434

3535
void beginWrite(void) override;
3636
void endWrite(void) override;
3737

38+
void beginRead(void) override;
39+
void endRead(void) override;
40+
3841
private:
3942
bool _inWrite;
43+
bool _inRead;
4044

4145
// SPI Things
4246
SPIClass *_spiPort;

0 commit comments

Comments
 (0)