nrf5/modules/machine: Updating PWM module with two new kwargs parameters. One for setting pulse with more fine grained. This value should not exceed the period value. Also, adding support for setting PWM mode, whether it is LOW duty cycle or HIGH duty cycle. By default, high to low is set (this could be changed).

This commit is contained in:
Glenn Ruben Bakke 2017-04-18 21:00:52 +02:00
parent d0d350da16
commit 071e551b79

View File

@ -111,6 +111,8 @@ enum {
ARG_NEW_freq,
ARG_NEW_period,
ARG_NEW_duty,
ARG_NEW_pulse_width,
ARG_NEW_mode
};
// for init
@ -137,6 +139,8 @@ STATIC mp_obj_t machine_pwm_make_new(const mp_obj_type_t *type, size_t n_args, s
{ MP_QSTR_freq, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
{ MP_QSTR_period, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
{ MP_QSTR_duty, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
{ MP_QSTR_pulse_width, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
{ MP_QSTR_mode, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
};
// parse args
@ -217,6 +221,7 @@ STATIC const mp_rom_map_elem_t machine_pwm_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_freq), MP_ROM_PTR(&mp_machine_pwm_freq_obj) },
{ MP_ROM_QSTR(MP_QSTR_period), MP_ROM_PTR(&mp_machine_pwm_period_obj) },
{ MP_ROM_QSTR(MP_QSTR_duty), MP_ROM_PTR(&mp_machine_pwm_duty_obj) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_FREQ_16MHZ), MP_OBJ_NEW_SMALL_INT(HAL_PWM_FREQ_16Mhz) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_FREQ_8MHZ), MP_OBJ_NEW_SMALL_INT(HAL_PWM_FREQ_8Mhz) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_FREQ_4MHZ), MP_OBJ_NEW_SMALL_INT(HAL_PWM_FREQ_4Mhz) },
@ -225,6 +230,9 @@ STATIC const mp_rom_map_elem_t machine_pwm_locals_dict_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR_FREQ_500KHZ), MP_OBJ_NEW_SMALL_INT(HAL_PWM_FREQ_500khz) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_FREQ_250KHZ), MP_OBJ_NEW_SMALL_INT(HAL_PWM_FREQ_250khz) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_FREQ_125KHZ), MP_OBJ_NEW_SMALL_INT(HAL_PWM_FREQ_125khz) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_MODE_LOW_HIGH), MP_OBJ_NEW_SMALL_INT(HAL_PWM_MODE_LOW_HIGH) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_MODE_HIGH_LOW), MP_OBJ_NEW_SMALL_INT(HAL_PWM_MODE_HIGH_LOW) },
};
STATIC MP_DEFINE_CONST_DICT(machine_pwm_locals_dict, machine_pwm_locals_dict_table);
@ -272,6 +280,18 @@ STATIC mp_obj_t machine_hard_pwm_make_new(mp_arg_val_t *args) {
self->pyb->pwm->init.duty = 50; // 50% by default.
}
if (args[ARG_NEW_pulse_width].u_obj != MP_OBJ_NULL) {
self->pyb->pwm->init.pulse_width = mp_obj_get_int(args[ARG_NEW_pulse_width].u_obj);
} else {
self->pyb->pwm->init.pulse_width = 0;
}
if (args[ARG_NEW_mode].u_obj != MP_OBJ_NULL) {
self->pyb->pwm->init.mode = mp_obj_get_int(args[ARG_NEW_mode].u_obj);
} else {
self->pyb->pwm->init.mode = HAL_PWM_MODE_HIGH_LOW;
}
hal_pwm_init(self->pyb->pwm->instance, &self->pyb->pwm->init);
return MP_OBJ_FROM_PTR(self);