Skip to content

esp32: Fix unitialized flags field in init of the RMT driver - #1308

Merged
samguyer merged 1 commit into
FastLED:masterfrom
alorbach:master-rmtfix
Dec 15, 2021
Merged

samguyer merged 1 commit into
FastLED:masterfrom
alorbach:master-rmtfix

Conversation

@alorbach

Copy link
Copy Markdown
Contributor

The flags field was not initialized to 0 (No flags) which caused
random failures of the esp32 RMT driver (Depending on what
was in the unitialized memory).

Also added support for debugging output in ESP32 RMT Driver
(Disabled by default).

related: #1300

@urish

urish commented Oct 20, 2021

Copy link
Copy Markdown
Contributor

Verified this issue on Wokwi: this sketch works well with Arduino Core 1.0.6, but is flakey with the latest Arduino Core (2.0.0). I tracked it down to the flags not being initialized, causing the wrong clock source to be selected occasionally.

@urish

urish commented Oct 20, 2021

Copy link
Copy Markdown
Contributor

@samguyer any thoughts on what's left to be done before merging this?

@alorbach

Copy link
Copy Markdown
Contributor Author

Verified this issue on Wokwi: this sketch works well with Arduino Core 1.0.6, but is flakey with the latest Arduino Core (2.0.0). I tracked it down to the flags not being initialized, causing the wrong clock source to be selected occasionally.

My guess is that all memory was always initialized to 0x00 in 1.0.6 after reboot (Which would be the correct flag) and not anymore in 2.0.0, so state was random after reboot.

My code works flawless since I applied that patch with 2.0.0 and I have a large sketch (1.5MB) with lots of background tasks.

@tavdog

tavdog commented Oct 25, 2021

Copy link
Copy Markdown

Nice work. This fix works for me using esp core 2.0.0

@samguyer

samguyer commented Nov 3, 2021

Copy link
Copy Markdown
Contributor

@alorbach Thank you so much for identifying this problem. One issue: the rmt_tx.flags part doesn't seem to compile in our commit checks. Does it compile under the old ESP core?

@urish

urish commented Nov 3, 2021

Copy link
Copy Markdown
Contributor

@samguyer note that there's an alternative way to achieve a similar effect, without depending on the existence of the flags field:

rmt_config_t rmt_tx = {}:

As far as I can tell, this initializes all the fields to 0 (including flags).

@samguyer

samguyer commented Nov 3, 2021

Copy link
Copy Markdown
Contributor

@urish OK, great, thanks! I figured there was some way to do this in a newer version of C++. Do you know if this syntax works on older versions of the Arduino IDE?

@henrygab

henrygab commented Nov 4, 2021

Copy link
Copy Markdown
Contributor

Do you know if this syntax works on older versions of the Arduino IDE?

Both C++ and C requires this to be supported.

In C, non-specified values are zero-initialized. In C++, it's a bit more complex, but presuming rmt_config_t is an aggregate type, and the field is an integral type, it's pretty safe to just consider it as being zero.

Or, if you don't trust the compilers conform to the standards, then you'd need to manually review the output of the initialization....

@urish

urish commented Nov 4, 2021

Copy link
Copy Markdown
Contributor

The definition of rmt_config_t:

/**
* @brief Data struct of RMT configure parameters
*/
typedef struct {
    rmt_mode_t rmt_mode;   /*!< RMT mode: transmitter or receiver */
    rmt_channel_t channel; /*!< RMT channel */
    gpio_num_t gpio_num;   /*!< RMT GPIO number */
    uint8_t clk_div;       /*!< RMT channel counter divider */
    uint8_t mem_block_num; /*!< RMT memory block number */
    uint32_t flags;        /*!< RMT channel extra configurations, OR'd with RMT_CHANNEL_FLAGS_[*] */
    union {
        rmt_tx_config_t tx_config; /*!< RMT TX parameter */
        rmt_rx_config_t rx_config; /*!< RMT RX parameter */
    };
} rmt_config_t;

And with older SDK, 3.0 (link):

/**
 * @brief Data struct of RMT configure parameters
 */
typedef struct {
    rmt_mode_t rmt_mode;               /*!< RMT mode: transmitter or receiver */
    rmt_channel_t channel;             /*!< RMT channel */
    uint8_t clk_div;                   /*!< RMT channel counter divider */
    gpio_num_t gpio_num;               /*!< RMT GPIO number */
    uint8_t mem_block_num;             /*!< RMT memory block number */
    union{
        rmt_tx_config_t tx_config;     /*!< RMT TX parameter */
        rmt_rx_config_t rx_config;     /*!< RMT RX parameter */
    };
} rmt_config_t;

@samguyer

samguyer commented Nov 4, 2021

Copy link
Copy Markdown
Contributor

@henrygab Ah ok, thank you. It's been a while since I thought about initializers. I would probably have gone old-school -- with memset.

@urish Yeah, I saw that they added that flags field. I haven't lookup much (yet) at what those flags can do.

@alorbach Would you mind modifying your code to use the = {} initialization? Then we can run the commit checks and get this code in the repo.

@henrygab

henrygab commented Nov 4, 2021

Copy link
Copy Markdown
Contributor

That's definitely aggregate initialization. For the union, only the first listed item (tx_config) gets initialized.

Thus, if sizeof(rmt_tx_config_t) >= sizeof(rmt_rx_config_t), then all fields will be initialized. However, depending on what is #defined, that may not be true.

Thus, I'd recommend proper initialization to zero via memset(&rmt_tx, 0, sizeof(rmt_config_t)). It's not as pretty, and might be redundant, but if the compiler optimizes properly, it should not be twice-initialized.

@alorbach

alorbach commented Nov 9, 2021

Copy link
Copy Markdown
Contributor Author

@henrygab Ah ok, thank you. It's been a while since I thought about initializers. I would probably have gone old-school -- with memset.

@urish Yeah, I saw that they added that flags field. I haven't lookup much (yet) at what those flags can do.

@alorbach Would you mind modifying your code to use the = {} initialization? Then we can run the commit checks and get this code in the repo.

Sure no problem, I will amend my commit

@urish

urish commented Nov 9, 2021 •

Copy link
Copy Markdown
Contributor

@alorbach these are the flags that are currently available:

#define RMT_CHANNEL_FLAGS_AWARE_DFS (1 << 0) /*!< Channel can work during APB clock scaling */
#define RMT_CHANNEL_FLAGS_INVERT_SIG (1 << 1) /*!< Invert RMT signal */

Quoting the docs, which explain why setting the RMT_CHANNEL_FLAGS_AWARE_DFS can mess up the timing of the WS2812 signals:

  • Extra miscellaneous parameters for the channel can be set in the flags.

    • When RMT_CHANNEL_FLAGS_AWARE_DFS is set, RMT channel will take REF_TICK or XTAL as source clock. The benefit is, RMT channel can continue work even when APB clock is changing. See :doc:power_management <../system/power_management> for more information.
      * When RMT_CHANNEL_FLAGS_INVERT_SIG is set, the driver will invert the RMT signal sending to or receiving from the channel. It just works like an external inverter connected to the GPIO of certain RMT channel.
  • A clock divider, that will determine the range of pulse length generated by the RMT transmitter or discriminated by the receiver. Selected by setting clk_div to a value within [1 .. 255] range. The RMT source clock is typically APB CLK, 80Mhz by default. But when RMT_CHANNEL_FLAGS_AWARE_DFS is set in flags, RMT source clock is changed to REF_TICK or XTAL.

@henrygab

henrygab commented Nov 9, 2021

Copy link
Copy Markdown
Contributor

@alorbach Would you mind modifying your code to use the = {} initialization? Then we can run the commit checks and get this code in the repo.

Actually, using brace-init is not guaranteed to initialize the entirety of the structure.

Depending on which symbols are defined, sizeof(rmt_tx_config_t) might be larger or smaller than sizeof(rmt_rx_config_t). Only when sizeof(rmt_tx_config_t) is larger will brace-initialization ensure the full structure is set to defaults / zero. (This is because both those fields are in a union, and only the first member of the union gets initialized.)

Thus, I actually recommended the use of memset() in this case. If you are not concerned about the initialization of the rmt_rx_config_t, then the brace-init will work fine.

@alorbach

Copy link
Copy Markdown
Contributor Author

@henrygab: I have changed the initialization tp 0 with memset as recommended.

@alorbach

alorbach commented Dec 9, 2021

Copy link
Copy Markdown
Contributor Author

I have fixed the merge conflicts, can this now be merged?
Without the FIX I doubt that anybody can use FastLED successfully with newer ESP32 Arduino Core (2.0.0).

The flags field was not initialized to 0 (No flags) which caused
random failures of the esp32 RMT driver (Depending on what
was in the unitialized memory) on newer esp-idf versions.

Also added support for debugging output in ESP32 RMT Driver
(Disabled by default).

related: FastLED#1300
@ckohrt

ckohrt commented Dec 15, 2021 •

Copy link
Copy Markdown

When will this be delivered?
I am currently using version 3.3.3 which is working. 3.4.0 is not working. As a newbi I needed a while to find out that there is a bug ...
Thanks

@samguyer

Copy link
Copy Markdown
Contributor

@alorbach and @henrygab I am running the merge test. As soon as that comes back I'll merge it in!
Thank you for your work on this problem.

@samguyer
samguyer merged commit 1d236ab into FastLED:master Dec 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants