From ddae6607f9bf1d0fbb0509771ec241a5354e84de Mon Sep 17 00:00:00 2001 From: Gabriel Ozouf Date: Fri, 26 Feb 2021 10:53:50 +0100 Subject: [PATCH] [ion/device/n0110] Deduce Vbus config from PCB version Revised PCB (version number > 0) should configure Vbus as a GPIO instead of an alternate function. --- ion/src/device/n0100/Makefile | 1 + ion/src/device/n0100/drivers/usb.cpp | 18 ++++++++++++ ion/src/device/n0110/Makefile | 1 + ion/src/device/n0110/drivers/config/usb.h | 4 ++- ion/src/device/n0110/drivers/usb.cpp | 35 +++++++++++++++++++++++ ion/src/device/shared/drivers/usb.cpp | 22 ++++++-------- ion/src/device/shared/drivers/usb.h | 2 ++ ion/src/device/shared/usb/Makefile | 1 + 8 files changed, 69 insertions(+), 15 deletions(-) create mode 100644 ion/src/device/n0100/drivers/usb.cpp create mode 100644 ion/src/device/n0110/drivers/usb.cpp diff --git a/ion/src/device/n0100/Makefile b/ion/src/device/n0100/Makefile index 4111b2025..23f6aee1c 100644 --- a/ion/src/device/n0100/Makefile +++ b/ion/src/device/n0100/Makefile @@ -3,6 +3,7 @@ ion_device_src += $(addprefix ion/src/device/n0100/drivers/, \ led.cpp \ power.cpp \ reset.cpp \ + usb.cpp \ ) LDSCRIPT ?= ion/src/device/n0100/flash.ld diff --git a/ion/src/device/n0100/drivers/usb.cpp b/ion/src/device/n0100/drivers/usb.cpp new file mode 100644 index 000000000..5a1c7839e --- /dev/null +++ b/ion/src/device/n0100/drivers/usb.cpp @@ -0,0 +1,18 @@ +#include +#include + +namespace Ion { +namespace Device { +namespace USB { + +void initVbus() { + Config::VbusPin.init(); +} + +void shutdownVbus() { + Config::VbusPin.shutdown(); +} + +} +} +} diff --git a/ion/src/device/n0110/Makefile b/ion/src/device/n0110/Makefile index afdfcb7ce..a45b545d1 100644 --- a/ion/src/device/n0110/Makefile +++ b/ion/src/device/n0110/Makefile @@ -4,6 +4,7 @@ ion_device_src += $(addprefix ion/src/device/n0110/drivers/, \ led.cpp \ power.cpp \ reset.cpp \ + usb.cpp \ ) LDSCRIPT ?= ion/src/device/n0110/flash.ld diff --git a/ion/src/device/n0110/drivers/config/usb.h b/ion/src/device/n0110/drivers/config/usb.h index bbd3fa5a1..4ce6a6d68 100644 --- a/ion/src/device/n0110/drivers/config/usb.h +++ b/ion/src/device/n0110/drivers/config/usb.h @@ -10,7 +10,9 @@ namespace Config { using namespace Regs; -constexpr static AFGPIOPin VbusPin = AFGPIOPin(GPIOA, 9, GPIO::AFR::AlternateFunction::AF10, GPIO::PUPDR::Pull::None, GPIO::OSPEEDR::OutputSpeed::Fast); +constexpr static GPIOPin VbusPin = GPIOPin(GPIOA, 9); +constexpr static AFGPIOPin VbusAFPin = AFGPIOPin(GPIOA, 9, GPIO::AFR::AlternateFunction::AF10, GPIO::PUPDR::Pull::None, GPIO::OSPEEDR::OutputSpeed::Fast); + constexpr static AFGPIOPin DmPin = AFGPIOPin(GPIOA, 11, GPIO::AFR::AlternateFunction::AF10, GPIO::PUPDR::Pull::None, GPIO::OSPEEDR::OutputSpeed::Fast); constexpr static AFGPIOPin DpPin = AFGPIOPin(GPIOA, 12, GPIO::AFR::AlternateFunction::AF10, GPIO::PUPDR::Pull::None, GPIO::OSPEEDR::OutputSpeed::Fast); diff --git a/ion/src/device/n0110/drivers/usb.cpp b/ion/src/device/n0110/drivers/usb.cpp new file mode 100644 index 000000000..60031e4bf --- /dev/null +++ b/ion/src/device/n0110/drivers/usb.cpp @@ -0,0 +1,35 @@ +#include +#include +#include + +namespace Ion { +namespace Device { + +using namespace Regs; + +namespace USB { + +bool useAlternateFunctionVbus() { + return Board::readPCBVersion() == 0; +} + +void initVbus() { + if (useAlternateFunctionVbus()) { + Config::VbusAFPin.init(); + } else { + Config::VbusPin.group().MODER()->setMode(Config::VbusPin.pin(), GPIO::MODER::Mode::Input); + Config::VbusPin.group().PUPDR()->setPull(Config::VbusPin.pin(), GPIO::PUPDR::Pull::None); + } +} + +void shutdownVbus() { + if (useAlternateFunctionVbus()) { + Config::VbusAFPin.shutdown(); + } else { + Config::VbusPin.group().MODER()->setMode(Config::VbusPin.pin(), GPIO::MODER::Mode::Analog); + } +} + +} +} +} diff --git a/ion/src/device/shared/drivers/usb.cpp b/ion/src/device/shared/drivers/usb.cpp index ec79da42a..d7932b044 100644 --- a/ion/src/device/shared/drivers/usb.cpp +++ b/ion/src/device/shared/drivers/usb.cpp @@ -65,25 +65,19 @@ void initGPIO() { GPIOC.MODER()->setMode(11, GPIO::MODER::Mode::Output); GPIOC.ODR()->set(11, false); - /* Configure the GPIO - * The VBUS pin is connected to the USB VBUS port. To read if the USB is - * plugged, the pin must be pulled down. */ - // FIXME: Understand how the Vbus pin really works! -#if 0 - Config::VbusPin.group().MODER()->setMode(Config::VbusPin.pin(), GPIO::MODER::Mode::Input); - Config::VbusPin.group().PUPDR()->setPull(Config::VbusPin.pin(), GPIO::PUPDR::Pull::Down); -#else - Config::VbusPin.init(); -#endif + /* Configure the GPIO */ + /* The Vbus pin is connected to the USB Vbus port. Depending on the + * hardware, it should either be configured as an AlternateFunction, or as a + * floating Input. */ + initVbus(); Config::DmPin.init(); Config::DpPin.init(); } void shutdownGPIO() { - constexpr static AFGPIOPin Pins[] = {Config::DpPin, Config::DmPin, Config::VbusPin}; - for (const AFGPIOPin & p : Pins) { - p.shutdown(); - } + Config::DpPin.shutdown(); + Config::DmPin.shutdown(); + shutdownVbus(); } void initOTG() { diff --git a/ion/src/device/shared/drivers/usb.h b/ion/src/device/shared/drivers/usb.h index a53beb8a0..4dc590713 100644 --- a/ion/src/device/shared/drivers/usb.h +++ b/ion/src/device/shared/drivers/usb.h @@ -7,6 +7,8 @@ namespace USB { void init(); void shutdown(); +void initVbus(); +void shutdownVbus(); void initGPIO(); void shutdownGPIO(); void initOTG(); diff --git a/ion/src/device/shared/usb/Makefile b/ion/src/device/shared/usb/Makefile index 3db2b7d51..791592d5c 100644 --- a/ion/src/device/shared/usb/Makefile +++ b/ion/src/device/shared/usb/Makefile @@ -44,6 +44,7 @@ ion_device_dfu_src += ion/src/device/shared/usb/boot.cpp ion_device_dfu_src += ion/src/device/$(MODEL)/drivers/board.cpp ion_device_dfu_src += ion/src/device/$(MODEL)/drivers/cache.cpp ion_device_dfu_src += ion/src/device/$(MODEL)/drivers/reset.cpp +ion_device_dfu_src += ion/src/device/$(MODEL)/drivers/usb.cpp ion_device_dfu_src += $(addprefix ion/src/device/shared/drivers/, \ backlight.cpp \ battery.cpp \