Skip to content

Commit

Permalink
esp32/MCPWM: Add motor control MCPWM driver.
Browse files Browse the repository at this point in the history
esp32/MCPWM: Add complementary PWMs.

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>
  • Loading branch information
3 people committed Jun 5, 2024
1 parent 4dacfe9 commit 3e92461
Show file tree
Hide file tree
Showing 8 changed files with 945 additions and 2 deletions.
31 changes: 30 additions & 1 deletion docs/esp32/quickref.rst
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ These are working configurations for LAN interfaces of popular boards::
# Olimex ESP32-GATEWAY: power controlled by Pin(5)
# Olimex ESP32 PoE and ESP32-PoE ISO: power controlled by Pin(12)

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

Expand Down Expand Up @@ -330,6 +330,35 @@ possible at the same frequency.

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

MCPWM (motor control pulse width modulation)
--------------------------------------------

MCPWM can be enabled on all output-enabled pins. The API differs slightly from **machine.PWM** to allow explicit timer selection.
Currently, this implementation only covers basic functionality.

For more details, see Espressif's `MCPWM
<https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/mcpwm.html>`_ documentation.

Use the esp32.MCPWM class::

from machine import Pin
from esp32 import MCPWM

mcpwm = MCPWM(0, (Pin(16), Pin(17)), freq=1000, duty_u16=32768) # create MCPWM object with timer (0..5)

freq = mcpwm.freq() # get current frequency
# mcpwm.freq(500) # set frequency in Hz. Not supported!

duty_u16 = mcpwm.duty_u16() # get current duty cycle, range 0-65535, (now 32768, 50% of duty)
duty_ns = mcpwm.duty_ns() # get current pulse width in ns, (now 500000 ns, 50% of duty)

mcpwm.duty_u16(2**16 * 3 // 4) # set duty cycle from 0 to 65536 as a ratio duty_u16/65536, (now 75% of duty)
mcpwm.duty_ns(250000) # set pulse width in nanoseconds from 0 to 1_000_000_000/freq, (now 25% of duty)

print(mcpwm) # view MCPWM settings

mcpwm.deinit() # turn of MCPWM on pins

ADC (analog to digital conversion)
----------------------------------

Expand Down
1 change: 1 addition & 0 deletions ports/esp32/esp32_common.cmake
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ list(APPEND MICROPY_SOURCE_PORT
esp32_nvs.c
esp32_partition.c
esp32_rmt.c
esp32_mcpwm.c
esp32_ulp.c
modesp32.c
machine_hw_spi.c
Expand Down
905 changes: 905 additions & 0 deletions ports/esp32/esp32_mcpwm.c

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions ports/esp32/modesp32.c
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ STATIC const mp_rom_map_elem_t esp32_module_globals_table[] = {
#if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
{ MP_ROM_QSTR(MP_QSTR_ULP), MP_ROM_PTR(&esp32_ulp_type) },
#endif
{ MP_ROM_QSTR(MP_QSTR_MCPWM), MP_ROM_PTR(&esp32_mcpwm_type) },

{ MP_ROM_QSTR(MP_QSTR_WAKEUP_ALL_LOW), MP_ROM_FALSE },
{ MP_ROM_QSTR(MP_QSTR_WAKEUP_ANY_HIGH), MP_ROM_TRUE },
Expand Down
1 change: 1 addition & 0 deletions ports/esp32/modesp32.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ extern const mp_obj_type_t esp32_nvs_type;
extern const mp_obj_type_t esp32_partition_type;
extern const mp_obj_type_t esp32_rmt_type;
extern const mp_obj_type_t esp32_ulp_type;
extern const mp_obj_type_t esp32_mcpwm_type;

esp_err_t rmt_driver_install_core1(uint8_t channel_id);

Expand Down
5 changes: 4 additions & 1 deletion ports/esp32/mpconfigport.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,15 @@
#define MICROPY_ENABLE_GC (1)
#define MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF (1)
#define MICROPY_LONGINT_IMPL (MICROPY_LONGINT_IMPL_MPZ)
#define MICROPY_ERROR_REPORTING (MICROPY_ERROR_REPORTING_NORMAL)
// #define MICROPY_ERROR_REPORTING (MICROPY_ERROR_REPORTING_NORMAL)
#define MICROPY_ERROR_REPORTING (MICROPY_ERROR_REPORTING_DETAILED)
#define MICROPY_WARNINGS (1)
#define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_FLOAT)
#define MICROPY_STREAMS_POSIX_API (1)
#define MICROPY_USE_INTERNAL_ERRNO (0) // errno.h from xtensa-esp32-elf/sys-include/sys
#define MICROPY_USE_INTERNAL_PRINTF (0) // ESP32 SDK requires its own printf
#define MICROPY_DEBUG_PRINTERS (1)
#define MICROPY_DEBUG_VERBOSE (0)
#define MICROPY_SCHEDULER_DEPTH (8)
#define MICROPY_VFS (1)

Expand Down
2 changes: 2 additions & 0 deletions ports/esp32/mphalport.c
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
#include "usb_serial_jtag.h"
#include "uart.h"

#include "py/mpprint.h"

TaskHandle_t mp_main_task_handle;

STATIC uint8_t stdin_ringbuf_array[260];
Expand Down
1 change: 1 addition & 0 deletions ports/esp32/mphalport.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ void mp_hal_wake_main_task_from_isr(void);
#include "driver/gpio.h"
#define MP_HAL_PIN_FMT "%u"
#define mp_hal_pin_obj_t gpio_num_t
// #define pin_find(p) machine_pin_find(p)
mp_hal_pin_obj_t machine_pin_get_id(mp_obj_t pin_in);
#define mp_hal_get_pin_obj(o) machine_pin_get_id(o)
#define mp_hal_pin_name(p) (p)
Expand Down

0 comments on commit 3e92461

Please sign in to comment.