Unhandled exception in spi_device_polling_start()

janick
Posts: 2
Joined: Wed May 29, 2024 2:39 am

Unhandled exception in spi_device_polling_start()

Postby janick » Wed May 29, 2024 2:48 am

I'm trying to interface with an ADX345 (https://www.analog.com/media/en/technic ... dxl345.pdf) using 5MHz 4-wire SPI on HSPI_HOST controller. I worked through the various error messages but now I hit an unhandled exception error in the esp_spi_driver component.

Error message exerpt:
  1. I (31) boot: ESP-IDF v5.3-dev-3593-g64a1ed07d9-dirty 2nd stage bootloader
  2. I (37) boot: chip revision: v3.0
  3. I (41) boot.esp32: SPI Speed      : 40MHz
  4. I (46) boot.esp32: SPI Mode       : DIO
  5. I (50) boot.esp32: SPI Flash Size : 2MB
  6. I (224) cpu_start: Pro cpu start user code
  7. I (224) cpu_start: cpu freq: 160000000 Hz
  8. I (249) app_init: ESP-IDF:          v5.3-dev-3593-g64a1ed07d9-dirty
  9. I (256) efuse_init: Min chip rev:     v0.0
  10. I (261) efuse_init: Max chip rev:     v3.99
  11. I (266) efuse_init: Chip rev:         v3.0
  12. I (311) spi_flash: detected chip: generic
  13. I (347) Main: Initializing SPI bus... on core 0
  14. I (347) Main: Adding device to SPI bus...
  15. I (357) Main: Probing accelerator...
  16. Guru Meditation Error: Core  0 panic'ed (InstrFetchProhibited). Exception was unhandled.
  17.  
  18. Core  0 register dump:
  19. PC      : 0x00000003  PS      : 0x00060e30  A0      : 0x800858f8  A1      : 0x3ffb1c00  
  20. A2      : 0x3ffb3120  A3      : 0x3ffb3064  A4      : 0x3ffb2f7c  A5      : 0x3ffb3158  
  21. A6      : 0x3ffb1cb0  A7      : 0x3ffb302c  A8      : 0x800855c4  A9      : 0x3ffb1be0  
  22. A10     : 0x3ffb1cb0  A11     : 0x3ffb1c00  A12     : 0x3ffb1c00  A13     : 0x00000008  
  23. A14     : 0x00000000  A15     : 0x3ffb300c  SAR     : 0x00000008  EXCCAUSE: 0x00000014  
  24. EXCVADDR: 0x00000000  LBEG    : 0x4000c2e0  LEND    : 0x4000c2f6  LCOUNT  : 0x00000000  
  25. 0x4000c2e0: memcpy in ROM
  26. 0x4000c2f6: memcpy in ROM
  27.  
  28. Backtrace: 0x00000000:0x3ffb1c00 0x400858f5:0x3ffb1c50 0x400859c2:0x3ffb1c80 0x400d6446:0x3ffb1ca0 0x400d65e5:0x3ffb1d00 0x400d6939:0x3ffb1da0 0x400d6971:0x3ffb1dd0 0x400e9518:0x3ffb1df0 0x40087a65:0x3ffb1e20
  29. 0x400858f5: spi_device_polling_start at /Users/janick/Documents/IDF/esp-idf/components/esp_driver_spi/src/gpspi/spi_master.c:1376
  30. 0x400859c2: spi_device_polling_transmit at /Users/janick/Documents/IDF/esp-idf/components/esp_driver_spi/src/gpspi/spi_master.c:1431
My code:
  1.     spi_bus_config_t busConfig;
  2.         busConfig.mosi_io_num     = 23;
  3.         busConfig.miso_io_num     = 19;
  4.         busConfig.sclk_io_num     = 18;
  5.         busConfig.quadwp_io_num   = -1;
  6.         busConfig.quadhd_io_num   = -1;
  7.         busConfig.max_transfer_sz = 4;  // bytes
  8.         busConfig.flags           = SPICOMMON_BUSFLAG_MASTER | SPICOMMON_BUSFLAG_SCLK | SPICOMMON_BUSFLAG_MISO | SPICOMMON_BUSFLAG_MOSI;
  9.         busConfig.intr_flags      = 0;
  10.         busConfig.isr_cpu_id      = ESP_INTR_CPU_AFFINITY_AUTO;
  11.         ESP_LOGI(TAG, "Initializing SPI bus... on core %d", xTaskGetCoreID(xTaskGetCurrentTaskHandle()));
  12.         ESP_ERROR_CHECK(spi_bus_initialize(HSPI_HOST, &busConfig, SPI_DMA_DISABLED));
  13.  
  14.         spi_device_interface_config_t devConfig;
  15.         devConfig.command_bits   = 0;
  16.         devConfig.address_bits   = 8;
  17.         devConfig.dummy_bits     = 0;
  18.         devConfig.mode           = 0;
  19.         devConfig.clock_speed_hz = 5000000;
  20.         devConfig.spics_io_num   = 5;
  21.         devConfig.queue_size     = 32;
  22.         devConfig.flags          = SPI_DEVICE_HALFDUPLEX;
  23.         ESP_LOGI(TAG, "Adding device to SPI bus...");
  24.         ESP_ERROR_CHECK(spi_bus_add_device(HSPI_HOST, &devConfig, &mSPIDev));
  25.  
  26.         ESP_LOGI(TAG, "Probing accelerator...");
  27.         uint8_t devId = accRead(0x00);
  28.  
  29. uint32_t accRead(uint8_t addr, uint8_t bytes = 1)
  30.     {
  31.         if (bytes == 0) return 0;
  32.  
  33.         if (bytes > 4) bytes = 4;
  34.  
  35.         addr &= 0x3F;
  36.  
  37.         spi_transaction_t trans;
  38.         trans.flags     = SPI_TRANS_USE_RXDATA;
  39.         trans.tx_buffer = nullptr;
  40.         trans.addr      = 0x80 | addr;
  41.         if (bytes > 1) trans.addr |= 0x40;
  42.         trans.length    = 0;
  43.         trans.rxlength  = bytes * 8U;
  44.  
  45.         ESP_ERROR_CHECK(spi_device_polling_transmit(mSPIDev, &trans));
  46.  
  47.         auto rdat = *((uint32_t*)trans.rx_data);
  48.         if (bytes < 4) rdat &= (1U << (bytes * 8)) - 1;
  49.  
  50.         ESP_LOGI(TAG, "accRead 0x%02x = 0x%08lx", addr, rdat);
  51.        
  52.         return rdat;
  53.     }

janick
Posts: 2
Joined: Wed May 29, 2024 2:39 am

Re: Unhandled exception in spi_device_polling_start()

Postby janick » Wed May 29, 2024 1:10 pm

Turns out I needed to explicitly set the callback to NULL. I miss constructors and properly initialized structs!
  1.         spi_device_interface_config_t devConfig;
  2.         devConfig.command_bits     = 0;
  3.         devConfig.address_bits     = 8;
  4.         devConfig.dummy_bits       = 0;
  5.         devConfig.mode             = 0;
  6.         devConfig.clock_speed_hz   = 5000000;
  7.         devConfig.duty_cycle_pos   = 0;
  8.         devConfig.cs_ena_pretrans  = 1;
  9.         devConfig.cs_ena_posttrans = 1;
  10.         devConfig.spics_io_num     = 5;
  11.         devConfig.queue_size       = 32;
  12.         devConfig.flags            = SPI_DEVICE_HALFDUPLEX;
  13.         devConfig.pre_cb           = nullptr;
  14.         devConfig.post_cb          = nullptr;
  15.         ESP_LOGI(TAG, "Adding device to SPI bus...");
  16.         ESP_ERROR_CHECK(spi_bus_add_device(HSPI_HOST, &devConfig, &mSPIDev));

ESP_Sprite
Posts: 9727
Joined: Thu Nov 26, 2015 4:08 am

Re: Unhandled exception in spi_device_polling_start()

Postby ESP_Sprite » Thu May 30, 2024 5:59 am

In general, when initializing structs, you probably want to either use named initializers, or do 'my_struct_t thingy={}' to zero-initialize the struct.

Who is online

Users browsing this forum: Baidu [Spider] and 70 guests