summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrobert-hh <robert@hammelrath.com>2025-01-19 10:31:56 +0100
committerDamien George <damien@micropython.org>2025-02-28 16:50:39 +1100
commite009ab06c581cf2e8b6dbf8d2002105c97131a1f (patch)
treeb5a817e58fc89b79f7480e30cdef00ca3558b3e8
parent61cb293b761d22de9ece8d7cfdb38555fc0a410a (diff)
esp8266/machine_pin: Implement Pin.toggle() method.
Tested with a generic ESP8266 device. The actual output value is taken from the output register, not by reading the pad level. Signed-off-by: robert-hh <robert@hammelrath.com>
-rw-r--r--ports/esp8266/esp_mphal.h7
-rw-r--r--ports/esp8266/machine_pin.c9
2 files changed, 16 insertions, 0 deletions
diff --git a/ports/esp8266/esp_mphal.h b/ports/esp8266/esp_mphal.h
index e677fa450..0a4f92ac0 100644
--- a/ports/esp8266/esp_mphal.h
+++ b/ports/esp8266/esp_mphal.h
@@ -100,6 +100,13 @@ void mp_hal_pin_open_drain(mp_hal_pin_obj_t pin);
else { gpio_output_set(1 << (p), 0, 1 << (p), 0); } \
} while (0)
#define mp_hal_pin_read(p) pin_get(p)
+static inline int mp_hal_pin_read_output(mp_hal_pin_obj_t pin) {
+ if (pin >= 16) {
+ return READ_PERI_REG(RTC_GPIO_OUT) & 1;
+ } else {
+ return (GPIO_REG_READ(GPIO_OUT_ADDRESS) >> pin) & 1;
+ }
+}
#define mp_hal_pin_write(p, v) pin_set((p), (v))
void *ets_get_esf_buf_ctlblk(void);
diff --git a/ports/esp8266/machine_pin.c b/ports/esp8266/machine_pin.c
index ec22c7c58..658966679 100644
--- a/ports/esp8266/machine_pin.c
+++ b/ports/esp8266/machine_pin.c
@@ -373,6 +373,14 @@ static mp_obj_t pyb_pin_on(mp_obj_t self_in) {
}
static MP_DEFINE_CONST_FUN_OBJ_1(pyb_pin_on_obj, pyb_pin_on);
+// pin.toggle()
+static mp_obj_t machine_pin_toggle(mp_obj_t self_in) {
+ pyb_pin_obj_t *self = self_in;
+ pin_set(self->phys_port, 1 - mp_hal_pin_read_output(self->phys_port));
+ 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, hard=False)
static mp_obj_t pyb_pin_irq(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum { ARG_handler, ARG_trigger, ARG_hard };
@@ -435,6 +443,7 @@ static const mp_rom_map_elem_t pyb_pin_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_value), MP_ROM_PTR(&pyb_pin_value_obj) },
{ MP_ROM_QSTR(MP_QSTR_off), MP_ROM_PTR(&pyb_pin_off_obj) },
{ MP_ROM_QSTR(MP_QSTR_on), MP_ROM_PTR(&pyb_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(&pyb_pin_irq_obj) },
// class constants