Skip to content

Commit

Permalink
Merge branch 'libretiny-eu:master' into bk72xx-wifi-sta-adv
Browse files Browse the repository at this point in the history
  • Loading branch information
Cossid authored Dec 22, 2023
2 parents ba55155 + eed39c9 commit f3926b2
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 83 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);
}
}
72 changes: 41 additions & 31 deletions cores/beken-72xx/arduino/src/wiring_analog.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ static uint8_t gpioToAdc(GPIO_INDEX gpio) {
return 0;
}

static pwm_param_t pwm;
static uint16_t adcData[1];

uint16_t analogReadVoltage(pin_size_t pinNumber) {
Expand Down Expand Up @@ -87,46 +86,57 @@ 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;
pwm.channel = gpioToPwm(pin->gpio);
uint32_t channel = data->pwm.channel;
#if CFG_SOC_NAME != SOC_BK7231N
pwm.duty_cycle = dutyCycle;
data->pwm.duty_cycle = dutyCycle;
#else
pwm.duty_cycle1 = dutyCycle;
pwm.duty_cycle2 = 0;
pwm.duty_cycle3 = 0;
data->pwm.duty_cycle1 = dutyCycle;
data->pwm.duty_cycle2 = 0;
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
pwm.cfg.bits.en = PWM_ENABLE;
pwm.cfg.bits.int_en = PWM_INT_DIS;
pwm.cfg.bits.mode = PWM_PWM_MODE;
pwm.cfg.bits.clk = PWM_CLK_26M;
pwm.end_value = frequency;
pwm.p_Int_Handler = NULL;

__wrap_bk_printf_disable();
sddev_control(PWM_DEV_NAME, CMD_PWM_INIT_PARAM, &pwm);
sddev_control(PWM_DEV_NAME, CMD_PWM_INIT_LEVL_SET_HIGH, &pwm.channel);
sddev_control(PWM_DEV_NAME, CMD_PWM_UNIT_ENABLE, &pwm.channel);
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();
// pass global PWM object pointer
data->pwm = &pwm;
pinEnable(pin, PIN_PWM);
} else {
// update duty cycle
sddev_control(PWM_DEV_NAME, CMD_PWM_SET_DUTY_CYCLE, &pwm);

data->pwmState = LT_PWM_RUNNING;
}
} else {
if (pinEnabled(pin, PIN_PWM)) {
// disable PWM
pinRemoveMode(pin, PIN_PWM);
} 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);
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;
}
// force level as LOW
pinMode(pinNumber, OUTPUT);
digitalWrite(pinNumber, LOW);
}
}
5 changes: 4 additions & 1 deletion 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;
pwm_param_t pwm;
lt_pwm_state_t pwmState;
PinMode gpioMode;
PinStatus irqMode;
void *irqHandler;
Expand Down
Loading

0 comments on commit f3926b2

Please sign in to comment.