Skip to content

Commit a2d5d84

Browse files
committed
esp8266: Convert mp_hal_pin_obj_t from pin ptr to simple integer.
Most pin I/O can be done just knowing the pin number as a simple integer, and it's more efficient this way (code size, speed) because it doesn't require a memory lookup to get the pin id from the pin object. If the full pin object is needed then it can be easily looked up in the pin table.
1 parent 624738c commit a2d5d84

4 files changed

Lines changed: 21 additions & 16 deletions

File tree

esp8266/esp_mphal.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,3 +212,15 @@ void dupterm_task_init() {
212212
void mp_hal_signal_dupterm_input(void) {
213213
system_os_post(DUPTERM_TASK_ID, 0, 0);
214214
}
215+
216+
void mp_hal_pin_config_od(mp_hal_pin_obj_t pin_id) {
217+
const pyb_pin_obj_t *pin = &pyb_pin_obj[pin_id];
218+
ETS_GPIO_INTR_DISABLE();
219+
PIN_FUNC_SELECT(pin->periph, pin->func);
220+
GPIO_REG_WRITE(GPIO_PIN_ADDR(GPIO_ID_PIN(pin->phys_port)),
221+
GPIO_REG_READ(GPIO_PIN_ADDR(GPIO_ID_PIN(pin->phys_port)))
222+
| GPIO_PIN_PAD_DRIVER_SET(GPIO_PAD_DRIVER_ENABLE)); // open drain
223+
GPIO_REG_WRITE(GPIO_ENABLE_ADDRESS,
224+
GPIO_REG_READ(GPIO_ENABLE_ADDRESS) | (1 << pin->phys_port));
225+
ETS_GPIO_INTR_ENABLE();
226+
}

esp8266/esp_mphal.h

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -63,20 +63,11 @@ void ets_event_poll(void);
6363
#include "etshal.h"
6464
#include "gpio.h"
6565
#include "esp8266/modpyb.h"
66-
#define mp_hal_pin_obj_t pyb_pin_obj_t*
67-
#define mp_hal_get_pin_obj(o) mp_obj_get_pin_obj(o)
68-
#define mp_hal_pin_config_od(p) do { \
69-
ETS_GPIO_INTR_DISABLE(); \
70-
PIN_FUNC_SELECT((p)->periph, (p)->func); \
71-
GPIO_REG_WRITE(GPIO_PIN_ADDR(GPIO_ID_PIN((p)->phys_port)), \
72-
GPIO_REG_READ(GPIO_PIN_ADDR(GPIO_ID_PIN((p)->phys_port))) \
73-
| GPIO_PIN_PAD_DRIVER_SET(GPIO_PAD_DRIVER_ENABLE)); /* open drain */ \
74-
GPIO_REG_WRITE(GPIO_ENABLE_ADDRESS, \
75-
GPIO_REG_READ(GPIO_ENABLE_ADDRESS) | (1 << (p)->phys_port)); \
76-
ETS_GPIO_INTR_ENABLE(); \
77-
} while (0)
78-
#define mp_hal_pin_low(p) gpio_output_set(0, 1 << (p)->phys_port, 1 << (p)->phys_port, 0)
79-
#define mp_hal_pin_od_high(p) gpio_output_set(1 << (p)->phys_port, 0, 1 << (p)->phys_port, 0)
80-
#define mp_hal_pin_read(p) GPIO_INPUT_GET(GPIO_ID_PIN((p)->phys_port))
66+
#define mp_hal_pin_obj_t uint32_t
67+
#define mp_hal_get_pin_obj(o) mp_obj_get_pin(o)
68+
void mp_hal_pin_config_od(mp_hal_pin_obj_t pin);
69+
#define mp_hal_pin_low(p) gpio_output_set(0, 1 << (p), 1 << (p), 0)
70+
#define mp_hal_pin_od_high(p) gpio_output_set(1 << (p), 0, 1 << (p), 0)
71+
#define mp_hal_pin_read(p) GPIO_INPUT_GET(GPIO_ID_PIN((p)))
8172

8273
#endif // _INCLUDED_MPHAL_H_

esp8266/modpyb.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ typedef struct _pyb_pin_obj_t {
1818
uint32_t periph;
1919
} pyb_pin_obj_t;
2020

21+
const pyb_pin_obj_t pyb_pin_obj[16 + 1];
22+
2123
void pin_init0(void);
2224
void pin_intr_handler_iram(void *arg);
2325
void pin_intr_handler(uint32_t);

esp8266/modpybpin.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ typedef struct _pin_irq_obj_t {
5959
uint16_t phys_port;
6060
} pin_irq_obj_t;
6161

62-
STATIC const pyb_pin_obj_t pyb_pin_obj[16 + 1] = {
62+
const pyb_pin_obj_t pyb_pin_obj[16 + 1] = {
6363
{{&pyb_pin_type}, 0, FUNC_GPIO0, PERIPHS_IO_MUX_GPIO0_U},
6464
{{&pyb_pin_type}, 1, FUNC_GPIO1, PERIPHS_IO_MUX_U0TXD_U},
6565
{{&pyb_pin_type}, 2, FUNC_GPIO2, PERIPHS_IO_MUX_GPIO2_U},

0 commit comments

Comments
 (0)