cdcacm: move non-constant parts to platform.[hc]

This commit is contained in:
Uwe Bonnes 2013-01-13 19:15:16 +01:00
parent 533608a2f4
commit 04415582bd
6 changed files with 167 additions and 59 deletions

View File

@ -26,10 +26,9 @@
* The device's unique id is used as the USB serial number string.
*/
#include <libopencm3/stm32/f1/rcc.h>
#include <libopencm3/stm32/rcc.h>
#include <libopencm3/cm3/nvic.h>
#include <libopencm3/stm32/exti.h>
#include <libopencm3/stm32/f1/gpio.h>
#include <libopencm3/stm32/gpio.h>
#include <libopencm3/usb/usbd.h>
#include <libopencm3/usb/cdc.h>
#include <libopencm3/cm3/scb.h>
@ -37,8 +36,10 @@
#include <stdlib.h>
#include "platform.h"
#if defined(PLATFORM_HAS_TRACESWO)
#include "traceswo.h"
#include "usbuart.h"
#endif
#include <usbuart.h>
#define DFU_IF_NO 4
@ -310,6 +311,7 @@ static const struct usb_iface_assoc_descriptor dfu_assoc = {
.iFunction = 6,
};
#if defined(PLATFORM_HAS_TRACESWO)
static const struct usb_endpoint_descriptor trace_endp[] = {{
.bLength = USB_DT_ENDPOINT_SIZE,
.bDescriptorType = USB_DT_ENDPOINT,
@ -343,6 +345,7 @@ static const struct usb_iface_assoc_descriptor trace_assoc = {
.bFunctionProtocol = 0xFF,
.iFunction = 7,
};
#endif
static const struct usb_interface ifaces[] = {{
.num_altsetting = 1,
@ -362,17 +365,23 @@ static const struct usb_interface ifaces[] = {{
.num_altsetting = 1,
.iface_assoc = &dfu_assoc,
.altsetting = &dfu_iface,
#if defined(PLATFORM_HAS_TRACESWO)
}, {
.num_altsetting = 1,
.iface_assoc = &trace_assoc,
.altsetting = &trace_iface,
#endif
}};
static const struct usb_config_descriptor config = {
.bLength = USB_DT_CONFIGURATION_SIZE,
.bDescriptorType = USB_DT_CONFIGURATION,
.wTotalLength = 0,
#if defined(PLATFORM_HAS_TRACESWO)
.bNumInterfaces = 6,
#else
.bNumInterfaces = 5,
#endif
.bConfigurationValue = 1,
.iConfiguration = 0,
.bmAttributes = 0x80,
@ -390,7 +399,9 @@ static const char *usb_strings[] = {
"Black Magic GDB Server",
"Black Magic UART Port",
"Black Magic Firmware Upgrade",
#if defined(PLATFORM_HAS_TRACESWO)
"Black Magic Trace Capture",
#endif
};
static void dfu_detach_complete(usbd_device *dev, struct usb_setup_data *req)
@ -399,12 +410,10 @@ static void dfu_detach_complete(usbd_device *dev, struct usb_setup_data *req)
(void)req;
/* Disconnect USB cable */
gpio_set_mode(USB_PU_PORT, GPIO_MODE_INPUT, 0, USB_PU_PIN);
disconnect_usb();
/* Assert boot-request pin */
gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_2_MHZ,
GPIO_CNF_OUTPUT_PUSHPULL, GPIO12);
gpio_clear(GPIOB, GPIO12);
assert_boot_pin();
/* Reset core to enter bootloader */
scb_reset_core();
@ -490,9 +499,11 @@ static void cdcacm_set_config(usbd_device *dev, u16 wValue)
CDCACM_PACKET_SIZE, usbuart_usb_in_cb);
usbd_ep_setup(dev, 0x84, USB_ENDPOINT_ATTR_INTERRUPT, 16, NULL);
#if defined(PLATFORM_HAS_TRACESWO)
/* Trace interface */
usbd_ep_setup(dev, 0x85, USB_ENDPOINT_ATTR_BULK,
64, trace_buf_drain);
#endif
usbd_register_control_callback(dev,
USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE,
@ -526,50 +537,20 @@ void cdcacm_init(void)
get_dev_unique_id(serial_no);
usbdev = usbd_init(&stm32f103_usb_driver,
&dev, &config, usb_strings, 7);
usbdev = usbd_init(&USB_DRIVER, &dev, &config, usb_strings, sizeof(usb_strings)/sizeof(char *));
usbd_set_control_buffer_size(usbdev, sizeof(usbd_control_buffer));
usbd_register_set_config_callback(usbdev, cdcacm_set_config);
nvic_set_priority(NVIC_USB_LP_CAN_RX0_IRQ, IRQ_PRI_USB);
nvic_enable_irq(NVIC_USB_LP_CAN_RX0_IRQ);
nvic_set_priority(USB_VBUS_IRQ, IRQ_PRI_USB_VBUS);
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_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();
nvic_set_priority(USB_IRQ, IRQ_PRI_USB);
nvic_enable_irq(USB_IRQ);
setup_vbus_irq();
}
void usb_lp_can_rx0_isr(void)
void USB_ISR(void)
{
usbd_poll(usbdev);
}
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;

View File

@ -26,6 +26,7 @@
#include <libopencm3/cm3/systick.h>
#include <libopencm3/cm3/scb.h>
#include <libopencm3/cm3/nvic.h>
#include <libopencm3/stm32/exti.h>
#include <libopencm3/stm32/usart.h>
#include <libopencm3/usb/usbd.h>
#include <libopencm3/stm32/f1/adc.h>
@ -262,3 +263,44 @@ const char *platform_target_voltage(void)
return ret;
}
void assert_boot_pin(void)
{
gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_2_MHZ,
GPIO_CNF_OUTPUT_PUSHPULL, GPIO12);
gpio_clear(GPIOB, GPIO12);
}
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);
}
void setup_vbus_irq(void)
{
nvic_set_priority(USB_VBUS_IRQ, IRQ_PRI_USB_VBUS);
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_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();
}

View File

@ -95,6 +95,8 @@ extern usbd_device *usbdev;
#define LED_IDLE_RUN GPIO10
#define LED_ERROR GPIO11
#define USB_IRQ NVIC_USB_LP_CAN_RX0_IRQ
#define USB_ISR usb_lp_can_rx0_isr
/* Interrupt priorities. Low numbers are high priority.
* For now USART1 preempts USB which may spin while buffer is drained.
* TIM3 is used for traceswo capture and must be highest priority.
@ -184,3 +186,7 @@ static inline u16 _gpio_get(u32 gpioport, u16 gpios)
#endif
#endif
#define disconnect_usb() gpio_set_mode(USB_PU_PORT, GPIO_MODE_INPUT, 0, USB_PU_PIN);
void assert_boot_pin(void);
void setup_vbus_irq(void);

View File

@ -28,7 +28,6 @@
#include <libopencm3/stm32/f1/rcc.h>
#include <libopencm3/cm3/nvic.h>
#include <libopencm3/stm32/exti.h>
#include <libopencm3/stm32/f1/gpio.h>
#include <libopencm3/usb/usbd.h>
#include <libopencm3/usb/cdc.h>
@ -37,6 +36,9 @@
#include <stdlib.h>
#include "platform.h"
#if defined(PLATFORM_HAS_TRACESWO)
#include "traceswo.h"
#endif
#include <usbuart.h>
#define DFU_IF_NO 4
@ -309,6 +311,42 @@ static const struct usb_iface_assoc_descriptor dfu_assoc = {
.iFunction = 6,
};
#if defined(PLATFORM_HAS_TRACESWO)
static const struct usb_endpoint_descriptor trace_endp[] = {{
.bLength = USB_DT_ENDPOINT_SIZE,
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = 0x85,
.bmAttributes = USB_ENDPOINT_ATTR_BULK,
.wMaxPacketSize = 64,
.bInterval = 0,
}};
const struct usb_interface_descriptor trace_iface = {
.bLength = USB_DT_INTERFACE_SIZE,
.bDescriptorType = USB_DT_INTERFACE,
.bInterfaceNumber = 5,
.bAlternateSetting = 0,
.bNumEndpoints = 1,
.bInterfaceClass = 0xFF,
.bInterfaceSubClass = 0xFF,
.bInterfaceProtocol = 0xFF,
.iInterface = 7,
.endpoint = trace_endp,
};
static const struct usb_iface_assoc_descriptor trace_assoc = {
.bLength = USB_DT_INTERFACE_ASSOCIATION_SIZE,
.bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
.bFirstInterface = 5,
.bInterfaceCount = 1,
.bFunctionClass = 0xFF,
.bFunctionSubClass = 0xFF,
.bFunctionProtocol = 0xFF,
.iFunction = 7,
};
#endif
static const struct usb_interface ifaces[] = {{
.num_altsetting = 1,
.iface_assoc = &gdb_assoc,
@ -327,13 +365,23 @@ static const struct usb_interface ifaces[] = {{
.num_altsetting = 1,
.iface_assoc = &dfu_assoc,
.altsetting = &dfu_iface,
#if defined(PLATFORM_HAS_TRACESWO)
}, {
.num_altsetting = 1,
.iface_assoc = &trace_assoc,
.altsetting = &trace_iface,
#endif
}};
static const struct usb_config_descriptor config = {
.bLength = USB_DT_CONFIGURATION_SIZE,
.bDescriptorType = USB_DT_CONFIGURATION,
.wTotalLength = 0,
#if defined(PLATFORM_HAS_TRACESWO)
.bNumInterfaces = 6,
#else
.bNumInterfaces = 5,
#endif
.bConfigurationValue = 1,
.iConfiguration = 0,
.bmAttributes = 0x80,
@ -351,6 +399,9 @@ static const char *usb_strings[] = {
"Black Magic GDB Server",
"Black Magic UART Port",
"Black Magic Firmware Upgrade",
#if defined(PLATFORM_HAS_TRACESWO)
"Black Magic Trace Capture",
#endif
};
static void dfu_detach_complete(usbd_device *dev, struct usb_setup_data *req)
@ -358,20 +409,11 @@ static void dfu_detach_complete(usbd_device *dev, struct usb_setup_data *req)
(void)dev;
(void)req;
/* Disconnect USB cable by resetting USB Device and pulling USB_DP low*/
rcc_peripheral_reset(&RCC_APB1RSTR, RCC_APB1ENR_USBEN);
rcc_peripheral_clear_reset(&RCC_APB1RSTR, RCC_APB1ENR_USBEN);
rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_USBEN);
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN);
gpio_clear(GPIOA, GPIO12);
gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ,
GPIO_CNF_OUTPUT_OPENDRAIN, GPIO12);
/* Disconnect USB cable */
disconnect_usb();
/* Assert boot-request pin */
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN);
gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_2_MHZ,
GPIO_CNF_OUTPUT_PUSHPULL, GPIO13);
gpio_set(GPIOC, GPIO13);
assert_boot_pin();
/* Reset core to enter bootloader */
scb_reset_core();
@ -457,6 +499,12 @@ static void cdcacm_set_config(usbd_device *dev, u16 wValue)
CDCACM_PACKET_SIZE, usbuart_usb_in_cb);
usbd_ep_setup(dev, 0x84, USB_ENDPOINT_ATTR_INTERRUPT, 16, NULL);
#if defined(PLATFORM_HAS_TRACESWO)
/* Trace interface */
usbd_ep_setup(dev, 0x85, USB_ENDPOINT_ATTR_BULK,
64, trace_buf_drain);
#endif
usbd_register_control_callback(dev,
USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE,
USB_REQ_TYPE_TYPE | USB_REQ_TYPE_RECIPIENT,
@ -490,12 +538,18 @@ void cdcacm_init(void)
get_dev_unique_id(serial_no);
usbdev = usbd_init(&stm32f103_usb_driver,
&dev, &config, usb_strings, 6);
#if defined(PLATFORM_HAS_TRACESWO)
&dev, &config, usb_strings, 6
#else
&dev, &config, usb_strings, 7
#endif
);
usbd_set_control_buffer_size(usbdev, sizeof(usbd_control_buffer));
usbd_register_set_config_callback(usbdev, cdcacm_set_config);
nvic_set_priority(NVIC_USB_LP_CAN_RX0_IRQ, IRQ_PRI_USB);
nvic_enable_irq(NVIC_USB_LP_CAN_RX0_IRQ);
nvic_set_priority(USB_IRQ, IRQ_PRI_USB);
nvic_enable_irq(USB_IRQ);
setup_vbus_irq();
}
void usb_lp_can_rx0_isr(void)

View File

@ -108,3 +108,24 @@ const char *platform_target_voltage(void)
{
return "unknown";
}
void disconnect_usb(void)
{
/* Disconnect USB cable by resetting USB Device and pulling USB_DP low*/
rcc_peripheral_reset(&RCC_APB1RSTR, RCC_APB1ENR_USBEN);
rcc_peripheral_clear_reset(&RCC_APB1RSTR, RCC_APB1ENR_USBEN);
rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_USBEN);
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN);
gpio_clear(GPIOA, GPIO12);
gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ,
GPIO_CNF_OUTPUT_OPENDRAIN, GPIO12);
}
void assert_boot_pin(void)
{
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN);
gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_2_MHZ,
GPIO_CNF_OUTPUT_PUSHPULL, GPIO13);
gpio_set(GPIOC, GPIO13);
}
void setup_vbus_irq(void){};

View File

@ -81,6 +81,8 @@ extern usbd_device *usbdev;
#define LED_UART GPIO14
#define LED_IDLE_RUN GPIO8
#define USB_IRQ NVIC_USB_LP_CAN_RX0_IRQ
#define USB_ISR usb_lp_can_rx0_isr
/* Interrupt priorities. Low numbers are high priority.
* For now USART2 preempts USB which may spin while buffer is drained.
* TIM3 is used for traceswo capture and must be highest priority.
@ -168,3 +170,5 @@ static inline u16 _gpio_get(u32 gpioport, u16 gpios)
#endif
void disconnect_usb(void);
void assert_boot_pin(void);