platformio / platform-espressif32

Espressif 32: development platform for PlatformIO
https://registry.platformio.org/platforms/platformio/espressif32
Apache License 2.0
905 stars 610 forks source link

Using arduino as component in the espidf-peripherals-usb example #945

Closed xtrinch closed 1 year ago

xtrinch commented 1 year ago

Hello,

I've been wondering if it's at all possible to use arduino as component when using tinyusb.

I've tried adding arduino to https://github.com/platformio/platform-espressif32/tree/develop/examples/espidf-peripherals-usb, but the project does not compile and fails with a bunch of errors. (And yes it compiles fine without adding arduino as component!)

It would be really nice to have an example project that does both usb and arduino as component.

Config:

[env]
platform = espressif32
framework = espidf, arduino
monitor_speed = 115200

[env:esp32-s2-saola-1]
board = esp32-s2-saola-1

Errors:

Compiling .pio/build/esp32-s2-saola-1/wiring_pulse.o
Compiling .pio/build/esp32-s2-saola-1/wiring_shift.o
/home/xtrinch/.platformio/packages/framework-arduinoespressif32/cores/esp32/USBCDC.cpp: In function 'uint16_t load_cdc_descriptor(uint8_t*, uint8_t*)':
/home/xtrinch/.platformio/packages/framework-arduinoespressif32/cores/esp32/USBCDC.cpp:29:25: error: 'tinyusb_add_string_descriptor' was not declared in this scope
     uint8_t str_index = tinyusb_add_string_descriptor("TinyUSB CDC");
                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Compiling .pio/build/esp32-s2-saola-1/WMath.o
/home/xtrinch/.platformio/packages/framework-arduinoespressif32/cores/esp32/USBCDC.cpp:30:24: error: 'TUD_CDC_DESC_LEN' was not declared in this scope
     uint8_t descriptor[TUD_CDC_DESC_LEN] = {
                        ^~~~~~~~~~~~~~~~
Compiling .pio/build/esp32-s2-saola-1/WString.o
/home/xtrinch/.platformio/packages/framework-arduinoespressif32/cores/esp32/USBCDC.cpp:32:13: error: 'TUD_CDC_DESCRIPTOR' was not declared in this scope
             TUD_CDC_DESCRIPTOR(*itf, str_index, 0x85, 64, 0x03, 0x84, 64)
             ^~~~~~~~~~~~~~~~~~
/home/xtrinch/.platformio/packages/framework-arduinoespressif32/cores/esp32/USBCDC.cpp:35:17: error: 'descriptor' was not declared in this scope
     memcpy(dst, descriptor, TUD_CDC_DESC_LEN);
                 ^~~~~~~~~~
/home/xtrinch/.platformio/packages/framework-arduinoespressif32/cores/esp32/USBCDC.cpp:35:17: note: suggested alternative: 'encrypt'
     memcpy(dst, descriptor, TUD_CDC_DESC_LEN);
                 ^~~~~~~~~~
                 encrypt
/home/xtrinch/.platformio/packages/framework-arduinoespressif32/cores/esp32/USBCDC.cpp: At global scope:
/home/xtrinch/.platformio/packages/framework-arduinoespressif32/cores/esp32/USBCDC.cpp:48:42: error: 'cdc_line_coding_t' has not been declared
 void tud_cdc_line_coding_cb(uint8_t itf, cdc_line_coding_t const* p_line_coding)
                                          ^~~~~~~~~~~~~~~~~
/home/xtrinch/.platformio/packages/framework-arduinoespressif32/cores/esp32/USBCDC.cpp: In function 'void tud_cdc_line_coding_cb(uint8_t, const int*)':
/home/xtrinch/.platformio/packages/framework-arduinoespressif32/cores/esp32/USBCDC.cpp:51:52: error: request for member 'bit_rate' in '* p_line_coding', which is of non-class type 'const int'
         devices[itf]->_onLineCoding(p_line_coding->bit_rate, p_line_coding->stop_bits, p_line_coding->parity, p_line_coding->data_bits);
                                                    ^~~~~~~~
/home/xtrinch/.platformio/packages/framework-arduinoespressif32/cores/esp32/USBCDC.cpp:51:77: error: request for member 'stop_bits' in '* p_line_coding', which is of non-class type 'const int'
         devices[itf]->_onLineCoding(p_line_coding->bit_rate, p_line_coding->stop_bits, p_line_coding->parity, p_line_coding->data_bits);
                                                                             ^~~~~~~~~
/home/xtrinch/.platformio/packages/framework-arduinoespressif32/cores/esp32/USBCDC.cpp:51:103: error: request for member 'parity' in '* p_line_coding', which is of non-class type 'const int'
         devices[itf]->_onLineCoding(p_line_coding->bit_rate, p_line_coding->stop_bits, p_line_coding->parity, p_line_coding->data_bits);
                                                                                                       ^~~~~~
/home/xtrinch/.platformio/packages/framework-arduinoespressif32/cores/esp32/USBCDC.cpp:51:126: error: request for member 'data_bits' in '* p_line_coding', which is of non-class type 'const int'
         devices[itf]->_onLineCoding(p_line_coding->bit_rate, p_line_coding->stop_bits, p_line_coding->parity, p_line_coding->data_bits);
                                                                                                                              ^~~~~~~~~
/home/xtrinch/.platformio/packages/framework-arduinoespressif32/cores/esp32/USBCDC.cpp: In constructor 'USBCDC::USBCDC(uint8_t)':
/home/xtrinch/.platformio/packages/framework-arduinoespressif32/cores/esp32/USBCDC.cpp:99:30: error: 'USB_INTERFACE_CDC' was not declared in this scope
     tinyusb_enable_interface(USB_INTERFACE_CDC, TUD_CDC_DESC_LEN, load_cdc_descriptor);
                              ^~~~~~~~~~~~~~~~~
Compiling .pio/build/esp32-s2-saola-1/ArduinoOTA.o
/home/xtrinch/.platformio/packages/framework-arduinoespressif32/cores/esp32/USBCDC.cpp:99:49: error: 'TUD_CDC_DESC_LEN' was not declared in this scope
     tinyusb_enable_interface(USB_INTERFACE_CDC, TUD_CDC_DESC_LEN, load_cdc_descriptor);
                                                 ^~~~~~~~~~~~~~~~
/home/xtrinch/.platformio/packages/framework-arduinoespressif32/cores/esp32/USBCDC.cpp:99:5: error: 'tinyusb_enable_interface' was not declared in this scope
     tinyusb_enable_interface(USB_INTERFACE_CDC, TUD_CDC_DESC_LEN, load_cdc_descriptor);
     ^~~~~~~~~~~~~~~~~~~~~~~~
/home/xtrinch/.platformio/packages/framework-arduinoespressif32/cores/esp32/USBCDC.cpp:101:49: error: 'ARDUINO_USB_EVENTS' was not declared in this scope
         arduino_usb_event_handler_register_with(ARDUINO_USB_EVENTS, ARDUINO_USB_STOPPED_EVENT, usb_unplugged_cb, this);
                                                 ^~~~~~~~~~~~~~~~~~
/home/xtrinch/.platformio/packages/framework-arduinoespressif32/cores/esp32/USBCDC.cpp:101:49: note: suggested alternative: 'ARDUINO_USB_CDC_EVENTS'
         arduino_usb_event_handler_register_with(ARDUINO_USB_EVENTS, ARDUINO_USB_STOPPED_EVENT, usb_unplugged_cb, this);
                                                 ^~~~~~~~~~~~~~~~~~
                                                 ARDUINO_USB_CDC_EVENTS
/home/xtrinch/.platformio/packages/framework-arduinoespressif32/cores/esp32/USBCDC.cpp:101:69: error: 'ARDUINO_USB_STOPPED_EVENT' was not declared in this scope
         arduino_usb_event_handler_register_with(ARDUINO_USB_EVENTS, ARDUINO_USB_STOPPED_EVENT, usb_unplugged_cb, this);
                                                                     ^~~~~~~~~~~~~~~~~~~~~~~~~
Compiling .pio/build/esp32-s2-saola-1/AsyncUDP.o
/home/xtrinch/.platformio/packages/framework-arduinoespressif32/cores/esp32/USBCDC.cpp:101:69: note: suggested alternative: 'ARDUINO_USB_CDC_MAX_EVENT'
         arduino_usb_event_handler_register_with(ARDUINO_USB_EVENTS, ARDUINO_USB_STOPPED_EVENT, usb_unplugged_cb, this);
                                                                     ^~~~~~~~~~~~~~~~~~~~~~~~~
                                                                     ARDUINO_USB_CDC_MAX_EVENT
/home/xtrinch/.platformio/packages/framework-arduinoespressif32/cores/esp32/USBCDC.cpp: In member function 'void USBCDC::_onLineState(bool, bool)':
/home/xtrinch/.platformio/packages/framework-arduinoespressif32/cores/esp32/USBCDC.cpp:229:37: error: 'RESTART_BOOTLOADER' was not declared in this scope
                 usb_persist_restart(RESTART_BOOTLOADER);
                                     ^~~~~~~~~~~~~~~~~~
/home/xtrinch/.platformio/packages/framework-arduinoespressif32/cores/esp32/USBCDC.cpp:229:37: note: suggested alternative: 'XCHAL_HAVE_BOOTLOADER'
                 usb_persist_restart(RESTART_BOOTLOADER);
                                     ^~~~~~~~~~~~~~~~~~
                                     XCHAL_HAVE_BOOTLOADER
/home/xtrinch/.platformio/packages/framework-arduinoespressif32/cores/esp32/USBCDC.cpp:229:17: error: 'usb_persist_restart' was not declared in this scope
                 usb_persist_restart(RESTART_BOOTLOADER);
                 ^~~~~~~~~~~~~~~~~~~
Compiling .pio/build/esp32-s2-saola-1/BluetoothSerial.o
/home/xtrinch/.platformio/packages/framework-arduinoespressif32/cores/esp32/USBCDC.cpp:229:17: note: suggested alternative: 'esp_restart'
                 usb_persist_restart(RESTART_BOOTLOADER);
                 ^~~~~~~~~~~~~~~~~~~
                 esp_restart
/home/xtrinch/.platformio/packages/framework-arduinoespressif32/cores/esp32/USBCDC.cpp: In member function 'void USBCDC::_onLineCoding(uint32_t, uint8_t, uint8_t, uint8_t)':
/home/xtrinch/.platformio/packages/framework-arduinoespressif32/cores/esp32/USBCDC.cpp:258:33: error: 'RESTART_BOOTLOADER' was not declared in this scope
             usb_persist_restart(RESTART_BOOTLOADER);
                                 ^~~~~~~~~~~~~~~~~~
/home/xtrinch/.platformio/packages/framework-arduinoespressif32/cores/esp32/USBCDC.cpp:258:33: note: suggested alternative: 'XCHAL_HAVE_BOOTLOADER'
             usb_persist_restart(RESTART_BOOTLOADER);
                                 ^~~~~~~~~~~~~~~~~~
                                 XCHAL_HAVE_BOOTLOADER
/home/xtrinch/.platformio/packages/framework-arduinoespressif32/cores/esp32/USBCDC.cpp:258:13: error: 'usb_persist_restart' was not declared in this scope
             usb_persist_restart(RESTART_BOOTLOADER);
             ^~~~~~~~~~~~~~~~~~~
/home/xtrinch/.platformio/packages/framework-arduinoespressif32/cores/esp32/USBCDC.cpp:258:13: note: suggested alternative: 'esp_restart'
             usb_persist_restart(RESTART_BOOTLOADER);
             ^~~~~~~~~~~~~~~~~~~
             esp_restart
/home/xtrinch/.platformio/packages/framework-arduinoespressif32/cores/esp32/USBCDC.cpp: In member function 'void USBCDC::_onRX()':
/home/xtrinch/.platformio/packages/framework-arduinoespressif32/cores/esp32/USBCDC.cpp:277:22: error: 'tud_cdc_n_read' was not declared in this scope
     uint32_t count = tud_cdc_n_read(itf, buf, CONFIG_TINYUSB_CDC_RX_BUFSIZE);
                      ^~~~~~~~~~~~~~
/home/xtrinch/.platformio/packages/framework-arduinoespressif32/cores/esp32/USBCDC.cpp:277:22: note: suggested alternative: 'tud_cdc_rx_cb'
     uint32_t count = tud_cdc_n_read(itf, buf, CONFIG_TINYUSB_CDC_RX_BUFSIZE);
                      ^~~~~~~~~~~~~~
                      tud_cdc_rx_cb
/home/xtrinch/.platformio/packages/framework-arduinoespressif32/cores/esp32/USBCDC.cpp: In member function 'virtual void USBCDC::flush()':
/home/xtrinch/.platformio/packages/framework-arduinoespressif32/cores/esp32/USBCDC.cpp:352:58: error: 'tud_cdc_n_connected' was not declared in this scope
     if(itf >= MAX_USB_CDC_DEVICES || tx_lock == NULL || !tud_cdc_n_connected(itf)){
                                                          ^~~~~~~~~~~~~~~~~~~
/home/xtrinch/.platformio/packages/framework-arduinoespressif32/cores/esp32/USBCDC.cpp:352:58: note: suggested alternative: 'tud_cdc_tx_complete_cb'
     if(itf >= MAX_USB_CDC_DEVICES || tx_lock == NULL || !tud_cdc_n_connected(itf)){
                                                          ^~~~~~~~~~~~~~~~~~~
                                                          tud_cdc_tx_complete_cb
/home/xtrinch/.platformio/packages/framework-arduinoespressif32/cores/esp32/USBCDC.cpp:358:5: error: 'tud_cdc_n_write_flush' was not declared in this scope
     tud_cdc_n_write_flush(itf);
     ^~~~~~~~~~~~~~~~~~~~~
Compiling .pio/build/esp32-s2-saola-1/BTAddress.o
/home/xtrinch/.platformio/packages/framework-arduinoespressif32/cores/esp32/USBCDC.cpp: In member function 'virtual int USBCDC::availableForWrite()':
/home/xtrinch/.platformio/packages/framework-arduinoespressif32/cores/esp32/USBCDC.cpp:364:58: error: 'tud_cdc_n_connected' was not declared in this scope
     if(itf >= MAX_USB_CDC_DEVICES || tx_lock == NULL || !tud_cdc_n_connected(itf)){
                                                          ^~~~~~~~~~~~~~~~~~~
/home/xtrinch/.platformio/packages/framework-arduinoespressif32/cores/esp32/USBCDC.cpp:364:58: note: suggested alternative: 'tud_cdc_tx_complete_cb'
     if(itf >= MAX_USB_CDC_DEVICES || tx_lock == NULL || !tud_cdc_n_connected(itf)){
                                                          ^~~~~~~~~~~~~~~~~~~
                                                          tud_cdc_tx_complete_cb
/home/xtrinch/.platformio/packages/framework-arduinoespressif32/cores/esp32/USBCDC.cpp:370:16: error: 'tud_cdc_n_write_available' was not declared in this scope
     size_t a = tud_cdc_n_write_available(itf);
                ^~~~~~~~~~~~~~~~~~~~~~~~~
Compiling .pio/build/esp32-s2-saola-1/BTAdvertisedDeviceSet.o
Compiling .pio/build/esp32-s2-saola-1/BTScanResultsSet.o
/home/xtrinch/.platformio/packages/framework-arduinoespressif32/cores/esp32/USBCDC.cpp: In member function 'virtual size_t USBCDC::write(const uint8_t*, size_t)':
/home/xtrinch/.platformio/packages/framework-arduinoespressif32/cores/esp32/USBCDC.cpp:377:89: error: 'tud_cdc_n_connected' was not declared in this scope
     if(itf >= MAX_USB_CDC_DEVICES || tx_lock == NULL || buffer == NULL || size == 0 || !tud_cdc_n_connected(itf)){
                                                                                         ^~~~~~~~~~~~~~~~~~~
/home/xtrinch/.platformio/packages/framework-arduinoespressif32/cores/esp32/USBCDC.cpp:377:89: note: suggested alternative: 'tud_cdc_tx_complete_cb'
     if(itf >= MAX_USB_CDC_DEVICES || tx_lock == NULL || buffer == NULL || size == 0 || !tud_cdc_n_connected(itf)){
                                                                                         ^~~~~~~~~~~~~~~~~~~
                                                                                         tud_cdc_tx_complete_cb
/home/xtrinch/.platformio/packages/framework-arduinoespressif32/cores/esp32/USBCDC.cpp:390:13: error: 'tud_cdc_n_connected' was not declared in this scope
Compiling .pio/build/esp32-s2-saola-1/DNSServer.o
         if(!tud_cdc_n_connected(itf)){
             ^~~~~~~~~~~~~~~~~~~
/home/xtrinch/.platformio/packages/framework-arduinoespressif32/cores/esp32/USBCDC.cpp:390:13: note: suggested alternative: 'tud_cdc_tx_complete_cb'
         if(!tud_cdc_n_connected(itf)){
             ^~~~~~~~~~~~~~~~~~~
             tud_cdc_tx_complete_cb
/home/xtrinch/.platformio/packages/framework-arduinoespressif32/cores/esp32/USBCDC.cpp:394:24: error: 'tud_cdc_n_write_available' was not declared in this scope
         size_t space = tud_cdc_n_write_available(itf);
                        ^~~~~~~~~~~~~~~~~~~~~~~~~
/home/xtrinch/.platformio/packages/framework-arduinoespressif32/cores/esp32/USBCDC.cpp:396:13: error: 'tud_cdc_n_write_flush' was not declared in this scope
             tud_cdc_n_write_flush(itf);
             ^~~~~~~~~~~~~~~~~~~~~
/home/xtrinch/.platformio/packages/framework-arduinoespressif32/cores/esp32/USBCDC.cpp:402:23: error: 'tud_cdc_n_write' was not declared in this scope
         size_t sent = tud_cdc_n_write(itf, buffer+so_far, space);
                       ^~~~~~~~~~~~~~~
/home/xtrinch/.platformio/packages/framework-arduinoespressif32/cores/esp32/USBCDC.cpp:402:23: note: suggested alternative: 'tud_cdc_rx_cb'
         size_t sent = tud_cdc_n_write(itf, buffer+so_far, space);
                       ^~~~~~~~~~~~~~~
                       tud_cdc_rx_cb
/home/xtrinch/.platformio/packages/framework-arduinoespressif32/cores/esp32/USBCDC.cpp:406:13: error: 'tud_cdc_n_write_flush' was not declared in this scope
             tud_cdc_n_write_flush(itf);
             ^~~~~~~~~~~~~~~~~~~~~
Compiling .pio/build/esp32-s2-saola-1/EEPROM.o
Compiling .pio/build/esp32-s2-saola-1/ESPmDNS.o
Compiling .pio/build/esp32-s2-saola-1/ETH.o
*** [.pio/build/esp32-s2-saola-1/USBCDC.o] Error 1
Jason2866 commented 1 year ago

You have to clone (not recursive!) this repo https://github.com/hathach/tinyusb in .../components/ to get a working Arduino tinyusb See. You can adopt this example https://github.com/platformio/platform-espressif32/tree/develop/examples/arduino-usb-keyboard In short it is not possible as example since the needed component tinyusb (Arduino) is not included in standard IDF!

xtrinch commented 1 year ago

Could you please be more specific as to where I need to clone tinyusb to? What is the path of “.../components“? Do I need to find internally where platformio installs the esp32-arduino-lib-builder?

Jason2866 commented 1 year ago

Platformio does not install "Arduino Lib Builder". The libs for the Arduino framework are compiled with https://github.com/espressif/esp32-arduino-lib-builder With this script https://github.com/espressif/esp32-arduino-lib-builder/blob/master/tools/update-components.sh the needed additional components needed for Arduino are added to compile the complete Arduino components with IDF. So easiest is to clone the Arduino Lib Builder run the Builder one time and you have everything you need to copy in folder components/Arduino

xtrinch commented 1 year ago

So I compiled with the lib builder, and copied ${esp32-arduino-lib-builder_home}/components/arduino and ${esp32-arduino-lib-builder_home}/components/arduino_tinyusb into the project directory (under ${my_project_dir_home}/components/*), but the error messages remain the same.

EDIT: I also tried removing arduino from framework = espidf, arduino from platformio.ini, and now I get a different set of errors:

Reading CMake configuration...
-- Found Git: /usr/bin/git (found version "2.34.1") 
-- The C compiler identification is GNU 8.4.0
-- The CXX compiler identification is GNU 8.4.0
-- The ASM compiler identification is GNU
-- Found assembler: /home/xtrinch/.platformio/packages/toolchain-xtensa-esp32s2@8.4.0+2021r2-patch3/bin/xtensa-esp32s2-elf-gcc
-- Check for working C compiler: /home/xtrinch/.platformio/packages/toolchain-xtensa-esp32s2@8.4.0+2021r2-patch3/bin/xtensa-esp32s2-elf-gcc
-- Check for working C compiler: /home/xtrinch/.platformio/packages/toolchain-xtensa-esp32s2@8.4.0+2021r2-patch3/bin/xtensa-esp32s2-elf-gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /home/xtrinch/.platformio/packages/toolchain-xtensa-esp32s2@8.4.0+2021r2-patch3/bin/xtensa-esp32s2-elf-g++
-- Check for working CXX compiler: /home/xtrinch/.platformio/packages/toolchain-xtensa-esp32s2@8.4.0+2021r2-patch3/bin/xtensa-esp32s2-elf-g++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Building ESP-IDF components for target esp32s2
-- Configuring incomplete, errors occurred!
See also "/home/xtrinch/repos/platform-espressif32/examples/espidf-peripherals-usb/.pio/build/esp32-s2-saola-1/CMakeFiles/CMakeOutput.log".

fatal: not a git repository (or any of the parent directories): .git
CMake Error at /home/xtrinch/.platformio/packages/framework-espidf/tools/cmake/build.cmake:201 (message):
  Failed to resolve component 'main'.
Call Stack (most recent call first):
  /home/xtrinch/.platformio/packages/framework-espidf/tools/cmake/build.cmake:232 (__build_resolve_and_add_req)
  /home/xtrinch/.platformio/packages/framework-espidf/tools/cmake/build.cmake:509 (__build_expand_requirements)
  /home/xtrinch/.platformio/packages/framework-espidf/tools/cmake/project.cmake:384 (idf_build_process)
  CMakeLists.txt:6 (project)
Jason2866 commented 1 year ago

You can not use a esp idf programm and compile it without changes together with Arduino components. You have to adopt (add) code parts. See the Idf/Arduino examples And yes you have to specify only framework = espidf since you have already added the Arduino code parts in components

stale[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. Please provide more details or it will be closed if no further activity occurs. Thank you for your contributions.