ESP32 master to SAMD21(Arduino Zero) slave full duplex SPI data exchange issue.
Posted: Thu Jul 06, 2023 10:24 am
ESP32(master) and SAMD21(slave) use SPI bus to communicate. At ESP32 side MISO = GPIO_NUM_19, MOSI = GPIO_NUM_23, CLK = GPIO_NUM_18 though CS = GPIO 12(gpio 5 and 2 will be used for another SPI devices) . The master has 32 bytes TX buffer and the same size RX buffer. Once a second the master sends a pack of 32 bytes (0x00... 0x1F) and supposes to receive 32 bytes (0xA0... 0xBF).
I implemented SPI handler logger at slave side and it seems that the slave receives/sends data all right. There is no data corruption at the slave side. However there are the first 4 spi handler envokings which look strange.
Unfortunately there is worse situation at the master's side. The received pack is always shifted to the right on 2 bytes because of unknown reason:
Any ideas why the master acts like that? Any suggestions?
- #include <stdio.h>
- #include <string.h>
- #include "freertos/FreeRTOS.h"
- #include "freertos/task.h"
- #include "driver/spi_master.h"
- #include "esp_log.h"
- #include "driver/gpio.h"
- #define CS_SAMD21 GPIO_NUM_12
- #define RADIO_RX_BUF_LEN 32
- #define RADIO_TX_BUF_LEN 32
- typedef enum{
- NONE = 0xF0,
- GET_ALL = 0xF1,
- GET_RADIO = 0xF2,
- GET_LASER = 0xF3,
- GET_AUX = 0xF4,
- GET_EXT = 0xF5,
- TAKE_RADIO = 0xF6,
- TAKE_DISCR = 0xF7
- }SPI_cmd_t;
- #define SPI_CLOCK_SPEED 1000000
- #define TAG "M"
- spi_device_handle_t SPI_W25Q128;
- spi_device_handle_t SPI_SAMD21;
- spi_device_handle_t SPI_NANO;
- uint8_t tx_buf[RADIO_TX_BUF_LEN] = {
- 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
- 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
- 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
- 30, 31
- };
- void spi_init()
- {
- esp_err_t ret;
- spi_bus_config_t buscfg = {
- .miso_io_num = GPIO_NUM_19,
- .mosi_io_num = GPIO_NUM_23,
- .sclk_io_num = GPIO_NUM_18,
- .quadwp_io_num = -1,
- .quadhd_io_num = -1,
- .max_transfer_sz = 0,
- };
- spi_device_interface_config_t devcfg = {
- .command_bits = 0,
- .address_bits = 0,
- .dummy_bits = 0,
- .mode = 0,
- .duty_cycle_pos = 0,
- .cs_ena_pretrans = 0,
- .cs_ena_posttrans = 0,
- .clock_speed_hz = SPI_CLOCK_SPEED,
- .input_delay_ns = 0,
- .spics_io_num = -1,
- .flags = 0,
- .queue_size = 1,
- .pre_cb = NULL,
- .post_cb = NULL,
- };
- // SPI bus init
- ret = spi_bus_initialize(SPI2_HOST, &buscfg, 1);
- assert(ret == ESP_OK);
- // add slave to the bus
- devcfg.spics_io_num = CS_SAMD21;
- ret = spi_bus_add_device(SPI2_HOST, &devcfg, &SPI_SAMD21);
- assert(ret == ESP_OK);
- }
- void spi_exchange_data(spi_device_handle_t device, const uint8_t *tx_data, uint8_t *rx_data, size_t len)
- {
- spi_transaction_t t;
- memset(&t, 0, sizeof(t));
- t.length = len * 8;
- t.rxlength = len * 8;
- t.rx_buffer = rx_data;
- t.tx_buffer = tx_data;
- esp_err_t ret = spi_device_transmit(device, &t);
- assert(ret == ESP_OK);
- }
- void app_main(void)
- {
- spi_init();
- vTaskDelay(1000 / portTICK_PERIOD_MS);
- while (1)
- {
- vTaskDelay(1000 / portTICK_PERIOD_MS);
- uint8_t rx_data[RADIO_RX_BUF_LEN] = {0};
- spi_exchange_data(SPI_SAMD21, tx_buf, rx_data, RADIO_TX_BUF_LEN);
- ESP_LOGI(TAG, "received: ");
- for(uint8_t i = 0; i<RADIO_RX_BUF_LEN; i++){
- printf("%x ", rx_data[i]);
- }
- printf("\n");
- }
- }
- trig 1 write A0 leave //wtf?
- trig 8 SSL leave //wtf?
- trig 8 SSL leave //wtf?
- trig 2 TXC leave //wtf?
- trig 8 SSL leave // since here all looks as expected
- trig 5 read 0 write A0 leave
- trig 5 read 1 write A1 leave
- trig 5 read 2 write A2 leave
- trig 5 read 3 write A3 leave
- trig 5 read 4 write A4 leave
- ...etc
- I (10979324) M: received:
- 0 a0 a0 a1 a2 a3 a4 a5 a6 a7 9 a8 a9 aa ab ac ad ae af b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 ba bb bc
- I (10980324) M: received:
- bd be a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 ba bb bc bd
- I (10981324) M: received:
- be bf a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 ba bb bc bd