Skip to content

Commit 53591e0

Browse files
committed
esp8266: Add the methods duty_u16() and duty_ns() for consistency.
1 parent fb82d0c commit 53591e0

File tree

2 files changed

+44
-8
lines changed

2 files changed

+44
-8
lines changed

ports/esp8266/machine_pwm.c

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,12 @@ STATIC void mp_machine_pwm_print(const mp_print_t *print, mp_obj_t self_in, mp_p
5353
}
5454

5555
STATIC void mp_machine_pwm_init_helper(machine_pwm_obj_t *self, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
56-
enum { ARG_freq, ARG_duty };
56+
enum { ARG_freq, ARG_duty, ARG_duty_u16, ARG_duty_ns };
5757
static const mp_arg_t allowed_args[] = {
5858
{ MP_QSTR_freq, MP_ARG_INT, {.u_int = -1} },
5959
{ MP_QSTR_duty, MP_ARG_INT, {.u_int = -1} },
60+
{ MP_QSTR_duty_u16, MP_ARG_INT, {.u_int = -1} },
61+
{ MP_QSTR_duty_ns, MP_ARG_INT, {.u_int = -1} },
6062
};
6163
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
6264
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
@@ -74,6 +76,15 @@ STATIC void mp_machine_pwm_init_helper(machine_pwm_obj_t *self, size_t n_args, c
7476
if (args[ARG_duty].u_int != -1) {
7577
pwm_set_duty(args[ARG_duty].u_int, self->channel);
7678
}
79+
if (args[ARG_duty_u16].u_int != -1) {
80+
pwm_set_duty(args[ARG_duty].u_int * 100 / 65536, self->channel);
81+
}
82+
if (args[ARG_duty_u16].u_int != -1) {
83+
uint32_t freq = pwm_get_freq(0);
84+
if (freq > 0) {
85+
pwm_set_duty(args[ARG_duty].u_int * freq / 10000000, self->channel);
86+
}
87+
}
7788

7889
if (pin_mode[self->pin->phys_port] == GPIO_MODE_OPEN_DRAIN) {
7990
mp_hal_pin_open_drain(self->pin->phys_port);
@@ -121,23 +132,47 @@ STATIC void mp_machine_pwm_freq_set(machine_pwm_obj_t *self, mp_int_t freq) {
121132
pwm_start();
122133
}
123134

124-
STATIC mp_obj_t mp_machine_pwm_duty_get(machine_pwm_obj_t *self) {
135+
STATIC void set_active(machine_pwm_obj_t *self, bool set_pin) {
125136
if (!self->active) {
126137
pwm_add(self->pin->phys_port, self->pin->periph, self->pin->func);
127138
self->active = 1;
128-
129-
if (pin_mode[self->pin->phys_port] == GPIO_MODE_OPEN_DRAIN) {
139+
if (set_pin && pin_mode[self->pin->phys_port] == GPIO_MODE_OPEN_DRAIN) {
130140
mp_hal_pin_open_drain(self->pin->phys_port);
131141
}
132142
}
143+
}
144+
145+
STATIC mp_obj_t mp_machine_pwm_duty_get(machine_pwm_obj_t *self) {
146+
set_active(self, true);
133147
return MP_OBJ_NEW_SMALL_INT(pwm_get_duty(self->channel));
134148
}
135149

136150
STATIC void mp_machine_pwm_duty_set(machine_pwm_obj_t *self, mp_int_t duty) {
137-
if (!self->active) {
138-
pwm_add(self->pin->phys_port, self->pin->periph, self->pin->func);
139-
self->active = 1;
140-
}
151+
set_active(self, false);
141152
pwm_set_duty(duty, self->channel);
142153
pwm_start();
143154
}
155+
156+
STATIC mp_obj_t mp_machine_pwm_duty_get_u16(machine_pwm_obj_t *self) {
157+
set_active(self, true);
158+
return MP_OBJ_NEW_SMALL_INT(pwm_get_duty(self->channel) * 65536 / 1024);
159+
}
160+
161+
STATIC void mp_machine_pwm_duty_set_u16(machine_pwm_obj_t *self, mp_int_t duty) {
162+
set_active(self, false);
163+
pwm_set_duty(duty * 1024 / 65536, self->channel);
164+
pwm_start();
165+
}
166+
167+
STATIC mp_obj_t mp_machine_pwm_duty_get_ns(machine_pwm_obj_t *self) {
168+
set_active(self, true);
169+
uint32_t freq = pwm_get_freq(0);
170+
return MP_OBJ_NEW_SMALL_INT(pwm_get_duty(self->channel) * 976563 / freq);
171+
}
172+
173+
STATIC void mp_machine_pwm_duty_set_ns(machine_pwm_obj_t *self, mp_int_t duty) {
174+
set_active(self, false);
175+
uint32_t freq = pwm_get_freq(0);
176+
pwm_set_duty(duty * freq / 976562, self->channel); // 1e9/1024 = 976562.5
177+
pwm_start();
178+
}

ports/esp8266/mpconfigport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
#define MICROPY_PY_MACHINE_PWM (1)
6767
#define MICROPY_PY_MACHINE_PWM_INIT (1)
6868
#define MICROPY_PY_MACHINE_PWM_DUTY (1)
69+
#define MICROPY_PY_MACHINE_PWM_DUTY_U16_NS (1)
6970
#define MICROPY_PY_MACHINE_PWM_INCLUDEFILE "ports/esp8266/machine_pwm.c"
7071
#define MICROPY_PY_MACHINE_I2C (1)
7172
#define MICROPY_PY_MACHINE_SOFTI2C (1)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy