2016-11-03 18:50:59 -04:00
|
|
|
/*
|
2017-08-27 15:02:50 -04:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2016-11-03 18:50:59 -04:00
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
2017-09-22 21:05:51 -04:00
|
|
|
* Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
|
2016-11-03 18:50:59 -04:00
|
|
|
*
|
|
|
|
* 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 <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "py/runtime.h"
|
|
|
|
#include "py/mphal.h"
|
|
|
|
|
2017-09-22 21:05:51 -04:00
|
|
|
#include "hal/include/hal_gpio.h"
|
|
|
|
|
2017-04-12 18:24:50 -04:00
|
|
|
#include "common-hal/microcontroller/Pin.h"
|
2017-04-10 16:32:19 -04:00
|
|
|
#include "shared-bindings/digitalio/DigitalInOut.h"
|
2022-05-27 15:59:54 -04:00
|
|
|
#include "supervisor/shared/translate/translate.h"
|
2016-11-03 18:50:59 -04:00
|
|
|
|
2017-04-10 16:32:19 -04:00
|
|
|
digitalinout_result_t common_hal_digitalio_digitalinout_construct(
|
2021-04-30 11:47:37 -04:00
|
|
|
digitalio_digitalinout_obj_t *self, const mcu_pin_obj_t *pin) {
|
2017-04-12 18:24:50 -04:00
|
|
|
claim_pin(pin);
|
2016-11-03 18:50:59 -04:00
|
|
|
self->pin = pin;
|
2018-08-31 17:46:03 -04:00
|
|
|
self->output = false;
|
|
|
|
self->open_drain = false;
|
2016-11-03 18:50:59 -04:00
|
|
|
|
2018-07-16 09:01:46 -04:00
|
|
|
// Must set pull after setting direction.
|
2018-07-31 17:01:01 -04:00
|
|
|
gpio_set_pin_direction(pin->number, GPIO_DIRECTION_IN);
|
|
|
|
gpio_set_pin_pull_mode(pin->number, GPIO_PULL_OFF);
|
2016-11-03 18:50:59 -04:00
|
|
|
return DIGITALINOUT_OK;
|
|
|
|
}
|
|
|
|
|
2019-04-05 22:06:37 -04:00
|
|
|
void common_hal_digitalio_digitalinout_never_reset(
|
2021-04-30 11:47:37 -04:00
|
|
|
digitalio_digitalinout_obj_t *self) {
|
2019-04-05 22:06:37 -04:00
|
|
|
never_reset_pin_number(self->pin->number);
|
|
|
|
}
|
|
|
|
|
2021-04-30 11:47:37 -04:00
|
|
|
bool common_hal_digitalio_digitalinout_deinited(digitalio_digitalinout_obj_t *self) {
|
2020-02-28 23:32:24 -05:00
|
|
|
return self->pin == NULL;
|
2017-10-02 20:49:40 -04:00
|
|
|
}
|
|
|
|
|
2021-04-30 11:47:37 -04:00
|
|
|
void common_hal_digitalio_digitalinout_deinit(digitalio_digitalinout_obj_t *self) {
|
2017-10-02 20:49:40 -04:00
|
|
|
if (common_hal_digitalio_digitalinout_deinited(self)) {
|
|
|
|
return;
|
|
|
|
}
|
2018-08-31 17:46:03 -04:00
|
|
|
reset_pin_number(self->pin->number);
|
2020-02-28 23:32:24 -05:00
|
|
|
self->pin = NULL;
|
2016-11-03 18:50:59 -04:00
|
|
|
}
|
|
|
|
|
2022-08-25 15:01:14 -04:00
|
|
|
digitalinout_result_t common_hal_digitalio_digitalinout_switch_to_input(
|
2021-04-30 11:47:37 -04:00
|
|
|
digitalio_digitalinout_obj_t *self, digitalio_pull_t pull) {
|
2016-11-03 18:50:59 -04:00
|
|
|
self->output = false;
|
2018-07-16 09:01:46 -04:00
|
|
|
// This also sets direction to input.
|
2017-04-10 16:32:19 -04:00
|
|
|
common_hal_digitalio_digitalinout_set_pull(self, pull);
|
2022-08-25 15:01:14 -04:00
|
|
|
return DIGITALINOUT_OK;
|
2016-11-03 18:50:59 -04:00
|
|
|
}
|
|
|
|
|
2020-05-20 12:23:42 -04:00
|
|
|
digitalinout_result_t common_hal_digitalio_digitalinout_switch_to_output(
|
2021-04-30 11:47:37 -04:00
|
|
|
digitalio_digitalinout_obj_t *self, bool value,
|
|
|
|
digitalio_drive_mode_t drive_mode) {
|
2018-07-31 17:01:01 -04:00
|
|
|
const uint8_t pin = self->pin->number;
|
2017-10-23 12:29:17 -04:00
|
|
|
gpio_set_pin_pull_mode(pin, GPIO_PULL_OFF);
|
|
|
|
// Turn on "strong" pin driving (more current available). See DRVSTR doc in datasheet.
|
2018-05-10 23:09:51 -04:00
|
|
|
hri_port_set_PINCFG_DRVSTR_bit(PORT, (enum gpio_port)GPIO_PORT(pin), GPIO_PIN(pin));
|
2016-11-03 18:50:59 -04:00
|
|
|
|
|
|
|
self->output = true;
|
|
|
|
self->open_drain = drive_mode == DRIVE_MODE_OPEN_DRAIN;
|
2018-07-16 09:01:46 -04:00
|
|
|
|
|
|
|
// Direction is set in set_value. We don't need to do it here.
|
2017-04-10 16:32:19 -04:00
|
|
|
common_hal_digitalio_digitalinout_set_value(self, value);
|
2020-05-20 12:23:42 -04:00
|
|
|
return DIGITALINOUT_OK;
|
2016-11-03 18:50:59 -04:00
|
|
|
}
|
|
|
|
|
2018-01-02 21:25:41 -05:00
|
|
|
digitalio_direction_t common_hal_digitalio_digitalinout_get_direction(
|
2021-04-30 11:47:37 -04:00
|
|
|
digitalio_digitalinout_obj_t *self) {
|
2018-08-31 17:46:03 -04:00
|
|
|
return self->output ? DIRECTION_OUTPUT : DIRECTION_INPUT;
|
2016-11-03 18:50:59 -04:00
|
|
|
}
|
|
|
|
|
2017-04-10 16:32:19 -04:00
|
|
|
void common_hal_digitalio_digitalinout_set_value(
|
2021-04-30 11:47:37 -04:00
|
|
|
digitalio_digitalinout_obj_t *self, bool value) {
|
2018-07-31 17:01:01 -04:00
|
|
|
const uint8_t pin = self->pin->number;
|
2018-07-16 09:01:46 -04:00
|
|
|
const uint8_t port = GPIO_PORT(pin);
|
|
|
|
const uint32_t pin_mask = 1U << GPIO_PIN(pin);
|
2016-11-03 18:50:59 -04:00
|
|
|
if (value) {
|
|
|
|
if (self->open_drain) {
|
2018-07-16 09:01:46 -04:00
|
|
|
// Assertion: pull is off, so the pin is floating in this case.
|
|
|
|
// We do open-drain high output (no sinking of current)
|
|
|
|
// by changing the direction to input with no pulls.
|
|
|
|
hri_port_clear_DIR_DIR_bf(PORT, port, pin_mask);
|
2016-11-03 18:50:59 -04:00
|
|
|
} else {
|
2018-07-16 09:01:46 -04:00
|
|
|
hri_port_set_DIR_DIR_bf(PORT, port, pin_mask);
|
|
|
|
hri_port_set_OUT_OUT_bf(PORT, port, pin_mask);
|
2016-11-03 18:50:59 -04:00
|
|
|
}
|
|
|
|
} else {
|
2018-07-16 09:01:46 -04:00
|
|
|
hri_port_set_DIR_DIR_bf(PORT, port, pin_mask);
|
|
|
|
hri_port_clear_OUT_OUT_bf(PORT,port, pin_mask);
|
2016-11-03 18:50:59 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-10 16:32:19 -04:00
|
|
|
bool common_hal_digitalio_digitalinout_get_value(
|
2021-04-30 11:47:37 -04:00
|
|
|
digitalio_digitalinout_obj_t *self) {
|
2018-07-31 17:01:01 -04:00
|
|
|
const uint8_t pin = self->pin->number;
|
2016-11-03 18:50:59 -04:00
|
|
|
if (!self->output) {
|
2017-10-23 12:29:17 -04:00
|
|
|
return gpio_get_pin_level(pin);
|
2016-11-03 18:50:59 -04:00
|
|
|
} else {
|
2018-07-16 09:01:46 -04:00
|
|
|
if (self->open_drain && hri_port_get_DIR_reg(PORT, GPIO_PORT(pin), 1U << GPIO_PIN(pin)) == 0) {
|
2016-11-03 18:50:59 -04:00
|
|
|
return true;
|
|
|
|
} else {
|
2018-07-16 09:01:46 -04:00
|
|
|
return hri_port_get_OUT_reg(PORT, GPIO_PORT(pin), 1U << GPIO_PIN(pin));
|
2016-11-03 18:50:59 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-20 12:23:42 -04:00
|
|
|
digitalinout_result_t common_hal_digitalio_digitalinout_set_drive_mode(
|
2021-04-30 11:47:37 -04:00
|
|
|
digitalio_digitalinout_obj_t *self,
|
|
|
|
digitalio_drive_mode_t drive_mode) {
|
2017-04-10 16:32:19 -04:00
|
|
|
bool value = common_hal_digitalio_digitalinout_get_value(self);
|
2016-11-03 18:50:59 -04:00
|
|
|
self->open_drain = drive_mode == DRIVE_MODE_OPEN_DRAIN;
|
|
|
|
// True is implemented differently between modes so reset the value to make
|
2018-07-16 09:01:46 -04:00
|
|
|
// sure it's correct for the new mode.
|
2016-11-03 18:50:59 -04:00
|
|
|
if (value) {
|
2017-04-10 16:32:19 -04:00
|
|
|
common_hal_digitalio_digitalinout_set_value(self, value);
|
2016-11-03 18:50:59 -04:00
|
|
|
}
|
2020-05-20 12:23:42 -04:00
|
|
|
return DIGITALINOUT_OK;
|
2016-11-03 18:50:59 -04:00
|
|
|
}
|
|
|
|
|
2018-01-02 21:25:41 -05:00
|
|
|
digitalio_drive_mode_t common_hal_digitalio_digitalinout_get_drive_mode(
|
2021-04-30 11:47:37 -04:00
|
|
|
digitalio_digitalinout_obj_t *self) {
|
2016-11-03 18:50:59 -04:00
|
|
|
if (self->open_drain) {
|
|
|
|
return DRIVE_MODE_OPEN_DRAIN;
|
|
|
|
} else {
|
|
|
|
return DRIVE_MODE_PUSH_PULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-25 15:01:14 -04:00
|
|
|
digitalinout_result_t common_hal_digitalio_digitalinout_set_pull(
|
2021-04-30 11:47:37 -04:00
|
|
|
digitalio_digitalinout_obj_t *self, digitalio_pull_t pull) {
|
2017-09-22 21:05:51 -04:00
|
|
|
enum gpio_pull_mode asf_pull = GPIO_PULL_OFF;
|
2016-11-03 18:50:59 -04:00
|
|
|
switch (pull) {
|
|
|
|
case PULL_UP:
|
2017-09-22 21:05:51 -04:00
|
|
|
asf_pull = GPIO_PULL_UP;
|
2016-11-03 18:50:59 -04:00
|
|
|
break;
|
|
|
|
case PULL_DOWN:
|
2017-09-22 21:05:51 -04:00
|
|
|
asf_pull = GPIO_PULL_DOWN;
|
2016-11-03 18:50:59 -04:00
|
|
|
break;
|
|
|
|
case PULL_NONE:
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2018-07-16 09:01:46 -04:00
|
|
|
// Must set pull after setting direction.
|
2018-07-31 17:01:01 -04:00
|
|
|
gpio_set_pin_direction(self->pin->number, GPIO_DIRECTION_IN);
|
|
|
|
gpio_set_pin_pull_mode(self->pin->number, asf_pull);
|
2022-08-25 15:01:14 -04:00
|
|
|
return DIGITALINOUT_OK;
|
2016-11-03 18:50:59 -04:00
|
|
|
}
|
|
|
|
|
2018-01-02 21:25:41 -05:00
|
|
|
digitalio_pull_t common_hal_digitalio_digitalinout_get_pull(
|
2021-04-30 11:47:37 -04:00
|
|
|
digitalio_digitalinout_obj_t *self) {
|
2018-07-31 17:01:01 -04:00
|
|
|
uint32_t pin = self->pin->number;
|
2016-11-03 18:50:59 -04:00
|
|
|
if (self->output) {
|
2018-07-31 19:53:54 -04:00
|
|
|
mp_raise_AttributeError(translate("Cannot get pull while in output mode"));
|
2016-11-03 18:50:59 -04:00
|
|
|
return PULL_NONE;
|
|
|
|
} else {
|
2018-07-16 09:01:46 -04:00
|
|
|
if (hri_port_get_PINCFG_PULLEN_bit(PORT, GPIO_PORT(pin), GPIO_PIN(pin)) == 0) {
|
2016-11-03 18:50:59 -04:00
|
|
|
return PULL_NONE;
|
2021-04-30 11:47:37 -04:00
|
|
|
}
|
|
|
|
if (hri_port_get_OUT_reg(PORT, GPIO_PORT(pin), 1U << GPIO_PIN(pin)) > 0) {
|
2016-11-03 18:50:59 -04:00
|
|
|
return PULL_UP;
|
|
|
|
} else {
|
|
|
|
return PULL_DOWN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-01-04 15:15:20 -05:00
|
|
|
|
|
|
|
bool common_hal_digitalio_has_reg_op(digitalinout_reg_op_t op) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
volatile uint32_t *common_hal_digitalio_digitalinout_get_reg(digitalio_digitalinout_obj_t *self, digitalinout_reg_op_t op, uint32_t *mask) {
|
|
|
|
const uint8_t pin = self->pin->number;
|
|
|
|
int port = GPIO_PORT(pin);
|
|
|
|
|
|
|
|
*mask = 1u << GPIO_PIN(pin);
|
|
|
|
|
|
|
|
|
|
|
|
switch (op) {
|
|
|
|
case DIGITALINOUT_REG_READ:
|
|
|
|
return (volatile uint32_t *)&PORT->Group[port].IN.reg;
|
|
|
|
case DIGITALINOUT_REG_WRITE:
|
|
|
|
return &PORT->Group[port].OUT.reg;
|
|
|
|
case DIGITALINOUT_REG_SET:
|
|
|
|
return &PORT->Group[port].OUTSET.reg;
|
|
|
|
case DIGITALINOUT_REG_RESET:
|
|
|
|
return &PORT->Group[port].OUTCLR.reg;
|
|
|
|
case DIGITALINOUT_REG_TOGGLE:
|
|
|
|
return &PORT->Group[port].OUTTGL.reg;
|
|
|
|
default:
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|