Skip to content

Commit

Permalink
[beken-72xx] Pause PWM instead of stopping, track PWM state (libretin…
Browse files Browse the repository at this point in the history
…y-eu#222)

* Pause PWM instead of stopping on duty cycle 0.

* Merged paused and stopped conditions
  • Loading branch information
szupi-ipuzs authored Dec 16, 2023
1 parent 7bd6d1d commit eed39c9
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 25 deletions.
11 changes: 7 additions & 4 deletions cores/beken-72xx/arduino/src/wiring.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,13 @@ void pinRemoveMode(PinInfo *pin, uint32_t mask) {
pinDisable(pin, PIN_IRQ);
}
if ((mask & PIN_PWM) && (pin->enabled & PIN_PWM)) {
data->pwm.cfg.bits.en = PWM_DISABLE;
__wrap_bk_printf_disable();
sddev_control(PWM_DEV_NAME, CMD_PWM_DEINIT_PARAM, &data->pwm);
__wrap_bk_printf_enable();
if (data->pwmState != LT_PWM_STOPPED) {
data->pwmState = LT_PWM_STOPPED;
data->pwm.cfg.bits.en = PWM_DISABLE;
__wrap_bk_printf_disable();
sddev_control(PWM_DEV_NAME, CMD_PWM_DEINIT_PARAM, &data->pwm);
__wrap_bk_printf_enable();
}
pinDisable(pin, PIN_PWM);
}
}
53 changes: 32 additions & 21 deletions cores/beken-72xx/arduino/src/wiring_analog.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,22 @@ void analogWrite(pin_size_t pinNumber, int value) {
// GPIO can't be used together with PWM
pinRemoveMode(pin, PIN_GPIO | PIN_IRQ);

float percent = value * 1.0 / ((1 << _analogWriteResolution) - 1);
uint32_t frequency = 26 * _analogWritePeriod - 1;

if (!pinEnabled(pin, PIN_PWM)) {
pinEnable(pin, PIN_PWM);
data->pwmState = LT_PWM_STOPPED;
data->pwm.channel = gpioToPwm(pin->gpio);
data->pwm.cfg.bits.en = PWM_ENABLE;
data->pwm.cfg.bits.int_en = PWM_INT_DIS;
data->pwm.cfg.bits.mode = PWM_PWM_MODE;
data->pwm.cfg.bits.clk = PWM_CLK_26M;
data->pwm.end_value = frequency;
data->pwm.p_Int_Handler = NULL;
}

float percent = value * 1.0 / ((1 << _analogWriteResolution) - 1);
uint32_t dutyCycle = percent * frequency;
data->pwm.channel = gpioToPwm(pin->gpio);
uint32_t channel = data->pwm.channel;
#if CFG_SOC_NAME != SOC_BK7231N
data->pwm.duty_cycle = dutyCycle;
Expand All @@ -99,33 +111,32 @@ void analogWrite(pin_size_t pinNumber, int value) {
data->pwm.duty_cycle3 = 0;
#endif

if (dutyCycle) {
if (!pinEnabled(pin, PIN_PWM)) {
if ((data->pwmState == LT_PWM_STOPPED) || (data->pwmState == LT_PWM_PAUSED)) {
if (dutyCycle) {
// enable PWM and set its value
data->pwm.cfg.bits.en = PWM_ENABLE;
data->pwm.cfg.bits.int_en = PWM_INT_DIS;
data->pwm.cfg.bits.mode = PWM_PWM_MODE;
data->pwm.cfg.bits.clk = PWM_CLK_26M;
data->pwm.end_value = frequency;
data->pwm.p_Int_Handler = NULL;

__wrap_bk_printf_disable();
sddev_control(PWM_DEV_NAME, CMD_PWM_INIT_PARAM, &data->pwm);
sddev_control(PWM_DEV_NAME, CMD_PWM_INIT_LEVL_SET_HIGH, &channel);
sddev_control(PWM_DEV_NAME, CMD_PWM_UNIT_ENABLE, &channel);
__wrap_bk_printf_enable();
pinEnable(pin, PIN_PWM);
} else {

data->pwmState = LT_PWM_RUNNING;
}
} else if (data->pwmState == LT_PWM_RUNNING) {
if (dutyCycle) {
__wrap_bk_printf_disable();
sddev_control(PWM_DEV_NAME, CMD_PWM_INIT_LEVL_SET_HIGH, &channel);
// update duty cycle
sddev_control(PWM_DEV_NAME, CMD_PWM_SET_DUTY_CYCLE, &data->pwm);
__wrap_bk_printf_enable();
} else {
__wrap_bk_printf_disable();
sddev_control(PWM_DEV_NAME, CMD_PWM_INIT_LEVL_SET_LOW, &channel);
sddev_control(PWM_DEV_NAME, CMD_PWM_SET_DUTY_CYCLE, &data->pwm);
sddev_control(PWM_DEV_NAME, CMD_PWM_UNIT_DISABLE, &channel);
__wrap_bk_printf_enable();

data->pwmState = LT_PWM_PAUSED;
}
} else {
if (pinEnabled(pin, PIN_PWM)) {
// disable PWM
pinRemoveMode(pin, PIN_PWM);
}
// force level as LOW
pinMode(pinNumber, OUTPUT);
digitalWrite(pinNumber, LOW);
}
}
3 changes: 3 additions & 0 deletions cores/beken-72xx/arduino/src/wiring_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@
extern "C" {
#endif

typedef enum lt_pwm_state_tag { LT_PWM_STOPPED, LT_PWM_RUNNING, LT_PWM_PAUSED } lt_pwm_state_t;

struct PinData_s {
pwm_param_t pwm;
lt_pwm_state_t pwmState;
PinMode gpioMode;
PinStatus irqMode;
void *irqHandler;
Expand Down

0 comments on commit eed39c9

Please sign in to comment.