2017-12-21 07:49:14 -05:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2018-07-08 11:48:32 -04:00
|
|
|
#include "shared-bindings/digitalio/DigitalInOut.h"
|
2017-12-21 07:49:14 -05:00
|
|
|
#include "py/runtime.h"
|
2022-05-27 15:59:54 -04:00
|
|
|
#include "supervisor/shared/translate/translate.h"
|
2017-12-21 07:49:14 -05:00
|
|
|
|
2018-07-08 11:48:32 -04:00
|
|
|
#include "nrf_gpio.h"
|
2017-12-21 07:49:14 -05:00
|
|
|
|
2018-10-19 21:46:22 -04:00
|
|
|
void common_hal_digitalio_digitalinout_never_reset(
|
2021-03-15 09:57:36 -04:00
|
|
|
digitalio_digitalinout_obj_t *self) {
|
2018-10-19 21:46:22 -04:00
|
|
|
never_reset_pin_number(self->pin->number);
|
|
|
|
}
|
|
|
|
|
2017-12-21 07:49:14 -05: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) {
|
2018-08-31 17:46:03 -04:00
|
|
|
claim_pin(pin);
|
2017-12-21 07:49:14 -05:00
|
|
|
self->pin = pin;
|
2018-07-08 11:48:32 -04:00
|
|
|
|
2018-08-30 21:42:25 -04:00
|
|
|
nrf_gpio_cfg_input(pin->number, NRF_GPIO_PIN_NOPULL);
|
2018-07-08 11:48:32 -04:00
|
|
|
|
2017-12-21 07:49:14 -05:00
|
|
|
return DIGITALINOUT_OK;
|
|
|
|
}
|
|
|
|
|
2018-07-08 11:48:32 -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-12-21 07:49:14 -05:00
|
|
|
}
|
|
|
|
|
2018-07-08 11:48:32 -04:00
|
|
|
void common_hal_digitalio_digitalinout_deinit(digitalio_digitalinout_obj_t *self) {
|
2017-12-21 07:49:14 -05:00
|
|
|
if (common_hal_digitalio_digitalinout_deinited(self)) {
|
|
|
|
return;
|
|
|
|
}
|
2018-08-30 21:42:25 -04:00
|
|
|
nrf_gpio_cfg_default(self->pin->number);
|
2018-07-08 11:48:32 -04:00
|
|
|
|
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;
|
2017-12-21 07:49:14 -05: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) {
|
2018-08-30 21:42:25 -04:00
|
|
|
nrf_gpio_cfg_input(self->pin->number, NRF_GPIO_PIN_NOPULL);
|
2017-12-21 07:49:14 -05:00
|
|
|
common_hal_digitalio_digitalinout_set_pull(self, pull);
|
2022-08-25 15:01:14 -04:00
|
|
|
return DIGITALINOUT_OK;
|
2017-12-21 07:49:14 -05:00
|
|
|
}
|
|
|
|
|
2020-05-20 12:23:42 -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) {
|
2018-07-08 11:48:32 -04:00
|
|
|
|
2018-09-09 15:01:28 -04:00
|
|
|
common_hal_digitalio_digitalinout_set_drive_mode(self, drive_mode);
|
2017-12-21 07:49:14 -05:00
|
|
|
common_hal_digitalio_digitalinout_set_value(self, value);
|
2020-05-20 12:23:42 -04:00
|
|
|
return DIGITALINOUT_OK;
|
2017-12-21 07:49:14 -05:00
|
|
|
}
|
|
|
|
|
2018-01-03 16:50:57 -05:00
|
|
|
digitalio_direction_t common_hal_digitalio_digitalinout_get_direction(
|
2021-03-15 09:57:36 -04:00
|
|
|
digitalio_digitalinout_obj_t *self) {
|
2018-09-09 15:01:28 -04:00
|
|
|
|
|
|
|
return (nrf_gpio_pin_dir_get(self->pin->number) == NRF_GPIO_PIN_DIR_INPUT)
|
|
|
|
? DIRECTION_INPUT : DIRECTION_OUTPUT;
|
2017-12-21 07:49:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void common_hal_digitalio_digitalinout_set_value(
|
2021-03-15 09:57:36 -04:00
|
|
|
digitalio_digitalinout_obj_t *self, bool value) {
|
2018-09-09 15:01:28 -04:00
|
|
|
nrf_gpio_pin_write(self->pin->number, value);
|
2017-12-21 07:49:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
bool common_hal_digitalio_digitalinout_get_value(
|
2021-03-15 09:57:36 -04:00
|
|
|
digitalio_digitalinout_obj_t *self) {
|
2018-09-09 15:01:28 -04:00
|
|
|
return (nrf_gpio_pin_dir_get(self->pin->number) == NRF_GPIO_PIN_DIR_INPUT)
|
|
|
|
? nrf_gpio_pin_read(self->pin->number)
|
|
|
|
: nrf_gpio_pin_out_read(self->pin->number);
|
2017-12-21 07:49:14 -05:00
|
|
|
}
|
|
|
|
|
2020-05-20 12:23:42 -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) {
|
2018-09-09 15:01:28 -04:00
|
|
|
nrf_gpio_cfg(self->pin->number,
|
2021-03-15 09:57:36 -04:00
|
|
|
NRF_GPIO_PIN_DIR_OUTPUT,
|
|
|
|
NRF_GPIO_PIN_INPUT_DISCONNECT,
|
|
|
|
NRF_GPIO_PIN_NOPULL,
|
|
|
|
drive_mode == DRIVE_MODE_OPEN_DRAIN ? NRF_GPIO_PIN_H0D1 : NRF_GPIO_PIN_H0H1,
|
|
|
|
NRF_GPIO_PIN_NOSENSE);
|
2020-05-20 12:23:42 -04:00
|
|
|
return DIGITALINOUT_OK;
|
2017-12-21 07:49:14 -05:00
|
|
|
}
|
|
|
|
|
2018-01-03 16:50:57 -05: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) {
|
2018-09-09 15:01:28 -04:00
|
|
|
uint32_t pin = self->pin->number;
|
|
|
|
// Changes pin to be a relative pin number in port.
|
|
|
|
NRF_GPIO_Type *reg = nrf_gpio_pin_port_decode(&pin);
|
|
|
|
|
|
|
|
switch ((reg->PIN_CNF[pin] & GPIO_PIN_CNF_DRIVE_Msk) >> GPIO_PIN_CNF_DRIVE_Pos) {
|
2021-03-15 09:57:36 -04:00
|
|
|
case NRF_GPIO_PIN_S0D1:
|
|
|
|
case NRF_GPIO_PIN_H0D1:
|
|
|
|
return DRIVE_MODE_OPEN_DRAIN;
|
|
|
|
default:
|
|
|
|
return DRIVE_MODE_PUSH_PULL;
|
2018-08-31 17:46:03 -04:00
|
|
|
}
|
2017-12-21 07:49:14 -05: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) {
|
2018-07-08 11:48:32 -04:00
|
|
|
nrf_gpio_pin_pull_t hal_pull = NRF_GPIO_PIN_NOPULL;
|
|
|
|
|
2017-12-21 07:49:14 -05:00
|
|
|
switch (pull) {
|
|
|
|
case PULL_UP:
|
2018-07-08 11:48:32 -04:00
|
|
|
hal_pull = NRF_GPIO_PIN_PULLUP;
|
2017-12-21 07:49:14 -05:00
|
|
|
break;
|
|
|
|
case PULL_DOWN:
|
2018-07-08 11:48:32 -04:00
|
|
|
hal_pull = NRF_GPIO_PIN_PULLDOWN;
|
2017-12-21 07:49:14 -05:00
|
|
|
break;
|
|
|
|
case PULL_NONE:
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2018-07-08 11:48:32 -04:00
|
|
|
|
2018-08-30 21:42:25 -04:00
|
|
|
nrf_gpio_cfg_input(self->pin->number, hal_pull);
|
2022-08-25 15:01:14 -04:00
|
|
|
return DIGITALINOUT_OK;
|
2017-12-21 07:49:14 -05:00
|
|
|
}
|
|
|
|
|
2018-01-03 16:50:57 -05:00
|
|
|
digitalio_pull_t common_hal_digitalio_digitalinout_get_pull(
|
2021-03-15 09:57:36 -04:00
|
|
|
digitalio_digitalinout_obj_t *self) {
|
2018-08-30 21:42:25 -04:00
|
|
|
uint32_t pin = self->pin->number;
|
|
|
|
// Changes pin to be a relative pin number in port.
|
2018-07-08 11:48:32 -04:00
|
|
|
NRF_GPIO_Type *reg = nrf_gpio_pin_port_decode(&pin);
|
|
|
|
|
2018-08-31 17:46:03 -04:00
|
|
|
if (nrf_gpio_pin_dir_get(self->pin->number) == NRF_GPIO_PIN_DIR_OUTPUT) {
|
2018-08-16 17:21:44 -04:00
|
|
|
mp_raise_AttributeError(translate("Cannot get pull while in output mode"));
|
2018-07-08 11:48:32 -04:00
|
|
|
}
|
2017-12-21 07:49:14 -05:00
|
|
|
|
2018-09-09 15:01:28 -04:00
|
|
|
switch ((reg->PIN_CNF[pin] & GPIO_PIN_CNF_PULL_Msk) >> GPIO_PIN_CNF_PULL_Pos) {
|
2018-07-08 11:48:32 -04:00
|
|
|
case NRF_GPIO_PIN_PULLUP:
|
|
|
|
return PULL_UP;
|
|
|
|
case NRF_GPIO_PIN_PULLDOWN:
|
|
|
|
return PULL_DOWN;
|
|
|
|
default:
|
|
|
|
return PULL_NONE;
|
2017-12-21 07:49:14 -05:00
|
|
|
}
|
|
|
|
}
|