Skip to content
This repository was archived by the owner on Sep 6, 2023. It is now read-only.

Commit e60b3bc

Browse files
Andy Valenciadpgeorge
Andy Valencia
authored andcommitted
esp32: Add PWM support via machine.PWM class.
This patch adds the machine.PWM class. Code lineage: I started by copying the esp8266 machine_pwm.c. I used information from the ESP32 Technical Reference Manual, the esp-idf documentation, and the SDK's sample ledc example code (but I did not copy that code, just studied it to understand the SDK's API for PWM). So aside from the code copied from the esp8266 PWM support, everything else you see is just new code I wrote. I wasn't an employee of anybody when I wrote it, and I wrote it with the understanding and intention that it's simply a derivative work of the existing micropython code. I freely and willingly contribute it to the project and intend that it not change the legal status of the micropython code base in any way, even if it is included in that base in whole or part.
1 parent 51642d7 commit e60b3bc

File tree

4 files changed

+280
-0
lines changed

4 files changed

+280
-0
lines changed

esp32/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ SRC_C = \
123123
machine_touchpad.c \
124124
machine_adc.c \
125125
machine_dac.c \
126+
machine_pwm.c \
126127
modmachine.c \
127128
modnetwork.c \
128129
modsocket.c \

esp32/machine_pwm.c

Lines changed: 277 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,277 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2016 Damien P. George
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
#include <stdio.h>
27+
#include "driver/ledc.h"
28+
#include "esp_err.h"
29+
30+
#include "py/nlr.h"
31+
#include "py/runtime.h"
32+
#include "modmachine.h"
33+
#include "mphalport.h"
34+
35+
// Forward dec'l
36+
extern const mp_obj_type_t machine_pwm_type;
37+
38+
typedef struct _esp32_pwm_obj_t {
39+
mp_obj_base_t base;
40+
gpio_num_t pin;
41+
uint8_t active;
42+
uint8_t channel;
43+
} esp32_pwm_obj_t;
44+
45+
// Which channel has which GPIO pin assigned?
46+
// (-1 if not assigned)
47+
STATIC int chan_gpio[LEDC_CHANNEL_MAX];
48+
49+
// Params for PW operation
50+
// 5khz
51+
#define PWFREQ (5000)
52+
// High speed mode
53+
#define PWMODE (LEDC_HIGH_SPEED_MODE)
54+
// 10-bit resolution (compatible with esp8266 PWM)
55+
#define PWRES (LEDC_TIMER_10_BIT)
56+
// Timer 1
57+
#define PWTIMER (LEDC_TIMER_1)
58+
59+
// Config of timer upon which we run all PWM'ed GPIO pins
60+
STATIC bool pwm_inited = false;
61+
STATIC ledc_timer_config_t timer_cfg = {
62+
.bit_num = PWRES,
63+
.freq_hz = PWFREQ,
64+
.speed_mode = PWMODE,
65+
.timer_num = PWTIMER
66+
};
67+
68+
STATIC void pwm_init(void) {
69+
70+
// Initial condition: no channels assigned
71+
for (int x = 0; x < LEDC_CHANNEL_MAX; ++x) {
72+
chan_gpio[x] = -1;
73+
}
74+
75+
// Init with default timer params
76+
ledc_timer_config(&timer_cfg);
77+
}
78+
79+
STATIC int set_freq(int newval) {
80+
int oval = timer_cfg.freq_hz;
81+
82+
timer_cfg.freq_hz = newval;
83+
if (ledc_timer_config(&timer_cfg) != ESP_OK) {
84+
timer_cfg.freq_hz = oval;
85+
return 0;
86+
}
87+
return 1;
88+
}
89+
90+
/******************************************************************************/
91+
92+
// MicroPython bindings for PWM
93+
94+
STATIC void esp32_pwm_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
95+
esp32_pwm_obj_t *self = MP_OBJ_TO_PTR(self_in);
96+
mp_printf(print, "PWM(%u", self->pin);
97+
if (self->active) {
98+
mp_printf(print, ", freq=%u, duty=%u", timer_cfg.freq_hz,
99+
ledc_get_duty(PWMODE, self->channel));
100+
}
101+
mp_printf(print, ")");
102+
}
103+
104+
STATIC void esp32_pwm_init_helper(esp32_pwm_obj_t *self,
105+
size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
106+
enum { ARG_freq, ARG_duty };
107+
static const mp_arg_t allowed_args[] = {
108+
{ MP_QSTR_freq, MP_ARG_INT, {.u_int = -1} },
109+
{ MP_QSTR_duty, MP_ARG_INT, {.u_int = -1} },
110+
};
111+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
112+
mp_arg_parse_all(n_args, pos_args, kw_args,
113+
MP_ARRAY_SIZE(allowed_args), allowed_args, args);
114+
115+
int channel;
116+
int avail = -1;
117+
118+
// Find a free PWM channel, also spot if our pin is
119+
// already mentioned.
120+
for (channel = 0; channel < LEDC_CHANNEL_MAX; ++channel) {
121+
if (chan_gpio[channel] == self->pin) {
122+
break;
123+
}
124+
if ((avail == -1) && (chan_gpio[channel] == -1)) {
125+
avail = channel;
126+
}
127+
}
128+
if (channel >= LEDC_CHANNEL_MAX) {
129+
if (avail == -1) {
130+
mp_raise_ValueError("out of PWM channels");
131+
}
132+
channel = avail;
133+
}
134+
135+
// New PWM assignment
136+
self->active = 1;
137+
if (chan_gpio[channel] == -1) {
138+
ledc_channel_config_t cfg = {
139+
.channel = channel,
140+
.duty = (1 << PWRES) / 2,
141+
.gpio_num = self->pin,
142+
.intr_type = LEDC_INTR_DISABLE,
143+
.speed_mode = PWMODE,
144+
.timer_sel = PWTIMER,
145+
};
146+
if (ledc_channel_config(&cfg) != ESP_OK) {
147+
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
148+
"PWM not supported on pin %d", self->pin));
149+
}
150+
chan_gpio[channel] = self->pin;
151+
self->channel = channel;
152+
}
153+
154+
// Maybe change PWM timer
155+
int tval = args[ARG_freq].u_int;
156+
if (tval != -1) {
157+
if (tval != timer_cfg.freq_hz) {
158+
if (!set_freq(tval)) {
159+
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
160+
"Bad frequency %d", tval));
161+
}
162+
}
163+
}
164+
165+
// Set duty cycle?
166+
int dval = args[ARG_duty].u_int;
167+
if (dval != -1) {
168+
dval &= ((1 << PWRES)-1);
169+
ledc_set_duty(PWMODE, channel, dval);
170+
ledc_update_duty(PWMODE, channel);
171+
}
172+
}
173+
174+
STATIC mp_obj_t esp32_pwm_make_new(const mp_obj_type_t *type,
175+
size_t n_args, size_t n_kw, const mp_obj_t *args) {
176+
mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true);
177+
gpio_num_t pin_id = machine_pin_get_id(args[0]);
178+
179+
// create PWM object from the given pin
180+
esp32_pwm_obj_t *self = m_new_obj(esp32_pwm_obj_t);
181+
self->base.type = &machine_pwm_type;
182+
self->pin = pin_id;
183+
self->active = 0;
184+
self->channel = -1;
185+
186+
// start the PWM subsystem if it's not already running
187+
if (!pwm_inited) {
188+
pwm_init();
189+
pwm_inited = true;
190+
}
191+
192+
// start the PWM running for this channel
193+
mp_map_t kw_args;
194+
mp_map_init_fixed_table(&kw_args, n_kw, args + n_args);
195+
esp32_pwm_init_helper(self, n_args - 1, args + 1, &kw_args);
196+
197+
return MP_OBJ_FROM_PTR(self);
198+
}
199+
200+
STATIC mp_obj_t esp32_pwm_init(size_t n_args,
201+
const mp_obj_t *args, mp_map_t *kw_args) {
202+
esp32_pwm_init_helper(args[0], n_args - 1, args + 1, kw_args);
203+
return mp_const_none;
204+
}
205+
MP_DEFINE_CONST_FUN_OBJ_KW(esp32_pwm_init_obj, 1, esp32_pwm_init);
206+
207+
STATIC mp_obj_t esp32_pwm_deinit(mp_obj_t self_in) {
208+
esp32_pwm_obj_t *self = MP_OBJ_TO_PTR(self_in);
209+
int chan = self->channel;
210+
211+
// Valid channel?
212+
if ((chan >= 0) && (chan < LEDC_CHANNEL_MAX)) {
213+
// Mark it unused, and tell the hardware to stop routing
214+
chan_gpio[chan] = -1;
215+
ledc_stop(PWMODE, chan, 0);
216+
self->active = 0;
217+
self->channel = -1;
218+
}
219+
return mp_const_none;
220+
}
221+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(esp32_pwm_deinit_obj, esp32_pwm_deinit);
222+
223+
STATIC mp_obj_t esp32_pwm_freq(size_t n_args, const mp_obj_t *args) {
224+
if (n_args == 1) {
225+
// get
226+
return MP_OBJ_NEW_SMALL_INT(timer_cfg.freq_hz);
227+
}
228+
229+
// set
230+
int tval = mp_obj_get_int(args[1]);
231+
if (!set_freq(tval)) {
232+
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
233+
"Bad frequency %d", tval));
234+
}
235+
return mp_const_none;
236+
}
237+
238+
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(esp32_pwm_freq_obj, 1, 2, esp32_pwm_freq);
239+
240+
STATIC mp_obj_t esp32_pwm_duty(size_t n_args, const mp_obj_t *args) {
241+
esp32_pwm_obj_t *self = MP_OBJ_TO_PTR(args[0]);
242+
int duty;
243+
244+
if (n_args == 1) {
245+
// get
246+
duty = ledc_get_duty(PWMODE, self->channel);
247+
return MP_OBJ_NEW_SMALL_INT(duty);
248+
}
249+
250+
// set
251+
duty = mp_obj_get_int(args[1]);
252+
duty &= ((1 << PWRES)-1);
253+
ledc_set_duty(PWMODE, self->channel, duty);
254+
ledc_update_duty(PWMODE, self->channel);
255+
256+
return mp_const_none;
257+
}
258+
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(esp32_pwm_duty_obj,
259+
1, 2, esp32_pwm_duty);
260+
261+
STATIC const mp_rom_map_elem_t esp32_pwm_locals_dict_table[] = {
262+
{ MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&esp32_pwm_init_obj) },
263+
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&esp32_pwm_deinit_obj) },
264+
{ MP_ROM_QSTR(MP_QSTR_freq), MP_ROM_PTR(&esp32_pwm_freq_obj) },
265+
{ MP_ROM_QSTR(MP_QSTR_duty), MP_ROM_PTR(&esp32_pwm_duty_obj) },
266+
};
267+
268+
STATIC MP_DEFINE_CONST_DICT(esp32_pwm_locals_dict,
269+
esp32_pwm_locals_dict_table);
270+
271+
const mp_obj_type_t machine_pwm_type = {
272+
{ &mp_type_type },
273+
.name = MP_QSTR_PWM,
274+
.print = esp32_pwm_print,
275+
.make_new = esp32_pwm_make_new,
276+
.locals_dict = (mp_obj_dict_t*)&esp32_pwm_locals_dict,
277+
};

esp32/modmachine.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = {
118118
{ MP_ROM_QSTR(MP_QSTR_ADC), MP_ROM_PTR(&machine_adc_type) },
119119
{ MP_ROM_QSTR(MP_QSTR_DAC), MP_ROM_PTR(&machine_dac_type) },
120120
{ MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&machine_i2c_type) },
121+
{ MP_ROM_QSTR(MP_QSTR_PWM), MP_ROM_PTR(&machine_pwm_type) },
121122
{ MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&mp_machine_soft_spi_type) },
122123
};
123124

esp32/modmachine.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ extern const mp_obj_type_t machine_pin_type;
77
extern const mp_obj_type_t machine_touchpad_type;
88
extern const mp_obj_type_t machine_adc_type;
99
extern const mp_obj_type_t machine_dac_type;
10+
extern const mp_obj_type_t machine_pwm_type;
1011
extern const mp_obj_type_t machine_hw_spi_type;
1112

1213
void machine_pins_init(void);

0 commit comments

Comments
 (0)