PWM Driver

[中文]

Overview

PWM (Pulse Width Modulation) driver provides flexible pulse width control for motor control, LED dimming, and audio applications.

Functional Description

  • Multiple PWM channels with independent control

  • Configurable period and duty cycle

  • Phase shift functionality for multi-channel operation

  • Fade functionality for smooth transitions

  • Interrupt support for PWM events

  • Frequency and duty cycle control

  • Phase offset control

  • Low power mode support

Development Guide

  1. Initialization - Call bk_pwm_driver_init() before any PWM operations - Configure PWM parameters: bk_pwm_init(chan, &config) - Set period and duty cycle: bk_pwm_set_period_duty()

  2. Basic Operations - Start/stop: bk_pwm_start(), bk_pwm_stop() - Duty control: bk_pwm_set_duty() - Frequency: bk_pwm_set_frequency()

  3. Phase Shift Mode - Initialize: bk_pwm_phase_shift_init(&config) - Start: bk_pwm_phase_shift_start() - Configure multiple channels with phase offsets

  4. Fade Mode - Initialize: bk_pwm_fade_init(&config) - Start fade: bk_pwm_fade_start() - Set start/end duty and fade time

Notes

  • Duty cycle cannot exceed period value

  • Consider frequency limitations

  • Use proper interrupt priorities

API Reference

  • bk_pwm_driver_init/deinit() - Driver management

  • bk_pwm_init/deinit() - Channel management

  • bk_pwm_start/stop() - Control operations

  • bk_pwm_set_period_duty() - Period and duty configuration

  • bk_pwm_set_duty/frequency/phase() - Parameter control

  • bk_pwm_phase_shift_init/start/stop() - Phase shift mode

  • bk_pwm_fade_init/start/stop() - Fade mode

  • bk_pwm_register_isr() - Interrupt handling

Examples

Basic PWM control:

pwm_init_config_t config = {
    .period_cycle = 1000,
    .duty_cycle = 500,
    .freq = 1000
};

bk_pwm_driver_init();
bk_pwm_init(PWM_CHAN_0, &config);
bk_pwm_start(PWM_CHAN_0);
bk_pwm_set_duty(PWM_CHAN_0, 750);  // 75% duty

Phase shift mode:

pwm_phase_shift_config_t config = {
    .psc = 0,
    .period_cycle = 1000,
    .chan_num = 2,
    .duty_config = {
        {.chan = PWM_CHAN_0, .duty_cycle = 300},
        {.chan = PWM_CHAN_1, .duty_cycle = 300}
    }
};

bk_pwm_phase_shift_init(&config);
bk_pwm_phase_shift_start();

Fade mode:

pwm_fade_config_t config = {
    .chan = PWM_CHAN_0,
    .start_duty = 0,
    .end_duty = 1000,
    .fade_time_ms = 2000
};

bk_pwm_fade_init(&config);
bk_pwm_fade_start(PWM_CHAN_0, PWM_DUTY_DIR_INCREASE);

Multi-channel control:

pwm_init_config_t config = {
    .period_cycle = 1000,
    .duty_cycle = 500,
    .freq = 1000
};

bk_pwm_init(PWM_CHAN_0, &config);
bk_pwm_init(PWM_CHAN_1, &config);
bk_pwm_init(PWM_CHAN_2, &config);

bk_pwm_start(PWM_CHAN_0);
bk_pwm_start(PWM_CHAN_1);
bk_pwm_start(PWM_CHAN_2);

bk_pwm_set_duty(PWM_CHAN_0, 250);  // 25%
bk_pwm_set_duty(PWM_CHAN_1, 500);  // 50%
bk_pwm_set_duty(PWM_CHAN_2, 750);  // 75%

FAQs

  • PWM output incorrect: Check period and duty cycle settings

  • Phase shift not working: Verify phase shift configuration

  • Fade not smooth: Adjust fade time and duty range

Error Codes

  • BK_ERR_PWM_PERIOD_DUTY: Invalid period/duty configuration

  • BK_ERR_PWM_PHASE_SHIFT_CHAN_NUM: Insufficient channels for phase shift

  • BK_ERR_NO_MEM: Phase shift configuration memory allocation failed