Skip to content

Commit 61cb293

Browse files
robert-hhdpgeorge
authored andcommitted
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>
1 parent cbd21b3 commit 61cb293

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

ports/esp32/machine_pin.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,15 @@ static mp_obj_t machine_pin_on(mp_obj_t self_in) {
287287
}
288288
static MP_DEFINE_CONST_FUN_OBJ_1(machine_pin_on_obj, machine_pin_on);
289289

290+
// pin.toggle()
291+
static mp_obj_t machine_pin_toggle(mp_obj_t self_in) {
292+
machine_pin_obj_t *self = MP_OBJ_TO_PTR(self_in);
293+
gpio_num_t index = PIN_OBJ_PTR_INDEX(self);
294+
gpio_set_level(index, 1 - mp_hal_pin_read_output(index));
295+
return mp_const_none;
296+
}
297+
static MP_DEFINE_CONST_FUN_OBJ_1(machine_pin_toggle_obj, machine_pin_toggle);
298+
290299
// pin.irq(handler=None, trigger=IRQ_FALLING|IRQ_RISING)
291300
static mp_obj_t machine_pin_irq(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
292301
enum { ARG_handler, ARG_trigger, ARG_wake };
@@ -366,6 +375,7 @@ static const mp_rom_map_elem_t machine_pin_locals_dict_table[] = {
366375
{ MP_ROM_QSTR(MP_QSTR_value), MP_ROM_PTR(&machine_pin_value_obj) },
367376
{ MP_ROM_QSTR(MP_QSTR_off), MP_ROM_PTR(&machine_pin_off_obj) },
368377
{ MP_ROM_QSTR(MP_QSTR_on), MP_ROM_PTR(&machine_pin_on_obj) },
378+
{ MP_ROM_QSTR(MP_QSTR_toggle), MP_ROM_PTR(&machine_pin_toggle_obj) },
369379
{ MP_ROM_QSTR(MP_QSTR_irq), MP_ROM_PTR(&machine_pin_irq_obj) },
370380

371381
// class attributes

ports/esp32/mphalport.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include "freertos/task.h"
3737

3838
#include "driver/spi_master.h"
39+
#include "soc/gpio_reg.h"
3940

4041
#define MICROPY_PLATFORM_VERSION "IDF" IDF_VER
4142

@@ -133,6 +134,15 @@ static inline void mp_hal_pin_od_high(mp_hal_pin_obj_t pin) {
133134
static inline int mp_hal_pin_read(mp_hal_pin_obj_t pin) {
134135
return gpio_get_level(pin);
135136
}
137+
static inline int mp_hal_pin_read_output(mp_hal_pin_obj_t pin) {
138+
#if defined(GPIO_OUT1_REG)
139+
return pin < 32
140+
? (*(uint32_t *)GPIO_OUT_REG >> pin) & 1
141+
: (*(uint32_t *)GPIO_OUT1_REG >> (pin - 32)) & 1;
142+
#else
143+
return (*(uint32_t *)GPIO_OUT_REG >> pin) & 1;
144+
#endif
145+
}
136146
static inline void mp_hal_pin_write(mp_hal_pin_obj_t pin, int v) {
137147
gpio_set_level(pin, v);
138148
}

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