Skip to content

Commit 48ce11a

Browse files
committed
work in progress for SPI impl - getting there
1 parent 0f1daa7 commit 48ce11a

File tree

4 files changed

+50
-15
lines changed

4 files changed

+50
-15
lines changed

examples/Example05_NavigationSPI/Example05_NavigationSPI.ino

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
// ESP32 IoT RedBoard
6161
#define IRQ_PIN 26
6262
#define RST_PIN 27
63-
#define CS_PIN 6
63+
#define CS_PIN 25
6464

6565
// rp2350 thing plus
6666
// #define IRQ_PIN 11
@@ -119,6 +119,7 @@ static void on_is_ready_change(bool isReady)
119119
{
120120
// Place the sensor in Navigation mode and print out a menue.
121121
startNavigation = false;
122+
Serial.println("[SETUP]\tStarting Navigation mode...");
122123
fpc_result_t rc = mySensor.startNavigationMode(0);
123124

124125
// error?
@@ -282,6 +283,8 @@ void loop()
282283
{
283284
Serial.print("[ERROR] Sensor Processing Error: ");
284285
Serial.println(rc);
286+
// mySensor.clearData();
287+
// reset_sensor();
285288
}
286289

287290
delay(200);

src/sfTk/sfDevFPC2534.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,13 @@ fpc_result_t sfDevFPC2534::sendCommand(fpc_cmd_hdr_t &cmd, size_t size)
3232
frameHeader.payload_size = (uint16_t)size;
3333

3434
// send message header, then payload
35+
_comm->beginWrite();
3536
fpc_result_t rc = _comm->write((uint8_t *)&frameHeader, sizeof(fpc_frame_hdr_t));
3637

3738
if (rc == FPC_RESULT_OK)
3839
rc = _comm->write((uint8_t *)&cmd, size);
3940

41+
_comm->endWrite();
4042
return rc;
4143
}
4244
//--------------------------------------------------------------------------------------------
@@ -213,6 +215,8 @@ fpc_result_t sfDevFPC2534::parseStatusCommand(fpc_cmd_hdr_t *cmd_hdr, size_t siz
213215

214216
fpc_cmd_status_response_t *status = (fpc_cmd_status_response_t *)cmd_hdr;
215217

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);
216220
// TODO: Implement secure interface handling
217221
// if (status->state & STATE_SECURE_INTERFACE)
218222
// {
@@ -540,8 +544,11 @@ fpc_result_t sfDevFPC2534::parseCommand(uint8_t *payload, size_t size)
540544
if (payload == nullptr || size == 0)
541545
return FPC_RESULT_INVALID_PARAM;
542546

547+
Serial.printf("Parsing command of size %d\n\r", size);
548+
543549
fpc_cmd_hdr_t *cmdHeader = (fpc_cmd_hdr_t *)payload;
544550

551+
Serial.printf("Command ID: 0x%02X, Type: 0x%02X\n\r", cmdHeader->cmd_id, cmdHeader->type);
545552
// look legit?
546553
if (cmdHeader->type != FPC_FRAME_TYPE_CMD_EVENT && cmdHeader->type != FPC_FRAME_TYPE_CMD_RESPONSE)
547554
return FPC_RESULT_INVALID_PARAM;
@@ -644,8 +651,8 @@ fpc_result_t sfDevFPC2534::processNextResponse(bool flushNone)
644651
return rc;
645652

646653
// Debug output - helpful when developing
647-
// Serial.printf("Frame Header: ver 0x%04X, type 0x%02X, flags 0x%04X, payload size %d\n\r", frameHeader.version,
648-
// frameHeader.type, frameHeader.flags, frameHeader.payload_size);
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);
649656

650657
// Sanity check of the header...
651658
if (frameHeader.version != FPC_FRAME_PROTOCOL_VERSION ||

src/sfTk/sfDevFPC2534SPI.cpp

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

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

@@ -42,7 +42,7 @@ bool sfDevFPC2534SPI::initialize(SPIClass &spiPort, SPISettings &busSPISettings,
4242
bool sfDevFPC2534SPI::initialize(uint8_t csPin, uint32_t interruptPin, bool bInit)
4343
{
4444
// If the transaction settings are not provided by the user they are built here.
45-
SPISettings spiSettings = SPISettings(3000000, MSBFIRST, SPI_MODE3);
45+
SPISettings spiSettings = SPISettings(3000000, MSBFIRST, SPI_MODE0);
4646
return initialize(SPI, spiSettings, csPin, interruptPin, bInit);
4747
}
4848
//--------------------------------------------------------------------------------------------
@@ -67,29 +67,45 @@ void sfDevFPC2534SPI::clearData()
6767
clearISRDataAvailable();
6868
}
6969

70-
//--------------------------------------------------------------------------------------------
71-
// Write data to the device
72-
//
73-
uint16_t sfDevFPC2534SPI::write(const uint8_t *data, size_t len)
70+
void sfDevFPC2534SPI::beginWrite(void)
7471
{
72+
7573
if (_spiPort == nullptr)
76-
return FPC_RESULT_IO_RUNTIME_FAILURE; // I2C bus not initialized
74+
return; // SPI bus not initialized
7775

7876
_spiPort->beginTransaction(_spiSettings);
7977

8078
// Signal communication start
8179
digitalWrite(_csPin, LOW);
82-
8380
// the datasheet specifiies a delay greater than 500us after CS goes low
8481
delayMicroseconds(600);
82+
_inWrite = true;
83+
Serial.println("Begin SPI Write");
84+
}
8585

86-
// now send the data
87-
for (size_t i = 0; i < len; i++)
88-
_spiPort->transfer(*data++);
89-
86+
void sfDevFPC2534SPI ::endWrite(void)
87+
{
88+
if (_spiPort == nullptr)
89+
return; // SPI bus not initialized
9090
// End comms
9191
digitalWrite(_csPin, HIGH);
9292
_spiPort->endTransaction();
93+
_inWrite = false;
94+
Serial.println("End SPI Write");
95+
}
96+
//--------------------------------------------------------------------------------------------
97+
// Write data to the device
98+
//
99+
uint16_t sfDevFPC2534SPI::write(const uint8_t *data, size_t len)
100+
{
101+
if (_spiPort == nullptr)
102+
return FPC_RESULT_IO_RUNTIME_FAILURE; // I2C bus not initialized
103+
104+
Serial.printf("Writing %d bytes to SPI\r\n", len);
105+
// now send the data
106+
107+
for (size_t i = 0; i < len; i++)
108+
_spiPort->transfer(*data++);
93109

94110
return FPC_RESULT_OK;
95111
}
@@ -108,6 +124,9 @@ uint16_t sfDevFPC2534SPI::read(uint8_t *data, size_t len)
108124
if (data == nullptr)
109125
return FPC_RESULT_INVALID_PARAM;
110126

127+
if (_inWrite)
128+
endWrite();
129+
111130
_spiPort->beginTransaction(_spiSettings);
112131

113132
// Signal communication start
@@ -116,6 +135,7 @@ uint16_t sfDevFPC2534SPI::read(uint8_t *data, size_t len)
116135
// the datasheet specifiies a delay greater than 500us after CS goes low
117136
delayMicroseconds(600);
118137

138+
Serial.printf("Reading %d bytes from SPI\r\n", len);
119139
// Lets read the data...
120140
for (size_t i = 0; i < len; i++)
121141
*data++ = _spiPort->transfer(0x00);

src/sfTk/sfDevFPC2534SPI.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,12 @@ class sfDevFPC2534SPI : public sfDevFPC2534IComm
3232
uint16_t write(const uint8_t *data, size_t len);
3333
uint16_t read(uint8_t *data, size_t len);
3434

35+
void beginWrite(void) override;
36+
void endWrite(void) override;
37+
3538
private:
39+
bool _inWrite;
40+
3641
// SPI Things
3742
SPIClass *_spiPort;
3843
SPISettings _spiSettings;

0 commit comments

Comments
 (0)