-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from hideakitai/fix/transaction-timeout
fix: transaction timeout
- Loading branch information
Showing
11 changed files
with
286 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
examples/transfer_one_by_one_with_timeout/transfer_one_by_one_with_timeout_master/helper.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#pragma once | ||
|
||
#include <Arduino.h> | ||
#include <cstdint> | ||
#include <cstddef> | ||
|
||
void dumpBuffers(const char *title, const uint8_t *buf, size_t start, size_t len) | ||
{ | ||
// show title and range | ||
if (len == 1) | ||
printf("%s [%d]: ", title, start); | ||
else | ||
printf("%s [%d-%d]: ", title, start, start + len - 1); | ||
|
||
// show data in the range | ||
for (size_t i = 0; i < len; i++) { | ||
printf("%02X ", buf[start + i]); | ||
} | ||
printf("\n"); | ||
} | ||
|
||
bool verifyAndDumpDifference(const char *a_title, const uint8_t *a_buf, size_t a_size, const char *b_title, const uint8_t *b_buf, size_t b_size) | ||
{ | ||
bool verified = true; | ||
|
||
if (a_size != b_size) { | ||
printf("received data size does not match: expected = %d / actual = %d\n", a_size, b_size); | ||
return false; | ||
} | ||
|
||
for (size_t i = 0; i < a_size; i++) { | ||
// if a_buf and b_buf is same, continue | ||
if (a_buf[i] == b_buf[i]) { | ||
continue; | ||
} | ||
|
||
verified = false; | ||
|
||
// if a_buf[i] and b_buf[i] is not same, check the range that has difference | ||
size_t j = 1; | ||
while (a_buf[i + j] != b_buf[i + j]) { | ||
j++; | ||
} | ||
|
||
// dump different data range | ||
dumpBuffers(a_title, a_buf, i, j); | ||
dumpBuffers(b_title, b_buf, i, j); | ||
|
||
// restart from next same index (-1 considers i++ in for()) | ||
i += j - 1; | ||
} | ||
return verified; | ||
} | ||
|
||
void initializeBuffers(uint8_t *tx, uint8_t *rx, size_t size, size_t offset = 0) | ||
{ | ||
if (tx) { | ||
for (size_t i = 0; i < size; i++) { | ||
tx[i] = (i + offset) & 0xFF; | ||
} | ||
} | ||
if (rx) { | ||
memset(rx, 0, size); | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
...meout/transfer_one_by_one_with_timeout_master/transfer_one_by_one_with_timeout_master.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#include <SPI.h> | ||
#include "helper.h" | ||
|
||
SPIClass master(HSPI); | ||
#ifdef CONFIG_IDF_TARGET_ESP32 | ||
static constexpr uint8_t PIN_SS = 15; | ||
#else | ||
static constexpr uint8_t PIN_SS = SS; | ||
#endif | ||
|
||
static constexpr size_t BUFFER_SIZE = 8; | ||
uint8_t tx_buf[BUFFER_SIZE] {1, 2, 3, 4, 5, 6, 7, 8}; | ||
uint8_t rx_buf[BUFFER_SIZE] {0, 0, 0, 0, 0, 0, 0, 0}; | ||
|
||
void setup() | ||
{ | ||
Serial.begin(115200); | ||
|
||
delay(2000); | ||
|
||
pinMode(PIN_SS, OUTPUT); | ||
digitalWrite(PIN_SS, HIGH); | ||
master.begin(SCK, MISO, MOSI, PIN_SS); | ||
|
||
delay(2000); | ||
|
||
Serial.println("start spi master"); | ||
} | ||
|
||
void loop() | ||
{ | ||
// initialize tx/rx buffers | ||
initializeBuffers(tx_buf, rx_buf, BUFFER_SIZE); | ||
|
||
master.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE0)); | ||
digitalWrite(PIN_SS, LOW); | ||
master.transferBytes(tx_buf, rx_buf, BUFFER_SIZE); | ||
digitalWrite(PIN_SS, HIGH); | ||
master.endTransaction(); | ||
|
||
// verify and dump difference with received data | ||
if (verifyAndDumpDifference("master", tx_buf, BUFFER_SIZE, "slave", rx_buf, BUFFER_SIZE)) { | ||
Serial.println("successfully received expected data from slave"); | ||
} else { | ||
Serial.println("unexpected difference found between master/slave data"); | ||
} | ||
|
||
delay(2500); // slave will timeout after 1000ms | ||
} |
Oops, something went wrong.