Skip to content

Conversation

@hujun260
Copy link
Contributor

@hujun260 hujun260 commented Jan 27, 2026

Overview

This PR introduces critical section protection to the Field-Oriented Control
(FOC) motor control examples. It adds a new configuration option
CONFIG_EXAMPLES_FOC_CONTROL_CRITSEC that enables atomic critical sections
around the main FOC control loop to prevent race conditions during concurrent
interrupt and task execution.

should merge before apache/nuttx#18135

Changes

Critical Section Support

  • Add CONFIG_EXAMPLES_FOC_CONTROL_CRITSEC Kconfig option for thread-safe
    motor control
  • Define foc_enter_critical() and foc_leave_critical() macros that wrap
    enter_critical_section() and leave_critical_section() APIs
  • Apply critical section protection to all FOC control thread variants:
    • foc_float_thr.c - Floating-point FOC implementation
    • foc_fixed16_thr.c - Fixed-point (B16) FOC implementation

Protected Operations

The critical section encompasses:

  • Motor queue message handling via foc_mq_handle()
  • Motor state and control updates via foc_motor_handle()
  • Device state queries and handling via foc_dev_state_handle()
  • Motor control calculations via foc_motor_control()
  • FOC handler execution via foc_handler_run()
  • Motor state retrieval via foc_motor_get()
  • Device parameter updates via foc_dev_params_set()

Motivation

The FOC control loop performs complex calculations and state updates that must
be protected from concurrent access during interrupt handling. Without critical
section protection:

  • Motor control parameters may be partially updated
  • ADC measurements and current readings may become inconsistent
  • Fault handling and motor state transitions may race with control loop updates
  • Real-time control performance may be compromised

By protecting the entire control loop iteration with a critical section, we
ensure that all state updates are atomic and consistent.

Implementation Details

Macro Implementation

When CONFIG_EXAMPLES_FOC_CONTROL_CRITSEC=y:

#define foc_enter_critical() irqstate_t intflags = enter_critical_section()
#define foc_leave_critical() leave_critical_section(intflags)

Initialize the flags variable to 0 to fix compiler warning about
potential use of uninitialized variable. The flags variable is
conditionally assigned in wdtest_rand() based on callback count.

Signed-off-by: hujun5 <hujun5@xiaomi.com>
Introduce CONFIG_EXAMPLES_FOC_CONTROL_CRITSEC configuration option to enable
critical section protection in the FOC (Field-Oriented Control) motor control
examples. This adds irq-safe critical sections around the main control loop
processing to prevent race conditions and ensure atomic updates of motor
control state and parameters during interrupt handling.

Signed-off-by: hujun5 <hujun5@xiaomi.com>
@xiaoxiang781216 xiaoxiang781216 merged commit 8aa48c4 into apache:master Jan 27, 2026
40 checks passed
@raiden00pl
Copy link
Member

raiden00pl commented Jan 27, 2026

@hujun260 @xiaoxiang781216 this commit message doesn't make any sense for FOC. Sorry, but it looks like AI generated garbage.
At least read the commit message to see if it makes sense, because in this case it is garbage not related to change.

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.

5 participants