implemented 'claim_pin' methodology; updated 'open_drain' handling

This commit is contained in:
sommersoft 2018-04-28 17:27:23 +00:00
parent 4e1f7d43eb
commit 396e4ffc34
4 changed files with 44 additions and 23 deletions

View File

@ -36,14 +36,12 @@
#include "user_interface.h"
volatile bool adc_in_use __attribute__((aligned(4))) = false;
void common_hal_analogio_analogin_construct(analogio_analogin_obj_t* self,
const mcu_pin_obj_t *pin) {
if (pin != &pin_TOUT) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "Pin %q does not have ADC capabilities", pin->name));
}
adc_in_use = true;
claim_pin(pin);
}
bool common_hal_analogio_analogin_deinited(analogio_analogin_obj_t* self) {
@ -54,7 +52,7 @@ void common_hal_analogio_analogin_deinit(analogio_analogin_obj_t* self) {
if (common_hal_analogio_analogin_deinited(self)) {
return;
}
adc_in_use = false;
reset_pin(&pin_TOUT);
self->deinited = true;
}

View File

@ -44,7 +44,7 @@ digitalinout_result_t common_hal_digitalio_digitalinout_construct(
WRITE_PERI_REG(PAD_XPD_DCDC_CONF, (READ_PERI_REG(PAD_XPD_DCDC_CONF) & 0xffffffbc) | 1); // mux configuration for XPD_DCDC and rtc_gpio0 connection
WRITE_PERI_REG(RTC_GPIO_CONF, READ_PERI_REG(RTC_GPIO_CONF) & ~1); //mux configuration for out enable
WRITE_PERI_REG(RTC_GPIO_ENABLE, READ_PERI_REG(RTC_GPIO_ENABLE) & ~1); //out disable
gpio16_in_use = true;
claim_pin(pin);
} else {
PIN_FUNC_SELECT(self->pin->peripheral, self->pin->gpio_function);
}
@ -65,10 +65,7 @@ void common_hal_digitalio_digitalinout_deinit(digitalio_digitalinout_obj_t* self
PIN_FUNC_SELECT(self->pin->peripheral, 0);
PIN_PULLUP_DIS(self->pin->peripheral);
} else {
WRITE_PERI_REG(PAD_XPD_DCDC_CONF, (READ_PERI_REG(PAD_XPD_DCDC_CONF) & 0xffffffbc) | 1); // mux configuration for XPD_DCDC and rtc_gpio0 connection
WRITE_PERI_REG(RTC_GPIO_CONF, READ_PERI_REG(RTC_GPIO_CONF) & ~1); //mux configuration for out enable
WRITE_PERI_REG(RTC_GPIO_ENABLE, READ_PERI_REG(RTC_GPIO_ENABLE) & ~1); //out disable
gpio16_in_use = false;
reset_pin(self->pin);
}
self->pin = mp_const_none;
}
@ -117,7 +114,7 @@ void common_hal_digitalio_digitalinout_set_value(
WRITE_PERI_REG(PAD_XPD_DCDC_CONF, (READ_PERI_REG(PAD_XPD_DCDC_CONF) & 0xffffffbc) | 1);
WRITE_PERI_REG(RTC_GPIO_CONF, READ_PERI_REG(RTC_GPIO_CONF) & ~1);
WRITE_PERI_REG(RTC_GPIO_ENABLE, (READ_PERI_REG(RTC_GPIO_ENABLE) & ~1)); // input
WRITE_PERI_REG(RTC_GPIO_OUT, (READ_PERI_REG(RTC_GPIO_OUT) & ~1)); // out=0
WRITE_PERI_REG(RTC_GPIO_OUT, (READ_PERI_REG(RTC_GPIO_OUT) & 1)); // out=1
return;
} else {
int out_en = self->output;
@ -158,7 +155,7 @@ bool common_hal_digitalio_digitalinout_get_value(
return GPIO_INPUT_GET(self->pin->gpio_number);
} else {
if (self->pin->gpio_number == 16) {
if (self->open_drain && (READ_PERI_REG(RTC_GPIO_OUT) | READ_PERI_REG(RTC_GPIO_ENABLE)) == 0) {
if (self->open_drain && READ_PERI_REG(RTC_GPIO_ENABLE) == 0) {
return true;
} else {
return READ_PERI_REG(RTC_GPIO_OUT) & 1;

View File

@ -32,25 +32,47 @@
#include "eagle_soc.h"
extern volatile bool adc_in_use;
volatile bool gpio16_in_use __attribute__((aligned(4))) = false;
bool adc_in_use;
bool gpio16_in_use;
bool common_hal_mcu_pin_is_free(const mcu_pin_obj_t* pin) {
if (pin == &pin_TOUT) {
return !adc_in_use;
}
if (pin == &pin_XPD_DCDC) {
return !gpio16_in_use;
}
if (pin->gpio_number == NO_GPIO) {
return false;
}
if (pin->gpio_number == 16) {
return !gpio16_in_use;
}
return (READ_PERI_REG(pin->peripheral) &
(PERIPHS_IO_MUX_FUNC<<PERIPHS_IO_MUX_FUNC_S)) == 0 &&
(GPIO_REG_READ(GPIO_ENABLE_ADDRESS) & (1 << pin->gpio_number)) == 0 &&
(READ_PERI_REG(pin->peripheral) & PERIPHS_IO_MUX_PULLUP) == 0;
}
void claim_pin(const mcu_pin_obj_t* pin) {
if (pin == &pin_XPD_DCDC) {
gpio16_in_use = true;
}
if (pin == &pin_TOUT) {
adc_in_use = true;
}
}
void reset_pin(const mcu_pin_obj_t* pin) {
if (pin == &pin_XPD_DCDC) {
// Set GPIO16 as input
WRITE_PERI_REG(PAD_XPD_DCDC_CONF, (READ_PERI_REG(PAD_XPD_DCDC_CONF) & 0xffffffbc) | 1); // mux configuration for XPD_DCDC and rtc_gpio0 connection
WRITE_PERI_REG(RTC_GPIO_CONF, READ_PERI_REG(RTC_GPIO_CONF) & ~1); //mux configuration for out enable
WRITE_PERI_REG(RTC_GPIO_ENABLE, READ_PERI_REG(RTC_GPIO_ENABLE) & ~1); //out disable
gpio16_in_use = false;
}
if (pin == &pin_TOUT) {
adc_in_use = false;
}
}
void reset_pins(void) {
for (int i = 0; i < 16; i++) {
// 5 is RXD, 6 is TXD
@ -67,5 +89,7 @@ void reset_pins(void) {
WRITE_PERI_REG(PAD_XPD_DCDC_CONF, (READ_PERI_REG(PAD_XPD_DCDC_CONF) & 0xffffffbc) | 1); // mux configuration for XPD_DCDC and rtc_gpio0 connection
WRITE_PERI_REG(RTC_GPIO_CONF, READ_PERI_REG(RTC_GPIO_CONF) & ~1); //mux configuration for out enable
WRITE_PERI_REG(RTC_GPIO_ENABLE, READ_PERI_REG(RTC_GPIO_ENABLE) & ~1); //out disable
adc_in_use = false;
gpio16_in_use = false;
}

View File

@ -41,6 +41,8 @@ typedef struct {
#define NO_GPIO 0xff
#define SPECIAL_CASE 0xfe
void claim_pin(const mcu_pin_obj_t* pin);
void reset_pin(const mcu_pin_obj_t* pin);
void reset_pins(void);
#endif // MICROPY_INCLUDED_ESP8266_COMMON_HAL_MICROCONTROLLER_PIN_H