common: Moved the USB initialisation code into usb.c

This commit is contained in:
dragonmux 2022-08-10 07:02:54 +01:00 committed by Piotr Esden-Tempski
parent ada59291c2
commit 259077a87d
5 changed files with 38 additions and 36 deletions

View File

@ -45,24 +45,16 @@
# include "traceswo.h" # include "traceswo.h"
#endif #endif
#include "usbuart.h" #include "usbuart.h"
#include "serialno.h"
#include "version.h"
#include <libopencm3/cm3/nvic.h>
#include <libopencm3/usb/cdc.h> #include <libopencm3/usb/cdc.h>
#include <libopencm3/cm3/scb.h> #include <libopencm3/cm3/scb.h>
#include <libopencm3/usb/dfu.h>
#include "usb_descriptors.h"
usbd_device * usbdev;
static int configured; static int configured;
static int cdcacm_gdb_dtr = 1; static int cdcacm_gdb_dtr = 1;
static void cdcacm_set_modem_state(usbd_device *dev, int iface, bool dsr, bool dcd); static void cdcacm_set_modem_state(usbd_device *dev, int iface, bool dsr, bool dcd);
char serial_no[DFU_SERIAL_LENGTH];
static void dfu_detach_complete(usbd_device *dev, struct usb_setup_data *req) static void dfu_detach_complete(usbd_device *dev, struct usb_setup_data *req)
{ {
(void)dev; (void)dev;
@ -165,7 +157,7 @@ static void cdcacm_set_modem_state(usbd_device *dev, int iface, bool dsr, bool d
usbd_ep_write_packet(dev, 0x82 + iface, buf, 10); usbd_ep_write_packet(dev, 0x82 + iface, buf, 10);
} }
static void cdcacm_set_config(usbd_device *dev, uint16_t wValue) void cdcacm_set_config(usbd_device *dev, uint16_t wValue)
{ {
configured = wValue; configured = wValue;
@ -208,19 +200,3 @@ static void cdcacm_set_config(usbd_device *dev, uint16_t wValue)
cdcacm_set_modem_state(dev, GDB_IF_NO, true, true); cdcacm_set_modem_state(dev, GDB_IF_NO, true, true);
cdcacm_set_modem_state(dev, UART_IF_NO, true, true); cdcacm_set_modem_state(dev, UART_IF_NO, true, true);
} }
/* We need a special large control buffer for this device: */
static uint8_t usbd_control_buffer[256];
void blackmagic_usb_init(void)
{
serial_no_read(serial_no);
usbdev = usbd_init(&USB_DRIVER, &dev_desc, &config, usb_strings, sizeof(usb_strings) / sizeof(char *),
usbd_control_buffer, sizeof(usbd_control_buffer));
usbd_register_set_config_callback(usbdev, cdcacm_set_config);
nvic_set_priority(USB_IRQ, IRQ_PRI_USB);
nvic_enable_irq(USB_IRQ);
}

View File

@ -31,6 +31,7 @@
#include "usb.h" #include "usb.h"
void blackmagic_usb_init(void); void blackmagic_usb_init(void);
void cdcacm_set_config(usbd_device *dev, uint16_t wValue);
/* Returns current usb configuration, or 0 if not configured. */ /* Returns current usb configuration, or 0 if not configured. */
int cdcacm_get_config(void); int cdcacm_get_config(void);
int cdcacm_get_dtr(void); int cdcacm_get_dtr(void);

View File

@ -18,8 +18,32 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <libopencm3/cm3/nvic.h>
#include "general.h" #include "general.h"
#include "usb.h" #include "usb.h"
#include "usb_descriptors.h"
#include "cdcacm.h"
#include "serialno.h"
char serial_no[DFU_SERIAL_LENGTH];
usbd_device *usbdev = NULL;
/* We need a special large control buffer for this device: */
static uint8_t usbd_control_buffer[256];
void blackmagic_usb_init(void)
{
serial_no_read(serial_no);
usbdev = usbd_init(&USB_DRIVER, &dev_desc, &config, usb_strings, sizeof(usb_strings) / sizeof(char *),
usbd_control_buffer, sizeof(usbd_control_buffer));
usbd_register_set_config_callback(usbdev, cdcacm_set_config);
nvic_set_priority(USB_IRQ, IRQ_PRI_USB);
nvic_enable_irq(USB_IRQ);
}
void USB_ISR(void) void USB_ISR(void)
{ {

View File

@ -31,4 +31,14 @@ extern usbd_device *usbdev;
#define CDCACM_UART_ENDPOINT 3 #define CDCACM_UART_ENDPOINT 3
#define TRACE_ENDPOINT 5 #define TRACE_ENDPOINT 5
#define GDB_IF_NO 0
#define UART_IF_NO 2
#define DFU_IF_NO 4
#ifdef PLATFORM_HAS_TRACESWO
#define TRACE_IF_NO 5
#define TOTAL_INTERFACES 6
#else
#define TOTAL_INTERFACES 5
#endif
#endif /*USB_H*/ #endif /*USB_H*/

View File

@ -26,19 +26,10 @@
#include <libopencm3/usb/dfu.h> #include <libopencm3/usb/dfu.h>
#include "usb.h" #include "usb.h"
#include "version.h"
#define BOARD_IDENT "Black Magic Probe " PLATFORM_IDENT FIRMWARE_VERSION #define BOARD_IDENT "Black Magic Probe " PLATFORM_IDENT FIRMWARE_VERSION
#define GDB_IF_NO 0
#define UART_IF_NO 2
#define DFU_IF_NO 4
#ifdef PLATFORM_HAS_TRACESWO
#define TRACE_IF_NO 5
#define TOTAL_INTERFACES 6
#else
#define TOTAL_INTERFACES 5
#endif
extern char serial_no[DFU_SERIAL_LENGTH]; extern char serial_no[DFU_SERIAL_LENGTH];
/* Top-level device descriptor */ /* Top-level device descriptor */