summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTrammell hudson <hudson@trmm.net>2020-02-25 22:53:46 +0100
committerDamien George <damien@micropython.org>2022-11-15 12:51:32 +1100
commit96a2cc5e13113338c3e4b12a145a852819b623cf (patch)
tree67410dbf397910024ec50a141887e93e9cbbbb3c
parent5228f4067da028f0663e1ec2ceda139e07865b9d (diff)
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>
-rw-r--r--ports/esp8266/esppwm.c1
-rw-r--r--ports/esp8266/machine_pin.c10
-rw-r--r--ports/esp8266/machine_pwm.c9
-rw-r--r--ports/esp8266/modmachine.h10
4 files changed, 20 insertions, 10 deletions
diff --git a/ports/esp8266/esppwm.c b/ports/esp8266/esppwm.c
index 6164e2fc8..3b705fb94 100644
--- a/ports/esp8266/esppwm.c
+++ b/ports/esp8266/esppwm.c
@@ -389,7 +389,6 @@ pwm_add(uint8_t pin_id, uint32_t pin_mux, uint32_t pin_func) {
pwm.duty[i] = 0;
pwm_gpio |= (1 << pin_num[channel]);
PIN_FUNC_SELECT(pin_mux, pin_func);
- 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;
pwm_channel_num++;
UNLOCK_PWM(critical); // leave critical
return channel;
diff --git a/ports/esp8266/machine_pin.c b/ports/esp8266/machine_pin.c
index ea17728e2..f5e17c7e1 100644
--- a/ports/esp8266/machine_pin.c
+++ b/ports/esp8266/machine_pin.c
@@ -46,14 +46,6 @@
(GPIO_REG_READ(GPIO_PIN_ADDR(phys_port)) & ~GPIO_PIN_INT_TYPE_MASK) \
| GPIO_PIN_INT_TYPE_SET(trig))) \
-#define GPIO_MODE_INPUT (0)
-#define GPIO_MODE_OUTPUT (1)
-#define GPIO_MODE_OPEN_DRAIN (2) // synthesised
-#define GPIO_PULL_NONE (0)
-#define GPIO_PULL_UP (1)
-// Removed in SDK 1.1.0
-// #define GPIO_PULL_DOWN (2)
-
typedef struct _pin_irq_obj_t {
mp_obj_base_t base;
uint16_t phys_port;
@@ -84,7 +76,7 @@ const pyb_pin_obj_t pyb_pin_obj[16 + 1] = {
{{&pyb_pin_type}, 16, -1, -1},
};
-STATIC uint8_t pin_mode[16 + 1];
+uint8_t pin_mode[16 + 1];
// forward declaration
STATIC const pin_irq_obj_t pin_irq_obj[16];
diff --git a/ports/esp8266/machine_pwm.c b/ports/esp8266/machine_pwm.c
index 00547195f..6b7728dea 100644
--- a/ports/esp8266/machine_pwm.c
+++ b/ports/esp8266/machine_pwm.c
@@ -25,6 +25,7 @@
*/
#include "py/runtime.h"
+#include "py/mphal.h"
#include "modmachine.h"
#include "esppwm.h"
@@ -74,6 +75,10 @@ STATIC void mp_machine_pwm_init_helper(machine_pwm_obj_t *self, size_t n_args, c
pwm_set_duty(args[ARG_duty].u_int, self->channel);
}
+ if (pin_mode[self->pin->phys_port] == GPIO_MODE_OPEN_DRAIN) {
+ mp_hal_pin_open_drain(self->pin->phys_port);
+ }
+
pwm_start();
}
@@ -120,6 +125,10 @@ STATIC mp_obj_t mp_machine_pwm_duty_get(machine_pwm_obj_t *self) {
if (!self->active) {
pwm_add(self->pin->phys_port, self->pin->periph, self->pin->func);
self->active = 1;
+
+ if (pin_mode[self->pin->phys_port] == GPIO_MODE_OPEN_DRAIN) {
+ mp_hal_pin_open_drain(self->pin->phys_port);
+ }
}
return MP_OBJ_NEW_SMALL_INT(pwm_get_duty(self->channel));
}
diff --git a/ports/esp8266/modmachine.h b/ports/esp8266/modmachine.h
index 4a73d3b8e..9b7a5e3cb 100644
--- a/ports/esp8266/modmachine.h
+++ b/ports/esp8266/modmachine.h
@@ -21,6 +21,16 @@ typedef struct _pyb_pin_obj_t {
const pyb_pin_obj_t pyb_pin_obj[16 + 1];
+#define GPIO_MODE_INPUT (0)
+#define GPIO_MODE_OUTPUT (1)
+#define GPIO_MODE_OPEN_DRAIN (2) // synthesised
+#define GPIO_PULL_NONE (0)
+#define GPIO_PULL_UP (1)
+// Removed in SDK 1.1.0
+// #define GPIO_PULL_DOWN (2)
+
+extern uint8_t pin_mode[16 + 1];
+
void pin_init0(void);
uint mp_obj_get_pin(mp_obj_t pin_in);