espressif / esp-idf

Espressif IoT Development Framework. Official development framework for Espressif SoCs.
Apache License 2.0
13.63k stars 7.28k forks source link

esp32c2 need #define SOC_SPI_HD_BOTH_INOUT_SUPPORTED 1,to use SPI Hafl3Wire (IDFGH-12550) #13554

Closed xiaguangbo closed 6 months ago

xiaguangbo commented 6 months ago

Answers checklist.

IDF version.

v5.1.3

Espressif SoC revision.

esp32c2 v1.0

Operating System used.

Linux

How did you build your project?

VS Code IDE

If you are using Windows, please specify command line type.

None

Development Kit.

nodemcu-esp32-c2

Power Supply used.

USB

What is the expected behavior?

use SPI Hafl3Wire

What is the actual behavior?

if not fix, can report some err https://github.com/esp-rs/esp-idf-hal/issues/401

Steps to reproduce.

https://github.com/esp-rs/esp-idf-hal/issues/401

Debug Logs.

ESP-ROM:esp8684-api2-20220127
Build:Jan 27 2022
rst:0x1 (POWERON),boot:0xf (SPI_FAST_FLASH_BOOT)
SPIWP:0xee
mode:DIO, clock div:2
load:0x3fcd5c80,len:0x16bc
load:0x403acb70,len:0x974
load:0x403aeb70,len:0x297c
entry 0x403acb70
I (32) boot: ESP-IDF v5.1.2 2nd stage bootloader
I (32) boot: compile time Jan 16 2024 14:05:06
I (32) boot: chip revision: v1.0
I (37) boot.esp32c2: MMU Page Size  : 32K
I (44) boot.esp32c2: SPI Speed      : 30MHz
I (51) boot.esp32c2: SPI Mode       : DIO
I (59) boot.esp32c2: SPI Flash Size : 4MB
I (66) boot: Enabling RNG early entropy source...
I (74) boot: Partition Table:
I (80) boot: ## Label            Usage          Type ST Offset   Length
I (91) boot:  0 nvs              WiFi data        01 02 00009000 00006000
I (102) boot:  1 phy_init         RF data          01 01 0000f000 00001000
I (114) boot:  2 factory          factory app      00 00 00010000 001f0000
I (126) boot: End of partition table
I (132) esp_image: segment 0: paddr=00010020 vaddr=3fcabc10 size=01418h (  5144) load
I (147) esp_image: segment 1: paddr=00011440 vaddr=40380000 size=06bd8h ( 27608) load
I (169) esp_image: segment 2: paddr=00018020 vaddr=3c068020 size=289b0h (166320) map
I (228) esp_image: segment 3: paddr=000409d8 vaddr=40386bd8 size=05030h ( 20528) load
I (236) esp_image: segment 4: paddr=00045a10 vaddr=00000000 size=0a608h ( 42504) 
I (251) esp_image: segment 5: paddr=00050020 vaddr=42000020 size=624a0h (402592) map
I (393) boot: Loaded app from partition at offset 0x10000
I (394) boot: Disabling RNG early entropy source...
I (411) cpu_start: Unicore app
I (412) cpu_start: Pro cpu up.
I (423) cpu_start: Pro cpu start user code
I (423) cpu_start: cpu freq: 120000000 Hz
I (423) cpu_start: Application information:
I (428) cpu_start: Project name:     libespidf
I (436) cpu_start: App version:      1
I (442) cpu_start: Compile time:     Apr  7 2024 13:15:59
I (452) cpu_start: ELF file SHA256:  0000000000000000...
I (461) cpu_start: ESP-IDF:          v5.1.3-dirty
I (469) cpu_start: Min chip rev:     v1.0
I (476) cpu_start: Max chip rev:     v1.99 
I (484) cpu_start: Chip rev:         v1.0
I (491) heap_init: Initializing. RAM available for dynamic allocation:
I (502) heap_init: At 3FCAE1D0 len 0002E9A0 (186 KiB): D/IRAM
I (512) heap_init: At 3FCDCB70 len 0000294C (10 KiB): STACK/DRAM
I (524) spi_flash: detected chip: gd
I (529) spi_flash: flash io: dio
W (535) timer_group: legacy driver is deprecated, please migrate to `driver/gptimer.h`
I (548) sleep: Configure to isolate all GPIO pins in sleep state
I (559) sleep: Enable automatic switching of GPIO sleep configuration
I (570) app_start: Starting scheduler on CPU0
I (577) main_task: Started on CPU0
I (583) main_task: Calling app_main()
I (592) esp32c2: Hello, world!
I (598) gpio: GPIO[18]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 0| Pulldown: 0| Intr:0 
I (610) gpio: GPIO[10]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 0| Pulldown: 0| Intr:0 
I (627) uart: queue free spaces: 10
E (646) spi_master: check_trans_valid(793): SPI half duplex mode is not supported when both MOSI and MISO phases are enabled.
thread '<unnamed>' panicked at src/component/max31865.rs:77:14:
called `Result::unwrap()` on an `Err` value: EspError(258)
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

abort() was called at PC 0x420392e7 on core 0
0x420392e7 - panic_abort::__rust_start_panic::abort
    at /home/xiaguangbo/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/panic_abort/src/lib.rs:48
Core  0 register dump:
MEPC    : 0x403824c2  RA      : 0x40385e88  SP      : 0x3fcbb090  GP      : 0x3fcac410  
0x403824c2 - panic_abort
    at /media/xiaguangbo/linux_data/project/xtempctrl/esp32c2/.embuild/espressif/esp-idf/v5.1.3/components/esp_system/panic.c:472
0x40385e88 - __ubsan_include
    at /media/xiaguangbo/linux_data/project/xtempctrl/esp32c2/.embuild/espressif/esp-idf/v5.1.3/components/esp_system/ubsan.c:313
0x3fcac410 - __global_pointer$
    at ??:??
TP      : 0x3fc933fc  T0      : 0x37363534  T1      : 0x7271706f  T2      : 0x33323130  
S0/FP   : 0x3fcbb0cc  S1      : 0x3fcbb0b0  A0      : 0x3fcbb0cc  A1      : 0x3fcbb0ae  
A2      : 0x00000000  A3      : 0x3fcbb0f9  A4      : 0x00000001  A5      : 0x3fcae000  
0x3fcae000 - curr_partition.2
    at ??:??
A6      : 0x00000000  A7      : 0x76757473  S2      : 0x3c06eec0  S3      : 0x3fcbb1d4  
0x3c06eec0 - $d.443
    at ??:??
S4      : 0x00000001  S5      : 0x00000000  S6      : 0x00000001  S7      : 0x00000000  
S8      : 0x00000000  S9      : 0x00000000  S10     : 0x00000000  S11     : 0x00000000  
T3      : 0x6e6d6c6b  T4      : 0x6a696867  T5      : 0x66656463  T6      : 0x62613938  
MSTATUS : 0x00001801  MTVEC   : 0x40380001  MCAUSE  : 0x00000007  MTVAL   : 0x00000000  
0x40380001 - _vector_table
    at ??:??
MHARTID : 0x00000000  

Stack memory:
3fcbb090: 0x3fcbb143 0x3c06edb8 0x3fcbb0ac 0x4038a48c 0x00000002 0x3fcbb104 0x3fcabc2c 0x3c060030
0x3c06edb8 - $d.429
    at ??:??
0x4038a48c - __assert_func
    at /media/xiaguangbo/linux_data/project/xtempctrl/esp32c2/.embuild/espressif/esp-idf/v5.1.3/components/newlib/assert.c:34
0x3fcabc2c - std::io::stdio::OUTPUT_CAPTURE::__getit::__KEY
    at ??:??
3fcbb0b0: 0x33303234 0x37653239 0x00000000 0x3fcac970 0x3fcbb0b0 0x3fcac98c 0x3fcbb0ac 0x726f6261
0x3fcac970 - __func__.0
    at ??:??
3fcbb0d0: 0x20292874 0x20736177 0x6c6c6163 0x61206465 0x43502074 0x34783020 0x39333032 0x20376532
3fcbb0f0: 0x63206e6f 0x2065726f 0x00000030 0x2cfa0000 0x1532d546 0x00000000 0x3fcad234 0x420392ea
0x3fcad234 - $d.420
    at ??:??
0x420392ea - object::endian::U16Bytes<E>::get
    at /home/xiaguangbo/.cargo/registry/src/index.crates.io-6f17d22bba15001f/object-0.32.2/src/endian.rs:636
3fcbb110: 0x02cbb86c 0x3c0683a0 0x00000000 0x420136a0 0x3fcb9330 0x3c06ed86 0x00000000 0x42011d60
0x3c0683a0 - $d.15
    at ??:??
0x420136a0 - std::sys_common::thread_local_key::StaticKey::get
    at ??:??
0x3c06ed86 - $d.427
    at ??:??
0x42011d60 - core::ptr::write
    at /home/xiaguangbo/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ptr/mod.rs:1498
3fcbb130: 0x3c06eec0 0x00000000 0x3fcbb850 0x42012170 0x00000009 0x00000000 0x3fcad234 0x42013104
0x3c06eec0 - $d.443
    at ??:??
0x42012170 - std::thread::local::LocalKey<T>::try_with
    at /home/xiaguangbo/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/thread/local.rs:283
0x3fcad234 - $d.420
    at ??:??
0x42013104 - core::fmt::Arguments::new_const
    at /home/xiaguangbo/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/fmt/mod.rs:333
3fcbb150: 0x3fc933fc 0x00000000 0x3fcbb1d4 0x3c06ee8c 0x3fcbb238 0x3c0683a0 0x3fcb0001 0x3fcbb1e0
0x3c06ee8c - $d.440
    at ??:??
0x3c0683a0 - $d.15
    at ??:??
3fcbb170: 0xffffffff 0x00000050 0x00000014 0x00000003 0x00000010 0x00000014 0x00000000 0x3fcbb260
3fcbb190: 0x3fcbb5e0 0x00000000 0x3fcbb5e0 0x00000080 0x4204aae0 0x42000464 0x3fcbb220 0x3fcac410
0x4204aae0 - core::result::unwrap_failed
    at /home/xiaguangbo/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/result.rs:1654
0x42000464 - OUTLINED_FUNCTION_0
    at ??:??
0x3fcac410 - __global_pointer$
    at ??:??
3fcbb1b0: 0x00000000 0x3fcddf80 0x00000000 0x3fcbb308 0x3fcbb320 0x3fcbb238 0x3fcbb1f4 0x42012f9e
0x42012f9e - std::panicking::begin_panic_handler::{{closure}}
    at /home/xiaguangbo/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/panicking.rs:651
3fcbb1d0: 0x3fcbb28c 0x00000056 0x3fcbb86c 0x0000003a 0x3fcbb238 0x3fcdd500 0x00000000 0x420127a8
0x420127a8 - std::sys_common::backtrace::output_filename
    at /home/xiaguangbo/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/sys_common/backtrace.rs:182
3fcbb1f0: 0x00000000 0x3fcbb238 0x3fcbb20c 0x3c0683a0 0x00000000 0x00000000 0x00000000 0x3c08208c
0x3c0683a0 - $d.15
    at ??:??
0x3c08208c - $d.224
    at ??:??
3fcbb210: 0x3c0820e4 0x3fcbb238 0x3c0683a0 0x00000001 0x00000000 0x3c068388 0x3c068309 0x0000002b
0x3c0820e4 - $d.251
    at ??:??
0x3c0683a0 - $d.15
    at ??:??
0x3c068388 - $d.12
    at ??:??
0x3c068309 - $d.6
    at ??:??
3fcbb230: 0x3fcbb28c 0x3c068334 0x3c084134 0x00000002 0x3fcbb250 0x00000002 0x00000000 0x3fcbb308
0x3c068334 - $d.7
    at ??:??
0x3c084134 - $d.56
    at ??:??
3fcbb250: 0x3fcbb228 0x4204673a 0x3fcbb230 0x4204671c 0x0000b320 0x00000001 0x3c06839c 0x00000001
0x4204673a - <&T as core::fmt::Display>::fmt
    at /home/xiaguangbo/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/fmt/mod.rs:2337
0x4204671c - <&T as core::fmt::Debug>::fmt
    at /home/xiaguangbo/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/fmt/mod.rs:2337
0x3c06839c - $d.14
    at ??:??
3fcbb270: 0x00000000 0x00000000 0x00000000 0x3fcbb262 0x00000002 0x00000000 0x00000000 0x00000102
3fcbb290: 0x3fcbb320 0x3fcdd500 0x00000000 0x42000d76 0x00000000 0x3fcddf80 0x00000000 0x3fcdd800
0x42000d76 - esp32c2::module::temperature::TemperatureMax31865<Spi>::work
    at /media/xiaguangbo/linux_data/project/xtempctrl/esp32c2/src/module/temperature.rs:119
3fcbb2b0: 0x00000000 0x3fcdd500 0x3fcbb320 0x420001e8 0x000f4240 0x00000000 0x00000001 0x00000000
0x420001e8 - core::result::Result<T,E>::unwrap
    at /home/xiaguangbo/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/result.rs:1075
3fcbb2d0: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x3fcddf80 0x00000000 0x3fcdd800
3fcbb2f0: 0x00000000 0x3fcdd500 0x00000000 0x420083ae 0x00000000 0x00000000 0x00000000 0x3fcdd500
0x420083ae - std::sys_common::backtrace::__rust_begin_short_backtrace
    at /home/xiaguangbo/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/sys_common/backtrace.rs:151
3fcbb310: 0x00000000 0x3fcdd800 0x00000000 0x3fcddf80 0x000003e8 0x3fcbb704 0x00000001 0x3fcde6cc
3fcbb330: 0x3f000101 0x00000000 0x3fcb93d8 0x00000000 0x00000000 0x3c06b78c 0x3fcb93d8 0x42008580
0x3c06b78c - $d.36
    at ??:??
0x42008580 - std::panicking::try
    at /home/xiaguangbo/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/panicking.rs:582
3fcbb350: 0x00000000 0x3fcdd500 0x00000000 0x3fcdd800 0x00000000 0x3fcddf80 0x3fcde6c4 0x42012284
0x42012284 - core::cell::once::OnceCell<T>::set
    at /home/xiaguangbo/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/cell/once.rs:90
3fcbb370: 0x00000000 0x3fcdd500 0x00000000 0x3fcdd800 0x00000000 0x3fcddf80 0x3fcde6c4 0x4200b2b8
0x4200b2b8 - std::panic::catch_unwind
    at /home/xiaguangbo/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/panic.rs:149
3fcbb390: 0x3fcae000 0x00000000 0x00000000 0x3fcdd500 0x00000000 0x3fcdd800 0x00000000 0x3fcddf80
0x3fcae000 - curr_partition.2
    at ??:??
3fcbb3b0: 0x3fcde6c4 0x00000000 0x00000000 0x3fcdd500 0x00000000 0x3fcdd800 0x00000000 0x3fcddf80
3fcbb3d0: 0x3fcde6c4 0x00000000 0x00000000 0x00000000 0x00000000 0x3c06b78c 0x3fcb93d8 0x4202c484
0x3c06b78c - $d.36
    at ??:??
0x4202c484 - <alloc::boxed::Box<T,A> as core::ops::drop::Drop>::drop
    at /home/xiaguangbo/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/boxed.rs:1241
3fcbb3f0: 0x00000000 0x00000000 0x3fcb9404 0x4202c4ca 0x00000000 0x00000000 0x3fcb9404 0x42031292
0x4202c4ca - <alloc::boxed::Box<T,A> as core::ops::drop::Drop>::drop
    at /home/xiaguangbo/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/boxed.rs:1243
0x42031292 - std::sys::pal::unix::thread::Thread::new::thread_start
    at /home/xiaguangbo/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/sys/pal/unix/thread.rs:111
3fcbb410: 0x00000000 0x00000000 0x3fcb9414 0x42053a3a 0x00000000 0x00000000 0x00000000 0x00000000
0x42053a3a - pthread_task_func
    at /media/xiaguangbo/linux_data/project/xtempctrl/esp32c2/.embuild/espressif/esp-idf/v5.1.3/components/pthread/pthread.c:199
3fcbb430: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5
3fcbb450: 0xa5a5a5a5 0x00000154 0x3fcbaf00 0x3fcae1e4 0x3fcad4e0 0x3fcad4e0 0x3fcbb458 0x3fcad4d8
0x3fcad4e0 - pxReadyTasksLists
    at ??:??
0x3fcad4e0 - pxReadyTasksLists
    at ??:??
0x3fcad4d8 - pxReadyTasksLists
    at ??:??
3fcbb470: 0x00000014 0xf11050e4 0x4bd945a5 0x3fcbb458 0x00000000 0x00000005 0x3fcb9454 0x72687470

ELF file SHA256: 0000000000000000

Rebooting...
ESP-ROM:esp8684-api2-20220127
Build:Jan 27 2022
rst:0xc (RTC_SW_CPU_RST),boot:0xf (SPI_FAST_FLASH_BOOT)
Saved PC:0x40382484
0x40382484 - esp_restart_noos
    at /media/xiaguangbo/linux_data/project/xtempctrl/esp32c2/.embuild/espressif/esp-idf/v5.1.3/components/esp_system/port/soc/esp32c2/system_internal.c:96
SPIWP:0xee
mode:DIO, clock div:2
load:0x3fcd5c80,len:0x16bc
load:0x403acb70,len:0x974
load:0x403aeb70,len:0x297c
entry 0x403acb70
I (36) boot: ESP-IDF v5.1.2 2nd stage bootloader
I (36) boot: compile time Jan 16 2024 14:05:06


### More Information.

`Hafl3wire` can be used using the modification method in the problem link. If full duplex is used and `miso` and `mosi` are connected together to imitate the behavior of `Hafl3wire`, errors will occur.
wanckl commented 6 months ago

@xiaguangbo Hi~ SOC_SPI_HD_BOTH_INOUT_SUPPORTED is really not supported for chips after esp32s2, it not a BUG but HW design changes.
But is not means these chips don't support half duplex, as error log reported:

SPI half duplex mode is not supported when both MOSI and MISO phases are enabled.

It just a small changes that you need split tx and rx to independent transactions, ensure one transaction have only one direction. I don't know how rust implement 3-wire HD mode, but for esp-idf, can refer to test test_spi_sio.c.

For the modification mentioned in https://github.com/esp-rs/esp-idf-hal/issues/401, it just work by accident, if txleng != rxleng, it not work as design.

xiaguangbo commented 6 months ago

@wanckl I don't know what that means both MOSI and MISO phases are enabled. phases enabledmeansCPHA = 1? or tx and rx are enabled?

I don't understand this code. .embuild/espressif/esp-idf/v5.1.3/components/driver/spi/gpspi/spi_master.c L790:

#if !SOC_SPI_HD_BOTH_INOUT_SUPPORTED
    //On these chips, HW doesn't support using both TX and RX phases when in halfduplex mode
    SPI_CHECK(!is_half_duplex || !tx_enabled || !rx_enabled, "SPI half duplex mode is not supported when both MOSI and MISO phases are enabled.", ESP_ERR_INVALID_ARG);
    SPI_CHECK(!is_half_duplex || !trans_desc->length || !trans_desc->rxlength, "SPI half duplex mode is not supported when both MOSI and MISO phases are enabled.", ESP_ERR_INVALID_ARG);
#endif

vscode prompt:

#define SPI_CHECK(a,str,ret_val) ESP_RETURN_ON_FALSE_ISR(a, ret_val, SPI_TAG, str)
扩展到:

do { if ((!(!is_half_duplex || !tx_enabled || !rx_enabled))) { ESP_EARLY_LOGE(SPI_TAG, "%s(%d): " "SPI half duplex mode is not supported when both MOSI and MISO phases are enabled.", __FUNCTION__, 792); return 0x102; } } while(0)
#define SPI_CHECK(a,str,ret_val) ESP_RETURN_ON_FALSE_ISR(a, ret_val, SPI_TAG, str)
扩展到:

do { if ((!(!is_half_duplex || !trans_desc->length || !trans_desc->rxlength))) { ESP_EARLY_LOGE(SPI_TAG, "%s(%d): " "SPI half duplex mode is not supported when both MOSI and MISO phases are enabled.", __FUNCTION__, 793); return 0x102; } } while(0)
wanckl commented 6 months ago

@xiaguangbo ,,Sorry for confusing you,,

The code block you mentioned is a check, it is a hardware limitation
phases enabled not means CPHA

half duplex means only one direction at a moment, then

both MOSI and MISO phases are enabled

This limitation is: in one transaction constructed in spi_transaction_t, can only contain tx OR rx, if you need one TX followed by one RX, you need construct 2 spi_transaction_t then start them independently

xiaguangbo commented 6 months ago

@wanckl Problem is found ! It's a library function parameter problem. /home/xiaguangbo/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp-idf-hal-0.43.1/src/spi.rs L1610:

fn spi_read_transactions(
    words: &mut [u8],
    chunk_size: usize,
) -> impl Iterator<Item = spi_transaction_t> + '_ {
    words.chunks_mut(chunk_size).map(|chunk| {
        spi_create_transaction(
            chunk.as_mut_ptr(),
            core::ptr::null(),
            chunk.len(), // problem is here. changed to 0
            chunk.len(),
        )
    })
}

...

fn spi_create_transaction(
    read: *mut u8,
    write: *const u8,
    transaction_length: usize,
    rx_length: usize,
) -> spi_transaction_t {
    spi_transaction_t {
        flags: 0,
        __bindgen_anon_1: spi_transaction_t__bindgen_ty_1 {
            tx_buffer: write as *const _,
        },
        __bindgen_anon_2: spi_transaction_t__bindgen_ty_2 {
            rx_buffer: read as *mut _,
        },
        length: (transaction_length * 8) as _,
        rxlength: (rx_length * 8) as _,
        ..Default::default()
    }
}

changed, cargo clean cargo run