circuitpython/ports/nrf/common-hal/digitalio/DigitalInOut.c

172 lines
5.7 KiB
C
Raw Normal View History

/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2017 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"
#include "nrf_gpio.h"
digitalinout_result_t common_hal_digitalio_digitalinout_construct(
digitalio_digitalinout_obj_t *self, const mcu_pin_obj_t *pin) {
self->pin = pin;
nrf_gpio_cfg_input(NRF_GPIO_PIN_MAP(pin->port, pin->pin), NRF_GPIO_PIN_NOPULL);
return DIGITALINOUT_OK;
}
bool common_hal_digitalio_digitalinout_deinited(digitalio_digitalinout_obj_t *self) {
return self->pin == mp_const_none;
}
void common_hal_digitalio_digitalinout_deinit(digitalio_digitalinout_obj_t *self) {
if (common_hal_digitalio_digitalinout_deinited(self)) {
return;
}
const uint32_t pin = NRF_GPIO_PIN_MAP(self->pin->port, self->pin->pin);
nrf_gpio_cfg_default(pin);
self->pin = mp_const_none;
}
void common_hal_digitalio_digitalinout_switch_to_input(
digitalio_digitalinout_obj_t *self, digitalio_pull_t pull) {
const uint32_t pin = NRF_GPIO_PIN_MAP(self->pin->port, self->pin->pin);
nrf_gpio_cfg_input(pin, NRF_GPIO_PIN_NOPULL);
common_hal_digitalio_digitalinout_set_pull(self, pull);
}
void common_hal_digitalio_digitalinout_switch_to_output(
digitalio_digitalinout_obj_t *self, bool value,
digitalio_drive_mode_t drive_mode) {
const uint32_t pin = NRF_GPIO_PIN_MAP(self->pin->port, self->pin->pin);
self->open_drain = (drive_mode == DRIVE_MODE_OPEN_DRAIN);
nrf_gpio_cfg_input(pin, NRF_GPIO_PIN_NOPULL);
common_hal_digitalio_digitalinout_set_value(self, value);
}
digitalio_direction_t common_hal_digitalio_digitalinout_get_direction(
digitalio_digitalinout_obj_t *self) {
const uint32_t pin = NRF_GPIO_PIN_MAP(self->pin->port, self->pin->pin);
return (nrf_gpio_pin_dir_get(pin) == NRF_GPIO_PIN_DIR_OUTPUT) ? DIRECTION_OUTPUT : DIRECTION_INPUT;
}
void common_hal_digitalio_digitalinout_set_value(
digitalio_digitalinout_obj_t *self, bool value) {
const uint32_t pin = NRF_GPIO_PIN_MAP(self->pin->port, self->pin->pin);
if (value && self->open_drain) {
nrf_gpio_pin_dir_set(pin, NRF_GPIO_PIN_DIR_INPUT);
} else {
nrf_gpio_pin_dir_set(pin, NRF_GPIO_PIN_DIR_OUTPUT);
nrf_gpio_pin_write(pin, value);
}
}
bool common_hal_digitalio_digitalinout_get_value(
digitalio_digitalinout_obj_t *self) {
const uint32_t pin = NRF_GPIO_PIN_MAP(self->pin->port, self->pin->pin);
const nrf_gpio_pin_dir_t dir = nrf_gpio_pin_dir_get(pin);
if (dir == NRF_GPIO_PIN_DIR_INPUT) {
if (self->open_drain)
return true;
return nrf_gpio_pin_read(pin);
}
return nrf_gpio_pin_out_read(pin);
}
void common_hal_digitalio_digitalinout_set_drive_mode(
digitalio_digitalinout_obj_t *self,
digitalio_drive_mode_t drive_mode) {
const bool value = common_hal_digitalio_digitalinout_get_value(self);
self->open_drain = drive_mode == DRIVE_MODE_OPEN_DRAIN;
// True is implemented differently between modes so reset the value to make
// sure its correct for the new mode.
if (value)
common_hal_digitalio_digitalinout_set_value(self, value);
}
digitalio_drive_mode_t common_hal_digitalio_digitalinout_get_drive_mode(
digitalio_digitalinout_obj_t *self) {
if (self->open_drain)
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) {
const uint32_t pin = NRF_GPIO_PIN_MAP(self->pin->port, self->pin->pin);
nrf_gpio_pin_pull_t hal_pull = NRF_GPIO_PIN_NOPULL;
switch (pull) {
case PULL_UP:
hal_pull = NRF_GPIO_PIN_PULLUP;
break;
case PULL_DOWN:
hal_pull = NRF_GPIO_PIN_PULLDOWN;
break;
case PULL_NONE:
default:
break;
}
nrf_gpio_cfg_input(pin, hal_pull);
}
digitalio_pull_t common_hal_digitalio_digitalinout_get_pull(
digitalio_digitalinout_obj_t *self) {
uint32_t pin = NRF_GPIO_PIN_MAP(self->pin->port, self->pin->pin);
NRF_GPIO_Type *reg = nrf_gpio_pin_port_decode(&pin);
if (nrf_gpio_pin_dir_get(pin) == NRF_GPIO_PIN_DIR_OUTPUT) {
mp_raise_AttributeError("Cannot get pull while in output mode");
return PULL_NONE;
}
switch (reg->PIN_CNF[self->pin->pin] & GPIO_PIN_CNF_PULL_Msk) {
case NRF_GPIO_PIN_PULLUP:
return PULL_UP;
case NRF_GPIO_PIN_PULLDOWN:
return PULL_DOWN;
default:
return PULL_NONE;
}
}