26 #include <libopencmsis/core_cm3.h> 27 #include <libopencm3/cm3/scb.h> 28 #include <libopencm3/stm32/rcc.h> 29 #include <libopencm3/stm32/gpio.h> 30 #include <libopencm3/stm32/desig.h> 31 #include <libopencm3/usb/usbd.h> 32 #include <libopencm3/usb/dfu.h> 51 .bLength = USB_DT_DEVICE_SIZE,
52 .bDescriptorType = USB_DT_DEVICE,
57 .bMaxPacketSize0 = 64,
64 .bNumConfigurations = 1,
71 .bLength =
sizeof(
struct usb_dfu_descriptor),
72 .bDescriptorType = DFU_FUNCTIONAL,
73 .bmAttributes = USB_DFU_CAN_DOWNLOAD | USB_DFU_WILL_DETACH,
74 .wDetachTimeout = 200,
76 .bcdDFUVersion = 0x0110,
83 .bLength = USB_DT_INTERFACE_SIZE,
84 .bDescriptorType = USB_DT_INTERFACE,
85 .bInterfaceNumber = 0,
86 .bAlternateSetting = 0,
88 .bInterfaceClass = 0xFE,
89 .bInterfaceSubClass = 1,
90 .bInterfaceProtocol = 2,
106 .bLength = USB_DT_CONFIGURATION_SIZE,
107 .bDescriptorType = USB_DT_CONFIGURATION,
110 .bConfigurationValue = 1,
112 .bmAttributes = 0x80,
123 "BusVoodoo multi-protocol debugging adapter ",
124 "DFU bootloader (DFU mode)",
130 #if defined(MAPLE_MINI) 132 rcc_periph_clock_enable(RCC_GPIOB);
133 gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO9);
134 gpio_set(GPIOB, GPIO9);
135 for (uint32_t i = 0; i < 0x2000; i++) {
138 gpio_clear(GPIOB, GPIO9);
141 rcc_periph_clock_enable(RCC_GPIOA);
142 gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO12);
143 gpio_clear(GPIOA, GPIO12);
144 for (uint32_t i = 0; i < 0x2000; i++) {
155 static void usb_dfu_flash(usbd_device *usbd_dev,
struct usb_setup_data *req)
175 static void usb_dfu_reset(usbd_device *usbd_dev,
struct usb_setup_data *req)
193 static enum usbd_request_return_codes
usb_dfu_control_request(usbd_device *usbd_dev,
struct usb_setup_data *req, uint8_t **buf, uint16_t *len,
void (**complete)(usbd_device *usbd_dev,
struct usb_setup_data *req))
199 if ((req->bmRequestType & USB_REQ_TYPE_TYPE)!=USB_REQ_TYPE_CLASS) {
200 return USBD_REQ_NOTSUPP;
204 int to_return = USBD_REQ_HANDLED;
205 switch (req->bRequest) {
214 }
else if (STATE_DFU_IDLE==
usb_dfu_state && ((NULL==len) || (0 == *len))) {
218 }
else if (STATE_DFU_DNLOAD_IDLE==
usb_dfu_state && ((NULL==len) || (0 == *len))) {
230 }
else if ((uint32_t)&__application_end<FLASH_BASE && flash_pointer+*len>=(uint32_t)(FLASH_BASE+DESIG_FLASH_SIZE*1024)) {
246 to_return = USBD_REQ_NOTSUPP;
279 to_return = USBD_REQ_NOTSUPP;
289 rcc_periph_reset_pulse(RST_USB);
291 rcc_periph_clock_enable(RCC_GPIOA);
292 rcc_periph_clock_enable(RCC_USB);
static enum dfu_status usb_dfu_status
current DFU status
static const struct usb_config_descriptor usb_dfu_configuration
USB DFU configuration descriptor.
static uint32_t flash_pointer
where the downloaded data should be flashed
static const char * usb_dfu_strings[]
USB string table.
static usbd_device * usb_device
structure holding all the info related to the USB device
char __application_beginning
symbol for beginning of the application
void led_off(void)
switch off board LED
static uint16_t download_length
length of downloaded data
global definitions and methods (API)
static void usb_disconnect(void)
disconnect USB to force re-enumerate
void usb_dfu_start(void)
start USB DFU handling
void usb_dfu_setup(void)
setup USB DFU peripheral
library to read/write internal flash (API)
static uint8_t download_data[sizeof(usbd_control_buffer)]
downloaded data to be programmed in flash
static enum dfu_state usb_dfu_state
current DFU state
library for USB DFU to write on internal flash (API)
static uint8_t usbd_control_buffer[1024]
buffer to be used for control requests (fit to flash page size)
static void usb_dfu_flash(usbd_device *usbd_dev, struct usb_setup_data *req)
flash downloaded data block
static void usb_dfu_reset(usbd_device *usbd_dev, struct usb_setup_data *req)
disconnect USB and perform system reset
static enum usbd_request_return_codes usb_dfu_control_request(usbd_device *usbd_dev, struct usb_setup_data *req, uint8_t **buf, uint16_t *len, void(**complete)(usbd_device *usbd_dev, struct usb_setup_data *req))
handle incoming USB DFU control request
static const struct usb_interface_descriptor usb_dfu_interface
USB DFU interface descriptor.
static const struct usb_device_descriptor usb_dfu_device
USB DFU device descriptor.
#define LENGTH(x)
get the length of an array
bool flash_internal_write(uint32_t address, uint8_t *buffer, size_t size)
write data to internal flash
static const struct usb_interface usb_dfu_interfaces[]
USB DFU interface descriptor list.
char __application_end
symbol for end of the application
void led_on(void)
switch on board LED
static const struct usb_dfu_descriptor usb_dfu_functional
USB DFU functional descriptor.