aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGareth McMullin2012-03-24 19:25:16 +1300
committerGareth McMullin2012-03-24 19:25:16 +1300
commitc0493ee0e8a8569ee5857ab6430dee52e91e9022 (patch)
tree62212ce72c6116c9413e769965988f649cc6e660
parentebf15cf5e2755ee2de3462876de8505ac3f5386c (diff)
Monitor USB VBUS to control D+ pull-up resistor.
-rw-r--r--src/stm32/cdcacm.c34
-rw-r--r--src/stm32/platform.h10
2 files changed, 38 insertions, 6 deletions
diff --git a/src/stm32/cdcacm.c b/src/stm32/cdcacm.c
index 0ecb553..962bd06 100644
--- a/src/stm32/cdcacm.c
+++ b/src/stm32/cdcacm.c
@@ -28,6 +28,7 @@
#include <libopencm3/stm32/f1/rcc.h>
#include <libopencm3/stm32/nvic.h>
+#include <libopencm3/stm32/exti.h>
#include <libopencm3/stm32/f1/gpio.h>
#include <libopencm3/usb/usbd.h>
#include <libopencm3/usb/cdc.h>
@@ -436,8 +437,6 @@ static int cdcacm_control_request(struct usb_setup_data *req, uint8_t **buf,
#endif
case DFU_GETSTATUS:
if(req->wIndex == DFU_IF_NO) {
- u32 bwPollTimeout = 0; /* 24-bit integer in DFU class spec */
-
(*buf)[0] = DFU_STATUS_OK;
(*buf)[1] = 0;
(*buf)[2] = 0;
@@ -526,6 +525,8 @@ uint8_t usbd_control_buffer[256];
void cdcacm_init(void)
{
+ void exti15_10_isr(void);
+
get_dev_unique_id(serial_no);
usbd_init(&stm32f103_usb_driver, &dev, &config, usb_strings);
@@ -533,10 +534,20 @@ void cdcacm_init(void)
usbd_register_set_config_callback(cdcacm_set_config);
nvic_enable_irq(NVIC_USB_LP_CAN_RX0_IRQ);
+ nvic_enable_irq(USB_VBUS_IRQ);
+ gpio_set(USB_VBUS_PORT, USB_VBUS_PIN);
gpio_set(USB_PU_PORT, USB_PU_PIN);
- gpio_set_mode(USB_PU_PORT, GPIO_MODE_OUTPUT_10_MHZ,
- GPIO_CNF_OUTPUT_PUSHPULL, USB_PU_PIN);
+
+ gpio_set_mode(USB_VBUS_PORT, GPIO_MODE_INPUT,
+ GPIO_CNF_INPUT_PULL_UPDOWN, USB_VBUS_PIN);
+
+ /* Configure EXTI for USB VBUS monitor */
+ exti_select_source(USB_VBUS_PIN, USB_VBUS_PORT);
+ exti_set_trigger(USB_VBUS_PIN, EXTI_TRIGGER_BOTH);
+ exti_enable_request(USB_VBUS_PIN);
+
+ exti15_10_isr();
}
void usb_lp_can_rx0_isr(void)
@@ -544,6 +555,21 @@ void usb_lp_can_rx0_isr(void)
usbd_poll();
}
+void exti15_10_isr(void)
+{
+ if (gpio_get(USB_VBUS_PORT, USB_VBUS_PIN)) {
+ /* Drive pull-up high if VBUS connected */
+ gpio_set_mode(USB_PU_PORT, GPIO_MODE_OUTPUT_10_MHZ,
+ GPIO_CNF_OUTPUT_PUSHPULL, USB_PU_PIN);
+ } else {
+ /* Allow pull-up to float if VBUS disconnected */
+ gpio_set_mode(USB_PU_PORT, GPIO_MODE_INPUT,
+ GPIO_CNF_INPUT_FLOAT, USB_PU_PIN);
+ }
+
+ exti_reset_request(USB_VBUS_PIN);
+}
+
static char *get_dev_unique_id(char *s)
{
volatile uint32_t *unique_id_p = (volatile uint32_t *)0x1FFFF7E8;
diff --git a/src/stm32/platform.h b/src/stm32/platform.h
index 85ade4f..075717c 100644
--- a/src/stm32/platform.h
+++ b/src/stm32/platform.h
@@ -41,7 +41,7 @@
* LED1 = PB10 (Yellow LED : Idle)
* LED2 = PB11 (Red LED : Error)
*
- * TPWR = RB0 (input)
+ * TPWR = RB0 (input) -- analogue on mini design ADC1, ch8
* nTRST = PB1
* SRST_OUT = PA2
* TDI = PA3
@@ -50,7 +50,9 @@
* TDO = PA6 (input)
* nSRST = PA7 (input)
*
- * USB cable pull-up: PA8 // was PA10 on prototype
+ * USB cable pull-up: PA8
+ * USB VBUS detect: PB13 -- New on mini design.
+ * Enable pull up for compatibility.
* Force DFU mode button: PB12
*/
@@ -68,6 +70,10 @@
#define USB_PU_PORT GPIOA
#define USB_PU_PIN GPIO8
+#define USB_VBUS_PORT GPIOB
+#define USB_VBUS_PIN GPIO13
+#define USB_VBUS_IRQ NVIC_EXTI15_10_IRQ
+
#define LED_PORT GPIOB
#define LED_RUN GPIO2
#define LED_IDLE GPIO10