Page 1 of 1

SPI communication only works at size 8??

Posted: Thu Dec 19, 2024 2:26 pm
by Jammer
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?