examples: foc: Add critical section protection for thread-safe motor control #3381
+5
−1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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_CRITSECthat enables atomic critical sectionsaround 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
CONFIG_EXAMPLES_FOC_CONTROL_CRITSECKconfig option for thread-safemotor control
foc_enter_critical()andfoc_leave_critical()macros that wrapenter_critical_section()andleave_critical_section()APIsfoc_float_thr.c- Floating-point FOC implementationfoc_fixed16_thr.c- Fixed-point (B16) FOC implementationProtected Operations
The critical section encompasses:
foc_mq_handle()foc_motor_handle()foc_dev_state_handle()foc_motor_control()foc_handler_run()foc_motor_get()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:
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: