This repository provides a reference solution for evaluation and demonstration purposes only. It is not a certified medical device and must not be used for clinical diagnosis or treatment. Any product derived from this reference design must undergo independent verification, validation, and regulatory approval in accordance with applicable medical standards.
This repository contains an MPLAB® X project demonstrating a BLDC motor control algorithm tailored for respiratory equipment applications, such as CPAP and BiPAP. The solution integrates Sensirion flow, pressure, temperature sensors, and provides a robust MCU-MPU communication interface to the Graphical User Interface (GUI).
The implementation is developed using Microchip Motor Control Development Boards (MCLV-48V-300W) in conjunction with the PIC32MK MCM DIM, and interfaces with a SAMA7D65 MPU driving a 10-inch touchscreen display. The motor control firmware runs on the PIC32MK024MCM100, based on the MIPS32® microAptiv™ core with an integrated FPU, enabling efficient real-time motor control, sensor acquisition and processing, and system safety monitoring.
This reference solution demonstrates a distributed MCU–MPU architecture, where:
- The MCU performs all time-critical and safety-related tasks, such as motor control, sensor acquisition and processing, and closed-loop pressure regulation
- The MPU handles user interaction, configuration, visualization, and system monitoring through a touchscreen-based graphical interface
- MPLAB® X IDE v6.25 or newer
- MPLAB® XC32 v5.00 or newer
- MPLAB® Code Configurator (MCC) v5.6.3 or newer
Figure 1. High-Level Block Diagram
The system follows a partitioned control architecture in which real-time motor control and safety functions are decoupled on the MCU, while the MPU manages high-level user interaction and visualization. Communication between the MCU and MPU is implemented using a UART-based protocol with heartbeat and timeout supervision.
The firmware is structured around a deterministic state machine with each operational mode implemented as a non-blocking state function. This approach ensures predictable execution, robust fault handling, and safe transitions between operating modes.
Note: This reference solution currently supports only the two operating modes described below. Volume control is not enabled in the current release and will be supported in a future version.
- Operates in open-loop control without pressure or flow feedback
- Implements bi-level speed control using independent inhale and exhale speed references
- Inhale and exhale timing is configurable
- Speed transitions are smoothed using configurable ramp rates
- Motor speed reference is applied directly to the Field-Oriented Control (FOC) speed control loop
- Can be used for basic operation, system characterization, or fallback operation
-
Regulates airway pressure using real-time pressure and flow sensor feedback
-
Supports independent pressure targets:
- Inspiratory Positive Airway Pressure
- Expiratory Positive Airway Pressure
-
Implements a cascaded control structure:
- Pressure control loop -> speed reference
- Speed control loop -> torque-producing current
-
Automatically compensates for load variations and air leakage
-
Smooth transitions between pressure levels using configurable ramp rates
The PIC32MK MCU is responsible for all real-time control and safety-critical tasks in the system. It executes the BLDC motor control algorithms, interfaces with respiratory sensors, and manages closed-loop pressure regulation while maintaining robust communication with the MPU.
At a functional level, the MCU firmware provides the following capabilities:
-
Sensorless Field-Oriented Control (FOC) of the BLDC blower motor
-
Speed control with torque and current regulation (Id/Iq)
-
Open-loop pressure control where the motor speed reference is set directly without airway pressure feedback, while FOC current and torque loops remain closed-loop
- Sensirion sensor interfacing, including:
- Airflow measurement
- Airway pressure sensing
- Temperature monitoring
-
Closed-loop pressure control achieved by regulating airway pressure and dynamically adjusting motor speed
-
Air-leak detection and recovery mechanism to ensure stable operation under leak conditions
Figure 2. Control Block Diagram
- Fault detection and protection covering motor, sensor, communication, and internal MCU faults
- Robust MCU-MPU communication, including timeout detection and automatic recovery handling
Figure 3. MCU Operation Flow
The following application-specific parameters are defined in the cpap_application.h file located at src/config/default.
These parameters configure the default operating limits and control behavior for the CPAP/BiPAP reference solution and should be adjusted based on:
- Blower motor characteristics
- Target operating range of the respiratory system
- Control strategy evaluation and system tuning
| Macro | Description | Unit |
|---|---|---|
| CPAP_INHALE_PRES_DFLT | Default target pressure during the inhale phase | cmH₂O |
| CPAP_EXHALE_PRES_DFLT | Default target pressure during the exhale phase | cmH₂O |
| CPAP_MIN_PRES | Minimum allowable airway pressure | cmH₂O |
| CPAP_MAX_PRES | Maximum allowable airway pressure | cmH₂O |
| CPAP_RAMP_RATE_DFLT | Rate at which blower speed is increased or decreased | rpm/s |
| CPAP_INHALE_OPLSPEED_DFLT | Default blower speed during inhale in open loop operation | rpm |
| CPAP_EXHALE_OPLSPEED_DFLT | Default blower speed during exhale in open loop operation | rpm |
| CPAP_LEAK_DETECT_TIME | Time window used for leak detection evaluation | sec |
| CPAP_PRESS_LEAK_FACTOR | Threshold factor used to detect pressure drops due to leaks | (0-1) |
| CPAP_RPM_PRESSURE_FACTOR_OPL | Empirical factor relating blower speed to generated pressure | cmH₂O/rpm |
Parameter Description:
-
Inhale and exhale pressure parameters define the target airway pressure for each breathing phase, enabling pressure support during inhalation and pressure relief during exhalation
-
Minimum and maximum pressure limits enforce safe operating boundaries and prevent the system from operating outside the intended pressure range
-
Ramp rate controls the rate at which the blower speed transitions between inhale and exhale operating points, improving comfort by avoiding abrupt pressure changes
-
Inhale and exhale operating speed references define the default blower speeds used during bi-level open-loop operation, providing a stable fallback mode when closed-loop pressure regulation is unavailable
-
Leak detection parameters enable identification of abnormal pressure drops over a defined time window, helping detect mask leaks or system disconnections
-
RPM-pressure scaling factor represents an empirical relationship between blower speed and generated airway pressure and is used for pressure estimation and control calculations
Note: The parameter values provided in this reference solution are for engineering evaluation and demonstration purposes only and do not represent clinical or therapy prescriptions.
The SAMA7D65 MPU hosts a touchscreen-based GUI implemented using Microchip Graphics Suite (MGS) with MPLAB Harmony in a bare-metal environment. The GUI enables:
-
User configuration of operating modes and parameters
-
Real-time visualization of motor speed, pressure, flow, and volume
-
System status and alarm indication
The GUI application and simulation are developed using MGS on Harmony, which offers a comprehensive framework for building embedded graphics applications on Microchip MPUs.
The following resources provide additional details on the SAMA7D65 hardware and MGS development:
- SAMA7D65 Curiosity + New Vision LCD Help - Board and display connection details
- Microchip Graphics Suite (MGS) Harmony User Guide - End to end guidance for MGS development in Harmony
- SAMA7D65 User Guide for MGS Harmony - MGS usage specific to the SAMA7D65 Curiosity Kit
- MGS Application Examples (GitHub) - Repository contains example graphics projects for the SAMA7D65 Curiosity board
- Within the GitHub repository, the example project
mgsh_sama7d65/mgs_quickstart/firmware/mgs_qs_a7d65_curiosity_nvdi_10_1inch.Xcomes preconfigured with a basic graphics layout and fully functional touch drivers, making it an ideal starting point for custom GUI development
- Interface: UART
- Configuration: 115200 baud, 8 data bits, no parity, 1 stop bit (115200-8-N-1)
The MPU (graphics touchscreen) sends control commands to the MCU in response to user interactions through the GUI. A heartbeat mechanism is used to monitor MPU presence. If heartbeat packets are not received within the configured timeout window, the MCU transitions the system to a safe state and stops motor operation.
| Byte | Field | Description |
|---|---|---|
| 0 | Header1 | 0xAA |
| 1 | Header2 | 0x55 |
| 2 | Command | 0x00 = Stop motor 0x01 = Start motor 0x02 = Heartbeat (device present) |
| 3 | Control mode | 0x00 = No closed-loop control 0x01 = Volume control 0x02 = Pressure control |
| 4 | Volume value | Tidal volume setting (experimental, e.g., 8-10 mL/kg) |
| 5 | Lower pressure | Lower target pressure (4-20 cmH₂O) |
| 6 | Upper pressure | Upper target pressure (4-20 cmH₂O) |
| 7 | Low speed set time | Exhalation time (seconds) |
| 8 | High speed set time | Inhalation time (seconds) |
| 9 | Low speed L | Exhalation speed (low byte), 3000-30000 RPM |
| 10 | Low speed H | Exhalation speed (high byte) |
| 11 | High speed L | Inhalation speed (low byte), 3000-30000 RPM |
| 12 | High speed H | Inhalation speed (high byte) |
| 13 | Ramp rate | Speed ramp rate (RPM/sec) |
| 14 | Footer1 | 0xFC |
| 15 | Footer2 | 0xCF |
Once the MCU receives the start command, the MCU continuously transmits motor, sensor, and system status data to the MPU for real-time visualization and monitoring.
- Transmission rate: 100 Hz
| Byte | Field | Description |
|---|---|---|
| 0 | Header1 | 0xAA |
| 1 | Header2 | 0x55 |
| 2 | Motor speed L | Motor speed low byte (RPM) |
| 3 | Motor speed H | Motor speed high byte (RPM) |
| 4 | Flow L | Flow measurement low byte (L/min) |
| 5 | Flow H | Flow measurement high byte (L/min) |
| 6 | Pressure L | Pressure measurement low byte (cmH₂O) |
| 7 | Pressure H | Pressure measurement high byte (cmH₂O) |
| 8 | Temperature L | Temperature low byte (°C) |
| 9 | Temperature H | Temperature high byte (°C) |
| 10 | Volume L | Calculated volume low byte (mL) |
| 11 | Volume H | Calculated volume high byte (mL) |
| 12 | Respiratory rate | Breaths per minute (typically 12-16 bpm for adults) |
| 13 | Alarm status | Alarm and fault status bits |
| 14 | Footer1 | 0xFC |
| 15 | Footer2 | 0xCF |
Alarm Status Bit Definitions
| Bit | Description |
|---|---|
| 0 | Reserved |
| 1 | MPU communication error |
| 2 | Sensirion Flow Sensor (SFM) communication error |
| 3 | MCU internal fault |
| 4 | Air leakage detected (low pressure despite high flow or motor speed) |
- Flow, pressure, temperature, and volume values are transmitted as signed integers
- Flow, pressure, temperature, and volume values are scaled by a factor of 10 (i.e., the actual value = reported value ÷ 10)
- Multi-byte values are represented using low byte followed by high byte (L -> H), implying little-endian format
- The integer width is implied by the presence of L/H bytes (typically 16-bit signed), though it is not explicitly defined
This repository contains the motor control MPLAB X project and MPU firmware binaries required to run the complete CPAP/BiPAP demo system.
- Motor control MPLAB X project:
/pic32mk_dim_cpap_bipap.X - MPU firmware binaries for SD card:
/sd_card_firmware
Follow the steps below to bring up the complete demo system.
-
Insert the PIC32MK MCM Motor Control DIM into the J8 interface header on the MCLV-48V-300W Motor Control Development Board
-
Connect the CPAP blower motor to the Motor Control MCLV-48V-300W board. See the connections in the following table:
Motor Control Board (MCLV-48V-300W) CPAP blower Motor J4, PHA CPAP Motor, Red J4, PHB CPAP Motor, Black J4, PHC CPAP Motor, Yellow -
Power the MCLV-48V-300W board with a 24V DC supply using J1 or J3(+,-) header
- Connect the MCLV-48V-300W board to the PC using the USB Micro-B connector (J16)
- Open the motor control project
pic32mk_dim_cpap_bipap.Xin MPLAB X IDE - Perform a Clean and Build
- Copy the firmware binaries
boot.binandharmony.binto a FAT32-formatted microSD card - Insert the microSD card into the SAMA7D65 Curiosity Kit
- Connect the 10-inch display to the SAMA7D65 Curiosity Kit using the LVDS connector
-
Disconnect the MCLV-48V-300W power supply, then connect the UART interfaces of the MCLV-48V-300W board to the Sensirion sensor and the SAMA7D65 Curiosity board. See the connections in the following tables:
Motor Control Board (MCLV-48V-300W) MPU Graphics Board (SAMA7D65 Curiosity Kit) J13, Pin#14, XPRO2_UART_TX J25, Pin#14, MBUS1_RX J13, Pin#13, XPRO2_UART_RX J25, Pin#13 MBUS1_TX J13, Pin#19, DGND J25, Pin#9, GND Motor Control Board (MCLV-48V-300W) Sensirion SFM3300 J11, Pin#14, XPRO1_UART_TX Sensor, Green_RX J11, Pin#13, XPRO1_UART_RX Sensor, Pink_TX J11, Pin#19, DGND Sensor, White_GND J11, Pin#20, VCC Sensor, Brown +5V
- Power the SAMA7D65 Curiosity board using either:
- USB Type-C® connector on USBA port (J3), or
- External supply through the DC jack (J1)
- Reconnect the Power supply to the MCLV-48V-300W board
- The system is now ready for operation
The touchscreen GUI allows configuration of open-loop and closed-loop operating modes, parameter tuning, and real-time waveform visualization for motor speed, pressure, flow, and volume.
-
Once the hardware setup is complete, the main screen is displayed on the touchscreen after powering up the Motor Control board and the SAMA7D65 Curiosity board

-
From the main screen, the user can press Start Demo to begin operation or select one of the available configuration options (Open Loop or Pressure Control) to modify operating parameters
-
Selecting Open Loop allows the user to configure inhale and exhale motor speeds, phase durations, and ramp rate. The Reset button restores all parameters to their default values

-
Selecting Pressure Control allows the user to configure inhale and exhale pressure targets, phase durations, and ramp rate. The Reset button restores all parameters to their default values

-
After completing the configuration, press Start Demo to run the system. During operation, the main screen displays real-time plots of motor speed, airflow, pressure, and volume with corresponding numeric indicators updated continuously
This reference solution demonstrates a scalable and modular architecture for implementing CPAP and BiPAP respiratory equipment using Microchip MCUs and MPUs. By integrating sensorless BLDC motor control, closed-loop pressure regulation, and a rich GUI, the design provides a solid foundation for system evaluation, rapid prototyping, and future product development.
The solution highlights Microchip’s BLDC motor control capabilities for medical respiratory applications, including CPAP, BiPAP, portable oxygen concentrators, and ventilator systems.
For additional information, design resources, and related reference solutions, refer to the Microchip Medical Solutions webpage.
The following resources provide additional information on the Microchip motor control solutions and the MPLAB ecosystem for developing embedded firmware on 32-bit SAM and PIC32 devices:






