I am using the <ESP32SPISlave.h> library to setup communication between my esp32 as slave and stm32 h503rb as master.
I've been struggling to make it work but finally it did, or so i thought. The example code only works when the BUFFER_SIZE = 8??
When i change it to lower or higher I see that it doesn't even wait for a transfer anymore?? Can someone give me some insight to this weird issue? I'm literally going crazy that it only works at a certain size and I've never seen an issue like this before.
This is my Arduino code:
// the dump and initialize buffer functions just empty and print rx_buf
#include <ESP32SPISlave.h>
#include "helper.h"
ESP32SPISlave slave;
#define SPI_CS 5 // Chip Select pin
#define SPI_MOSI 23 // Master Out Slave In
#define SPI_MISO 19 // Master In Slave Out
#define SPI_SCK 18 // Clock pin); // default: HSPI (please refer README for pin assignments)
static constexpr size_t BUFFER_SIZE = 8;
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 setup()
{
slave.setDataMode(SPI_MODE0); // default: SPI_MODE0
slave.setQueueSize(QUEUE_SIZE); // default: 1
slave.begin(VSPI, SPI_SCK, SPI_MISO, SPI_MOSI, SPI_CS);
}
void loop()
{
// initialize tx/rx buffers
initializeBuffers(tx_buf, rx_buf, BUFFER_SIZE);
// start and wait to complete one BIG transaction (same data will be received from slave)
const size_t received_bytes = slave.transfer(tx_buf, rx_buf, BUFFER_SIZE);
dumpBuffers("rx", rx_buf, 0, 3);
}
And this is my relevant stm32 code:
char uart_buf[50];
int uart_buf_len;
uint8_t spi_buf[20];
uint8_t rx_buf[8] = {0}; // Buffer to receive data
uint8_t array8[128]={0};
uint16_t array16[128]={0};
array8[0]=1;
array8[1]=2;
array8[2]=3;
array16[0]=1;
array16[1]=2;
array16[2]=3;
void receiveTupleArray(uint8_t tupleCount, uint8_t uint8Array[], uint16_t uint16Array[]) {
uint8_t buffer[tupleCount * 3]; // Each tuple is 3 bytes
int index = 0;
// Deserialize the buffer into the tuple array
for (uint8_t i = 0; i < tupleCount; i++) {
buffer[index++] = uint8Array; // Add uint8_t
buffer[index++] = (uint8_t)((uint16Array & 0xFF00) >> 8); // Add high byte of uint16_t
buffer[index++] = (uint8_t)(uint16Array & 0x00FF); // Add low byte of uint16_t
}
for (uint8_t i = 0; i < 9; i++) {
spi_buf=buffer;
}
}
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
// Say something
uart_buf_len = sprintf(uart_buf, "SPI Test\r\n");
HAL_UART_Transmit(&huart3, (uint8_t *)uart_buf, uart_buf_len, 100);
receiveTupleArray(3,array8, array16);
// Start SPI transaction and transmit/receive data
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_4, GPIO_PIN_RESET); // Set CS pin low (select SPI)
// Transmit data and receive simultaneously
HAL_SPI_TransmitReceive(&hspi1, (uint8_t *)spi_buf, rx_buf, 9, 100);
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_4, GPIO_PIN_SET); // Set CS pin high (deselect SPI)
uart_buf_len = sprintf(uart_buf, "\r\nData Received: ");
HAL_UART_Transmit(&huart3, (uint8_t *)uart_buf, uart_buf_len, 100);
// Print received data
for (int i = 0; i < 9; i++) {
uart_buf_len = sprintf(uart_buf, "0x%02X ", spi_buf);
HAL_UART_Transmit(&huart3, (uint8_t *)uart_buf, uart_buf_len, 100);
}
uart_buf_len = sprintf(uart_buf, "\r\n");
HAL_UART_Transmit(&huart3, (uint8_t *)uart_buf, uart_buf_len, 100);
HAL_Delay(5000);
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
then this is the output of Arduino with buffersize 8 which is correct:
rx [0-2]: 01 0001 02 0002 03 0005 // the last should be 5 but the buffer is only 8 long so it prints random or something(not my issue)
and it spams this super rapidly when buffersize is anything else:
rx [0-2]: 00 0000 00 0000 00 0000
anyone have a solution?
SPI communication only works at size 8??
Jump to
- English Forum
- Explore
- News
- General Discussion
- FAQ
- Documentation
- Documentation
- Sample Code
- Discussion Forum
- ESP32-S31
- Hardware
- ESP-IDF
- ESP-BOX
- ESP-ADF
- ESP-MDF
- ESP-WHO
- ESP-SkaiNet
- ESP32 Arduino
- IDEs for ESP-IDF
- ESP-AT
- ESP IoT Solution
- ESP RainMaker
- Rust
- ESP8266
- Report Bugs
- Showcase
- Chinese Forum 中文社区
- 活动区
- 乐鑫活动专区
- 讨论区
- ESP32-S31 中文讨论版
- 喵伴 中文讨论版
- ESP-IDF 中文讨论版
- 《ESP32-C3 物联网工程开发实战》书籍讨论版
- 中文文档讨论版
- ESP-AT 中文讨论版
- ESP-BOX 中文讨论版
- ESP IoT Solution 中文讨论版
- ESP-ADF 中文讨论版
- ESP Mesh 中文讨论版
- ESP Cloud 中文讨论版
- ESP-WHO 中文讨论版
- ESP-SkaiNet 中文讨论版
- ESP 生产支持讨论版
- 硬件问题讨论
- 项目展示
Who is online
Users browsing this forum: PetalBot and 1 guest
- All times are UTC
- Top
- Delete cookies
About Us
Espressif Systems is a fabless semiconductor company providing cutting-edge low power WiFi SoCs and wireless solutions for wireless communications and Internet of Things applications. ESP8266EX and ESP32 are some of our products.
Information
Espressif ESP32 ... Available now!