hujun260 opened a new pull request, #3381:
URL: https://github.com/apache/nuttx-apps/pull/3381
## 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.
## 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`:
```c
#define foc_enter_critical() irqstate_t intflags = enter_critical_section()
#define foc_leave_critical() leave_critical_section(intflags)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]