2020-04-17 19:23:28 -04:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2017-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 "shared-bindings/digitalio/DigitalInOut.h"
|
|
|
|
#include "py/runtime.h"
|
|
|
|
|
2020-09-28 18:59:29 -04:00
|
|
|
#include "components/driver/include/driver/gpio.h"
|
2020-04-17 19:23:28 -04:00
|
|
|
|
2021-06-24 11:41:12 -04:00
|
|
|
#include "components/hal/include/hal/gpio_hal.h"
|
2020-04-17 19:23:28 -04:00
|
|
|
|
2022-09-12 09:48:41 -04:00
|
|
|
STATIC bool _pin_is_input(uint8_t pin_number) {
|
|
|
|
const uint32_t iomux = READ_PERI_REG(GPIO_PIN_MUX_REG[pin_number]);
|
|
|
|
return (iomux & FUN_IE) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void digitalio_digitalinout_preserve_for_deep_sleep(size_t n_dios, digitalio_digitalinout_obj_t *preserve_dios[]) {
|
|
|
|
// Mark the pin states of the given DigitalInOuts for preservation during deep sleep
|
|
|
|
for (size_t i = 0; i < n_dios; i++) {
|
|
|
|
if (!common_hal_digitalio_digitalinout_deinited(preserve_dios[i])) {
|
|
|
|
preserve_pin_number(preserve_dios[i]->pin->number);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-17 19:23:28 -04:00
|
|
|
void common_hal_digitalio_digitalinout_never_reset(
|
2021-03-15 09:57:36 -04:00
|
|
|
digitalio_digitalinout_obj_t *self) {
|
2020-05-19 20:46:29 -04:00
|
|
|
never_reset_pin_number(self->pin->number);
|
2020-04-17 19:23:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
digitalinout_result_t common_hal_digitalio_digitalinout_construct(
|
2021-03-15 09:57:36 -04:00
|
|
|
digitalio_digitalinout_obj_t *self, const mcu_pin_obj_t *pin) {
|
2020-05-19 20:46:29 -04:00
|
|
|
claim_pin(pin);
|
2020-04-17 19:23:28 -04:00
|
|
|
self->pin = pin;
|
|
|
|
|
2020-09-29 16:04:39 -04:00
|
|
|
gpio_config_t config;
|
|
|
|
config.pin_bit_mask = 1ull << pin->number;
|
|
|
|
config.mode = GPIO_MODE_INPUT;
|
|
|
|
config.pull_up_en = GPIO_PULLUP_DISABLE;
|
|
|
|
config.pull_down_en = GPIO_PULLDOWN_DISABLE;
|
|
|
|
config.intr_type = GPIO_INTR_DISABLE;
|
|
|
|
if (gpio_config(&config) != ESP_OK) {
|
|
|
|
return DIGITALINOUT_PIN_BUSY;
|
|
|
|
}
|
|
|
|
|
2020-04-17 19:23:28 -04:00
|
|
|
return DIGITALINOUT_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool common_hal_digitalio_digitalinout_deinited(digitalio_digitalinout_obj_t *self) {
|
2020-06-26 15:33:50 -04:00
|
|
|
return self->pin == NULL;
|
2020-04-17 19:23:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void common_hal_digitalio_digitalinout_deinit(digitalio_digitalinout_obj_t *self) {
|
|
|
|
if (common_hal_digitalio_digitalinout_deinited(self)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-19 20:46:29 -04:00
|
|
|
reset_pin_number(self->pin->number);
|
2020-06-26 15:33:50 -04:00
|
|
|
self->pin = NULL;
|
2020-04-17 19:23:28 -04:00
|
|
|
}
|
|
|
|
|
2022-08-25 15:01:14 -04:00
|
|
|
digitalinout_result_t common_hal_digitalio_digitalinout_switch_to_input(
|
2021-03-15 09:57:36 -04:00
|
|
|
digitalio_digitalinout_obj_t *self, digitalio_pull_t pull) {
|
2020-05-19 20:46:29 -04:00
|
|
|
common_hal_digitalio_digitalinout_set_pull(self, pull);
|
2022-01-15 23:05:33 -05:00
|
|
|
gpio_set_direction(self->pin->number, GPIO_MODE_INPUT);
|
2022-08-25 15:01:14 -04:00
|
|
|
return DIGITALINOUT_OK;
|
2020-04-17 19:23:28 -04:00
|
|
|
}
|
|
|
|
|
2020-05-19 20:46:29 -04:00
|
|
|
digitalinout_result_t common_hal_digitalio_digitalinout_switch_to_output(
|
2021-03-15 09:57:36 -04:00
|
|
|
digitalio_digitalinout_obj_t *self, bool value,
|
|
|
|
digitalio_drive_mode_t drive_mode) {
|
2020-09-29 16:04:39 -04:00
|
|
|
common_hal_digitalio_digitalinout_set_value(self, value);
|
2020-05-19 20:46:29 -04:00
|
|
|
return common_hal_digitalio_digitalinout_set_drive_mode(self, drive_mode);
|
2020-04-17 19:23:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
digitalio_direction_t common_hal_digitalio_digitalinout_get_direction(
|
2021-03-15 09:57:36 -04:00
|
|
|
digitalio_digitalinout_obj_t *self) {
|
2022-09-12 09:48:41 -04:00
|
|
|
if (_pin_is_input(self->pin->number)) {
|
2020-05-19 20:46:29 -04:00
|
|
|
return DIRECTION_INPUT;
|
|
|
|
}
|
2020-04-17 19:23:28 -04:00
|
|
|
return DIRECTION_OUTPUT;
|
|
|
|
}
|
|
|
|
|
|
|
|
void common_hal_digitalio_digitalinout_set_value(
|
2021-03-15 09:57:36 -04:00
|
|
|
digitalio_digitalinout_obj_t *self, bool value) {
|
2020-09-29 16:04:39 -04:00
|
|
|
self->output_value = value;
|
2020-04-17 19:23:28 -04:00
|
|
|
gpio_set_level(self->pin->number, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool common_hal_digitalio_digitalinout_get_value(
|
2021-03-15 09:57:36 -04:00
|
|
|
digitalio_digitalinout_obj_t *self) {
|
2020-09-29 16:04:39 -04:00
|
|
|
if (common_hal_digitalio_digitalinout_get_direction(self) == DIRECTION_INPUT) {
|
|
|
|
return gpio_get_level(self->pin->number) == 1;
|
|
|
|
}
|
|
|
|
return self->output_value;
|
2020-04-17 19:23:28 -04:00
|
|
|
}
|
|
|
|
|
2020-05-19 20:46:29 -04:00
|
|
|
digitalinout_result_t common_hal_digitalio_digitalinout_set_drive_mode(
|
2021-03-15 09:57:36 -04:00
|
|
|
digitalio_digitalinout_obj_t *self,
|
|
|
|
digitalio_drive_mode_t drive_mode) {
|
2020-05-19 20:46:29 -04:00
|
|
|
gpio_num_t number = self->pin->number;
|
2022-01-15 23:05:33 -05:00
|
|
|
gpio_mode_t mode = GPIO_MODE_OUTPUT;
|
2020-05-19 20:46:29 -04:00
|
|
|
if (drive_mode == DRIVE_MODE_OPEN_DRAIN) {
|
2022-01-15 23:05:33 -05:00
|
|
|
mode |= GPIO_MODE_OUTPUT_OD;
|
2020-05-19 20:46:29 -04:00
|
|
|
}
|
|
|
|
esp_err_t result = gpio_set_direction(number, mode);
|
|
|
|
if (result != ESP_OK) {
|
|
|
|
return DIGITALINOUT_INPUT_ONLY;
|
|
|
|
}
|
|
|
|
return DIGITALINOUT_OK;
|
2020-04-17 19:23:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
digitalio_drive_mode_t common_hal_digitalio_digitalinout_get_drive_mode(
|
2021-03-15 09:57:36 -04:00
|
|
|
digitalio_digitalinout_obj_t *self) {
|
2020-05-19 20:46:29 -04:00
|
|
|
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;
|
2020-04-17 19:23:28 -04:00
|
|
|
}
|
|
|
|
|
2022-08-25 15:01:14 -04:00
|
|
|
digitalinout_result_t common_hal_digitalio_digitalinout_set_pull(
|
2021-03-15 09:57:36 -04:00
|
|
|
digitalio_digitalinout_obj_t *self, digitalio_pull_t pull) {
|
2020-05-19 20:46:29 -04:00
|
|
|
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);
|
|
|
|
}
|
2022-08-25 15:01:14 -04:00
|
|
|
return DIGITALINOUT_OK;
|
2020-04-17 19:23:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
digitalio_pull_t common_hal_digitalio_digitalinout_get_pull(
|
2021-03-15 09:57:36 -04:00
|
|
|
digitalio_digitalinout_obj_t *self) {
|
2020-05-19 20:46:29 -04:00
|
|
|
gpio_num_t gpio_num = self->pin->number;
|
2022-05-04 22:51:27 -04:00
|
|
|
if (REG_GET_BIT(GPIO_PIN_MUX_REG[gpio_num], FUN_PU)) {
|
2020-05-19 20:46:29 -04:00
|
|
|
return PULL_UP;
|
2022-05-04 22:51:27 -04:00
|
|
|
} else if (REG_GET_BIT(GPIO_PIN_MUX_REG[gpio_num], FUN_PD)) {
|
2020-05-19 20:46:29 -04:00
|
|
|
return PULL_DOWN;
|
|
|
|
}
|
2020-04-17 19:23:28 -04:00
|
|
|
return PULL_NONE;
|
|
|
|
}
|