2021-01-20 19:47:18 -05:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2021 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 "py/runtime.h"
|
|
|
|
|
2021-01-25 17:23:57 -05:00
|
|
|
#include "common-hal/microcontroller/__init__.h"
|
2021-01-20 19:47:18 -05:00
|
|
|
#include "shared-bindings/microcontroller/Pin.h"
|
|
|
|
|
|
|
|
#include "src/rp2_common/hardware_gpio/include/hardware/gpio.h"
|
|
|
|
|
2022-08-25 15:01:14 -04:00
|
|
|
#if CIRCUITPY_CYW43
|
2022-09-28 15:17:27 -04:00
|
|
|
#include "bindings/cyw43/__init__.h"
|
2022-08-25 15:01:14 -04:00
|
|
|
#include "pico/cyw43_arch.h"
|
2021-01-20 19:47:18 -05:00
|
|
|
|
2022-08-25 15:01:14 -04:00
|
|
|
bool cyw_ever_init;
|
|
|
|
static uint32_t cyw_pin_claimed;
|
|
|
|
|
2022-09-28 15:17:27 -04:00
|
|
|
void reset_pin_number_cyw(uint8_t pin_no) {
|
|
|
|
cyw_pin_claimed &= ~(1 << pin_no);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
STATIC uint32_t never_reset_pins;
|
|
|
|
|
2021-01-20 19:47:18 -05:00
|
|
|
void reset_all_pins(void) {
|
2022-10-19 22:03:28 -04:00
|
|
|
for (size_t i = 0; i < NUM_BANK0_GPIOS; i++) {
|
2021-01-20 19:47:18 -05:00
|
|
|
if ((never_reset_pins & (1 << i)) != 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
reset_pin_number(i);
|
|
|
|
}
|
2022-08-25 15:01:14 -04:00
|
|
|
#if CIRCUITPY_CYW43
|
|
|
|
if (cyw_ever_init) {
|
2022-10-19 22:03:28 -04:00
|
|
|
// reset LED and SMPS_MODE to Low; don't touch VBUS_SENSE
|
|
|
|
// otherwise it is switched to output mode forever!
|
|
|
|
cyw43_arch_gpio_put(0, 0);
|
|
|
|
cyw43_arch_gpio_put(1, 0);
|
2022-08-25 15:01:14 -04:00
|
|
|
}
|
|
|
|
cyw_pin_claimed = 0;
|
|
|
|
#endif
|
2021-01-20 19:47:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void never_reset_pin_number(uint8_t pin_number) {
|
2022-10-19 22:03:28 -04:00
|
|
|
if (pin_number >= NUM_BANK0_GPIOS) {
|
2021-01-20 19:47:18 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
never_reset_pins |= 1 << pin_number;
|
|
|
|
}
|
|
|
|
|
|
|
|
void reset_pin_number(uint8_t pin_number) {
|
2022-10-19 22:03:28 -04:00
|
|
|
if (pin_number >= NUM_BANK0_GPIOS) {
|
2021-01-20 19:47:18 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
never_reset_pins &= ~(1 << pin_number);
|
|
|
|
|
|
|
|
// We are very aggressive in shutting down the pad fully. Both pulls are
|
|
|
|
// disabled and both buffers are as well.
|
|
|
|
gpio_init(pin_number);
|
|
|
|
hw_clear_bits(&padsbank0_hw->io[pin_number], PADS_BANK0_GPIO0_IE_BITS |
|
2021-03-15 09:57:36 -04:00
|
|
|
PADS_BANK0_GPIO0_PUE_BITS |
|
|
|
|
PADS_BANK0_GPIO0_PDE_BITS);
|
2021-01-20 19:47:18 -05:00
|
|
|
hw_set_bits(&padsbank0_hw->io[pin_number], PADS_BANK0_GPIO0_OD_BITS);
|
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
void common_hal_never_reset_pin(const mcu_pin_obj_t *pin) {
|
2021-01-20 19:47:18 -05:00
|
|
|
never_reset_pin_number(pin->number);
|
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
void common_hal_reset_pin(const mcu_pin_obj_t *pin) {
|
2022-08-25 15:01:14 -04:00
|
|
|
#if CIRCUITPY_CYW43
|
|
|
|
if (pin->base.type == &cyw43_pin_type) {
|
|
|
|
reset_pin_number_cyw(pin->number);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
2021-01-20 19:47:18 -05:00
|
|
|
reset_pin_number(pin->number);
|
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
void claim_pin(const mcu_pin_obj_t *pin) {
|
2022-08-25 15:01:14 -04:00
|
|
|
#if CIRCUITPY_CYW43
|
|
|
|
if (pin->base.type == &cyw43_pin_type) {
|
|
|
|
cyw_pin_claimed |= (1 << pin->number);
|
|
|
|
}
|
|
|
|
#endif
|
2021-04-16 19:18:01 -04:00
|
|
|
// Nothing to do because all changes will set the GPIO settings.
|
2021-01-20 19:47:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
bool pin_number_is_free(uint8_t pin_number) {
|
2022-10-19 22:03:28 -04:00
|
|
|
if (pin_number >= NUM_BANK0_GPIOS) {
|
2021-01-20 19:47:18 -05:00
|
|
|
return false;
|
|
|
|
}
|
2021-01-25 16:07:01 -05:00
|
|
|
|
2021-01-20 19:47:18 -05:00
|
|
|
uint32_t pad_state = padsbank0_hw->io[pin_number];
|
|
|
|
return (pad_state & PADS_BANK0_GPIO0_IE_BITS) == 0 &&
|
|
|
|
(pad_state & PADS_BANK0_GPIO0_OD_BITS) != 0;
|
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
bool common_hal_mcu_pin_is_free(const mcu_pin_obj_t *pin) {
|
2022-08-25 15:01:14 -04:00
|
|
|
#if CIRCUITPY_CYW43
|
|
|
|
if (pin->base.type == &cyw43_pin_type) {
|
|
|
|
return !(cyw_pin_claimed & (1 << pin->number));
|
|
|
|
}
|
|
|
|
#endif
|
2021-01-20 19:47:18 -05:00
|
|
|
return pin_number_is_free(pin->number);
|
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
uint8_t common_hal_mcu_pin_number(const mcu_pin_obj_t *pin) {
|
2021-01-20 19:47:18 -05:00
|
|
|
return pin->number;
|
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
void common_hal_mcu_pin_claim(const mcu_pin_obj_t *pin) {
|
2021-01-20 19:47:18 -05:00
|
|
|
return claim_pin(pin);
|
|
|
|
}
|
|
|
|
|
|
|
|
void common_hal_mcu_pin_reset_number(uint8_t pin_no) {
|
|
|
|
reset_pin_number(pin_no);
|
|
|
|
}
|