From 6d93a90e5d876667921835688e90bad734e30cc0 Mon Sep 17 00:00:00 2001 From: Brendan <2bndy5@gmail.com> Date: Tue, 9 Apr 2024 11:41:24 -0700 Subject: [PATCH] push WIP example This is an incomplete example --- examples_esp/getting_started/.gitignore | 12 +++ examples_esp/getting_started/CMakeLists.txt | 6 ++ examples_esp/getting_started/Kconfig | 10 ++ .../getting_started/dependencies.lock | 21 +++++ examples_esp/getting_started/include/README | 39 ++++++++ examples_esp/getting_started/lib/README | 46 ++++++++++ examples_esp/getting_started/platformio.ini | 23 +++++ .../getting_started/sdkconfig.defaults | 1 + .../getting_started/src/CMakeLists.txt | 19 ++++ .../getting_started/src/idf_component.yml | 4 + examples_esp/getting_started/src/main.cpp | 92 +++++++++++++++++++ utility/esp_idf/CMakeLists.txt | 12 --- 12 files changed, 273 insertions(+), 12 deletions(-) create mode 100644 examples_esp/getting_started/.gitignore create mode 100644 examples_esp/getting_started/CMakeLists.txt create mode 100644 examples_esp/getting_started/Kconfig create mode 100644 examples_esp/getting_started/dependencies.lock create mode 100644 examples_esp/getting_started/include/README create mode 100644 examples_esp/getting_started/lib/README create mode 100644 examples_esp/getting_started/platformio.ini create mode 100644 examples_esp/getting_started/sdkconfig.defaults create mode 100644 examples_esp/getting_started/src/CMakeLists.txt create mode 100644 examples_esp/getting_started/src/idf_component.yml create mode 100644 examples_esp/getting_started/src/main.cpp diff --git a/examples_esp/getting_started/.gitignore b/examples_esp/getting_started/.gitignore new file mode 100644 index 00000000..2bb6c13b --- /dev/null +++ b/examples_esp/getting_started/.gitignore @@ -0,0 +1,12 @@ +.pio +.vscode/.browse.c_cpp.db* +.vscode/c_cpp_properties.json +.vscode/launch.json +.vscode/ipch + +# ESP component registry cache +managed_components/ + +# generated sdkconfig files +sdkconfig.* +!sdkconfig.defaults diff --git a/examples_esp/getting_started/CMakeLists.txt b/examples_esp/getting_started/CMakeLists.txt new file mode 100644 index 00000000..07b0ba75 --- /dev/null +++ b/examples_esp/getting_started/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.16.0) +include($ENV{IDF_PATH}/tools/cmake/project.cmake) +# include parent folder (repo root) as a component library +list(APPEND EXTRA_COMPONENT_DIRS ../../) +# in user projects (external to RF24 repo), they should use `idf_component_register()` instead +project(getting_started) diff --git a/examples_esp/getting_started/Kconfig b/examples_esp/getting_started/Kconfig new file mode 100644 index 00000000..6a738399 --- /dev/null +++ b/examples_esp/getting_started/Kconfig @@ -0,0 +1,10 @@ +config RF24_CE_PIN + int "The GPIO pin number used for the radio's CE pin" + help + If undefined, then the main app's code defaults to using pin 7. + +config RF24_CSN_PIN + int "The GPIO pin number used for the radio's CSN pin" + help + If undefined, then the main app's code defaults to using pin 8. + \ No newline at end of file diff --git a/examples_esp/getting_started/dependencies.lock b/examples_esp/getting_started/dependencies.lock new file mode 100644 index 00000000..b3130528 --- /dev/null +++ b/examples_esp/getting_started/dependencies.lock @@ -0,0 +1,21 @@ +dependencies: + espressif/esp_tinyusb: + component_hash: f151d680d6847bfcfd5d8eb6d1c3ff926c208e6b963b2e83643a141bc70baa15 + source: + service_url: https://api.components.espressif.com/ + type: service + version: 1.4.4 + espressif/tinyusb: + component_hash: 6e128177d3fe5027750a75e1ff64c655a7b53fa64eff8c1a4a8a5a4daa7924d1 + source: + service_url: https://api.components.espressif.com/ + type: service + version: 0.15.0~7 + idf: + component_hash: null + source: + type: idf + version: 5.2.1 +manifest_hash: 65bdfdf3ce9ef7f7d3d9c4c075a17d649adf01fa2984a9b54e4b8b493274a309 +target: esp32s2 +version: 1.0.0 diff --git a/examples_esp/getting_started/include/README b/examples_esp/getting_started/include/README new file mode 100644 index 00000000..194dcd43 --- /dev/null +++ b/examples_esp/getting_started/include/README @@ -0,0 +1,39 @@ + +This directory is intended for project header files. + +A header file is a file containing C declarations and macro definitions +to be shared between several project source files. You request the use of a +header file in your project source file (C, C++, etc) located in `src` folder +by including it, with the C preprocessing directive `#include'. + +```src/main.c + +#include "header.h" + +int main (void) +{ + ... +} +``` + +Including a header file produces the same results as copying the header file +into each source file that needs it. Such copying would be time-consuming +and error-prone. With a header file, the related declarations appear +in only one place. If they need to be changed, they can be changed in one +place, and programs that include the header file will automatically use the +new version when next recompiled. The header file eliminates the labor of +finding and changing all the copies as well as the risk that a failure to +find one copy will result in inconsistencies within a program. + +In C, the usual convention is to give header files names that end with `.h'. +It is most portable to use only letters, digits, dashes, and underscores in +header file names, and at most one dot. + +Read more about using header files in official GCC documentation: + +* Include Syntax +* Include Operation +* Once-Only Headers +* Computed Includes + +https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html diff --git a/examples_esp/getting_started/lib/README b/examples_esp/getting_started/lib/README new file mode 100644 index 00000000..2593a33f --- /dev/null +++ b/examples_esp/getting_started/lib/README @@ -0,0 +1,46 @@ + +This directory is intended for project specific (private) libraries. +PlatformIO will compile them to static libraries and link into executable file. + +The source code of each library should be placed in an own separate directory +("lib/your_library_name/[here are source files]"). + +For example, see a structure of the following two libraries `Foo` and `Bar`: + +|--lib +| | +| |--Bar +| | |--docs +| | |--examples +| | |--src +| | |- Bar.c +| | |- Bar.h +| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html +| | +| |--Foo +| | |- Foo.c +| | |- Foo.h +| | +| |- README --> THIS FILE +| +|- platformio.ini +|--src + |- main.c + +and a contents of `src/main.c`: +``` +#include +#include + +int main (void) +{ + ... +} + +``` + +PlatformIO Library Dependency Finder will find automatically dependent +libraries scanning project source files. + +More information about PlatformIO Library Dependency Finder +- https://docs.platformio.org/page/librarymanager/ldf.html diff --git a/examples_esp/getting_started/platformio.ini b/examples_esp/getting_started/platformio.ini new file mode 100644 index 00000000..735fbd69 --- /dev/null +++ b/examples_esp/getting_started/platformio.ini @@ -0,0 +1,23 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html +[env] +platform = espressif32 +framework = espidf + +[env:adafruit_qtpy_esp32s2] +board = adafruit_qtpy_esp32s2 +build_flags = + ; using A1 for CE_PIN + -D CONFIG_RF24_CE_PIN=17 + ; using A2 for CSN_PIN + -D CONFIG_RF24_CSN_PIN=9 + -D CONFIG_RF24_DEFAULT_MOSI=35 + -D CONFIG_RF24_DEFAULT_MISO=37 + -D CONFIG_RF24_DEFAULT_SCLK=36 diff --git a/examples_esp/getting_started/sdkconfig.defaults b/examples_esp/getting_started/sdkconfig.defaults new file mode 100644 index 00000000..afec5843 --- /dev/null +++ b/examples_esp/getting_started/sdkconfig.defaults @@ -0,0 +1 @@ +CONFIG_TINYUSB_CDC_ENABLED=y diff --git a/examples_esp/getting_started/src/CMakeLists.txt b/examples_esp/getting_started/src/CMakeLists.txt new file mode 100644 index 00000000..0dc0513a --- /dev/null +++ b/examples_esp/getting_started/src/CMakeLists.txt @@ -0,0 +1,19 @@ +# This file was automatically generated for projects +# without default 'CMakeLists.txt' file. + +FILE(GLOB_RECURSE app_sources ${CMAKE_SOURCE_DIR}/src/*.*) + +idf_component_register(SRCS ${app_sources} + REQUIRES RF24 + # # for user projects (that don't belong in the RF24 repo), use the component registry + # REQUIRES nRF24/RF24 +) + +if(DEFINED CONFIG_RF24_CE_PIN) + target_compile_definitions(main PUBLIC CE_PIN=${CONFIG_RF24_CE_PIN}) +endif() + +if(DEFINED CONFIG_RF24_CSN_PIN) + target_compile_definitions(main PUBLIC CSN_PIN=${CONFIG_RF24_CSN_PIN}) +endif() + diff --git a/examples_esp/getting_started/src/idf_component.yml b/examples_esp/getting_started/src/idf_component.yml new file mode 100644 index 00000000..ff67b5da --- /dev/null +++ b/examples_esp/getting_started/src/idf_component.yml @@ -0,0 +1,4 @@ +## IDF Component Manager Manifest File +dependencies: + espressif/esp_tinyusb: "^1" + idf: "^5.0" diff --git a/examples_esp/getting_started/src/main.cpp b/examples_esp/getting_started/src/main.cpp new file mode 100644 index 00000000..0c7f27e9 --- /dev/null +++ b/examples_esp/getting_started/src/main.cpp @@ -0,0 +1,92 @@ +#include +#include "esp_log.h" +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "tinyusb.h" +#include "tusb_cdc_acm.h" +#include "tusb_console.h" +#include "sdkconfig.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef CONFIG_RF24_CE_PIN + #define CE_PIN CONFIG_RF24_CE_PIN +#else + #define CE_PIN 7 +#endif + +#ifdef CONFIG_RF24_CSN_PIN + #define CSN_PIN CONFIG_RF24_CSN_PIN +#else + #define CSN_PIN 8 +#endif + +RF24 radio = RF24(CE_PIN, CSN_PIN); + +static const char* TAG = "RF24/examples_esp/getting_started"; + +void setupSerialUSB(); // See definition below + +void app_main(void) +{ + setupSerialUSB(); + + SPIClass spi; + spi.begin(); + fprintf(stdout, "SPI bus configured\n"); + + // if (!radio.begin(&spi)) { + // while (true) { + // fprintf(stdout, "radio hardware not responding!\n"); + // } + // } + while (true) { + fprintf(stdout, "radio is ready to configure.\n"); + vTaskDelay(1000 / portTICK_PERIOD_MS); + } +} + +/** Setup TinyUSB for CDC serial communication */ +void setupSerialUSB() +{ + ESP_LOGI(TAG, "USB initialization"); + const tinyusb_config_t tusb_cfg = { + .device_descriptor = NULL, // Use the default device descriptor specified in Menuconfig + .string_descriptor = NULL, // Use the default string descriptors specified in Menuconfig + .string_descriptor_count = 0, + .external_phy = false, // Use internal USB PHY (in most cases) +#if (TUD_OPT_HIGH_SPEED) + .fs_configuration_descriptor = NULL, // Use the default full-speed configuration descriptor according to settings in Menuconfig + .hs_configuration_descriptor = NULL, // Use the default high-speed configuration descriptor according to settings in Menuconfig + .qualifier_descriptor = NULL, // Use the default qualifier descriptor, with values from default device descriptor +#else + .configuration_descriptor = NULL, // Use the default configuration descriptor according to settings in Menuconfig +#endif // TUD_OPT_HIGH_SPEED + .self_powered = false, + .vbus_monitor_io = 0, + }; + ESP_ERROR_CHECK(tinyusb_driver_install(&tusb_cfg)); + + const tinyusb_config_cdcacm_t acm_cfg = { + .usb_dev = TINYUSB_USBDEV_0, + .cdc_port = TINYUSB_CDC_ACM_0, + .rx_unread_buf_sz = 64, + .callback_rx = NULL, + .callback_rx_wanted_char = NULL, + .callback_line_state_changed = NULL, + .callback_line_coding_changed = NULL}; + ESP_ERROR_CHECK(tusb_cdc_acm_init(&acm_cfg)); + + ESP_LOGI(TAG, "USB initialization DONE"); + esp_tusb_init_console(TINYUSB_CDC_ACM_0); // log to usb + + // the delay here isn't needed, but any `abort()` called immediately after this function would + // prevent the OS from seeing the USB port enumerated as a CDC Serial device + vTaskDelay(1000 / portTICK_PERIOD_MS); // remove this when lib is working properly +} + +#ifdef __cplusplus +} +#endif diff --git a/utility/esp_idf/CMakeLists.txt b/utility/esp_idf/CMakeLists.txt index 99bc4451..e55b0e0d 100644 --- a/utility/esp_idf/CMakeLists.txt +++ b/utility/esp_idf/CMakeLists.txt @@ -1,6 +1,5 @@ ## Include this file if you want to use the RF24 library ## in YOUR (ESP-IDF) project. -## See examples_esp/CMakeLists.txt to see how it could be done cmake_minimum_required(VERSION 3.16.0) @@ -14,14 +13,3 @@ idf_component_register( INCLUDE_DIRS "${CMAKE_CURRENT_LIST_DIR}/../../" REQUIRES driver esp_timer # esp_driver_gpio esp_driver_spi ) - -# add_library(RF24 STATIC -# ${CMAKE_CURRENT_LIST_DIR}/../../RF24.cpp -# ${CMAKE_CURRENT_LIST_DIR}/compatibility.cpp -# ${CMAKE_CURRENT_LIST_DIR}/gpio.cpp -# ${CMAKE_CURRENT_LIST_DIR}/spi.cpp -# ) - -# target_include_directories(RF24 PUBLIC -# ${CMAKE_CURRENT_LIST_DIR}/../../ -# )