Fully implement digitalio and pin-in-use tracking.

Fixes #2901
This commit is contained in:
Scott Shawcroft 2020-05-19 17:46:29 -07:00
parent acf4b1bede
commit 49090d1378
No known key found for this signature in database
GPG Key ID: 9349BC7E64B1921E
17 changed files with 265 additions and 58 deletions

View File

@ -149,6 +149,7 @@ SRC_C += \
mphalport.c \
boards/$(BOARD)/board.c \
boards/$(BOARD)/pins.c \
modules/$(CIRCUITPY_MODULE).c \
lib/libc/string0.c \
lib/mp-readline/readline.c \
lib/oofatfs/ff.c \

View File

@ -26,8 +26,16 @@
#include "boards/board.h"
#include "mpconfigboard.h"
#include "shared-bindings/microcontroller/Pin.h"
void board_init(void) {
// USB
never_reset_pin(&pin_GPIO19);
never_reset_pin(&pin_GPIO20);
// Debug UART
never_reset_pin(&pin_GPIO43);
never_reset_pin(&pin_GPIO44);
}
bool board_requests_safe_mode(void) {

View File

@ -18,3 +18,5 @@ CIRCUITPY_MICROCONTROLLER = 0
CIRCUITPY_ESP_FLASH_MODE=dio
CIRCUITPY_ESP_FLASH_FREQ=40m
CIRCUITPY_ESP_FLASH_SIZE=4MB
CIRCUITPY_MODULE=wroom

View File

@ -28,6 +28,13 @@
#include "mpconfigboard.h"
void board_init(void) {
// USB
never_reset_pin(&pin_GPIO19);
never_reset_pin(&pin_GPIO20);
// Debug UART
never_reset_pin(&pin_GPIO43);
never_reset_pin(&pin_GPIO44);
}
bool board_requests_safe_mode(void) {

View File

@ -18,3 +18,5 @@ CIRCUITPY_MICROCONTROLLER = 0
CIRCUITPY_ESP_FLASH_MODE=dio
CIRCUITPY_ESP_FLASH_FREQ=40m
CIRCUITPY_ESP_FLASH_SIZE=4MB
CIRCUITPY_MODULE=wrover

View File

@ -30,24 +30,18 @@
#include "driver/gpio.h"
#include "esp_log.h"
static const char* TAG = "CircuitPython digitalio";
#include "esp-idf/components/soc/include/hal/gpio_hal.h"
void common_hal_digitalio_digitalinout_never_reset(
digitalio_digitalinout_obj_t *self) {
(void)self;
never_reset_pin_number(self->pin->number);
}
digitalinout_result_t common_hal_digitalio_digitalinout_construct(
digitalio_digitalinout_obj_t *self, const mcu_pin_obj_t *pin) {
// claim_pin(pin);
claim_pin(pin);
self->pin = pin;
ESP_EARLY_LOGW(TAG, "construct %d", pin->number);
return DIGITALINOUT_OK;
}
@ -60,78 +54,86 @@ void common_hal_digitalio_digitalinout_deinit(digitalio_digitalinout_obj_t *self
return;
}
// reset_pin_number(0, self->pin->number);
reset_pin_number(self->pin->number);
self->pin = mp_const_none;
}
void common_hal_digitalio_digitalinout_switch_to_input(
digitalio_digitalinout_obj_t *self, digitalio_pull_t pull) {
gpio_config_t config = {
.pin_bit_mask = 1 << self->pin->number,
.mode = GPIO_MODE_INPUT
};
if (pull == PULL_UP) {
config.pull_up_en = GPIO_PULLUP_ENABLE;
} else if (pull == PULL_DOWN) {
config.pull_down_en = GPIO_PULLDOWN_ENABLE;
}
gpio_config(&config);
common_hal_digitalio_digitalinout_set_pull(self, pull);
gpio_set_direction(self->pin->number, GPIO_MODE_DEF_INPUT);
}
void common_hal_digitalio_digitalinout_switch_to_output(
digitalinout_result_t common_hal_digitalio_digitalinout_switch_to_output(
digitalio_digitalinout_obj_t *self, bool value,
digitalio_drive_mode_t drive_mode) {
gpio_config_t config = {
.pin_bit_mask = 1ULL << self->pin->number,
.mode = GPIO_MODE_OUTPUT
};
if (drive_mode == DRIVE_MODE_OPEN_DRAIN) {
config.mode = GPIO_MODE_OUTPUT_OD;
}
gpio_set_level(self->pin->number, value);
gpio_config(&config);
return common_hal_digitalio_digitalinout_set_drive_mode(self, drive_mode);
}
digitalio_direction_t common_hal_digitalio_digitalinout_get_direction(
digitalio_digitalinout_obj_t *self) {
uint32_t iomux = READ_PERI_REG(GPIO_PIN_MUX_REG[self->pin->number]);
if ((iomux & FUN_IE) != 0) {
return DIRECTION_INPUT;
}
return DIRECTION_OUTPUT;
}
void common_hal_digitalio_digitalinout_set_value(
digitalio_digitalinout_obj_t *self, bool value) {
ESP_EARLY_LOGW(TAG, "set %d %d", self->pin->number, value);
gpio_set_level(self->pin->number, value);
}
bool common_hal_digitalio_digitalinout_get_value(
digitalio_digitalinout_obj_t *self) {
return true;
return gpio_get_level(self->pin->number) == 1;
}
void common_hal_digitalio_digitalinout_set_drive_mode(
digitalinout_result_t common_hal_digitalio_digitalinout_set_drive_mode(
digitalio_digitalinout_obj_t *self,
digitalio_drive_mode_t drive_mode) {
(void)self;
(void)drive_mode;
gpio_num_t number = self->pin->number;
gpio_mode_t mode;
if (drive_mode == DRIVE_MODE_OPEN_DRAIN) {
mode = GPIO_MODE_DEF_OD;
} else {
mode = GPIO_MODE_DEF_OUTPUT;
}
esp_err_t result = gpio_set_direction(number, mode);
if (result != ESP_OK) {
return DIGITALINOUT_INPUT_ONLY;
}
return DIGITALINOUT_OK;
}
digitalio_drive_mode_t common_hal_digitalio_digitalinout_get_drive_mode(
digitalio_digitalinout_obj_t *self) {
// if (common_hal_digitalio_digitalinout_get_direction(self) == DIRECTION_OUTPUT)
// return DRIVE_MODE_PUSH_PULL;
// else
return DRIVE_MODE_OPEN_DRAIN;
if (GPIO_HAL_GET_HW(GPIO_PORT_0)->pin[self->pin->number].pad_driver == 1) {
return DRIVE_MODE_OPEN_DRAIN;
}
return DRIVE_MODE_PUSH_PULL;
}
void common_hal_digitalio_digitalinout_set_pull(
digitalio_digitalinout_obj_t *self, digitalio_pull_t pull) {
(void)self;
(void)pull;
gpio_num_t number = self->pin->number;
gpio_pullup_dis(number);
gpio_pulldown_dis(number);
if (pull == PULL_UP) {
gpio_pullup_en(number);
} else if (pull == PULL_DOWN) {
gpio_pulldown_en(number);
}
}
digitalio_pull_t common_hal_digitalio_digitalinout_get_pull(
digitalio_digitalinout_obj_t *self) {
gpio_num_t gpio_num = self->pin->number;
if (REG_GET_BIT(GPIO_PIN_MUX_REG[gpio_num], FUN_PU) == 1) {
return PULL_UP;
} else if (REG_GET_BIT(GPIO_PIN_MUX_REG[gpio_num], FUN_PD) == 1) {
return PULL_DOWN;
}
return PULL_NONE;
}

View File

@ -29,18 +29,51 @@
#include "py/mphal.h"
// Mark pin as free and return it to a quiescent state.
void reset_pin_number(uint8_t pin_port, uint8_t pin_number) {
#include "esp-idf/components/driver/include/driver/gpio.h"
#include "esp-idf/components/soc/include/hal/gpio_hal.h"
STATIC uint32_t never_reset_pins[2];
STATIC uint32_t in_use[2];
void never_reset_pin_number(gpio_num_t pin_number) {
never_reset_pins[pin_number / 32] |= 1 << pin_number % 32;
}
void never_reset_pin(const mcu_pin_obj_t* pin) {
never_reset_pin_number(pin->number);
}
// Mark pin as free and return it to a quiescent state.
void reset_pin_number(gpio_num_t pin_number) {
never_reset_pins[pin_number / 32] &= ~(1 << pin_number % 32);
in_use[pin_number / 32] &= ~(1 << pin_number % 32);
}
void reset_all_pins(void) {
for (uint8_t i = 0; i < GPIO_PIN_COUNT; i++) {
uint32_t iomux_address = GPIO_PIN_MUX_REG[i];
if (iomux_address == 0 ||
(never_reset_pins[i / 32] & (1 << i % 32)) != 0) {
continue;
}
gpio_set_direction(i, GPIO_MODE_DEF_INPUT);
gpio_pullup_dis(i);
gpio_pulldown_dis(i);
}
in_use[0] = 0;
in_use[1] = 0;
}
void claim_pin(const mcu_pin_obj_t* pin) {
in_use[pin->number / 32] |= (1 << pin->number % 32);
}
bool pin_number_is_free(uint8_t pin_port, uint8_t pin_number) {
return true;
bool pin_number_is_free(gpio_num_t pin_number) {
uint8_t offset = pin_number / 32;
uint8_t mask = 1 << pin_number % 32;
return (never_reset_pins[offset] & mask) == 0 && (in_use[offset] & mask) == 0;
}
bool common_hal_mcu_pin_is_free(const mcu_pin_obj_t *pin) {
return pin_number_is_free(0, pin->number);
return pin_number_is_free(pin->number);
}

View File

@ -34,11 +34,10 @@
void reset_all_pins(void);
// reset_pin_number takes the pin number instead of the pointer so that objects don't
// need to store a full pointer.
void reset_pin_number(uint8_t pin_port, uint8_t pin_number);
void reset_pin_number(gpio_num_t pin_number);
void claim_pin(const mcu_pin_obj_t* pin);
bool pin_number_is_free(uint8_t pin_port, uint8_t pin_number);
void never_reset_pin_number(uint8_t pin_port, uint8_t pin_number);
// GPIO_TypeDef * pin_port(uint8_t pin_port);
uint16_t pin_mask(uint8_t pin_number);
bool pin_number_is_free(gpio_num_t pin_number);
void never_reset_pin_number(gpio_num_t pin_number);
void never_reset_pin(const mcu_pin_obj_t* pin);
#endif // MICROPY_INCLUDED_ESP32S2_COMMON_HAL_MICROCONTROLLER_PIN_H

View File

@ -0,0 +1,35 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2020 Scott Shawcroft for Adafruit Industries
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef MICROPY_INCLUDED_ESP32S2_MODULES_MODULE_H
#define MICROPY_INCLUDED_ESP32S2_MODULES_MODULE_H
#include "shared-bindings/microcontroller/Pin.h"
void never_reset_module_internal_pins(void);
#endif // MICROPY_INCLUDED_ESP32S2_MODULES_MODULE_H

View File

@ -0,0 +1,28 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2020 Scott Shawcroft for Adafruit Industries
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
void never_reset_module_internal_pins(void) {
}

View File

@ -0,0 +1,37 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2020 Scott Shawcroft for Adafruit Industries
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "modules/module.h"
void never_reset_module_internal_pins(void) {
// SPI Flash
never_reset_pin(&pin_GPIO27);
never_reset_pin(&pin_GPIO28);
never_reset_pin(&pin_GPIO29);
never_reset_pin(&pin_GPIO30);
never_reset_pin(&pin_GPIO31);
never_reset_pin(&pin_GPIO32);
}

View File

@ -0,0 +1,38 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2020 Scott Shawcroft for Adafruit Industries
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "modules/module.h"
void never_reset_module_internal_pins(void) {
// SPI Flash and RAM
never_reset_pin(&pin_GPIO26);
never_reset_pin(&pin_GPIO27);
never_reset_pin(&pin_GPIO28);
never_reset_pin(&pin_GPIO29);
never_reset_pin(&pin_GPIO30);
never_reset_pin(&pin_GPIO31);
never_reset_pin(&pin_GPIO32);
}

View File

@ -33,3 +33,5 @@ CIRCUITPY_TOUCHIO = 0
# Enable USB support
CIRCUITPY_USB_HID = 1
CIRCUITPY_USB_MIDI = 1
CIRCUITPY_MODULE ?= none

View File

@ -33,10 +33,12 @@
#include <stdint.h>
#include "esp32s2_peripherals_config.h"
#include "esp-idf/config/sdkconfig.h"
#include "esp-idf/components/soc/include/hal/gpio_types.h"
typedef struct {
PIN_PREFIX_FIELDS
uint8_t number;
gpio_num_t number;
} mcu_pin_obj_t;
extern const mcu_pin_obj_t pin_GPIO0;

View File

@ -29,10 +29,12 @@
#include <sys/time.h>
#include "supervisor/port.h"
#include "boards/board.h"
#include "modules/module.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "common-hal/microcontroller/Pin.h"
#include "supervisor/memory.h"
#include "supervisor/shared/tick.h"
@ -56,12 +58,14 @@ safe_mode_t port_init(void) {
if (result != ESP_OK) {
ESP_EARLY_LOGE(TAG, "Unable to create tick timer.");
}
never_reset_module_internal_pins();
ESP_EARLY_LOGW(TAG, "port init done");
return NO_SAFE_MODE;
}
void reset_port(void) {
reset_all_pins();
}
void reset_to_bootloader(void) {

View File

@ -131,7 +131,10 @@ STATIC mp_obj_t digitalio_digitalinout_switch_to_output(size_t n_args, const mp_
drive_mode = DRIVE_MODE_OPEN_DRAIN;
}
// do the transfer
common_hal_digitalio_digitalinout_switch_to_output(self, args[ARG_value].u_bool, drive_mode);
digitalinout_result_t result = common_hal_digitalio_digitalinout_switch_to_output(self, args[ARG_value].u_bool, drive_mode);
if (result == DIGITALINOUT_INPUT_ONLY) {
mp_raise_NotImplementedError(translate("Pin is input only"));
}
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_KW(digitalio_digitalinout_switch_to_output_obj, 1, digitalio_digitalinout_switch_to_output);
@ -207,7 +210,10 @@ STATIC mp_obj_t digitalio_digitalinout_obj_set_direction(mp_obj_t self_in, mp_ob
if (value == &digitalio_direction_input_obj) {
common_hal_digitalio_digitalinout_switch_to_input(self, PULL_NONE);
} else if (value == &digitalio_direction_output_obj) {
common_hal_digitalio_digitalinout_switch_to_output(self, false, DRIVE_MODE_PUSH_PULL);
digitalinout_result_t result = common_hal_digitalio_digitalinout_switch_to_output(self, false, DRIVE_MODE_PUSH_PULL);
if (result == DIGITALINOUT_INPUT_ONLY) {
mp_raise_NotImplementedError(translate("Pin is input only"));
}
} else {
mp_raise_ValueError(translate("Invalid direction."));
}

View File

@ -37,18 +37,19 @@ extern const mp_obj_type_t digitalio_digitalinout_type;
typedef enum {
DIGITALINOUT_OK,
DIGITALINOUT_PIN_BUSY
DIGITALINOUT_PIN_BUSY,
DIGITALINOUT_INPUT_ONLY
} digitalinout_result_t;
digitalinout_result_t common_hal_digitalio_digitalinout_construct(digitalio_digitalinout_obj_t* self, const mcu_pin_obj_t* pin);
void common_hal_digitalio_digitalinout_deinit(digitalio_digitalinout_obj_t* self);
bool common_hal_digitalio_digitalinout_deinited(digitalio_digitalinout_obj_t* self);
void common_hal_digitalio_digitalinout_switch_to_input(digitalio_digitalinout_obj_t* self, digitalio_pull_t pull);
void common_hal_digitalio_digitalinout_switch_to_output(digitalio_digitalinout_obj_t* self, bool value, digitalio_drive_mode_t drive_mode);
digitalinout_result_t common_hal_digitalio_digitalinout_switch_to_output(digitalio_digitalinout_obj_t* self, bool value, digitalio_drive_mode_t drive_mode);
digitalio_direction_t common_hal_digitalio_digitalinout_get_direction(digitalio_digitalinout_obj_t* self);
void common_hal_digitalio_digitalinout_set_value(digitalio_digitalinout_obj_t* self, bool value);
bool common_hal_digitalio_digitalinout_get_value(digitalio_digitalinout_obj_t* self);
void common_hal_digitalio_digitalinout_set_drive_mode(digitalio_digitalinout_obj_t* self, digitalio_drive_mode_t drive_mode);
digitalinout_result_t common_hal_digitalio_digitalinout_set_drive_mode(digitalio_digitalinout_obj_t* self, digitalio_drive_mode_t drive_mode);
digitalio_drive_mode_t common_hal_digitalio_digitalinout_get_drive_mode(digitalio_digitalinout_obj_t* self);
void common_hal_digitalio_digitalinout_set_pull(digitalio_digitalinout_obj_t* self, digitalio_pull_t pull);
digitalio_pull_t common_hal_digitalio_digitalinout_get_pull(digitalio_digitalinout_obj_t* self);