Skip to content

C++ compiler warning: compound assignment with 'volatile'-qualified left operand is deprecated #1017

@jonathangjertsen

Description

@jonathangjertsen

Including hardware/pio.h from a C++ file generates warnings like the following. My compiler version is arm-none-eabi-g++ (GNU Toolchain for the Arm Architecture 11.2-2022.02 (arm-11.16)) 11.2.1 20220111.

In file included from <snip>/ws2812.cpp:1:
<snip>/pio.h: In function 'void pio_sm_restart(PIO, uint)':
<snip>/pio.h:583:15: error: compound assignment with 'volatile'-qualified left operand is deprecated [-Werror=volatile]
  583 |     pio->ctrl |= 1u << (PIO_CTRL_SM_RESTART_LSB + sm);
      |     ~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<snip>/pio.h: In function 'void pio_restart_sm_mask(PIO, uint32_t)':
<snip>/pio.h:598:15: error: compound assignment with 'volatile'-qualified left operand is deprecated [-Werror=volatile]
  598 |     pio->ctrl |= (mask << PIO_CTRL_SM_RESTART_LSB) & PIO_CTRL_SM_RESTART_BITS;
<snip>/pio.h: In function 'void pio_sm_clkdiv_restart(PIO, uint)':

... and so on.

This is because operations such as |= and &= on volatile variables is deprecated in C++20. Here is a summary of the change: https://blog.feabhas.com/2021/05/modern-embedded-c-deprecation-of-volatile/

What the C++ committee wants you to do is to rewrite each such statement as follows:

- pio->ctrl |= 1u << (PIO_CTRL_SM_RESTART_LSB + sm);
+ pio->ctrl = pio->ctrl | (1u << (PIO_CTRL_SM_RESTART_LSB + sm));

Sample of responses from other projects:

  • Zephyr silenced the warning by adding -Wno-volatile, pointing to the fact that they do not control the CMSIS header files.
  • CMSIS will not fix the issue in their files as they do not officially support C++. The maintainer's suggestion that it should work with extern "C" is not correct since that's just a linkage directive.
  • esp-idf updated some macros which generated this warning.
  • STMCubeWB has not done anything

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions