Skip to content

Commit 537775e

Browse files
authored
Merge pull request #8937 from jepler/usb5v-as-gpio
Allow 5V power to be controlled from user code on usb host feather
2 parents 696aa83 + 6fd89b7 commit 537775e

File tree

4 files changed

+44
-14
lines changed
  • ports
    • atmel-samd/common-hal/microcontroller
    • raspberrypi

4 files changed

+44
-14
lines changed

ports/atmel-samd/common-hal/microcontroller/Pin.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,12 +161,11 @@ bool pin_number_is_free(uint8_t pin_number) {
161161
return false;
162162
}
163163
if (pin_number == PIN_PA30
164-
#ifdef SAM_D5X_E5X
165-
) {
166-
#endif
167164
#ifdef SAMD21
168-
|| pin_number == PIN_PA31) {
169-
#endif) {
165+
|| pin_number == PIN_PA31
166+
#endif
167+
)
168+
{
170169
return state->bit.PMUXEN == 1 && ((pmux->reg >> (4 * pin_index % 2)) & 0xf) == SWD_MUX;
171170
}
172171
}

ports/raspberrypi/boards/adafruit_feather_rp2040_usb_host/board.c

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,26 @@
2828

2929
#include "shared-bindings/digitalio/DigitalInOut.h"
3030
#include "shared-bindings/usb_host/Port.h"
31+
#include "hardware/gpio.h"
3132

3233
// Use the MP_WEAK supervisor/shared/board.c versions of routines not defined here.
3334

3435
digitalio_digitalinout_obj_t _host_power;
3536

36-
void board_init(void) {
37-
common_hal_digitalio_digitalinout_construct(&_host_power, &pin_GPIO18);
38-
common_hal_digitalio_digitalinout_never_reset(&_host_power);
39-
common_hal_digitalio_digitalinout_switch_to_output(&_host_power, true, DRIVE_MODE_PUSH_PULL);
37+
bool board_reset_pin_number(uint8_t pin_number) {
38+
if (pin_number == 18) {
39+
// doing this (rather than gpio_init) in this specific order ensures no
40+
// glitch if pin was already configured as a high output. gpio_init() temporarily
41+
// configures the pin as an input, so the power enable value would potentially
42+
// glitch.
43+
gpio_put(pin_number, 1);
44+
gpio_set_dir(pin_number, GPIO_OUT);
45+
gpio_set_function(pin_number, GPIO_FUNC_SIO);
4046

47+
return true;
48+
}
49+
return false;
50+
}
51+
void board_init(void) {
4152
common_hal_usb_host_port_construct(&pin_GPIO16, &pin_GPIO17);
4253
}

ports/raspberrypi/common-hal/microcontroller/Pin.c

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131

3232
#include "src/rp2_common/hardware_gpio/include/hardware/gpio.h"
3333

34+
static uint32_t gpio_bank0_pin_claimed;
35+
3436
#if CIRCUITPY_CYW43
3537
#include "bindings/cyw43/__init__.h"
3638
#include "pico/cyw43_arch.h"
@@ -71,13 +73,24 @@ void never_reset_pin_number(uint8_t pin_number) {
7173
never_reset_pins |= 1 << pin_number;
7274
}
7375

76+
// By default, all pins get reset in the same way
77+
MP_WEAK bool board_reset_pin_number(uint8_t pin_number) {
78+
return false;
79+
}
80+
7481
void reset_pin_number(uint8_t pin_number) {
7582
if (pin_number >= NUM_BANK0_GPIOS) {
7683
return;
7784
}
7885

86+
gpio_bank0_pin_claimed &= ~(1 << pin_number);
7987
never_reset_pins &= ~(1 << pin_number);
8088

89+
// Allow the board to override the reset state of any pin
90+
if (board_reset_pin_number(pin_number)) {
91+
return;
92+
}
93+
8194
// We are very aggressive in shutting down the pad fully. Both pulls are
8295
// disabled and both buffers are as well.
8396
gpio_init(pin_number);
@@ -105,19 +118,20 @@ void claim_pin(const mcu_pin_obj_t *pin) {
105118
#if CIRCUITPY_CYW43
106119
if (pin->base.type == &cyw43_pin_type) {
107120
cyw_pin_claimed |= (1 << pin->number);
121+
return;
108122
}
109123
#endif
110-
// Nothing to do because all changes will set the GPIO settings.
124+
if (pin->number >= NUM_BANK0_GPIOS) {
125+
return;
126+
}
127+
gpio_bank0_pin_claimed |= (1 << pin->number);
111128
}
112129

113130
bool pin_number_is_free(uint8_t pin_number) {
114131
if (pin_number >= NUM_BANK0_GPIOS) {
115132
return false;
116133
}
117-
118-
uint32_t pad_state = padsbank0_hw->io[pin_number];
119-
return (pad_state & PADS_BANK0_GPIO0_IE_BITS) == 0 &&
120-
(pad_state & PADS_BANK0_GPIO0_OD_BITS) != 0;
134+
return !(gpio_bank0_pin_claimed & (1 << pin_number));
121135
}
122136

123137
bool common_hal_mcu_pin_is_free(const mcu_pin_obj_t *pin) {

ports/raspberrypi/common-hal/microcontroller/Pin.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@
3434

3535
#include "peripherals/pins.h"
3636

37+
// If a board needs a different reset state for one or more pins, implement
38+
// board_reset_pin_number so that it sets this state and returns `true` for those
39+
// pin numbers, `false` for others.
40+
// A default weak implementation always returns `false`.
41+
bool board_reset_pin_number(uint8_t pin_number);
42+
3743
void reset_all_pins(void);
3844
// reset_pin_number takes the pin number instead of the pointer so that objects don't
3945
// need to store a full pointer.

0 commit comments

Comments
 (0)