8000 esp8266/machine_pwm: Enable real open drain output on pin driven by PWM. · pybricks/micropython@96a2cc5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 96a2cc5

Browse files
osresearchdpgeorge
authored andcommitted
esp8266/machine_pwm: Enable real open drain output on pin driven by PWM.
The PWM module now detects if the pin is open drain and if so switches it to hardware open drain before starting the PWM. The code that was explicitly turning off the open drain output during PWM is also removed. Together these changes allow driving external transistor high-current switches with PWM. Signed-off-by: Trammell hudson <hudson@trmm.net>
1 parent 5228f40 commit 96a2cc5

File tree

4 files changed

+20
-10
lines changed

4 files changed

+20
-10
lines changed

ports/esp8266/esppwm.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,6 @@ pwm_add(uint8_t pin_id, uint32_t pin_mux, uint32_t pin_func) {
389389
pwm.duty[i] = 0;
390390
pwm_gpio |= (1 << pin_num[channel]);
391391
PIN_FUNC_SELECT(pin_mux, pin_func);
392-
GPIO_REG_WRITE(GPIO_PIN_ADDR(GPIO_ID_PIN(pin_num[channel])), GPIO_REG_READ(GPIO_PIN_ADDR(GPIO_ID_PIN(pin_num[channel]))) & (~GPIO_PIN_PAD_DRIVER_SET(GPIO_PAD_DRIVER_ENABLE))); // disable open drain;
393392
pwm_channel_num++;
394393
UNLOCK_PWM(critical); // leave critical
395394
return channel;

ports/esp8266/machine_pin.c

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,6 @@
4646
(GPIO_REG_READ(GPIO_PIN_ADDR(phys_port)) & ~GPIO_PIN_INT_TYPE_MASK) \
4747
| GPIO_PIN_INT_TYPE_SET(trig))) \
4848

49-
#define GPIO_MODE_INPUT (0)
50-
#define GPIO_MODE_OUTPUT (1)
51-
#define GPIO_MODE_OPEN_DRAIN (2) // synthesised
52-
#define GPIO_PULL_NONE (0)
53-
#define GPIO_PULL_UP (1)
54-
// Removed in SDK 1.1.0
55-
// #define GPIO_PULL_DOWN (2)
56-
5749
typedef struct _pin_irq_obj_t {
5850
mp_obj_base_t base;
5951
uint16_t phys_port;
@@ -84,7 +76,7 @@ const pyb_pin_obj_t pyb_pin_obj[16 + 1] = {
8476
{{&pyb_pin_type}, 16, -1, -1},
8577
};
8678

87-
STATIC uint8_t pin_mode[16 + 1];
79+
uint8_t pin_mode[16 + 1];
8880

8981
// forward declaration
9082
STATIC const pin_irq_obj_t pin_irq_obj[16];

ports/esp8266/machine_pwm.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
*/
2626

2727
#include "py/runtime.h"
28+
#include "py/mphal.h"
2829
#include "modmachine.h"
2930

3031
#include "esppwm.h"
@@ -74,6 +75,10 @@ STATIC void mp_machine_pwm_init_helper(machine_pwm_obj_t *self, size_t n_args, c
7475
pwm_set_duty(args[ARG_duty].u_int, self->channel);
7576
}
7677

78+
if (pin_mode[self->pin->phys_port] == GPIO_MODE_OPEN_DRAIN) {
79+
mp_hal_pin_open_drain(self->pin->phys_port);
80+
}
81+
7782
pwm_start();
7883
}
7984

@@ -120,6 +125,10 @@ STATIC mp_obj_t mp_machine_pwm_duty_get(machine_pwm_obj_t *self) {
120125
if (!self->active) {
121126
pwm_add(self->pin->phys_port, self->pin->periph, self->pin->func);
122127
self->active = 1;
128+
129+
if (pin_mode[self->pin->phys_port] == GPIO_MODE_OPEN_DRAIN) {
130+
mp_hal_pin_open_drain(self->pin->phys_port);
131+
}
123132
}
124133
return MP_OBJ_NEW_SMALL_INT(pwm_get_duty(self->channel));
125134
}

ports/esp8266/modmachine.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ typedef struct _pyb_pin_obj_t {
2121

2222
const pyb_pin_obj_t pyb_pin_obj[16 + 1];
2323

24+
#define GPIO_MODE_INPUT (0)
25+
#define GPIO_MODE_OUTPUT (1)
26+
#define GPIO_MODE_OPEN_DRAIN (2) // synthesised
27+
#define GPIO_PULL_NONE (0)
28+
#define GPIO_PULL_UP (1)
29+
// Removed in SDK 1.1.0
30+
// #define GPIO_PULL_DOWN (2)
31+
32+
extern uint8_t pin_mode[16 + 1];
33+
2434
void pin_init0(void);
2535

2636
uint mp_obj_get_pin(mp_obj_t pin_in);

0 commit comments

Comments
 (0)
0