• Система автоматизации с открытым исходным кодом на базе esp8266/esp32 микроконтроллеров и приложения IoT Manager. Наша группа в Telegram

ESP32-S2 использование USB в режиме UART

dentra

New member
Всем привет!

Есть модуль c ESP32-S2 на нем распаян USB, ноги которого напрямую подключены к пинам GPIO19 (USB_DM) и GPIO20 (USB_DP). Мне необходимо задействовать эти ноги как обычный UART.
К сожалению все не просто, и связано это с внутренней подтяжкой, например в даташите на C3 (тут пины другие) прямо написано:
Код:
USB - GPIO18 and GPIO19 are USB pins.
The pull-up value of a USB pin is controlled by the pin’s pull-up value together with USB pull-up value.
If any of the two pull-up values is 1, the pin’s pull-up resistor will be enabled.
The pull-up resistors of USB pins are controlled by USB_SERIAL_JTAG_DP_PULLUP bit.
и для того чтобы активировать эту возможность необходимо выполнить:
Код:
CLEAR_PERI_REG_MASK(USB_SERIAL_JTAG_CONF0_REG, USB_SERIAL_JTAG_DP_PULLUP);
для S3, в даташите ничего нет, но на просторах интернета удалось выяснить, что необходимо сбросить другой регистр:
Код:
CLEAR_PERI_REG_MASK(USB_DEVICE_CONF0_REG, USB_DEVICE_USB_PAD_ENABLE);
На cтике LilyGo T-Dongle-S3, это проверено и 100% работает.

Вопрос к гуру, как программно активировать пины GPIO19 и GPIO20 в режим UART на ESP32-S2?
 

kvarc

New member
Если вы используете ESP-IDF, то через uart_set_pin:
Код:
/**
* @brief Assign signals of a UART peripheral to GPIO pins
*
* @note If the GPIO number configured for a UART signal matches one of the
*       IOMUX signals for that GPIO, the signal will be connected directly
*       via the IOMUX. Otherwise the GPIO and signal will be connected via
*       the GPIO Matrix. For example, if on an ESP32 the call
*       `uart_set_pin(0, 1, 3, -1, -1)` is performed, as GPIO1 is UART0's
*       default TX pin and GPIO3 is UART0's default RX pin, both will be
*       connected to respectively U0TXD and U0RXD through the IOMUX, totally
*       bypassing the GPIO matrix.
*       The check is performed on a per-pin basis. Thus, it is possible to have
*       RX pin binded to a GPIO through the GPIO matrix, whereas TX is binded
*       to its GPIO through the IOMUX.
*
* @note Internal signal can be output to multiple GPIO pads.
*       Only one GPIO pad can connect with input signal.
*
* @param uart_num   UART port number, the max port number is (UART_NUM_MAX -1).
* @param tx_io_num  UART TX pin GPIO number.
* @param rx_io_num  UART RX pin GPIO number.
* @param rts_io_num UART RTS pin GPIO number.
* @param cts_io_num UART CTS pin GPIO number.
*
* @return
*     - ESP_OK   Success
*     - ESP_FAIL Parameter error
*/
esp_err_t uart_set_pin(uart_port_t uart_num, int tx_io_num, int rx_io_num, int rts_io_num, int cts_io_num);
Если ардуино, то:
Код:
HardwareSerial S1(1);
S1.begin(115200,SERIAL_8N1,19,20); //19-RX, 20-TX
И от архитектуры чипа не зависит.
 

kvarc

New member
В ардуино можно еще проще, если нужен только один уарт:
Код:
Serial.begin(115200,SERIAL_8N1,rx_pin,tx_pin);
Если пины не "стандартные", то вход/выход будет направлен через GPIO-матрицу, и, насколько я понимаю, проблема с подтяжками сразу отпадает.
 

dentra

New member
Спасибо. Извиняюсь за долгий ответ, пытался копнуть глубже. Пока выяснил, что да, работает, но частично. С TX, все прекрасно, а когда подключаю RX, то отказывается стартовать WiFi.

пробовал и через arduino, и через esp-idf.
 

dentra

New member
если интересно лог для esp-idf

Код:
I (24) boot: ESP-IDF 4.4.2 2nd stage bootloader
I (24) boot: compile time 12:10:53
I (24) boot: chip revision: 0
I (25) boot.esp32s2: SPI Speed      : 80MHz
I (30) boot.esp32s2: SPI Mode       : DIO
I (35) boot.esp32s2: SPI Flash Size : 4MB
I (40) boot: Enabling RNG early entropy source...
I (45) boot: Partition Table:
I (49) boot: ## Label            Usage          Type ST Offset   Length
I (56) boot:  0 otadata          OTA data         01 00 00009000 00002000
I (63) boot:  1 phy_init         RF data          01 01 0000b000 00001000
I (71) boot:  2 app0             OTA app          00 10 00010000 001c0000
I (78) boot:  3 app1             OTA app          00 11 001d0000 001c0000
I (86) boot:  4 nvs              WiFi data        01 02 00390000 0006d000
I (93) boot: End of partition table
I (98) esp_image: segment 0: paddr=00010020 vaddr=3f000020 size=1c750h (116560) map
I (129) esp_image: segment 1: paddr=0002c778 vaddr=3ffc4c00 size=0337ch ( 13180) load
I (132) esp_image: segment 2: paddr=0002fafc vaddr=40022000 size=0051ch (  1308) load
I (135) esp_image: segment 3: paddr=00030020 vaddr=40080020 size=84b28h (543528) map
I (252) esp_image: segment 4: paddr=000b4b50 vaddr=4002251c size=126e4h ( 75492) load
I (271) esp_image: segment 5: paddr=000c723c vaddr=50000000 size=00010h (    16) load
I (280) boot: Loaded app from partition at offset 0x10000
I (280) boot: Disabling RNG early entropy source...
I (293) cache: Instruction cache    : size 8KB, 4Ways, cache line size 32Byte
I (293) cpu_start: Pro cpu up.
I (307) cpu_start: Pro cpu start user code
I (307) cpu_start: cpu freq: 160000000
I (307) cpu_start: Application information:
I (312) cpu_start: Project name:     tion4s-hw-s2
I (317) cpu_start: App version:      2022.12.7
I (322) cpu_start: Compile time:     Jan 27 2023 12:41:34
I (328) cpu_start: ELF file SHA256:  f10cb940b7cc9d52...
I (334) cpu_start: ESP-IDF:          4.4.2
I (339) heap_init: Initializing. RAM available for dynamic allocation:
I (346) heap_init: At 3FFCC480 len 0002FB80 (190 KiB): DRAM
I (353) heap_init: At 3FFFC000 len 00003A10 (14 KiB): DRAM
I (359) heap_init: At 3FF9E000 len 00002000 (8 KiB): RTCRAM
I (366) spi_flash: detected chip: generic
I (370) spi_flash: flash io: dio
I (378) cpu_start: Starting scheduler on PRO CPU.
I (561) system_api: Base MAC address is not set
I (566) system_api: read default base MAC address from EFUSE
I (594) wifi:wifi firmware version: eeaa27d
I (595) wifi:wifi certification version: v7.0
I (596) wifi:config NVS flash: enabled
I (600) wifi:config nano formating: disabled
I (604) wifi:Init data frame dynamic rx buffer num: 32
I (608) wifi:Init management frame dynamic rx buffer num: 32
I (614) wifi:Init management short buffer num: 32
I (618) wifi:Init dynamic tx buffer num: 32
I (622) wifi:Init static rx buffer size: 1600
I (626) wifi:Init static rx buffer num: 10
I (630) wifi:Init dynamic rx buffer num: 32
I (634) wifi_init: rx ba win: 6
I (638) wifi_init: tcpip mbox: 32
I (642) wifi_init: udp mbox: 6
I (646) wifi_init: tcp mbox: 6
I (649) wifi_init: tcp tx win: 5744
I (653) wifi_init: tcp rx win: 5744
I (658) wifi_init: tcp mss: 1440
I (662) wifi_init: WiFi IRAM OP enabled
I (666) wifi_init: WiFi RX IRAM OP enabled
I (673) phy_init: phy_version 2300,d67cf06,Feb 10 2022,10:03:07
I (693) phy: pll_cap_ext 10
I (695) phy: pll_cap_ext 10
I (698) phy: pll_cap_ext 10
I (702) phy: pll_cap_ext 10
I (705) phy: pll_cap_ext 10
I (709) phy: pll_cap_ext 10
I (712) phy: pll_cap_ext 10
I (716) phy: pll_cap_ext 10
I (719) phy: pll_cap_ext 10
I (723) phy: pll_cap_ext 10
I (726) phy: pll_cap_ext 10
I (738) phy: pll_cap_ext 10
I (740) phy: pll_cap_ext 10
I (744) phy: pll_cap_ext 10
I (744) phy: pll_cap_ext 10
I (745) phy: pll_cap_ext 10
I (747) phy: pll_cap_ext 10
I (760) phy: pll_cap_ext 10
I (767) phy: pll_cap_ext 10
I (769) phy: pll_cap_ext 10
I (769) phy: pll_cap_ext 10
I (770) phy: pll_cap_ext 10
I (772) phy: pll_cap_ext 10
I (776) phy: pll_cap_ext 10
I (779) phy: pll_cap_ext 10
I (783) phy: pll_cap_ext 10
I (786) phy: pll_cap_ext 10
I (789) phy: pll_cap_ext 10
I (793) phy: pll_cap_ext 10
I (797) phy: pll_cap_ext 10
I (800) phy: pll_cap_ext 10
I (803) phy: pll_cap_ext 10
abort() was called at PC 0x40024977 on core 0

Backtrace:0x40023e7e:0x3ffd9aa00x40029f05:0x3ffd9ac0 0x40030012:0x3ffd9ae0 0x40024977:0x3ffd9b50 0x40024ab1:0x3ffd9b80 0x40024b2a:0x3ffd9ba0 0x400f73ba:0x3ffd9bd0 0x400fa3ed:0x3ffd9ee0 0x40104b2d:0x3ffd9f10 0x4002ec21:0x3ffd9f40 0x400f0ae2:0x3ffd9f90 0x400f0b0b:0x3ffda010 0x400268db:0x3ffda060 0x400ea46b:0x3ffda0c0 0x400ea503:0x3ffda0e0 0x400ea574:0x3ffda100 0x400ec02f:0x3ffda120 0x400ec0ea:0x3ffda140 0x400f0efb:0x3ffda160 0x400f0f82:0x3ffda190 0x400d3236:0x3ffda1b0 0x400d3961:0x3ffda1d0 0x400d23ea:0x3ffda1f0 0x400317c1:0x3ffda210 



ELF file SHA256: f10cb940b7cc9d52
Rebooting...

и расшифровка бэктрейса

Код:
0x40023e7e: panic_abort at /Users/dentra/.platformio/packages/framework-espidf/components/esp_system/panic.c:402
0x40030012: abort at /Users/dentra/.platformio/packages/framework-espidf/components/newlib/abort.c:46
0x40024977: lock_acquire_generic at /Users/dentra/.platformio/packages/framework-espidf/components/newlib/locks.c:139
0x40024ab1: _lock_acquire_recursive at /Users/dentra/.platformio/packages/framework-espidf/components/newlib/locks.c:167
0x40024b2a: __retarget_lock_acquire_recursive at /Users/dentra/.platformio/packages/framework-espidf/components/newlib/locks.c:323
0x400f73ba: _vfprintf_r at /builds/idf/crosstool-NG/.build/HOST-x86_64-apple-darwin12/xtensa-esp32s2-elf/src/newlib/newlib/libc/stdio/vfprintf.c:853 (discriminator 2)
0x400fa3ed: vprintf at /builds/idf/crosstool-NG/.build/HOST-x86_64-apple-darwin12/xtensa-esp32s2-elf/src/newlib/newlib/libc/stdio/vprintf.c:34 (discriminator 5)
0x40104b2d: esp_log_writev at /Users/dentra/.platformio/packages/framework-espidf/components/log/log.c:200
0x4002ec21: esp_log_write at /Users/dentra/.platformio/packages/framework-espidf/components/log/log.c:210
0x400f0ae2: lib_printf at /Users/dentra/.platformio/packages/framework-espidf/components/esp_phy/src/lib_printf.c:39 (discriminator 13)
0x400f0b0b: phy_printf at /Users/dentra/.platformio/packages/framework-espidf/components/esp_phy/src/lib_printf.c:49
0x400268db: pll_vol_cal at ??:?
0x400ea46b: set_chan_freq_sw_start at ??:?
0x400ea503: set_channel_rfpll_freq at ??:?
0x400ea574: chip_v7_set_chan at ??:?
0x400ec02f: bb_init at ??:?
0x400ec0ea: register_chipv7_phy at ??:?
0x400f0efb: esp_phy_load_cal_and_init at /Users/dentra/.platformio/packages/framework-espidf/components/esp_phy/src/phy_init.c:716
0x400f0f82: esp_phy_enable at /Users/dentra/.platformio/packages/framework-espidf/components/esp_phy/src/phy_init.c:236
0x400d3236: wifi_hw_start at ??:?
0x400d3961: wifi_start_process at ??:?
0x400d23ea: ieee80211_ioctl_process at ??:?
0x400317c1: ppTask at ??:?
 

dentra

New member
Поковырял дальше, ребутов больше нет, но WiFi все так же не поднимается. В обработчике WIFI_EVENT_SCAN_DONE wifi_event_sta_scan_done_t.number абсолютно всегда 0, отключаю RX пин и все начинает работать
 

chiffa_cff

New member
Если вы используете ESP-IDF, то через uart_set_pin:
Код:
/**
* @brief Assign signals of a UART peripheral to GPIO pins
*
* @note If the GPIO number configured for a UART signal matches one of the
*       IOMUX signals for that GPIO, the signal will be connected directly
*       via the IOMUX. Otherwise the GPIO and signal will be connected via
*       the GPIO Matrix. For example, if on an ESP32 the call
*       `uart_set_pin(0, 1, 3, -1, -1)` is performed, as GPIO1 is UART0's
*       default TX pin and GPIO3 is UART0's default RX pin, both will be
*       connected to respectively U0TXD and U0RXD through the IOMUX, totally
*       bypassing the GPIO matrix.
*       The check is performed on a per-pin basis. Thus, it is possible to have
*       RX pin binded to a GPIO through the GPIO matrix, whereas TX is binded
*       to its GPIO through the IOMUX.
*
* @note Internal signal can be output to multiple GPIO pads.
*       Only one GPIO pad can connect with input signal.
*
* @param uart_num   UART port number, the max port number is (UART_NUM_MAX -1).
* @param tx_io_num  UART TX pin GPIO number.
* @param rx_io_num  UART RX pin GPIO number.
* @param rts_io_num UART RTS pin GPIO number.
* @param cts_io_num UART CTS pin GPIO number.
*
* @return
*     - ESP_OK   Success
*     - ESP_FAIL Parameter error
*/
esp_err_t uart_set_pin(uart_port_t uart_num, int tx_io_num, int rx_io_num, int rts_io_num, int cts_io_num);
Если ардуино, то:
Код:
HardwareSerial S1(1);
S1.begin(115200,SERIAL_8N1,19,20); //19-RX, 20-TX
И от архитектуры чипа не зависит.
Добрый день. Подскажите, а отправку данных делали:

Код:
    uint8_t sendData [] = {0x4D, 0x00, 0x07, 0x21, 0x17, 0x00, 0x1E, 0x00, 0x01, 0x00, 0x63};

    S1.write (sendData, sizeof(sendData));
 
Сверху Снизу