2016-12-06 13:31:38 -05:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2016 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.
|
|
|
|
*/
|
|
|
|
|
2020-08-24 10:00:08 -04:00
|
|
|
#include "py/runtime.h"
|
|
|
|
|
2016-12-06 13:31:38 -05:00
|
|
|
#include "shared-bindings/microcontroller/Pin.h"
|
2022-05-27 15:59:54 -04:00
|
|
|
#include "supervisor/shared/translate/translate.h"
|
2016-12-06 13:31:38 -05:00
|
|
|
|
2017-09-22 21:05:51 -04:00
|
|
|
#include "atmel_start_pins.h"
|
|
|
|
#include "hal/include/hal_gpio.h"
|
2017-05-12 15:55:31 -04:00
|
|
|
|
2018-06-15 19:16:21 -04:00
|
|
|
#include "samd/pins.h"
|
2017-04-12 18:24:50 -04:00
|
|
|
|
2017-05-12 15:55:31 -04:00
|
|
|
#ifdef SPEAKER_ENABLE_PIN
|
|
|
|
bool speaker_enable_in_use;
|
|
|
|
#endif
|
|
|
|
|
2018-10-19 21:46:22 -04:00
|
|
|
#define PORT_COUNT (PORT_BITS / 32 + 1)
|
|
|
|
|
2020-06-18 15:13:59 -04:00
|
|
|
#ifdef SAM_D5X_E5X
|
2019-03-14 14:11:35 -04:00
|
|
|
#define SWD_MUX GPIO_PIN_FUNCTION_H
|
|
|
|
#endif
|
|
|
|
#ifdef SAMD21
|
|
|
|
#define SWD_MUX GPIO_PIN_FUNCTION_G
|
|
|
|
#endif
|
|
|
|
|
2018-10-19 21:46:22 -04:00
|
|
|
STATIC uint32_t never_reset_pins[PORT_COUNT];
|
|
|
|
|
2017-05-12 15:55:31 -04:00
|
|
|
void reset_all_pins(void) {
|
2018-10-19 21:46:22 -04:00
|
|
|
uint32_t pin_mask[PORT_COUNT] = PORT_OUT_IMPLEMENTED;
|
2017-05-12 15:55:31 -04:00
|
|
|
|
2019-03-14 14:11:35 -04:00
|
|
|
// Do not full reset USB lines.
|
2021-10-04 04:57:39 -04:00
|
|
|
#if CIRCUITPY_USB
|
2019-03-14 14:11:35 -04:00
|
|
|
pin_mask[0] &= ~(PORT_PA24 | PORT_PA25);
|
2021-10-04 04:57:39 -04:00
|
|
|
#endif
|
2019-03-14 14:11:35 -04:00
|
|
|
|
|
|
|
// Do not reset SWD when a debugger is present.
|
|
|
|
if (DSU->STATUSB.bit.DBGPRES == 1) {
|
|
|
|
pin_mask[0] &= ~(PORT_PA30 | PORT_PA31);
|
|
|
|
}
|
2017-05-12 15:55:31 -04:00
|
|
|
|
2018-10-19 21:46:22 -04:00
|
|
|
for (uint32_t i = 0; i < PORT_COUNT; i++) {
|
|
|
|
pin_mask[i] &= ~never_reset_pins[i];
|
|
|
|
}
|
|
|
|
|
2021-07-15 19:20:04 -04:00
|
|
|
gpio_set_port_direction(GPIO_PORTA, pin_mask[0], GPIO_DIRECTION_OFF);
|
|
|
|
gpio_set_port_direction(GPIO_PORTB, pin_mask[1], GPIO_DIRECTION_OFF);
|
2017-09-22 21:05:51 -04:00
|
|
|
#if PORT_BITS > 64
|
2021-07-15 19:20:04 -04:00
|
|
|
gpio_set_port_direction(GPIO_PORTC, pin_mask[2], GPIO_DIRECTION_OFF);
|
2017-09-22 21:05:51 -04:00
|
|
|
#endif
|
|
|
|
#if PORT_BITS > 96
|
2021-07-15 19:20:04 -04:00
|
|
|
gpio_set_port_direction(GPIO_PORTD, pin_mask[3], GPIO_DIRECTION_OFF);
|
2017-09-22 21:05:51 -04:00
|
|
|
#endif
|
2017-05-12 15:55:31 -04:00
|
|
|
|
2019-03-14 14:11:35 -04:00
|
|
|
// Configure SWD. SWDIO will be automatically switched on PA31 when a signal is input on
|
|
|
|
// SWCLK.
|
2020-06-18 15:13:59 -04:00
|
|
|
#ifdef SAM_D5X_E5X
|
2017-11-02 17:00:38 -04:00
|
|
|
gpio_set_pin_function(PIN_PA30, MUX_PA30H_CM4_SWCLK);
|
2017-09-22 21:05:51 -04:00
|
|
|
#endif
|
|
|
|
#ifdef SAMD21
|
2019-03-14 14:11:35 -04:00
|
|
|
gpio_set_pin_function(PIN_PA30, GPIO_PIN_FUNCTION_G);
|
|
|
|
gpio_set_pin_function(PIN_PA31, GPIO_PIN_FUNCTION_G);
|
2017-09-22 21:05:51 -04:00
|
|
|
#endif
|
2017-05-12 15:55:31 -04:00
|
|
|
|
|
|
|
// After configuring SWD because it may be shared.
|
|
|
|
#ifdef SPEAKER_ENABLE_PIN
|
|
|
|
speaker_enable_in_use = false;
|
2018-07-31 17:01:01 -04:00
|
|
|
gpio_set_pin_function(SPEAKER_ENABLE_PIN->number, GPIO_PIN_FUNCTION_OFF);
|
|
|
|
gpio_set_pin_direction(SPEAKER_ENABLE_PIN->number, GPIO_DIRECTION_OUT);
|
|
|
|
gpio_set_pin_level(SPEAKER_ENABLE_PIN->number, false);
|
2017-05-12 15:55:31 -04:00
|
|
|
#endif
|
|
|
|
}
|
2017-04-12 18:24:50 -04:00
|
|
|
|
2018-10-19 21:46:22 -04:00
|
|
|
void never_reset_pin_number(uint8_t pin_number) {
|
2020-09-05 12:42:06 -04:00
|
|
|
if (pin_number >= PORT_BITS) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-10-19 21:46:22 -04:00
|
|
|
never_reset_pins[GPIO_PORT(pin_number)] |= 1 << GPIO_PIN(pin_number);
|
|
|
|
}
|
|
|
|
|
2018-08-31 17:46:03 -04:00
|
|
|
void reset_pin_number(uint8_t pin_number) {
|
|
|
|
if (pin_number >= PORT_BITS) {
|
2017-04-12 18:24:50 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-16 12:31:07 -04:00
|
|
|
never_reset_pins[GPIO_PORT(pin_number)] &= ~(1 << GPIO_PIN(pin_number));
|
|
|
|
|
2018-08-31 17:46:03 -04:00
|
|
|
if (pin_number == PIN_PA30
|
2020-06-18 15:13:59 -04:00
|
|
|
#ifdef SAM_D5X_E5X
|
2017-11-02 17:00:38 -04:00
|
|
|
) {
|
2017-09-22 21:05:51 -04:00
|
|
|
#endif
|
|
|
|
#ifdef SAMD21
|
2018-08-31 17:46:03 -04:00
|
|
|
|| pin_number == PIN_PA31) {
|
2017-09-22 21:05:51 -04:00
|
|
|
#endif
|
2019-03-14 14:11:35 -04:00
|
|
|
gpio_set_pin_function(pin_number, SWD_MUX);
|
2017-05-12 15:55:31 -04:00
|
|
|
} else {
|
2018-08-31 17:46:03 -04:00
|
|
|
gpio_set_pin_direction(pin_number, GPIO_DIRECTION_OFF);
|
|
|
|
gpio_set_pin_function(pin_number, GPIO_PIN_FUNCTION_OFF);
|
2017-05-12 15:55:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef SPEAKER_ENABLE_PIN
|
2018-08-31 17:46:03 -04:00
|
|
|
if (pin_number == SPEAKER_ENABLE_PIN->number) {
|
2017-05-12 15:55:31 -04:00
|
|
|
speaker_enable_in_use = false;
|
2018-08-31 17:46:03 -04:00
|
|
|
gpio_set_pin_function(pin_number, GPIO_PIN_FUNCTION_OFF);
|
2018-07-31 17:01:01 -04:00
|
|
|
gpio_set_pin_direction(SPEAKER_ENABLE_PIN->number, GPIO_DIRECTION_OUT);
|
|
|
|
gpio_set_pin_level(SPEAKER_ENABLE_PIN->number, false);
|
2017-05-12 15:55:31 -04:00
|
|
|
}
|
|
|
|
#endif
|
2017-04-12 18:24:50 -04:00
|
|
|
}
|
|
|
|
|
2019-11-15 12:47:00 -05:00
|
|
|
void common_hal_never_reset_pin(const mcu_pin_obj_t* pin) {
|
|
|
|
never_reset_pin_number(pin->number);
|
|
|
|
}
|
|
|
|
|
|
|
|
void common_hal_reset_pin(const mcu_pin_obj_t* pin) {
|
2020-12-28 09:34:00 -05:00
|
|
|
if (pin == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
2019-11-15 12:47:00 -05:00
|
|
|
reset_pin_number(pin->number);
|
|
|
|
}
|
|
|
|
|
2017-04-12 18:24:50 -04:00
|
|
|
void claim_pin(const mcu_pin_obj_t* pin) {
|
2017-05-12 15:55:31 -04:00
|
|
|
#ifdef SPEAKER_ENABLE_PIN
|
|
|
|
if (pin == SPEAKER_ENABLE_PIN) {
|
|
|
|
speaker_enable_in_use = true;
|
|
|
|
}
|
|
|
|
#endif
|
2017-04-12 18:24:50 -04:00
|
|
|
}
|
|
|
|
|
2019-01-16 15:04:42 -05:00
|
|
|
bool pin_number_is_free(uint8_t pin_number) {
|
|
|
|
PortGroup *const port = &PORT->Group[(enum gpio_port)GPIO_PORT(pin_number)];
|
|
|
|
uint8_t pin_index = GPIO_PIN(pin_number);
|
|
|
|
volatile PORT_PINCFG_Type *state = &port->PINCFG[pin_index];
|
|
|
|
volatile PORT_PMUX_Type *pmux = &port->PMUX[pin_index / 2];
|
|
|
|
|
|
|
|
if (pin_number == PIN_PA30 || pin_number == PIN_PA31) {
|
2019-03-14 14:11:35 -04:00
|
|
|
if (DSU->STATUSB.bit.DBGPRES == 1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (pin_number == PIN_PA30
|
2020-06-18 15:13:59 -04:00
|
|
|
#ifdef SAM_D5X_E5X
|
2019-03-14 14:11:35 -04:00
|
|
|
) {
|
|
|
|
#endif
|
|
|
|
#ifdef SAMD21
|
|
|
|
|| pin_number == PIN_PA31) {
|
|
|
|
#endif) {
|
|
|
|
return state->bit.PMUXEN == 1 && ((pmux->reg >> (4 * pin_index % 2)) & 0xf) == SWD_MUX;
|
|
|
|
}
|
2019-01-16 15:04:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return state->bit.PMUXEN == 0 && state->bit.INEN == 0 &&
|
|
|
|
state->bit.PULLEN == 0 && (port->DIR.reg & (1 << pin_index)) == 0;
|
|
|
|
}
|
|
|
|
|
2016-12-06 13:31:38 -05:00
|
|
|
bool common_hal_mcu_pin_is_free(const mcu_pin_obj_t* pin) {
|
2017-05-12 15:55:31 -04:00
|
|
|
#ifdef SPEAKER_ENABLE_PIN
|
|
|
|
if (pin == SPEAKER_ENABLE_PIN) {
|
|
|
|
return !speaker_enable_in_use;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-01-16 15:04:42 -05:00
|
|
|
return pin_number_is_free(pin->number);
|
2016-12-06 13:31:38 -05:00
|
|
|
}
|
2020-03-10 14:12:01 -04:00
|
|
|
|
|
|
|
uint8_t common_hal_mcu_pin_number(const mcu_pin_obj_t* pin) {
|
|
|
|
return pin->number;
|
|
|
|
}
|
|
|
|
|
|
|
|
void common_hal_mcu_pin_claim(const mcu_pin_obj_t* pin) {
|
|
|
|
return claim_pin(pin);
|
|
|
|
}
|
|
|
|
|
|
|
|
void common_hal_mcu_pin_reset_number(uint8_t pin_no) {
|
|
|
|
reset_pin_number(pin_no);
|
|
|
|
}
|
2020-08-24 10:00:08 -04:00
|
|
|
|
|
|
|
mcu_pin_function_t *mcu_find_pin_function(mcu_pin_function_t *table, const mcu_pin_obj_t *pin, int instance, uint16_t name) {
|
|
|
|
if (!pin) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(; table->obj; table++) {
|
|
|
|
if (instance != -1 && instance != table->instance) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (pin == table->obj) {
|
|
|
|
return table;
|
|
|
|
}
|
|
|
|
}
|
2022-07-30 01:09:49 -04:00
|
|
|
mp_raise_ValueError_varg(translate("Invalid %q pin"), name);
|
2020-08-24 10:00:08 -04:00
|
|
|
}
|