I just received my first ESP32-S2 boards and started porting a small example that was working on a "regular" ESP32, and things were going surprisingly good until SPI got in the way.
I'm testing an UWB module from Decawave attached to the SPI2_HOST bus, on both boards. On the regular ESP32 the result of reading register 0 (chip ID) is 0x30 0x01 0xCA 0xDE, however the ESP32-S2 returns 0x60 0x03 0x95 0xBC
I checked with the logic analyzer and in fact what the UWB module is outputting is correct, but for some reason data gets mixed up on the chip.
I have the device configured to use DMA transfers this way:
Initialization function:
Code: Select all
DWM1000_t dwm_init(void) {
DWM1000_t device;
esp_err_t ret = ESP_OK;
device.isError = 0;
#ifdef USE_DWM_LOGS
ESP_LOGI(TAG, "Initializing DWM1000 device.");
#endif
memset(&device.spi_config, 0x00, sizeof(device.spi_config));
memset(&device.spi_handle, 0x00, sizeof(device.spi_handle));
spi_bus_config_t buscfg={
.miso_io_num = PIN_SPI_MISO,
.mosi_io_num = PIN_SPI_MOSI,
.sclk_io_num = PIN_SPI_CLK,
.quadwp_io_num = -1,
.quadhd_io_num = -1,
.max_transfer_sz = SPI_MAX_DMA_LEN,
};
spi_device_interface_config_t devcfg={
.input_delay_ns = 100,
.command_bits = 0,
.address_bits = 0,
.dummy_bits = 0,
.duty_cycle_pos = 0,
.cs_ena_pretrans = 5,
.cs_ena_posttrans = 5,
.clock_speed_hz = SPI_CLK_FREQ,
.mode = SPI_MODE,
.spics_io_num = PIN_SPI_CS,
.queue_size = 1,
.flags = 0,
.pre_cb = NULL,
.post_cb = NULL,
};
ret = spi_bus_initialize(SPI_CHANNEL, &buscfg, SPI_DMA_CHANNEL);
ESP_ERROR_CHECK(ret);
device.spi_config.cs_io = PIN_SPI_CS;
device.spi_config.host = SPI_CHANNEL;
device.spi_config.miso_io = PIN_SPI_MISO;
ret = spi_init(&device.spi_config, &device.spi_handle, &devcfg);
ESP_ERROR_CHECK(ret);
#ifdef USE_DWM_LOGS
ESP_LOGI(TAG, "DWM1000 initialization done success.");
#endif
return (device);
}
Code: Select all
esp_err_t spi_dma_transfer_bytes(spi_context_t* ctx, uint8_t cmd, uint8_t *txBuffer, uint8_t *rxBuffer, uint16_t length) {
esp_err_t err;
err = spi_device_acquire_bus(ctx->spi, portMAX_DELAY);
if (err != ESP_OK) return err;
uint8_t* buf = (uint8_t*)malloc(sizeof(uint8_t)*length + 1);
if (buf == NULL) return ESP_ERR_NO_MEM;
memset(buf, 0x00,length*sizeof(uint8_t) + 1);
memmove(&buf[1], txBuffer, length*sizeof(uint8_t));
buf[0] = cmd;
spi_transaction_t trans_t;
trans_t.rx_buffer = rxBuffer;
trans_t.tx_buffer = buf;
trans_t.length = (8 * length) + 8;
trans_t.rxlength = trans_t.length;
trans_t.flags = 0;
trans_t.cmd = 0;
trans_t.addr = 0;
trans_t.user = NULL;
err = spi_device_transmit(ctx->spi, &trans_t);
memmove(rxBuffer, &rxBuffer[1], length*sizeof(uint8_t));
rxBuffer[length] = 0x00;
spi_device_release_bus(ctx->spi);
free (buf);
return err;
}
Thanks,