Skip to content

Commit 95e91aa

Browse files
IhorNehrutsabskprobert-hh
committed
esp32/MCPWM: Add motor control MCPWM driver.
Signed-off-by: Ihor Nehrutsa <Ihor.Nehrutsa@gmail.com> Co-Authored-By: bskp <bskp@posteo.ch> Co-Authored-By: Robert Hammelrath <12476868+robert-hh@users.noreply.github.com>
1 parent f81a63c commit 95e91aa

File tree

8 files changed

+786
-2
lines changed

8 files changed

+786
-2
lines changed

docs/esp32/quickref.rst

100644100755
Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ These are working configurations for LAN interfaces of popular boards::
141141
# Olimex ESP32-GATEWAY: power controlled by Pin(5)
142142
# Olimex ESP32 PoE and ESP32-PoE ISO: power controlled by Pin(12)
143143

144-
lan = network.LAN(mdc=machine.Pin(23), mdio=machine.Pin(18), power=machine.Pin(5),
144+
lan = network.LAN(mdc=machine.Pin(23), mdio=machine.Pin(18), power=machine.Pin(5),
145145
phy_type=network.PHY_LAN8720, phy_addr=0,
146146
ref_clk=machine.Pin(17), ref_clk_mode=machine.Pin.OUT)
147147

@@ -330,6 +330,35 @@ possible at the same frequency.
330330

331331
See more examples in the :ref:`esp32_pwm` tutorial.
332332

333+
MCPWM (motor control pulse width modulation)
334+
--------------------------------------------
335+
336+
MCPWM can be enabled on all output-enabled pins. The API differs slightly from **machine.PWM** to allow explicit timer selection.
337+
Currently, this implementation only covers basic functionality.
338+
339+
For more details, see Espressif's `MCPWM
340+
<https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/mcpwm.html>`_ documentation.
341+
342+
Use the esp32.MCPWM class::
343+
344+
from machine import Pin
345+
from esp32 import MCPWM
346+
347+
mcpwm = MCPWM(0, (Pin(16), Pin(17)), freq=1000, duty_u16=32768) # create MCPWM object with timer (0..5)
348+
349+
freq = mcpwm.freq() # get current frequency
350+
# mcpwm.freq(500) # set frequency in Hz. Not supported!
351+
352+
duty_u16 = mcpwm.duty_u16() # get current duty cycle, range 0-65535, (now 32768, 50% of duty)
353+
duty_ns = mcpwm.duty_ns() # get current pulse width in ns, (now 500000 ns, 50% of duty)
354+
355+
mcpwm.duty_u16(2**16 * 3 // 4) # set duty cycle from 0 to 65536 as a ratio duty_u16/65536, (now 75% of duty)
356+
mcpwm.duty_ns(250000) # set pulse width in nanoseconds from 0 to 1_000_000_000/freq, (now 25% of duty)
357+
358+
print(mcpwm) # view MCPWM settings
359+
360+
mcpwm.deinit() # turn of MCPWM on pins
361+
333362
ADC (analog to digital conversion)
334363
----------------------------------
335364

ports/esp32/esp32_common.cmake

100644100755
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ list(APPEND MICROPY_SOURCE_PORT
8383
esp32_nvs.c
8484
esp32_partition.c
8585
esp32_rmt.c
86+
esp32_mcpwm.c
8687
esp32_ulp.c
8788
modesp32.c
8889
machine_hw_spi.c

ports/esp32/esp32_mcpwm.c

Lines changed: 748 additions & 0 deletions
Large diffs are not rendered by default.

ports/esp32/modesp32.c

100644100755
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ STATIC const mp_rom_map_elem_t esp32_module_globals_table[] = {
211211
#if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
212212
{ MP_ROM_QSTR(MP_QSTR_ULP), MP_ROM_PTR(&esp32_ulp_type) },
213213
#endif
214+
{ MP_ROM_QSTR(MP_QSTR_MCPWM), MP_ROM_PTR(&esp32_mcpwm_type) },
214215

215216
{ MP_ROM_QSTR(MP_QSTR_WAKEUP_ALL_LOW), MP_ROM_FALSE },
216217
{ MP_ROM_QSTR(MP_QSTR_WAKEUP_ANY_HIGH), MP_ROM_TRUE },

ports/esp32/modesp32.h

100644100755
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ extern const mp_obj_type_t esp32_nvs_type;
6565
extern const mp_obj_type_t esp32_partition_type;
6666
extern const mp_obj_type_t esp32_rmt_type;
6767
extern const mp_obj_type_t esp32_ulp_type;
68+
extern const mp_obj_type_t esp32_mcpwm_type;
6869

6970
esp_err_t rmt_driver_install_core1(uint8_t channel_id);
7071

ports/esp32/mpconfigport.h

100644100755
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@
4747
#define MICROPY_ENABLE_GC (1)
4848
#define MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF (1)
4949
#define MICROPY_LONGINT_IMPL (MICROPY_LONGINT_IMPL_MPZ)
50-
#define MICROPY_ERROR_REPORTING (MICROPY_ERROR_REPORTING_NORMAL)
50+
// #define MICROPY_ERROR_REPORTING (MICROPY_ERROR_REPORTING_NORMAL)
51+
#define MICROPY_ERROR_REPORTING (MICROPY_ERROR_REPORTING_DETAILED)
5152
#define MICROPY_WARNINGS (1)
5253
#define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_FLOAT)
5354
#define MICROPY_STREAMS_POSIX_API (1)

ports/esp32/mphalport.c

100644100755
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848
#include "usb_serial_jtag.h"
4949
#include "uart.h"
5050

51+
#include "py/mpprint.h"
52+
5153
TaskHandle_t mp_main_task_handle;
5254

5355
STATIC uint8_t stdin_ringbuf_array[260];

ports/esp32/mphalport.h

100644100755
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ void mp_hal_wake_main_task_from_isr(void);
9090
#include "driver/gpio.h"
9191
#define MP_HAL_PIN_FMT "%u"
9292
#define mp_hal_pin_obj_t gpio_num_t
93+
// #define pin_find(p) machine_pin_find(p)
9394
mp_hal_pin_obj_t machine_pin_get_id(mp_obj_t pin_in);
9495
#define mp_hal_get_pin_obj(o) machine_pin_get_id(o)
9596
#define mp_hal_pin_name(p) (p)

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