Skip to content

Current sense: compare the pins with _NC as integers - #595

Open
Candas1 wants to merge 1 commit into
simplefoc:devfrom
Candas1:perf/current-sense-pins-int
Open

Candas1 wants to merge 1 commit into
simplefoc:devfrom
Candas1:perf/current-sense-pins-int

Conversation

@Candas1

@Candas1 Candas1 commented Nov 20, 2026

Copy link
Copy Markdown
Collaborator

Description

_isset(pin) compares the int pin with the float NOT_SET: the pin is converted to float, then a float compare is done. On an MCU without FPU both are software library calls, and getPhaseCurrents() does this 3 times per loop (the STM32 injected ADC read once more).

pin != _NC is the same test (_NC is (int)NOT_SET), done with integers: a single instruction.

Type of change

  • Performance improvement (non-breaking, no behaviour change)

How Has This Been Tested?

loopFOC time (DWT cycle counter), foc_current, velocity mode at 2 rad/s, mean of 10 × 256 loops, same sketch and build flags:

dev (ae7b3ad) this PR
loopFOC 297.1 µs 289.2 µs (-7.9 µs, -2.7 %)
flash 89604 B 89484 B (-120 B)
RAM 21752 B 21752 B

Test Configuration/Setup:

  • Hardware: STM32F103RC (no FPU, 64 MHz), hoverboard board, 6PWM, low-side current sense, hall sensors
  • IDE: PlatformIO
  • MCU package version: STM32 Arduino core 2.12 (PlatformIO ststm32 19.7.1)

🤖 Generated with Claude Code

* Inline/LowsideCurrentSense: compare the pins with _NC as integers

_isset(pin) compares an int pin with the float NOT_SET: on MCUs without FPU each check is an int to float
conversion and a soft float compare, three times per getPhaseCurrents() call. pin != _NC is the same test
(_NC = (int)NOT_SET, exact) on integers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* STM32 current sense: compare the injected pins with _NC as integers

Same as Inline/LowsideCurrentSense: _readADCInjectedChannelVoltage() checks the pins on every read.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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.

2 participants