🛠 PWM Motor Control and CAN FD Communication on HPM5100
Objectives
This tutorial aims to configure the HPM5100 microcontroller for a real-time motor control application. You will learn to initialize the 32 PWM channels for pulse-width modulation, configure the CAN FD interface for robust communication at 8 Mbps, and integrate FreeRTOS for task management. This use case combines the computing power of the 400 MHz RISC-V core with advanced analog peripherals.
Required Hardware
- HPM5100 Development Board (HPM5151 model recommended)
- USB Type-C cables for power and debugging
- CAN FD USB Interface (for PC testing)
- Development Environment: HPM SDK v1.13.0, RISC-V toolchain
Steps
Clone the HPM SDK v1.13.0 and configure your path. Ensure the RISC-V toolchain is correctly installed. ``bash git clone https://github.com/hpmicro/sdk_hpm5100.git cd sdk_hpm5100 ``
- Development Environment
The HPM5100 features 32 synchronized PWM channels. In your initialization code, enable the PWM module and set the switching frequency. For BLDC motors, use synchronized timers to ensure precise switching. ``c pwm_config_t pwm_cfg; pwm_get_default_config(&pwm_cfg); pwm_cfg.frequency = 20000; // 20 kHz pwm_init(HPM_PWM0, &pwm_cfg); pwm_enable_channel(HPM_PWM0, PWM_CHANNEL_0); ``
- PWM Configuration
Configure CAN FD for control data transmission. Enable FD mode to reach 8 Mbps. ``c canfd_config_t canfd_cfg; canfd_get_default_config(&canfd_cfg); canfd_cfg.bitrate = 500000; // Arbitration canfd_cfg.data_bitrate = 8000000; // Data phase canfd_init(HPM_CANFD0, &canfd_cfg); canfd_enable_interrupt(HPM_CANFD0, CANFD_INT_RX_FIFO); ``
- CAN FD Initialization
Create a task to read inputs (ADC or UART) and send commands via CAN. Another task handles PWM report updates. ``c void vMotorControlTask(void *pvParameters) { while(1) { uint16_t duty = read_adc_channel(0); pwm_set_duty(HPM_PWM0, PWM_CHANNEL_0, duty); canfd_send_frame(HPM_CANFD0, &tx_frame); vTaskDelay(pdMS_TO_TICKS(10)); } } ``
- FreeRTOS Integration
Verification
Connect the CAN FD interface to your PC. Use a tool like Wireshark or a CAN terminal to observe frames sent at 8 Mbps. Verify on an oscilloscope that PWM signals on corresponding pins show variable duty cycles based on ADC inputs. Ensure the system does not exceed thermal limits if using 480 MHz mode.
Further Reading
Explore using the four configurable operational amplifiers as PGAs for current signal conditioning. You can also enable secure boot via AES-128 to protect firmware from unauthorized modifications, a critical feature for industrial applications.