Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP32SPISlave.h>
- #pragma once
- #include <Arduino.h>
- #include <cstdint>
- #include <cstddef>
- ESP32SPISlave slave;
- static constexpr size_t BUFFER_SIZE = 16;
- static constexpr size_t QUEUE_SIZE = 1;
- 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 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;
- for (; i + j < a_size; ++j) {
- if (a_buf[i + j] != b_buf[i + j]) {
- break;
- }
- }
- // 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);
- }
- }
- void setup()
- {
- Serial.begin(115200);
- delay(2000);
- slave.setDataMode(SPI_MODE0); // default: SPI_MODE0
- slave.setQueueSize(QUEUE_SIZE); // default: 1, requres 2 in this example
- // begin() after setting
- slave.begin(); // default: HSPI (please refer README for pin assignments)
- Serial.println("start spi slave");
- }
- void loop()
- {
- // if no transaction is in flight and all results are handled, queue new transactions
- if (slave.hasTransactionsCompletedAndAllResultsHandled()) {
- // initialize tx/rx buffers
- Serial.println("initialize tx/rx buffers");
- initializeBuffers(tx_buf, rx_buf, BUFFER_SIZE, 0);
- // queue transaction and trigger it right now
- Serial.println("execute transaction in the background");
- slave.queue(tx_buf, rx_buf, BUFFER_SIZE);
- slave.trigger();
- Serial.println("wait for the completion of the queued transactions...");
- }
- // you can do some other stuff here
- // NOTE: you can't touch dma_tx/rx_buf because it's in-flight in the background
- // if all transactions are completed and all results are ready, handle results
- if (slave.hasTransactionsCompletedAndAllResultsReady(QUEUE_SIZE)) {
- // process received data from slave
- Serial.println("all queued transactions completed. start verifying received data from slave");
- // get the oldeest transfer result
- size_t received_bytes = slave.numBytesReceived();
- // verify and dump difference with received data
- // NOTE: we need only 1st results (received_bytes[0])
- if (verifyAndDumpDifference("slave", tx_buf, BUFFER_SIZE, "master", rx_buf, received_bytes)) {
- Serial.println("successfully received expected data from master");
- } else {
- Serial.println("unexpected difference found between master/slave data");
- }
- }
- delay(500);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement