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>
This commit is contained in:
Trammell hudson 2020-02-25 22:53:46 +01:00 committed by Damien George
parent 5228f4067d
commit 96a2cc5e13
4 changed files with 20 additions and 10 deletions

View File

@ -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;

View File

@ -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];

View File

@ -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));
}

View File

@ -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);