Skip to content

Commit 6fd89b7

Browse files
committed
rp2040: allow board to customize pin reset state
.. and use it on usb host feather to set the reset state of the USB power pin to "output high", while leaving it possible to create a DigitalInOut of this pin from CircuitPython code.
1 parent e06b3da commit 6fd89b7

File tree

3 files changed

+31
-4
lines changed
  • ports/raspberrypi

3 files changed

+31
-4
lines changed

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: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ void never_reset_pin_number(uint8_t pin_number) {
7373
never_reset_pins |= 1 << pin_number;
7474
}
7575

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+
7681
void reset_pin_number(uint8_t pin_number) {
7782
if (pin_number >= NUM_BANK0_GPIOS) {
7883
return;
@@ -81,6 +86,11 @@ void reset_pin_number(uint8_t pin_number) {
8186
gpio_bank0_pin_claimed &= ~(1 << pin_number);
8287
never_reset_pins &= ~(1 << pin_number);
8388

89+
// Allow the board to override the reset state of any pin
90+
if (board_reset_pin_number(pin_number)) {
91+
return;
92+
}
93+
8494
// We are very aggressive in shutting down the pad fully. Both pulls are
8595
// disabled and both buffers are as well.
8696
gpio_init(pin_number);

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)