mirror of
https://github.com/micropython/micropython.git
synced 2025-07-21 21:11:12 +02:00
esp32/machine_pin: Implement Pin.toggle() method.
The actual output pin value is taken from the OUT register, not from the pad. Tested with: - ESP32 low and high Pin numbers - ESP32C3 low Pin numbers - ESP32C6 low Pin numbers - ESP32S2 low and high Pin numbers - ESP32S3 low and high Pin numbers Signed-off-by: robert-hh <robert@hammelrath.com>
This commit is contained in:
@@ -287,6 +287,15 @@ static mp_obj_t machine_pin_on(mp_obj_t self_in) {
|
||||
}
|
||||
static MP_DEFINE_CONST_FUN_OBJ_1(machine_pin_on_obj, machine_pin_on);
|
||||
|
||||
// pin.toggle()
|
||||
static mp_obj_t machine_pin_toggle(mp_obj_t self_in) {
|
||||
machine_pin_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
||||
gpio_num_t index = PIN_OBJ_PTR_INDEX(self);
|
||||
gpio_set_level(index, 1 - mp_hal_pin_read_output(index));
|
||||
return mp_const_none;
|
||||
}
|
||||
static MP_DEFINE_CONST_FUN_OBJ_1(machine_pin_toggle_obj, machine_pin_toggle);
|
||||
|
||||
// pin.irq(handler=None, trigger=IRQ_FALLING|IRQ_RISING)
|
||||
static mp_obj_t machine_pin_irq(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
||||
enum { ARG_handler, ARG_trigger, ARG_wake };
|
||||
@@ -366,6 +375,7 @@ static const mp_rom_map_elem_t machine_pin_locals_dict_table[] = {
|
||||
{ MP_ROM_QSTR(MP_QSTR_value), MP_ROM_PTR(&machine_pin_value_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_off), MP_ROM_PTR(&machine_pin_off_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_on), MP_ROM_PTR(&machine_pin_on_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_toggle), MP_ROM_PTR(&machine_pin_toggle_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_irq), MP_ROM_PTR(&machine_pin_irq_obj) },
|
||||
|
||||
// class attributes
|
||||
|
@@ -36,6 +36,7 @@
|
||||
#include "freertos/task.h"
|
||||
|
||||
#include "driver/spi_master.h"
|
||||
#include "soc/gpio_reg.h"
|
||||
|
||||
#define MICROPY_PLATFORM_VERSION "IDF" IDF_VER
|
||||
|
||||
@@ -133,6 +134,15 @@ static inline void mp_hal_pin_od_high(mp_hal_pin_obj_t pin) {
|
||||
static inline int mp_hal_pin_read(mp_hal_pin_obj_t pin) {
|
||||
return gpio_get_level(pin);
|
||||
}
|
||||
static inline int mp_hal_pin_read_output(mp_hal_pin_obj_t pin) {
|
||||
#if defined(GPIO_OUT1_REG)
|
||||
return pin < 32
|
||||
? (*(uint32_t *)GPIO_OUT_REG >> pin) & 1
|
||||
: (*(uint32_t *)GPIO_OUT1_REG >> (pin - 32)) & 1;
|
||||
#else
|
||||
return (*(uint32_t *)GPIO_OUT_REG >> pin) & 1;
|
||||
#endif
|
||||
}
|
||||
static inline void mp_hal_pin_write(mp_hal_pin_obj_t pin, int v) {
|
||||
gpio_set_level(pin, v);
|
||||
}
|
||||
|
Reference in New Issue
Block a user