Skip to content

mimxrt/machine_encoder: Fix the numeric values of UP and DOWN.#19268

Open
robert-hh wants to merge 1 commit into
micropython:masterfrom
robert-hh:mimxrt_direction
Open

mimxrt/machine_encoder: Fix the numeric values of UP and DOWN.#19268
robert-hh wants to merge 1 commit into
micropython:masterfrom
robert-hh:mimxrt_direction

Conversation

@robert-hh
Copy link
Copy Markdown
Contributor

Summary

The values should be 0 for UP and 1 for DOWN to match the documentation. Instead they were -2 and -3, causing a test with a count direction set by a Pin to fail, when the symbols were used instead of numbers..

Testing

Tested with the machine_encoder.py and machine_counter.py script and the extended machine_counter.py script from PR #19239.

Generative AI

I did not use generative AI tools when creating this PR.

The values should be 0 for UP and 1 for DOWN to match the documentation.
Instead they were -2 and -3, causing a test with a count direction set
by a Pin to fail, when the symbols were used instead of numbers..

Tested with the machine_encoder.py and machine_counter.py script and
the extended machine_counter.py script from PR micropython#19239.

Signed-off-by: robert-hh <robert@hammelrath.com>
@github-actions
Copy link
Copy Markdown

Code size report:

Reference:  rp2/boards/SOLDERED_NULA_MAX_RP2350: Add Soldered NULA Max board def. [dd23554]
Comparison: mimxrt/machine_encoder: Fix the numeric values of UP and DOWN. [merge of 2d80d7a]
  mpy-cross:    +0 +0.000% 
   bare-arm:    +0 +0.000% 
minimal x86:    +0 +0.000% 
   unix x64:    +0 +0.000% standard
      stm32:    +0 +0.000% PYBV10
      esp32:    +0 +0.000% ESP32_GENERIC
     mimxrt:    +0 +0.000% TEENSY40
        rp2:    +0 +0.000% RPI_PICO_W
       samd:    +0 +0.000% ADAFRUIT_ITSYBITSY_M4_EXPRESS
  qemu rv32:    +0 +0.000% VIRT_RV32

Copy link
Copy Markdown
Contributor

@projectgus projectgus left a comment

Choose a reason for hiding this comment

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

This change seems fine to me, but as far as I can tell the docs don't specify integer values for these at all.

The only other place they're defined is ports/esp32/modules/machine.py which maps them to the PCNT.INCREMENT and PCNT.DECREMENT values which are set to constants in ESP-IDF.

If we want this to stay consistent in MicroPython (and I think we do) then the way to be sure would be:

  • Add a header extmod/machine_counter.h and define MACHINE_COUNTER_UP and MACHINE_COUNTER_DOWN.
  • Use that header in ports/mimxrt/machine_encoder.c
  • Also include that header into esp32_pcnt.c and static assert that MACHINE_COUNTER_UP == PCNT_CNT_INC and MACHINE_COUNTER_DOWN == PCNT_CNT_DEC with a comment that this is for the machine.Counter implementation found in ports/esp32/machine.py

Otherwise it could break at any time due to an ESP-IDF update, or a new machine.Counter implementation could be added which also breaks it.

This is a lot of extra work compared to changing two constants though, so please feel free to also ignore this suggestion and we can merge this as-is.

@robert-hh
Copy link
Copy Markdown
Contributor Author

Add a header extmod/machine_counter.h and define MACHINE_COUNTER_UP and MACHINE_COUNTER_DOWN.

The values of MACHINE_COUNTER_UP and MACHINE_COUNTER_DOWN have to be port-specific, because they depend on the hardware. So they cannot be the same for all ports. I stubled over that when testing parts of PR #19239, where the numeric values for counting up and down for ESP23 are opposite to the ones needed for MIMXRT. If the count direction is directly controlled by an input pin, the level cannot be arbitrarily defined.

The actual PR only ensures, that the symbols Counter.UP and Counter.DOWN behave identical for software- and pin-controlled counting.

@projectgus
Copy link
Copy Markdown
Contributor

projectgus commented May 27, 2026

Add a header extmod/machine_counter.h and define MACHINE_COUNTER_UP and MACHINE_COUNTER_DOWN.

The values of MACHINE_COUNTER_UP and MACHINE_COUNTER_DOWN have to be port-specific, because they depend on the hardware. So they cannot be the same for all ports. I stubled over that when testing parts of PR #19239, where the numeric values for counting up and down for ESP23 are opposite to the ones needed for MIMXRT. If the count direction is directly controlled by an input pin, the level cannot be arbitrarily defined.

OK, I see. I guess we could always map the Python-facing values into the hardware-specific internal values to make this consistent, but that's probably overkill.

The actual PR only ensures, that the symbols Counter.UP and Counter.DOWN behave identical for software- and pin-controlled counting.

Ah, I understand now. OK.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants