[ion] Add a method to know if the USB is plugged

Change-Id: I040ce7996bbec121c71d57a5092ffae4f5f76025
This commit is contained in:
Émilie Feral
2017-04-07 11:32:40 +02:00
parent 1056be4d4e
commit 3261701d2d
8 changed files with 82 additions and 0 deletions

View File

@@ -10,6 +10,7 @@
#include <ion/keyboard.h>
#include <ion/led.h>
#include <ion/power.h>
#include <ion/usb.h>
#include <stdint.h>
#include <string.h>

12
ion/include/ion/usb.h Normal file
View File

@@ -0,0 +1,12 @@
#ifndef ION_USB_H
#define ION_USB_H
namespace Ion {
namespace USB {
bool isPlugged();
}
}
#endif

View File

@@ -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

View File

@@ -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();

34
ion/src/device/usb.cpp Normal file
View File

@@ -0,0 +1,34 @@
#include <ion/usb.h>
#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);
}
}
}
}

24
ion/src/device/usb.h Normal file
View File

@@ -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

View File

@@ -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)

View File

@@ -0,0 +1,6 @@
#include <ion/usb.h>
bool Ion::USB::isPlugged() {
return false;
}