Skip to content

Commit de36e05

Browse files
committed
samples: drivers: moving to zephyr code style
This commit is to be merged with the previous one before merging the PR. Signed-off-by: Joel Guittet <joelguittet@gmail.com>
1 parent 4a717a2 commit de36e05

File tree

8 files changed

+114
-70
lines changed

8 files changed

+114
-70
lines changed

samples/drivers/misc/espressif_rmt/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2025 Joel Guittet (joelguittet@gmail.com)
1+
# SPDX-FileCopyrightText: Copyright The Zephyr Project Contributors
22
# SPDX-License-Identifier: Apache-2.0
33

44
cmake_minimum_required(VERSION 3.20.0)

samples/drivers/misc/espressif_rmt/Kconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Private config options for espressif_rmt sample
22

3-
# Copyright (c) 2025 Joel Guittet (joelguittet@gmail.com)
3+
# SPDX-FileCopyrightText: Copyright The Zephyr Project Contributors
44
# SPDX-License-Identifier: Apache-2.0
55

66
mainmenu "Espressif RMT sample application"

samples/drivers/misc/espressif_rmt/README.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
Overview
88
********
99

10-
This sample demonstrates how to use the :ref:`Espressif RMT driver API <rmt_api>`.
10+
This sample demonstrates how to use the :Espressif RMT driver API.
1111

1212
You should connect a WS2812 strip to the RMT output of the board.
1313

@@ -34,7 +34,7 @@ Sample output
3434
3535
*** Booting Zephyr OS build v4.3.0-2805-g63ff14e3d8d0 ***
3636
Create RMT TX channel
37-
I (141) gpio: GPIO[12]| InputEn: 0| OutputEn: 1| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
37+
I (141) gpio: GPIO[12]| InputEn: 0| OutputEn: 1| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
3838
Install led strip encoder
3939
Enable RMT TX channel
4040
Start LED rainbow chase
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
CONFIG_ESPRESSIF_RMT=y
1+
# SPDX-FileCopyrightText: Copyright The Zephyr Project Contributors
2+
# SPDX-License-Identifier: Apache-2.0

samples/drivers/misc/espressif_rmt/socs/esp32_procpu.overlay

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2025 Joel Guittet (joelguittet@gmail.com)
2+
* SPDX-FileCopyrightText: Copyright The Zephyr Project Contributors
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
Lines changed: 44 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
/*
2-
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: Copyright The Zephyr Project Contributors
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

7-
#include "esp_check.h"
7+
#include <stdlib.h>
8+
#include <zephyr/sys/printk.h>
89
#include "led_strip_encoder.h"
910

10-
static const char *TAG = "led_encoder";
11-
1211
typedef struct {
1312
rmt_encoder_t base;
1413
rmt_encoder_t *bytes_encoder;
@@ -26,90 +25,104 @@ static size_t rmt_encode_led_strip(rmt_encoder_t *encoder, rmt_channel_handle_t
2625
rmt_encode_state_t state = RMT_ENCODING_RESET;
2726
size_t encoded_symbols = 0;
2827
switch (led_encoder->state) {
29-
case 0: // send RGB data
28+
case 0: /* send RGB data */
3029
encoded_symbols += bytes_encoder->encode(bytes_encoder, channel, primary_data, data_size, &session_state);
3130
if (session_state & RMT_ENCODING_COMPLETE) {
32-
led_encoder->state = 1; // switch to next state when current encoding session finished
31+
led_encoder->state = 1; /* switch to next state when current encoding session finished */
3332
}
3433
if (session_state & RMT_ENCODING_MEM_FULL) {
3534
state |= RMT_ENCODING_MEM_FULL;
36-
goto out; // yield if there's no free space for encoding artifacts
35+
goto out; /* yield if there's no free space for encoding artifacts */
3736
}
38-
// fall-through
39-
case 1: // send reset code
37+
/* fall-through */
38+
case 1: /* send reset code */
4039
encoded_symbols += copy_encoder->encode(copy_encoder, channel, &led_encoder->reset_code,
4140
sizeof(led_encoder->reset_code), &session_state);
4241
if (session_state & RMT_ENCODING_COMPLETE) {
43-
led_encoder->state = RMT_ENCODING_RESET; // back to the initial encoding session
42+
led_encoder->state = RMT_ENCODING_RESET; /* back to the initial encoding session */
4443
state |= RMT_ENCODING_COMPLETE;
4544
}
4645
if (session_state & RMT_ENCODING_MEM_FULL) {
4746
state |= RMT_ENCODING_MEM_FULL;
48-
goto out; // yield if there's no free space for encoding artifacts
47+
goto out; /* yield if there's no free space for encoding artifacts */
4948
}
5049
}
5150
out:
5251
*ret_state = state;
5352
return encoded_symbols;
5453
}
5554

56-
static esp_err_t rmt_del_led_strip_encoder(rmt_encoder_t *encoder)
55+
static int rmt_del_led_strip_encoder(rmt_encoder_t *encoder)
5756
{
5857
rmt_led_strip_encoder_t *led_encoder = __containerof(encoder, rmt_led_strip_encoder_t, base);
5958
rmt_del_encoder(led_encoder->bytes_encoder);
6059
rmt_del_encoder(led_encoder->copy_encoder);
6160
free(led_encoder);
62-
return ESP_OK;
61+
return 0;
6362
}
6463

65-
static esp_err_t rmt_led_strip_encoder_reset(rmt_encoder_t *encoder)
64+
static int rmt_led_strip_encoder_reset(rmt_encoder_t *encoder)
6665
{
6766
rmt_led_strip_encoder_t *led_encoder = __containerof(encoder, rmt_led_strip_encoder_t, base);
6867
rmt_encoder_reset(led_encoder->bytes_encoder);
6968
rmt_encoder_reset(led_encoder->copy_encoder);
7069
led_encoder->state = RMT_ENCODING_RESET;
71-
return ESP_OK;
70+
return 0;
7271
}
7372

74-
esp_err_t rmt_new_led_strip_encoder(const led_strip_encoder_config_t *config, rmt_encoder_handle_t *ret_encoder)
73+
int rmt_new_led_strip_encoder(const led_strip_encoder_config_t *config, rmt_encoder_handle_t *ret_encoder)
7574
{
76-
esp_err_t ret = ESP_OK;
75+
int rc;
7776
rmt_led_strip_encoder_t *led_encoder = NULL;
78-
ESP_GOTO_ON_FALSE(config && ret_encoder, ESP_ERR_INVALID_ARG, err, TAG, "invalid argument");
77+
if (!(config && ret_encoder)) {
78+
printk("Invalid argument\n");
79+
return -EINVAL;
80+
}
7981
led_encoder = rmt_alloc_encoder_mem(sizeof(rmt_led_strip_encoder_t));
80-
ESP_GOTO_ON_FALSE(led_encoder, ESP_ERR_NO_MEM, err, TAG, "no mem for led strip encoder");
82+
if (!led_encoder) {
83+
printk("Unable to allocate memory for LED strip encoder\n");
84+
return -ENOMEM;
85+
}
8186
led_encoder->base.encode = rmt_encode_led_strip;
8287
led_encoder->base.del = rmt_del_led_strip_encoder;
8388
led_encoder->base.reset = rmt_led_strip_encoder_reset;
84-
// different led strip might have its own timing requirements, following parameter is for WS2812
89+
/* different led strip might have its own timing requirements, following parameter is for WS2812 */
8590
rmt_bytes_encoder_config_t bytes_encoder_config = {
8691
.bit0 = {
8792
.level0 = 1,
88-
.duration0 = 0.3 * config->resolution / 1000000, // T0H=0.3us
93+
.duration0 = 0.3 * config->resolution / 1000000, /* T0H=0.3us */
8994
.level1 = 0,
90-
.duration1 = 0.9 * config->resolution / 1000000, // T0L=0.9us
95+
.duration1 = 0.9 * config->resolution / 1000000, /* T0L=0.9us */
9196
},
9297
.bit1 = {
9398
.level0 = 1,
94-
.duration0 = 0.9 * config->resolution / 1000000, // T1H=0.9us
99+
.duration0 = 0.9 * config->resolution / 1000000, /* T1H=0.9us */
95100
.level1 = 0,
96-
.duration1 = 0.3 * config->resolution / 1000000, // T1L=0.3us
101+
.duration1 = 0.3 * config->resolution / 1000000, /* T1L=0.3us */
97102
},
98-
.flags.msb_first = 1 // WS2812 transfer bit order: G7...G0R7...R0B7...B0
103+
.flags.msb_first = 1 /* WS2812 transfer bit order: G7...G0R7...R0B7...B0 */
99104
};
100-
ESP_GOTO_ON_ERROR(rmt_new_bytes_encoder(&bytes_encoder_config, &led_encoder->bytes_encoder), err, TAG, "create bytes encoder failed");
105+
rc = rmt_new_bytes_encoder(&bytes_encoder_config, &led_encoder->bytes_encoder);
106+
if (rc) {
107+
printk("Create bytes encoder failed\n");
108+
goto err;
109+
}
101110
rmt_copy_encoder_config_t copy_encoder_config = {};
102-
ESP_GOTO_ON_ERROR(rmt_new_copy_encoder(&copy_encoder_config, &led_encoder->copy_encoder), err, TAG, "create copy encoder failed");
103-
104-
uint32_t reset_ticks = config->resolution / 1000000 * 50 / 2; // reset code duration defaults to 50us
111+
rc = rmt_new_copy_encoder(&copy_encoder_config, &led_encoder->copy_encoder);
112+
if (rc) {
113+
printk("create copy encoder failed\n");
114+
goto err;
115+
}
116+
uint32_t reset_ticks = config->resolution / 1000000 * 50 / 2; /* reset code duration defaults to 50us */
105117
led_encoder->reset_code = (rmt_symbol_word_t) {
106118
.level0 = 0,
107119
.duration0 = reset_ticks,
108120
.level1 = 0,
109121
.duration1 = reset_ticks,
110122
};
111123
*ret_encoder = &led_encoder->base;
112-
return ESP_OK;
124+
return 0;
125+
113126
err:
114127
if (led_encoder) {
115128
if (led_encoder->bytes_encoder) {
@@ -120,5 +133,5 @@ esp_err_t rmt_new_led_strip_encoder(const led_strip_encoder_config_t *config, rm
120133
}
121134
free(led_encoder);
122135
}
123-
return ret;
136+
return rc;
124137
}
Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
/*
2-
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: Copyright The Zephyr Project Contributors
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
6-
#pragma once
6+
7+
#ifndef LED_STRIP_ENCODER_H_
8+
#define LED_STRIP_ENCODER_H_
79

810
#include <stdint.h>
911
#include <zephyr/drivers/misc/espressif_rmt/rmt_encoder.h>
@@ -16,22 +18,23 @@ extern "C" {
1618
* @brief Type of led strip encoder configuration
1719
*/
1820
typedef struct {
19-
uint32_t resolution; /*!< Encoder resolution, in Hz */
21+
uint32_t resolution; /**< Encoder resolution, in Hz */
2022
} led_strip_encoder_config_t;
2123

2224
/**
23-
* @brief Create RMT encoder for encoding LED strip pixels into RMT symbols
25+
* @brief Create RMT encoder for encoding LED strip pixels into RMT symbols.
2426
*
25-
* @param[in] dev Pointer to the device structure for the driver instance
26-
* @param[in] config Encoder configuration
27-
* @param[out] ret_encoder Returned encoder handle
28-
* @return
29-
* - ESP_ERR_INVALID_ARG for any invalid arguments
30-
* - ESP_ERR_NO_MEM out of memory when creating led strip encoder
31-
* - ESP_OK if creating encoder successfully
27+
* @param config Encoder configuration.
28+
* @param ret_encoder Returned encoder handle.
29+
* @retval 0 If successful.
30+
* @retval -EINVAL for any invalid arguments.
31+
* @retval -ENOMEM out of memory when creating led strip encoder.
32+
* @retval -ENODEV because of other error.
3233
*/
33-
esp_err_t rmt_new_led_strip_encoder(const led_strip_encoder_config_t *config, rmt_encoder_handle_t *ret_encoder);
34+
int rmt_new_led_strip_encoder(const led_strip_encoder_config_t *config, rmt_encoder_handle_t *ret_encoder);
3435

3536
#ifdef __cplusplus
3637
}
3738
#endif
39+
40+
#endif /* LED_STRIP_ENCODER_H_ */

samples/drivers/misc/espressif_rmt/src/main.c

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2025 Joel Guittet (joelguittet@gmail.com)
2+
* SPDX-FileCopyrightText: Copyright The Zephyr Project Contributors
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -20,29 +20,27 @@ const struct gpio_dt_spec rmt_gpio = GPIO_DT_SPEC_GET(DT_PATH(zephyr_user), rmt_
2020

2121
static const struct device *const rmt_dev = DEVICE_DT_GET_ONE(espressif_esp32_rmt);
2222

23-
#define RMT_LED_STRIP_RESOLUTION_HZ 10000000 // 10MHz resolution, 1 tick = 0.1us (led strip needs a high resolution)
24-
23+
#define RMT_LED_STRIP_RESOLUTION_HZ 10000000 /* 10MHz resolution, 1 tick = 0.1us (led strip needs a high resolution) */
2524
#define EXAMPLE_LED_NUMBERS 24
2625
#define EXAMPLE_CHASE_SPEED_MS 10
2726

2827
static uint8_t led_strip_pixels[EXAMPLE_LED_NUMBERS * 3];
2928

3029
/**
31-
* @brief Simple helper function, converting HSV color space to RGB color space
30+
* @brief Simple helper function, converting HSV color space to RGB color space.
3231
*
3332
* Wiki: https://en.wikipedia.org/wiki/HSL_and_HSV
34-
*
3533
*/
3634
void led_strip_hsv2rgb(uint32_t h, uint32_t s, uint32_t v, uint32_t *r, uint32_t *g, uint32_t *b)
3735
{
38-
h %= 360; // h -> [0,360]
36+
h %= 360; /* h -> [0,360] */
3937
uint32_t rgb_max = v * 2.55f;
4038
uint32_t rgb_min = rgb_max * (100 - s) / 100.0f;
4139

4240
uint32_t i = h / 60;
4341
uint32_t diff = h % 60;
4442

45-
// RGB adjustment amount by hue
43+
/* RGB adjustment amount by hue */
4644
uint32_t rgb_adj = (rgb_max - rgb_min) * diff / 60;
4745

4846
switch (i) {
@@ -86,54 +84,83 @@ int main(void)
8684
uint32_t blue = 0;
8785
uint16_t hue = 0;
8886
uint16_t start_rgb = 0;
87+
int rc;
8988

9089
if (!device_is_ready(rmt_dev)) {
9190
printk("RMT device %s is not ready\n", rmt_dev->name);
92-
return 0;
91+
return -EINVAL;
9392
}
9493

9594
printk("Create RMT TX channel\n");
9695
rmt_channel_handle_t led_chan = NULL;
9796
rmt_tx_channel_config_t tx_chan_config = {
98-
.clk_src = RMT_CLK_SRC_DEFAULT, // select source clock
97+
.clk_src = RMT_CLK_SRC_DEFAULT, /* select source clock */
9998
.gpio_num = rmt_gpio.pin,
100-
.mem_block_symbols = 64, // increase the block size can make the LED less flickering
99+
.mem_block_symbols = 64, /* increase the block size can make the LED less flickering */
101100
.resolution_hz = RMT_LED_STRIP_RESOLUTION_HZ,
102-
.trans_queue_depth = 4, // set the number of transactions that can be pending in the background
101+
.trans_queue_depth = 4, /* set the number of transactions that can be pending in the background */
103102
};
104-
ESP_ERROR_CHECK(rmt_new_tx_channel(&tx_chan_config, &led_chan));
103+
rc = rmt_new_tx_channel(&tx_chan_config, &led_chan);
104+
if (rc) {
105+
printk("RMT TX channel creation failed\n");
106+
return rc;
107+
}
105108

106109
printk("Install led strip encoder\n");
107110
rmt_encoder_handle_t led_encoder = NULL;
108111
led_strip_encoder_config_t encoder_config = {
109112
.resolution = RMT_LED_STRIP_RESOLUTION_HZ,
110113
};
111-
ESP_ERROR_CHECK(rmt_new_led_strip_encoder(&encoder_config, &led_encoder));
114+
rc = rmt_new_led_strip_encoder(&encoder_config, &led_encoder);
115+
if (rc) {
116+
printk("Unable to create encoder\n");
117+
return rc;
118+
}
112119

113120
printk("Enable RMT TX channel\n");
114-
ESP_ERROR_CHECK(rmt_enable(led_chan));
121+
rc = rmt_enable(led_chan);
122+
if (rc) {
123+
printk("Unable to enable RMT TX channel\n");
124+
return rc;
125+
}
115126

116127
printk("Start LED rainbow chase\n");
117128
rmt_transmit_config_t tx_config = {
118-
.loop_count = 0, // no transfer loop
129+
.loop_count = 0, /* no transfer loop */
119130
};
120131
while (1) {
121132
for (int i = 0; i < 3; i++) {
122133
for (int j = i; j < EXAMPLE_LED_NUMBERS; j += 3) {
123-
// Build RGB pixels
134+
/* Build RGB pixels */
124135
hue = j * 360 / EXAMPLE_LED_NUMBERS + start_rgb;
125136
led_strip_hsv2rgb(hue, 100, 100, &red, &green, &blue);
126137
led_strip_pixels[j * 3 + 0] = green;
127138
led_strip_pixels[j * 3 + 1] = blue;
128139
led_strip_pixels[j * 3 + 2] = red;
129140
}
130-
// Flush RGB values to LEDs
131-
ESP_ERROR_CHECK(rmt_transmit(led_chan, led_encoder, led_strip_pixels, sizeof(led_strip_pixels), &tx_config));
132-
ESP_ERROR_CHECK(rmt_tx_wait_all_done(led_chan, K_FOREVER));
141+
/* Flush RGB values to LEDs */
142+
rc = rmt_transmit(led_chan, led_encoder, led_strip_pixels, sizeof(led_strip_pixels), &tx_config);
143+
if (rc) {
144+
printk("Unable to transmit data over TX channel\n");
145+
return rc;
146+
}
147+
rc = rmt_tx_wait_all_done(led_chan, K_FOREVER);
148+
if (rc) {
149+
printk("Waiting until all done failed\n");
150+
return rc;
151+
}
133152
k_sleep(K_MSEC(EXAMPLE_CHASE_SPEED_MS));
134153
memset(led_strip_pixels, 0, sizeof(led_strip_pixels));
135-
ESP_ERROR_CHECK(rmt_transmit(led_chan, led_encoder, led_strip_pixels, sizeof(led_strip_pixels), &tx_config));
136-
ESP_ERROR_CHECK(rmt_tx_wait_all_done(led_chan, K_FOREVER));
154+
rc = rmt_transmit(led_chan, led_encoder, led_strip_pixels, sizeof(led_strip_pixels), &tx_config);
155+
if (rc) {
156+
printk("Unable to transmit data over TX channel\n");
157+
return rc;
158+
}
159+
rc = rmt_tx_wait_all_done(led_chan, K_FOREVER);
160+
if (rc) {
161+
printk("Waiting until all done failed\n");
162+
return rc;
163+
}
137164
k_sleep(K_MSEC(EXAMPLE_CHASE_SPEED_MS));
138165
}
139166
start_rgb += 60;

0 commit comments

Comments
 (0)