diff --git a/ion/include/ion.h b/ion/include/ion.h index f7f16180e..74050fbdc 100644 --- a/ion/include/ion.h +++ b/ion/include/ion.h @@ -10,6 +10,7 @@ #include #include #include +#include #include #include diff --git a/ion/include/ion/usb.h b/ion/include/ion/usb.h new file mode 100644 index 000000000..760d9cc63 --- /dev/null +++ b/ion/include/ion/usb.h @@ -0,0 +1,12 @@ +#ifndef ION_USB_H +#define ION_USB_H + +namespace Ion { +namespace USB { + +bool isPlugged(); + +} +} + +#endif diff --git a/ion/src/device/Makefile b/ion/src/device/Makefile index 86dcce250..37c40dc8e 100644 --- a/ion/src/device/Makefile +++ b/ion/src/device/Makefile @@ -17,6 +17,7 @@ objs += $(addprefix ion/src/device/, \ power.o\ sd_card.o\ swd.o \ + usb.o \ ) # When using the register.h C++ file in production mode, we expect the compiler diff --git a/ion/src/device/device.cpp b/ion/src/device/device.cpp index 8ebbffce0..6c0376648 100644 --- a/ion/src/device/device.cpp +++ b/ion/src/device/device.cpp @@ -12,6 +12,7 @@ extern "C" { #include "backlight.h" #include "console.h" #include "swd.h" +#include "usb.h" #define USE_SD_CARD 0 @@ -124,6 +125,7 @@ void initPeripherals() { Keyboard::Device::init(); LED::Device::init(); Battery::Device::init(); + USB::Device::init(); #if USE_SD_CARD SDCard::Device::init(); #endif @@ -137,6 +139,7 @@ void shutdownPeripherals() { #if USE_SD_CARD SDCard::Device::shutdown(); #endif + USB::Device::shutdown(); Battery::Device::shutdown(); LED::Device::shutdown(); Keyboard::Device::shutdown(); diff --git a/ion/src/device/usb.cpp b/ion/src/device/usb.cpp new file mode 100644 index 000000000..f525079df --- /dev/null +++ b/ion/src/device/usb.cpp @@ -0,0 +1,34 @@ +#include +#include "usb.h" +#include "regs/regs.h" + +namespace Ion { +namespace USB { + +bool isPlugged() { + return Device::VbusPin.group().IDR()->get(Device::VbusPin.pin()); +} + +} +} + +namespace Ion { +namespace USB { +namespace Device { + +void init() { + /* 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. */ + VbusPin.group().MODER()->setMode(VbusPin.pin(), GPIO::MODER::Mode::Input); + VbusPin.group().PUPDR()->setPull(VbusPin.pin(), GPIO::PUPDR::Pull::Down); +} + +void shutdown() { + VbusPin.group().MODER()->setMode(VbusPin.pin(), GPIO::MODER::Mode::Analog); + VbusPin.group().PUPDR()->setPull(VbusPin.pin(), GPIO::PUPDR::Pull::None); +} + +} +} +} diff --git a/ion/src/device/usb.h b/ion/src/device/usb.h new file mode 100644 index 000000000..4c1a03227 --- /dev/null +++ b/ion/src/device/usb.h @@ -0,0 +1,24 @@ +#ifndef ION_DEVICE_USB_H +#define ION_DEVICE_USB_H + +#include "regs/regs.h" + +namespace Ion { +namespace USB { +namespace Device { + +/* Pin | Role | Mode | Function + * -----+-------------------+-----------------------+---------- + * PA9 | VBUS | Input, pulled down | Low = unplugged, high = plugged + */ + +void init(); +void shutdown(); + +constexpr static GPIOPin VbusPin = GPIOPin(GPIOA, 9); + +} +} +} + +#endif diff --git a/ion/src/simulator/Makefile b/ion/src/simulator/Makefile index cbee8b017..04147327c 100644 --- a/ion/src/simulator/Makefile +++ b/ion/src/simulator/Makefile @@ -2,6 +2,7 @@ objs += $(addprefix ion/src/simulator/, \ battery.o\ init.o\ led.o\ + usb.o\ ) objs += $(addprefix ion/src/simulator/boot/, main.o) diff --git a/ion/src/simulator/usb.cpp b/ion/src/simulator/usb.cpp new file mode 100644 index 000000000..447cca33a --- /dev/null +++ b/ion/src/simulator/usb.cpp @@ -0,0 +1,6 @@ +#include + +bool Ion::USB::isPlugged() { + return false; +} +