Advertisement
Guest User

esp32code

a guest
Jun 23rd, 2025
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.90 KB | None | 0 0
  1. #include <ESP32SPISlave.h>
  2.  
  3. #pragma once
  4.  
  5. #include <Arduino.h>
  6. #include <cstdint>
  7. #include <cstddef>
  8.  
  9. ESP32SPISlave slave;
  10.  
  11. static constexpr size_t BUFFER_SIZE = 16;
  12. static constexpr size_t QUEUE_SIZE = 1;
  13. uint8_t tx_buf[BUFFER_SIZE] {1, 2, 3, 4, 5, 6, 7, 8};
  14. uint8_t rx_buf[BUFFER_SIZE] {0, 0, 0, 0, 0, 0, 0, 0};
  15.  
  16. void dumpBuffers(const char *title, const uint8_t *buf, size_t start, size_t len)
  17. {
  18. // show title and range
  19. if (len == 1)
  20. printf("%s [%d]: ", title, start);
  21. else
  22. printf("%s [%d-%d]: ", title, start, start + len - 1);
  23.  
  24. // show data in the range
  25. for (size_t i = 0; i < len; i++) {
  26. printf("%02X ", buf[start + i]);
  27. }
  28. printf("\n");
  29. }
  30.  
  31. 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)
  32. {
  33. bool verified = true;
  34.  
  35. if (a_size != b_size) {
  36. printf("received data size does not match: expected = %d / actual = %d\n", a_size, b_size);
  37. return false;
  38. }
  39.  
  40. for (size_t i = 0; i < a_size; i++) {
  41. // if a_buf and b_buf is same, continue
  42. if (a_buf[i] == b_buf[i]) {
  43. continue;
  44. }
  45.  
  46. verified = false;
  47.  
  48. // if a_buf[i] and b_buf[i] is not same, check the range that has difference
  49. size_t j = 1;
  50. for (; i + j < a_size; ++j) {
  51. if (a_buf[i + j] != b_buf[i + j]) {
  52. break;
  53. }
  54. }
  55.  
  56. // dump different data range
  57. dumpBuffers(a_title, a_buf, i, j);
  58. dumpBuffers(b_title, b_buf, i, j);
  59.  
  60. // restart from next same index (-1 considers i++ in for())
  61. i += j - 1;
  62. }
  63. return verified;
  64. }
  65.  
  66. void initializeBuffers(uint8_t *tx, uint8_t *rx, size_t size, size_t offset = 0)
  67. {
  68. if (tx) {
  69. for (size_t i = 0; i < size; i++) {
  70. tx[i] = (i + offset) & 0xFF;
  71. }
  72. }
  73. if (rx) {
  74. memset(rx, 0, size);
  75. }
  76. }
  77.  
  78.  
  79. void setup()
  80. {
  81. Serial.begin(115200);
  82.  
  83. delay(2000);
  84.  
  85. slave.setDataMode(SPI_MODE0); // default: SPI_MODE0
  86. slave.setQueueSize(QUEUE_SIZE); // default: 1, requres 2 in this example
  87.  
  88. // begin() after setting
  89. slave.begin(); // default: HSPI (please refer README for pin assignments)
  90.  
  91. Serial.println("start spi slave");
  92. }
  93.  
  94. void loop()
  95. {
  96. // if no transaction is in flight and all results are handled, queue new transactions
  97. if (slave.hasTransactionsCompletedAndAllResultsHandled()) {
  98. // initialize tx/rx buffers
  99. Serial.println("initialize tx/rx buffers");
  100. initializeBuffers(tx_buf, rx_buf, BUFFER_SIZE, 0);
  101.  
  102. // queue transaction and trigger it right now
  103. Serial.println("execute transaction in the background");
  104. slave.queue(tx_buf, rx_buf, BUFFER_SIZE);
  105. slave.trigger();
  106.  
  107. Serial.println("wait for the completion of the queued transactions...");
  108. }
  109.  
  110. // you can do some other stuff here
  111. // NOTE: you can't touch dma_tx/rx_buf because it's in-flight in the background
  112.  
  113. // if all transactions are completed and all results are ready, handle results
  114. if (slave.hasTransactionsCompletedAndAllResultsReady(QUEUE_SIZE)) {
  115. // process received data from slave
  116. Serial.println("all queued transactions completed. start verifying received data from slave");
  117.  
  118. // get the oldeest transfer result
  119. size_t received_bytes = slave.numBytesReceived();
  120.  
  121. // verify and dump difference with received data
  122. // NOTE: we need only 1st results (received_bytes[0])
  123. if (verifyAndDumpDifference("slave", tx_buf, BUFFER_SIZE, "master", rx_buf, received_bytes)) {
  124. Serial.println("successfully received expected data from master");
  125. } else {
  126. Serial.println("unexpected difference found between master/slave data");
  127. }
  128. }
  129. delay(500);
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement