Skip to content

espressif: Fixes for Quadrature Encoder mode of PCNT peripheral#16391

Merged
acassis merged 5 commits intoapache:masterfrom
Vajnar:fix-qe
May 16, 2025
Merged

espressif: Fixes for Quadrature Encoder mode of PCNT peripheral#16391
acassis merged 5 commits intoapache:masterfrom
Vajnar:fix-qe

Conversation

@Vajnar
Copy link
Contributor

@Vajnar Vajnar commented May 15, 2025

Summary

After changes in PR #15079, there are few issues/inconsistencies which this PR addresses. Namely:

  1. Mixed assignment of CONFIG_ESP_PCNT_U0_CH1_EDGE_PIN and CONFIG_ESP_PCNT_U0_CH1_LEVEL_PIN in code.
  2. Inconsistent handling of ESP_PCNT_Ux_QE in Kconfig. For unit number zero, if ESP_PCNT_U0_QE is enabled, then CONFIG_ESP_PCNT_U0_CH0_LEVEL_PIN=-1 and CONFIG_ESP_PCNT_U0_CH1_EDGE_PIN=-1. Whereas for units 1,2 and 3 the respective Kconfig values are not defined at all when in Quadrature Encoder mode (i.e. there is no CONFIG_ESP_PCNT_U1_CH0_LEVEL_PIN neither CONFIG_ESP_PCNT_U1_CH1_EDGE_PIN)
  3. Build error when units 1,2 or 3 is enabled and used in Quadrature Encoder mode:
CC:  src/esp_board_pcnt.c src/esp_board_pcnt.c: In function 'board_pcnt_initialize':
src/esp_board_pcnt.c:234:30: error: 'CONFIG_ESP_PCNT_U1_CH0_LEVEL_PIN' undeclared (first use in this function); did you mean 'CONFIG_ESP_PCNT_U1_CH1_LEVEL_PIN'?
  234 |   chan0_cfg.level_gpio_num = CONFIG_ESP_PCNT_U1_CH0_LEVEL_PIN;
      |                              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      |                              CONFIG_ESP_PCNT_U1_CH1_LEVEL_PIN
src/esp_board_pcnt.c:234:30: note: each undeclared identifier is reported only once for each function it appears in
src/esp_board_pcnt.c:236:29: error: 'CONFIG_ESP_PCNT_U1_CH1_EDGE_PIN' undeclared (first use in this function); did you mean 'CONFIG_ESP_PCNT_U0_CH1_EDGE_PIN'?
  236 |   chan1_cfg.edge_gpio_num = CONFIG_ESP_PCNT_U1_CH1_EDGE_PIN;
      |                             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      |                             CONFIG_ESP_PCNT_U0_CH1_EDGE_PIN
src/esp_board_pcnt.c:267:30: error: 'CONFIG_ESP_PCNT_U2_CH0_LEVEL_PIN' undeclared (first use in this function); did you mean 'CONFIG_ESP_PCNT_U2_CH1_LEVEL_PIN'?
  267 |   chan0_cfg.level_gpio_num = CONFIG_ESP_PCNT_U2_CH0_LEVEL_PIN;
      |                              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      |                              CONFIG_ESP_PCNT_U2_CH1_LEVEL_PIN
src/esp_board_pcnt.c:269:29: error: 'CONFIG_ESP_PCNT_U2_CH1_EDGE_PIN' undeclared (first use in this function); did you mean 'CONFIG_ESP_PCNT_U0_CH1_EDGE_PIN'?
  269 |   chan1_cfg.edge_gpio_num = CONFIG_ESP_PCNT_U2_CH1_EDGE_PIN;
      |                             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      |                             CONFIG_ESP_PCNT_U0_CH1_EDGE_PIN
src/esp_board_pcnt.c:301:30: error: 'CONFIG_ESP_PCNT_U3_CH0_LEVEL_PIN' undeclared (first use in this function); did you mean 'CONFIG_ESP_PCNT_U3_CH1_LEVEL_PIN'?
  301 |   chan0_cfg.level_gpio_num = CONFIG_ESP_PCNT_U3_CH0_LEVEL_PIN;
      |                              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      |                              CONFIG_ESP_PCNT_U3_CH1_LEVEL_PIN
src/esp_board_pcnt.c:303:29: error: 'CONFIG_ESP_PCNT_U3_CH1_EDGE_PIN' undeclared (first use in this function); did you mean 'CONFIG_ESP_PCNT_U3_CH0_EDGE_PIN'?
  303 |   chan1_cfg.edge_gpio_num = CONFIG_ESP_PCNT_U3_CH1_EDGE_PIN;
      |                             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      |                             CONFIG_ESP_PCNT_U3_CH0_EDGE_PIN
  1. Depend on SENSORS_QENCODER.

Impact

  1. Handling of Quadrature Enocder Kconfig ESP_PCNT_Ux_QE and definition of CONFIG_ESP_PCNT_Ux_CH0_LEVEL_PIN and CONFIG_ESP_PCNT_Ux_CH1_EDGE_PIN is unified across all PCNT units.
  2. Build error is caused by code expecting that there are all four signals defined per unit. But this is not the case in Quadrature Encoder mode as Kconfig correctly marks the extra 2 signals as depends on !ESP_PCNT_Ux_QE and thus are not defined. We fix this by correctly assigning the 2 pins in crossed fashion in code when in Quadrature Encoder mode.

Testing

Testing was performed on esp32c6-devkitc and qencoder defconfig (in case of 2nd issue modified to include more units).

@github-actions github-actions bot added Arch: risc-v Issues related to the RISC-V (32-bit or 64-bit) architecture Arch: xtensa Issues related to the Xtensa architecture Board: risc-v Board: xtensa Size: M The size of the change in this PR is medium labels May 15, 2025
acassis
acassis previously approved these changes May 15, 2025
Remove SENSORS and SENSORS_QENCODER as it is selected by ESP_PCNT_AS_QE.
@Vajnar Vajnar dismissed stale reviews from xiaoxiang781216 and acassis via 0ba8fd4 May 16, 2025 08:09
@ppisa
Copy link
Contributor

ppisa commented May 16, 2025

The changes enabled @matiamic to use and test QENC support after breakages of original @Vajnar contribution by Espressif changes. It would be nice if Espressif people can check and approve changes (@tmedicci, @eren-terzioglu).
The another breakage, the time widow of incorrect values read, is demonstrated but that is for another fixes.

I have not found time to test the code myself so I am not sure if I should approve changes. I could do that on base of @matiamic report and his demonstration at the meeting at the university.

@tmedicci
Copy link
Contributor

Let it to be tested internally in our internal CI. Don't merge it yet, please.

Copy link
Contributor

@tmedicci tmedicci left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @Vajnar ! Good catches... It's cleaner now.

@acassis acassis merged commit 68b22fe into apache:master May 16, 2025
39 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Arch: risc-v Issues related to the RISC-V (32-bit or 64-bit) architecture Arch: xtensa Issues related to the Xtensa architecture Board: risc-v Board: xtensa Size: M The size of the change in this PR is medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants