From b739756cc4630967559737e904f0b630893a8b3a Mon Sep 17 00:00:00 2001 From: morris Date: Fri, 30 Jun 2023 11:09:32 +0800 Subject: [PATCH] feat(blink): Support different led_strip backend peripheral --- examples/get-started/blink/README.md | 22 +++++--------- .../get-started/blink/main/Kconfig.projbuild | 29 +++++++++++++++---- .../blink/main/blink_example_main.c | 15 +++++++++- .../get-started/blink/main/idf_component.yml | 2 +- .../blink/sdkconfig.ci.led_strip_spi | 2 ++ 5 files changed, 47 insertions(+), 23 deletions(-) create mode 100644 examples/get-started/blink/sdkconfig.ci.led_strip_spi diff --git a/examples/get-started/blink/README.md b/examples/get-started/blink/README.md index 4ec579ef61..038ac35c9e 100644 --- a/examples/get-started/blink/README.md +++ b/examples/get-started/blink/README.md @@ -5,9 +5,7 @@ (See the README.md file in the upper level 'examples' directory for more information about examples.) -This example demonstrates how to blink a LED using GPIO or using the [led_strip](https://components.espressif.com/component/espressif/led_strip) component for the addressable LED, i.e. [WS2812](https://cdn-shop.adafruit.com/datasheets/WS2812B.pdf). - -The `led_strip` is installed via [component manager](main/idf_component.yml). +This example demonstrates how to blink a LED by using the GPIO driver or using the [led_strip](https://components.espressif.com/component/espressif/led_strip) library if the LED is addressable e.g. [WS2812](https://cdn-shop.adafruit.com/datasheets/WS2812B.pdf). The `led_strip` library is installed via [component manager](main/idf_component.yml). ## How to Use Example @@ -15,19 +13,9 @@ Before project configuration and build, be sure to set the correct chip target u ### Hardware Required -* A development board with Espressif SoC (e.g., ESP32-DevKitC, ESP-WROVER-KIT, etc.) +* A development board with normal LED or addressable LED on-board (e.g., ESP32-S3-DevKitC, ESP32-C6-DevKitC etc.) * A USB cable for Power supply and programming -Some development boards use an addressable LED instead of a regular one. These development boards include: - -| Board | LED type | Pin | -| -------------------- | -------------------- | -------------------- | -| ESP32-C3-DevKitC-1 | Addressable | GPIO8 | -| ESP32-C3-DevKitM-1 | Addressable | GPIO8 | -| ESP32-S2-DevKitM-1 | Addressable | GPIO18 | -| ESP32-S2-Saola-1 | Addressable | GPIO18 | -| ESP32-S3-DevKitC-1 | Addressable | GPIO48 | - See [Development Boards](https://www.espressif.com/en/products/devkits) for more information about it. ### Configure the Project @@ -37,7 +25,11 @@ Open the project configuration menu (`idf.py menuconfig`). In the `Example Configuration` menu: * Select the LED type in the `Blink LED type` option. - * Use `GPIO` for regular LED blink. + * Use `GPIO` for regular LED + * Use `LED strip` for addressable LED +* If the LED type is `LED strip`, select the backend peripheral + * `RMT` is only available for ESP targets with RMT peripheral supported + * `SPI` is available for all ESP targets * Set the GPIO number used for the signal in the `Blink GPIO number` option. * Set the blinking period in the `Blink period in ms` option. diff --git a/examples/get-started/blink/main/Kconfig.projbuild b/examples/get-started/blink/main/Kconfig.projbuild index 4938b1ed1d..d0cd42e602 100644 --- a/examples/get-started/blink/main/Kconfig.projbuild +++ b/examples/get-started/blink/main/Kconfig.projbuild @@ -4,15 +4,32 @@ menu "Example Configuration" choice BLINK_LED prompt "Blink LED type" - default BLINK_LED_GPIO if IDF_TARGET_ESP32 || !SOC_RMT_SUPPORTED - default BLINK_LED_RMT + default BLINK_LED_GPIO if IDF_TARGET_ESP32 || IDF_TARGET_ESP32C2 + default BLINK_LED_STRIP help - Defines the default peripheral for blink example + Select the LED type. A normal level controlled LED or an addressable LED strip. + The default selection is based on the Espressif DevKit boards. + You can change the default selection according to your board. config BLINK_LED_GPIO bool "GPIO" - config BLINK_LED_RMT - bool "RMT - Addressable LED" + config BLINK_LED_STRIP + bool "LED strip" + endchoice + + choice BLINK_LED_STRIP_BACKEND + depends on BLINK_LED_STRIP + prompt "LED strip backend peripheral" + default BLINK_LED_STRIP_BACKEND_RMT if SOC_RMT_SUPPORTED + default BLINK_LED_STRIP_BACKEND_SPI + help + Select the backend peripheral to drive the LED strip. + + config BLINK_LED_STRIP_BACKEND_RMT + depends on SOC_RMT_SUPPORTED + bool "RMT" + config BLINK_LED_STRIP_BACKEND_SPI + bool "SPI" endchoice config BLINK_GPIO @@ -23,7 +40,7 @@ menu "Example Configuration" default 48 if IDF_TARGET_ESP32S3 default 8 help - GPIO number (IOxx) to blink on and off or the RMT signal for the addressable LED. + GPIO number (IOxx) to blink on and off the LED. Some GPIOs are used for other purposes (flash connections, etc.) and cannot be used to blink. config BLINK_PERIOD diff --git a/examples/get-started/blink/main/blink_example_main.c b/examples/get-started/blink/main/blink_example_main.c index 8a7c4ed50b..1b15c04904 100644 --- a/examples/get-started/blink/main/blink_example_main.c +++ b/examples/get-started/blink/main/blink_example_main.c @@ -23,7 +23,7 @@ static const char *TAG = "example"; static uint8_t s_led_state = 0; -#ifdef CONFIG_BLINK_LED_RMT +#ifdef CONFIG_BLINK_LED_STRIP static led_strip_handle_t led_strip; @@ -49,10 +49,21 @@ static void configure_led(void) .strip_gpio_num = BLINK_GPIO, .max_leds = 1, // at least one LED on board }; +#if CONFIG_BLINK_LED_STRIP_BACKEND_RMT led_strip_rmt_config_t rmt_config = { .resolution_hz = 10 * 1000 * 1000, // 10MHz + .flags.with_dma = false, }; ESP_ERROR_CHECK(led_strip_new_rmt_device(&strip_config, &rmt_config, &led_strip)); +#elif CONFIG_BLINK_LED_STRIP_BACKEND_SPI + led_strip_spi_config_t spi_config = { + .spi_bus = SPI2_HOST, + .flags.with_dma = true, + }; + ESP_ERROR_CHECK(led_strip_new_spi_device(&strip_config, &spi_config, &led_strip)); +#else +#error "unsupported LED strip backend" +#endif /* Set all LED off to clear all pixels */ led_strip_clear(led_strip); } @@ -73,6 +84,8 @@ static void configure_led(void) gpio_set_direction(BLINK_GPIO, GPIO_MODE_OUTPUT); } +#else +#error "unsupported LED type" #endif void app_main(void) diff --git a/examples/get-started/blink/main/idf_component.yml b/examples/get-started/blink/main/idf_component.yml index 234f9781ac..8723a2e11a 100644 --- a/examples/get-started/blink/main/idf_component.yml +++ b/examples/get-started/blink/main/idf_component.yml @@ -1,2 +1,2 @@ dependencies: - espressif/led_strip: "^2.0.0" + espressif/led_strip: "^2.4.1" diff --git a/examples/get-started/blink/sdkconfig.ci.led_strip_spi b/examples/get-started/blink/sdkconfig.ci.led_strip_spi new file mode 100644 index 0000000000..d41bc346d4 --- /dev/null +++ b/examples/get-started/blink/sdkconfig.ci.led_strip_spi @@ -0,0 +1,2 @@ +CONFIG_BLINK_LED_STRIP=y +CONFIG_BLINK_LED_STRIP_BACKEND_SPI=y