2021-01-20 13:34:08 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2016-2021 Damien P. George
|
|
|
|
*
|
|
|
|
* 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 <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "py/runtime.h"
|
|
|
|
#include "py/mphal.h"
|
2021-07-09 05:19:15 +01:00
|
|
|
#include "shared/runtime/mpirq.h"
|
2021-01-20 13:34:08 +00:00
|
|
|
#include "modmachine.h"
|
2022-10-25 17:43:38 +01:00
|
|
|
#include "machine_pin.h"
|
2021-01-20 13:34:08 +00:00
|
|
|
#include "extmod/virtpin.h"
|
|
|
|
|
|
|
|
#include "hardware/irq.h"
|
|
|
|
#include "hardware/regs/intctrl.h"
|
|
|
|
#include "hardware/structs/iobank0.h"
|
|
|
|
#include "hardware/structs/padsbank0.h"
|
|
|
|
|
|
|
|
// These can be or'd together.
|
|
|
|
#define GPIO_PULL_UP (1)
|
|
|
|
#define GPIO_PULL_DOWN (2)
|
|
|
|
|
|
|
|
#define GPIO_IRQ_ALL (0xf)
|
|
|
|
|
|
|
|
// Macros to access the state of the hardware.
|
|
|
|
#define GPIO_GET_FUNCSEL(id) ((iobank0_hw->io[(id)].ctrl & IO_BANK0_GPIO0_CTRL_FUNCSEL_BITS) >> IO_BANK0_GPIO0_CTRL_FUNCSEL_LSB)
|
|
|
|
#define GPIO_IS_OUT(id) (sio_hw->gpio_oe & (1 << (id)))
|
|
|
|
#define GPIO_IS_PULL_UP(id) (padsbank0_hw->io[(id)] & PADS_BANK0_GPIO0_PUE_BITS)
|
|
|
|
#define GPIO_IS_PULL_DOWN(id) (padsbank0_hw->io[(id)] & PADS_BANK0_GPIO0_PDE_BITS)
|
|
|
|
|
|
|
|
// Open drain behaviour is simulated.
|
|
|
|
#define GPIO_IS_OPEN_DRAIN(id) (machine_pin_open_drain_mask & (1 << (id)))
|
|
|
|
|
2022-06-30 07:01:02 +01:00
|
|
|
#ifndef MICROPY_HW_PIN_RESERVED
|
|
|
|
#define MICROPY_HW_PIN_RESERVED(i) (0)
|
|
|
|
#endif
|
|
|
|
|
2022-10-25 19:45:00 +01:00
|
|
|
MP_DEFINE_CONST_OBJ_TYPE(
|
|
|
|
machine_pin_af_type,
|
|
|
|
MP_QSTR_PinAF,
|
|
|
|
MP_TYPE_FLAG_NONE
|
|
|
|
);
|
|
|
|
|
|
|
|
MP_DEFINE_CONST_OBJ_TYPE(
|
|
|
|
pin_cpu_pins_obj_type,
|
|
|
|
MP_QSTR_cpu,
|
|
|
|
MP_TYPE_FLAG_NONE,
|
|
|
|
locals_dict, &pin_cpu_pins_locals_dict
|
|
|
|
);
|
|
|
|
|
|
|
|
MP_DEFINE_CONST_OBJ_TYPE(
|
|
|
|
pin_board_pins_obj_type,
|
|
|
|
MP_QSTR_board,
|
|
|
|
MP_TYPE_FLAG_NONE,
|
|
|
|
locals_dict, &pin_board_pins_locals_dict
|
|
|
|
);
|
|
|
|
|
2021-01-20 13:34:08 +00:00
|
|
|
typedef struct _machine_pin_irq_obj_t {
|
|
|
|
mp_irq_obj_t base;
|
|
|
|
uint32_t flags;
|
|
|
|
uint32_t trigger;
|
|
|
|
} machine_pin_irq_obj_t;
|
|
|
|
|
|
|
|
STATIC const mp_irq_methods_t machine_pin_irq_methods;
|
2022-10-25 19:45:00 +01:00
|
|
|
extern const machine_pin_obj_t *machine_pin_cpu_pins[NUM_BANK0_GPIOS];
|
2022-06-30 07:01:02 +01:00
|
|
|
|
2021-01-20 13:34:08 +00:00
|
|
|
// Mask with "1" indicating that the corresponding pin is in simulated open-drain mode.
|
|
|
|
uint32_t machine_pin_open_drain_mask;
|
|
|
|
|
2022-10-25 17:43:38 +01:00
|
|
|
#if MICROPY_HW_PIN_EXT_COUNT
|
|
|
|
STATIC inline bool is_ext_pin(__unused const machine_pin_obj_t *self) {
|
|
|
|
return self->is_ext;
|
2022-06-30 07:01:02 +01:00
|
|
|
}
|
|
|
|
#else
|
2022-10-25 17:43:38 +01:00
|
|
|
#define is_ext_pin(x) false
|
2022-06-30 07:01:02 +01:00
|
|
|
#endif
|
|
|
|
|
2021-01-20 13:34:08 +00:00
|
|
|
STATIC void gpio_irq(void) {
|
|
|
|
for (int i = 0; i < 4; ++i) {
|
|
|
|
uint32_t intr = iobank0_hw->intr[i];
|
|
|
|
if (intr) {
|
|
|
|
for (int j = 0; j < 8; ++j) {
|
|
|
|
if (intr & 0xf) {
|
|
|
|
uint32_t gpio = 8 * i + j;
|
|
|
|
gpio_acknowledge_irq(gpio, intr & 0xf);
|
|
|
|
machine_pin_irq_obj_t *irq = MP_STATE_PORT(machine_pin_irq_obj[gpio]);
|
|
|
|
if (irq != NULL && (intr & irq->trigger)) {
|
|
|
|
irq->flags = intr & irq->trigger;
|
|
|
|
mp_irq_handler(&irq->base);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
intr >>= 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void machine_pin_init(void) {
|
|
|
|
memset(MP_STATE_PORT(machine_pin_irq_obj), 0, sizeof(MP_STATE_PORT(machine_pin_irq_obj)));
|
2022-06-30 07:01:02 +01:00
|
|
|
irq_add_shared_handler(IO_IRQ_BANK0, gpio_irq, PICO_SHARED_IRQ_HANDLER_DEFAULT_ORDER_PRIORITY);
|
2021-01-20 13:34:08 +00:00
|
|
|
irq_set_enabled(IO_IRQ_BANK0, true);
|
2022-10-25 17:43:38 +01:00
|
|
|
#if MICROPY_HW_PIN_EXT_COUNT
|
|
|
|
machine_pin_ext_init();
|
2022-06-30 07:01:02 +01:00
|
|
|
#endif
|
2021-01-20 13:34:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void machine_pin_deinit(void) {
|
2021-02-01 18:21:12 +00:00
|
|
|
for (int i = 0; i < NUM_BANK0_GPIOS; ++i) {
|
2022-06-30 07:01:02 +01:00
|
|
|
if (MICROPY_HW_PIN_RESERVED(i)) {
|
|
|
|
continue;
|
|
|
|
}
|
2021-01-20 13:34:08 +00:00
|
|
|
gpio_set_irq_enabled(i, GPIO_IRQ_ALL, false);
|
|
|
|
}
|
|
|
|
irq_remove_handler(IO_IRQ_BANK0, gpio_irq);
|
|
|
|
}
|
|
|
|
|
2022-10-25 19:45:00 +01:00
|
|
|
const machine_pin_obj_t *machine_pin_find_named(const mp_obj_dict_t *named_pins, mp_obj_t name) {
|
|
|
|
const mp_map_t *named_map = &named_pins->map;
|
|
|
|
mp_map_elem_t *named_elem = mp_map_lookup((mp_map_t *)named_map, name, MP_MAP_LOOKUP);
|
|
|
|
if (named_elem != NULL && named_elem->value != NULL) {
|
|
|
|
return MP_OBJ_TO_PTR(named_elem->value);
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
const machine_pin_af_obj_t *machine_pin_find_alt(const machine_pin_obj_t *pin, uint8_t fn) {
|
|
|
|
const machine_pin_af_obj_t *af = pin->af;
|
|
|
|
for (mp_uint_t i = 0; i < pin->af_num; i++, af++) {
|
|
|
|
if (af->fn == fn) {
|
|
|
|
return af;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
const machine_pin_af_obj_t *machine_pin_find_alt_by_index(const machine_pin_obj_t *pin, mp_uint_t af_idx) {
|
|
|
|
const machine_pin_af_obj_t *af = pin->af;
|
|
|
|
for (mp_uint_t i = 0; i < pin->af_num; i++, af++) {
|
|
|
|
if (af->idx == af_idx) {
|
|
|
|
return af;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-01-20 13:34:08 +00:00
|
|
|
STATIC void machine_pin_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
|
|
|
|
machine_pin_obj_t *self = self_in;
|
|
|
|
uint funcsel = GPIO_GET_FUNCSEL(self->id);
|
|
|
|
qstr mode_qst;
|
2022-10-25 17:43:38 +01:00
|
|
|
if (!is_ext_pin(self)) {
|
2022-06-30 07:01:02 +01:00
|
|
|
if (funcsel == GPIO_FUNC_SIO) {
|
|
|
|
if (GPIO_IS_OPEN_DRAIN(self->id)) {
|
|
|
|
mode_qst = MP_QSTR_OPEN_DRAIN;
|
|
|
|
} else if (GPIO_IS_OUT(self->id)) {
|
|
|
|
mode_qst = MP_QSTR_OUT;
|
|
|
|
} else {
|
|
|
|
mode_qst = MP_QSTR_IN;
|
|
|
|
}
|
2021-01-20 13:34:08 +00:00
|
|
|
} else {
|
2022-06-30 07:01:02 +01:00
|
|
|
mode_qst = MP_QSTR_ALT;
|
|
|
|
}
|
2022-10-25 19:45:00 +01:00
|
|
|
mp_printf(print, "Pin(%q, mode=%q", self->name, mode_qst);
|
2022-06-30 07:01:02 +01:00
|
|
|
bool pull_up = false;
|
|
|
|
if (GPIO_IS_PULL_UP(self->id)) {
|
|
|
|
mp_printf(print, ", pull=%q", MP_QSTR_PULL_UP);
|
|
|
|
pull_up = true;
|
|
|
|
}
|
|
|
|
if (GPIO_IS_PULL_DOWN(self->id)) {
|
|
|
|
if (pull_up) {
|
|
|
|
mp_printf(print, "|%q", MP_QSTR_PULL_DOWN);
|
|
|
|
} else {
|
|
|
|
mp_printf(print, ", pull=%q", MP_QSTR_PULL_DOWN);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (funcsel != GPIO_FUNC_SIO) {
|
2022-10-25 19:45:00 +01:00
|
|
|
const machine_pin_af_obj_t *af = machine_pin_find_alt_by_index(self, funcsel);
|
|
|
|
if (af == NULL) {
|
|
|
|
mp_printf(print, ", alt=%u", funcsel);
|
|
|
|
} else {
|
|
|
|
mp_printf(print, ", alt=%q", af->name);
|
|
|
|
}
|
2021-01-20 13:34:08 +00:00
|
|
|
}
|
|
|
|
} else {
|
2022-10-25 17:43:38 +01:00
|
|
|
#if MICROPY_HW_PIN_EXT_COUNT
|
2022-10-25 19:45:00 +01:00
|
|
|
mode_qst = (self->is_output) ? MP_QSTR_OUT : MP_QSTR_IN;
|
|
|
|
mp_printf(print, "Pin(%q, mode=%q", self->name, mode_qst);
|
2022-06-30 07:01:02 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
mp_printf(print, ")");
|
|
|
|
}
|
|
|
|
|
|
|
|
enum {
|
|
|
|
ARG_mode, ARG_pull, ARG_value, ARG_alt
|
|
|
|
};
|
|
|
|
static const mp_arg_t allowed_args[] = {
|
|
|
|
{MP_QSTR_mode, MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE}},
|
|
|
|
{MP_QSTR_pull, MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE}},
|
|
|
|
{MP_QSTR_value, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE}},
|
|
|
|
{MP_QSTR_alt, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = GPIO_FUNC_SIO}},
|
|
|
|
};
|
|
|
|
|
2022-10-25 17:43:38 +01:00
|
|
|
STATIC mp_obj_t machine_pin_obj_init_helper(const machine_pin_obj_t *self, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
|
|
|
|
|
|
|
// parse args
|
2022-06-30 07:01:02 +01:00
|
|
|
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
|
|
|
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
|
|
|
|
2022-10-25 17:43:38 +01:00
|
|
|
if (is_ext_pin(self) && args[ARG_pull].u_obj != mp_const_none) {
|
2022-10-25 19:45:00 +01:00
|
|
|
mp_raise_ValueError("pulls are not supported for external pins");
|
2022-06-30 07:01:02 +01:00
|
|
|
}
|
|
|
|
|
2022-10-25 17:43:38 +01:00
|
|
|
if (is_ext_pin(self) && args[ARG_alt].u_int != GPIO_FUNC_SIO) {
|
2022-10-25 19:45:00 +01:00
|
|
|
mp_raise_ValueError("alternate functions are not supported for external pins");
|
2021-01-20 13:34:08 +00:00
|
|
|
}
|
2022-06-30 07:01:02 +01:00
|
|
|
|
2023-01-24 06:29:45 +00:00
|
|
|
// get initial value of pin (only valid for OUT and OPEN_DRAIN modes)
|
2022-06-30 07:01:02 +01:00
|
|
|
int value = -1;
|
|
|
|
if (args[ARG_value].u_obj != mp_const_none) {
|
|
|
|
value = mp_obj_is_true(args[ARG_value].u_obj);
|
2021-01-20 13:34:08 +00:00
|
|
|
}
|
2022-06-30 07:01:02 +01:00
|
|
|
|
2021-01-20 13:34:08 +00:00
|
|
|
// configure mode
|
|
|
|
if (args[ARG_mode].u_obj != mp_const_none) {
|
|
|
|
mp_int_t mode = mp_obj_get_int(args[ARG_mode].u_obj);
|
2022-10-25 17:43:38 +01:00
|
|
|
if (is_ext_pin(self)) {
|
|
|
|
#if MICROPY_HW_PIN_EXT_COUNT
|
|
|
|
// The regular Pins are const, but the external pins are mutable.
|
|
|
|
machine_pin_obj_t *mutable_self = (machine_pin_obj_t *)self;
|
|
|
|
machine_pin_ext_config(mutable_self, mode, value);
|
|
|
|
#endif
|
|
|
|
} else if (mode == MACHINE_PIN_MODE_IN) {
|
2021-01-20 13:34:08 +00:00
|
|
|
mp_hal_pin_input(self->id);
|
2022-10-25 17:43:38 +01:00
|
|
|
} else if (mode == MACHINE_PIN_MODE_OUT) {
|
2023-01-24 06:29:45 +00:00
|
|
|
if (value != -1) {
|
|
|
|
// set initial output value before configuring mode
|
|
|
|
gpio_put(self->id, value);
|
|
|
|
}
|
2021-01-20 13:34:08 +00:00
|
|
|
mp_hal_pin_output(self->id);
|
2022-10-25 17:43:38 +01:00
|
|
|
} else if (mode == MACHINE_PIN_MODE_OPEN_DRAIN) {
|
2023-01-24 06:29:45 +00:00
|
|
|
mp_hal_pin_open_drain_with_value(self->id, value == -1 ? 1 : value);
|
2021-01-20 13:34:08 +00:00
|
|
|
} else {
|
2022-10-25 17:43:38 +01:00
|
|
|
// Configure alternate function.
|
2022-10-25 19:45:00 +01:00
|
|
|
mp_uint_t af = args[ARG_alt].u_int;
|
|
|
|
if (machine_pin_find_alt(self, af) == NULL) {
|
|
|
|
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("invalid pin af: %d"), af);
|
|
|
|
}
|
|
|
|
gpio_set_function(self->id, af);
|
2021-01-20 13:34:08 +00:00
|
|
|
machine_pin_open_drain_mask &= ~(1 << self->id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-25 17:43:38 +01:00
|
|
|
if (!is_ext_pin(self)) {
|
|
|
|
// Configure pull (unconditionally because None means no-pull).
|
|
|
|
uint32_t pull = 0;
|
|
|
|
if (args[ARG_pull].u_obj != mp_const_none) {
|
|
|
|
pull = mp_obj_get_int(args[ARG_pull].u_obj);
|
|
|
|
}
|
|
|
|
gpio_set_pulls(self->id, pull & GPIO_PULL_UP, pull & GPIO_PULL_DOWN);
|
2021-01-20 13:34:08 +00:00
|
|
|
}
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
|
|
|
// constructor(id, ...)
|
|
|
|
mp_obj_t mp_pin_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
|
|
|
|
mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true);
|
2022-06-30 07:01:02 +01:00
|
|
|
const machine_pin_obj_t *self = NULL;
|
2022-10-25 19:45:00 +01:00
|
|
|
|
2022-06-30 07:01:02 +01:00
|
|
|
if (mp_obj_is_str(args[0])) {
|
|
|
|
const char *name = mp_obj_str_get_str(args[0]);
|
2022-10-25 19:45:00 +01:00
|
|
|
// Try to find the pin in the board pins first.
|
|
|
|
self = machine_pin_find_named(&pin_board_pins_locals_dict, args[0]);
|
|
|
|
if (!self) {
|
|
|
|
// If not found, try to find the pin in the cpu pins which include
|
|
|
|
// CPU and and externally controlled pins (if any).
|
|
|
|
self = machine_pin_find_named(&pin_cpu_pins_locals_dict, args[0]);
|
2022-06-30 07:01:02 +01:00
|
|
|
}
|
|
|
|
if (!self) {
|
2022-10-25 19:45:00 +01:00
|
|
|
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("unknown named pin \"%s\""), name);
|
2022-06-30 07:01:02 +01:00
|
|
|
}
|
2022-10-25 19:45:00 +01:00
|
|
|
} else if (mp_obj_is_int(args[0])) {
|
2022-06-30 07:01:02 +01:00
|
|
|
// get the wanted pin object
|
|
|
|
int wanted_pin = mp_obj_get_int(args[0]);
|
2022-10-25 19:45:00 +01:00
|
|
|
if (0 <= wanted_pin && wanted_pin < MP_ARRAY_SIZE(machine_pin_cpu_pins)) {
|
|
|
|
self = machine_pin_cpu_pins[wanted_pin];
|
2022-06-30 07:01:02 +01:00
|
|
|
}
|
2022-10-25 19:45:00 +01:00
|
|
|
}
|
|
|
|
if (!self) {
|
|
|
|
mp_raise_ValueError("invalid pin");
|
2022-06-30 07:01:02 +01:00
|
|
|
}
|
2022-10-25 17:43:38 +01:00
|
|
|
|
2021-01-20 13:34:08 +00:00
|
|
|
if (n_args > 1 || n_kw > 0) {
|
|
|
|
// pin mode given, so configure this GPIO
|
|
|
|
mp_map_t kw_args;
|
|
|
|
mp_map_init_fixed_table(&kw_args, n_kw, args + n_args);
|
2022-10-25 17:43:38 +01:00
|
|
|
machine_pin_obj_init_helper(self, n_args - 1, args + 1, &kw_args);
|
2021-01-20 13:34:08 +00:00
|
|
|
}
|
|
|
|
return MP_OBJ_FROM_PTR(self);
|
|
|
|
}
|
|
|
|
|
|
|
|
// fast method for getting/setting pin value
|
|
|
|
STATIC mp_obj_t machine_pin_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
|
|
|
|
mp_arg_check_num(n_args, n_kw, 0, 1, false);
|
|
|
|
machine_pin_obj_t *self = self_in;
|
|
|
|
if (n_args == 0) {
|
|
|
|
// get pin
|
2022-10-25 17:43:38 +01:00
|
|
|
if (is_ext_pin(self)) {
|
|
|
|
#if MICROPY_HW_PIN_EXT_COUNT
|
|
|
|
return MP_OBJ_NEW_SMALL_INT(machine_pin_ext_get(self));
|
|
|
|
#endif
|
|
|
|
} else {
|
2022-06-30 07:01:02 +01:00
|
|
|
return MP_OBJ_NEW_SMALL_INT(gpio_get(self->id));
|
|
|
|
}
|
2021-01-20 13:34:08 +00:00
|
|
|
} else {
|
|
|
|
// set pin
|
|
|
|
bool value = mp_obj_is_true(args[0]);
|
2022-10-25 17:43:38 +01:00
|
|
|
if (is_ext_pin(self)) {
|
|
|
|
#if MICROPY_HW_PIN_EXT_COUNT
|
|
|
|
machine_pin_ext_set(self, value);
|
|
|
|
#endif
|
|
|
|
} else if (GPIO_IS_OPEN_DRAIN(self->id)) {
|
|
|
|
MP_STATIC_ASSERT(GPIO_IN == 0 && GPIO_OUT == 1);
|
|
|
|
gpio_set_dir(self->id, 1 - value);
|
|
|
|
} else {
|
|
|
|
gpio_put(self->id, value);
|
2021-01-20 13:34:08 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-30 07:01:02 +01:00
|
|
|
return mp_const_none;
|
2021-01-20 13:34:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// pin.init(mode, pull)
|
|
|
|
STATIC mp_obj_t machine_pin_obj_init(size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
|
2022-10-25 17:43:38 +01:00
|
|
|
return machine_pin_obj_init_helper(args[0], n_args - 1, args + 1, kw_args);
|
2021-01-20 13:34:08 +00:00
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_KW(machine_pin_init_obj, 1, machine_pin_obj_init);
|
|
|
|
|
|
|
|
// pin.value([value])
|
|
|
|
STATIC mp_obj_t machine_pin_value(size_t n_args, const mp_obj_t *args) {
|
|
|
|
return machine_pin_call(args[0], n_args - 1, 0, args + 1);
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_pin_value_obj, 1, 2, machine_pin_value);
|
|
|
|
|
|
|
|
// pin.low()
|
|
|
|
STATIC mp_obj_t machine_pin_low(mp_obj_t self_in) {
|
|
|
|
machine_pin_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
2022-10-25 17:43:38 +01:00
|
|
|
if (is_ext_pin(self)) {
|
|
|
|
#if MICROPY_HW_PIN_EXT_COUNT
|
|
|
|
machine_pin_ext_set(self, 0);
|
|
|
|
#endif
|
|
|
|
} else if (GPIO_IS_OPEN_DRAIN(self->id)) {
|
|
|
|
gpio_set_dir(self->id, GPIO_OUT);
|
|
|
|
} else {
|
|
|
|
gpio_clr_mask(1u << self->id);
|
2021-01-20 13:34:08 +00:00
|
|
|
}
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_pin_low_obj, machine_pin_low);
|
|
|
|
|
|
|
|
// pin.high()
|
|
|
|
STATIC mp_obj_t machine_pin_high(mp_obj_t self_in) {
|
|
|
|
machine_pin_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
2022-10-25 17:43:38 +01:00
|
|
|
if (is_ext_pin(self)) {
|
|
|
|
#if MICROPY_HW_PIN_EXT_COUNT
|
|
|
|
machine_pin_ext_set(self, 1);
|
|
|
|
#endif
|
|
|
|
} else if (GPIO_IS_OPEN_DRAIN(self->id)) {
|
|
|
|
gpio_set_dir(self->id, GPIO_IN);
|
|
|
|
} else {
|
|
|
|
gpio_set_mask(1u << self->id);
|
2021-01-20 13:34:08 +00:00
|
|
|
}
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
2022-06-30 07:01:02 +01:00
|
|
|
|
2021-01-20 13:34:08 +00:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_pin_high_obj, machine_pin_high);
|
|
|
|
|
|
|
|
// pin.toggle()
|
|
|
|
STATIC mp_obj_t machine_pin_toggle(mp_obj_t self_in) {
|
|
|
|
machine_pin_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
2022-10-25 17:43:38 +01:00
|
|
|
if (is_ext_pin(self)) {
|
|
|
|
#if MICROPY_HW_PIN_EXT_COUNT
|
|
|
|
machine_pin_ext_set(self, self->last_output_value ^ 1);
|
|
|
|
#endif
|
|
|
|
} else if (GPIO_IS_OPEN_DRAIN(self->id)) {
|
|
|
|
if (GPIO_IS_OUT(self->id)) {
|
|
|
|
gpio_set_dir(self->id, GPIO_IN);
|
2021-01-20 13:34:08 +00:00
|
|
|
} else {
|
2022-10-25 17:43:38 +01:00
|
|
|
gpio_set_dir(self->id, GPIO_OUT);
|
2021-01-20 13:34:08 +00:00
|
|
|
}
|
2022-10-25 17:43:38 +01:00
|
|
|
} else {
|
|
|
|
gpio_xor_mask(1u << self->id);
|
2021-01-20 13:34:08 +00:00
|
|
|
}
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_pin_toggle_obj, machine_pin_toggle);
|
|
|
|
|
2022-04-30 03:45:43 +01:00
|
|
|
STATIC machine_pin_irq_obj_t *machine_pin_get_irq(mp_hal_pin_obj_t pin) {
|
2021-01-20 13:34:08 +00:00
|
|
|
// Get the IRQ object.
|
2022-04-30 03:45:43 +01:00
|
|
|
machine_pin_irq_obj_t *irq = MP_STATE_PORT(machine_pin_irq_obj[pin]);
|
2021-01-20 13:34:08 +00:00
|
|
|
|
|
|
|
// Allocate the IRQ object if it doesn't already exist.
|
|
|
|
if (irq == NULL) {
|
|
|
|
irq = m_new_obj(machine_pin_irq_obj_t);
|
|
|
|
irq->base.base.type = &mp_irq_type;
|
|
|
|
irq->base.methods = (mp_irq_methods_t *)&machine_pin_irq_methods;
|
2023-01-22 16:49:10 +00:00
|
|
|
irq->base.parent = MP_OBJ_FROM_PTR(machine_pin_cpu_pins[pin]);
|
2021-01-20 13:34:08 +00:00
|
|
|
irq->base.handler = mp_const_none;
|
|
|
|
irq->base.ishard = false;
|
2022-04-30 03:45:43 +01:00
|
|
|
MP_STATE_PORT(machine_pin_irq_obj[pin]) = irq;
|
2021-01-20 13:34:08 +00:00
|
|
|
}
|
2022-04-30 03:45:43 +01:00
|
|
|
return irq;
|
|
|
|
}
|
2021-01-20 13:34:08 +00:00
|
|
|
|
2022-04-30 03:45:43 +01:00
|
|
|
void mp_hal_pin_interrupt(mp_hal_pin_obj_t pin, mp_obj_t handler, mp_uint_t trigger, bool hard) {
|
|
|
|
machine_pin_irq_obj_t *irq = machine_pin_get_irq(pin);
|
2021-01-20 13:34:08 +00:00
|
|
|
|
2022-04-30 03:45:43 +01:00
|
|
|
// Disable all IRQs while data is updated.
|
|
|
|
gpio_set_irq_enabled(pin, GPIO_IRQ_ALL, false);
|
2021-01-20 13:34:08 +00:00
|
|
|
|
2022-04-30 03:45:43 +01:00
|
|
|
// Update IRQ data.
|
|
|
|
irq->base.handler = handler;
|
|
|
|
irq->base.ishard = hard;
|
|
|
|
irq->flags = 0;
|
|
|
|
irq->trigger = trigger;
|
|
|
|
|
|
|
|
// Enable IRQ if a handler is given.
|
|
|
|
if (handler != mp_const_none && trigger != MP_HAL_PIN_TRIGGER_NONE) {
|
|
|
|
gpio_set_irq_enabled(pin, trigger, true);
|
2021-01-20 13:34:08 +00:00
|
|
|
}
|
2022-04-30 03:45:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// pin.irq(handler=None, trigger=IRQ_FALLING|IRQ_RISING, hard=False)
|
|
|
|
STATIC mp_obj_t machine_pin_irq(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
|
|
|
enum { ARG_handler, ARG_trigger, ARG_hard };
|
|
|
|
static const mp_arg_t allowed_args[] = {
|
|
|
|
{ MP_QSTR_handler, MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
|
|
|
|
{ MP_QSTR_trigger, MP_ARG_INT, {.u_int = MP_HAL_PIN_TRIGGER_FALL | MP_HAL_PIN_TRIGGER_RISE} },
|
|
|
|
{ MP_QSTR_hard, MP_ARG_BOOL, {.u_bool = false} },
|
|
|
|
};
|
|
|
|
machine_pin_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
|
2022-10-25 17:43:38 +01:00
|
|
|
if (is_ext_pin(self)) {
|
2022-06-30 07:01:02 +01:00
|
|
|
mp_raise_ValueError(MP_ERROR_TEXT("expecting a regular GPIO Pin"));
|
|
|
|
}
|
|
|
|
|
2022-04-30 03:45:43 +01:00
|
|
|
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
|
|
|
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
2021-01-20 13:34:08 +00:00
|
|
|
|
2022-04-30 03:45:43 +01:00
|
|
|
machine_pin_irq_obj_t *irq = machine_pin_get_irq(self->id);
|
|
|
|
|
|
|
|
if (n_args > 1 || kw_args->used != 0) {
|
|
|
|
// Update IRQ data.
|
|
|
|
mp_obj_t handler = args[ARG_handler].u_obj;
|
|
|
|
mp_uint_t trigger = args[ARG_trigger].u_int;
|
|
|
|
bool hard = args[ARG_hard].u_bool;
|
|
|
|
mp_hal_pin_interrupt(self->id, handler, trigger, hard);
|
|
|
|
}
|
2021-01-20 13:34:08 +00:00
|
|
|
return MP_OBJ_FROM_PTR(irq);
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(machine_pin_irq_obj, 1, machine_pin_irq);
|
|
|
|
|
|
|
|
STATIC const mp_rom_map_elem_t machine_pin_locals_dict_table[] = {
|
|
|
|
// instance methods
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&machine_pin_init_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_value), MP_ROM_PTR(&machine_pin_value_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_low), MP_ROM_PTR(&machine_pin_low_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_high), MP_ROM_PTR(&machine_pin_high_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_off), MP_ROM_PTR(&machine_pin_low_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_on), MP_ROM_PTR(&machine_pin_high_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_toggle), MP_ROM_PTR(&machine_pin_toggle_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_irq), MP_ROM_PTR(&machine_pin_irq_obj) },
|
|
|
|
|
2022-10-25 19:45:00 +01:00
|
|
|
// class attributes
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_board), MP_ROM_PTR(&pin_board_pins_obj_type) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_cpu), MP_ROM_PTR(&pin_cpu_pins_obj_type) },
|
|
|
|
|
2021-01-20 13:34:08 +00:00
|
|
|
// class constants
|
2022-10-25 17:43:38 +01:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_IN), MP_ROM_INT(MACHINE_PIN_MODE_IN) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_OUT), MP_ROM_INT(MACHINE_PIN_MODE_OUT) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_OPEN_DRAIN), MP_ROM_INT(MACHINE_PIN_MODE_OPEN_DRAIN) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_ALT), MP_ROM_INT(MACHINE_PIN_MODE_ALT) },
|
2021-01-20 13:34:08 +00:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_PULL_UP), MP_ROM_INT(GPIO_PULL_UP) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_PULL_DOWN), MP_ROM_INT(GPIO_PULL_DOWN) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_IRQ_RISING), MP_ROM_INT(GPIO_IRQ_EDGE_RISE) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_IRQ_FALLING), MP_ROM_INT(GPIO_IRQ_EDGE_FALL) },
|
2022-10-25 19:45:00 +01:00
|
|
|
|
|
|
|
// Pins alternate functions
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_ALT_SPI), MP_ROM_INT(GPIO_FUNC_SPI) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_ALT_UART), MP_ROM_INT(GPIO_FUNC_UART) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_ALT_I2C), MP_ROM_INT(GPIO_FUNC_I2C) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_ALT_PWM), MP_ROM_INT(GPIO_FUNC_PWM) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_ALT_SIO), MP_ROM_INT(GPIO_FUNC_SIO) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_ALT_PIO0), MP_ROM_INT(GPIO_FUNC_PIO0) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_ALT_PIO1), MP_ROM_INT(GPIO_FUNC_PIO1) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_ALT_GPCK), MP_ROM_INT(GPIO_FUNC_GPCK) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_ALT_USB), MP_ROM_INT(GPIO_FUNC_USB) },
|
2021-01-20 13:34:08 +00:00
|
|
|
};
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(machine_pin_locals_dict, machine_pin_locals_dict_table);
|
|
|
|
|
|
|
|
STATIC mp_uint_t pin_ioctl(mp_obj_t self_in, mp_uint_t request, uintptr_t arg, int *errcode) {
|
|
|
|
(void)errcode;
|
|
|
|
machine_pin_obj_t *self = self_in;
|
|
|
|
|
|
|
|
switch (request) {
|
|
|
|
case MP_PIN_READ: {
|
2022-10-25 17:43:38 +01:00
|
|
|
if (is_ext_pin(self)) {
|
|
|
|
#if MICROPY_HW_PIN_EXT_COUNT
|
|
|
|
return machine_pin_ext_get(self);
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
return gpio_get(self->id);
|
|
|
|
}
|
2021-01-20 13:34:08 +00:00
|
|
|
}
|
|
|
|
case MP_PIN_WRITE: {
|
2022-10-25 17:43:38 +01:00
|
|
|
if (is_ext_pin(self)) {
|
|
|
|
#if MICROPY_HW_PIN_EXT_COUNT
|
|
|
|
machine_pin_ext_set(self, arg);
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
gpio_put(self->id, arg);
|
|
|
|
}
|
2021-01-20 13:34:08 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC const mp_pin_p_t pin_pin_p = {
|
|
|
|
.ioctl = pin_ioctl,
|
|
|
|
};
|
|
|
|
|
2021-07-14 05:38:38 +01:00
|
|
|
MP_DEFINE_CONST_OBJ_TYPE(
|
|
|
|
machine_pin_type,
|
|
|
|
MP_QSTR_Pin,
|
|
|
|
MP_TYPE_FLAG_NONE,
|
2022-09-16 15:31:23 +01:00
|
|
|
make_new, mp_pin_make_new,
|
2021-07-14 05:38:38 +01:00
|
|
|
print, machine_pin_print,
|
|
|
|
call, machine_pin_call,
|
|
|
|
protocol, &pin_pin_p,
|
2022-06-24 07:27:46 +01:00
|
|
|
locals_dict, &machine_pin_locals_dict
|
2021-07-14 05:38:38 +01:00
|
|
|
);
|
2021-01-20 13:34:08 +00:00
|
|
|
|
|
|
|
STATIC mp_uint_t machine_pin_irq_trigger(mp_obj_t self_in, mp_uint_t new_trigger) {
|
|
|
|
machine_pin_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
machine_pin_irq_obj_t *irq = MP_STATE_PORT(machine_pin_irq_obj[self->id]);
|
|
|
|
gpio_set_irq_enabled(self->id, GPIO_IRQ_ALL, false);
|
|
|
|
irq->flags = 0;
|
|
|
|
irq->trigger = new_trigger;
|
|
|
|
gpio_set_irq_enabled(self->id, new_trigger, true);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC mp_uint_t machine_pin_irq_info(mp_obj_t self_in, mp_uint_t info_type) {
|
|
|
|
machine_pin_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
machine_pin_irq_obj_t *irq = MP_STATE_PORT(machine_pin_irq_obj[self->id]);
|
|
|
|
if (info_type == MP_IRQ_INFO_FLAGS) {
|
|
|
|
return irq->flags;
|
|
|
|
} else if (info_type == MP_IRQ_INFO_TRIGGERS) {
|
|
|
|
return irq->trigger;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC const mp_irq_methods_t machine_pin_irq_methods = {
|
|
|
|
.trigger = machine_pin_irq_trigger,
|
|
|
|
.info = machine_pin_irq_info,
|
|
|
|
};
|
|
|
|
|
|
|
|
mp_hal_pin_obj_t mp_hal_get_pin_obj(mp_obj_t obj) {
|
|
|
|
if (!mp_obj_is_type(obj, &machine_pin_type)) {
|
|
|
|
mp_raise_ValueError(MP_ERROR_TEXT("expecting a Pin"));
|
|
|
|
}
|
|
|
|
machine_pin_obj_t *pin = MP_OBJ_TO_PTR(obj);
|
2022-10-25 17:43:38 +01:00
|
|
|
if (is_ext_pin(pin)) {
|
2022-06-30 07:01:02 +01:00
|
|
|
mp_raise_ValueError(MP_ERROR_TEXT("expecting a regular GPIO Pin"));
|
|
|
|
}
|
2021-01-20 13:34:08 +00:00
|
|
|
return pin->id;
|
|
|
|
}
|
2022-07-01 20:47:42 +01:00
|
|
|
|
|
|
|
MP_REGISTER_ROOT_POINTER(void *machine_pin_irq_obj[30]);
|