USBD ROM Stack  2.0
ROM based USB device stack
How to implement simple DFU device ?

The DFU (Device Firmware Upgrade) class consists primarily of devices that are required for device firmware update capability.

Initialization

After successfully initializing the USBD ROM stack i.e. USBD_HW_API_T::Init, the DFU class driver needs to be initialized as follows:

Defining USB Descriptors

The following example shows the configuration descriptor for a USB DFU device:

/* USB FSConfiguration Descriptor */
const uint8_t USB_FsConfigDescriptor[] = {
/* Configuration 1 */
USB_CONFIGURATION_DESC_SIZE, /* bLength */
USB_CONFIGURATION_DESCRIPTOR_TYPE, /* bDescriptorType */
WBVAL( /* wTotalLength */
1 * USB_CONFIGURATION_DESC_SIZE +
1 * USB_INTERFACE_DESC_SIZE +
DFU_FUNC_DESC_SIZE
),
0x01, /* bNumInterfaces */
0x01, /* bConfigurationValue */
0x00, /* iConfiguration */
USB_CONFIG_BUS_POWERED, /* bmAttributes */
USB_CONFIG_POWER_MA(100), /* bMaxPower */
/* Interface 0, Alternate Setting 0, DFU Class */
USB_INTERFACE_DESC_SIZE, /* bLength */
USB_INTERFACE_DESCRIPTOR_TYPE, /* bDescriptorType */
0x00, /* bInterfaceNumber */
0x00, /* bAlternateSetting */
0x00, /* bNumEndpoints */
USB_DEVICE_CLASS_APP, /* bInterfaceClass */
USB_DFU_SUBCLASS, /* bInterfaceSubClass */
0x02, /* bInterfaceProtocol */
0x04, /* iInterface */
/* DFU RunTime/DFU Mode Functional Descriptor */
DFU_FUNC_DESC_SIZE, /* bLength */
USB_DFU_DESCRIPTOR_TYPE, /* bDescriptorType */
USB_DFU_CAN_DOWNLOAD | USB_DFU_WILL_DETACH, /* bmAttributes */
WBVAL(0xFF00), /* wDetachTimeout */
WBVAL(USB_DFU_XFER_SIZE), /* wTransferSize */
WBVAL(0x0110), /* bcdDFUVersion */
0 /* Terminator */
};

Execution

After a successful DFU Initialization the stack is ready to receive the packets from host. Even if the device is physically connected to the host using an USB cable the host does not recognize the presence of device until the D+ line is pulled up using 1.5K ohm resistor (LPC device controllers operate in high-speed or full-speed mode only). To enable the connection the application software should call USBD_HW_API_T::Connect with enable set to 1. Before enabling the connection the application should enable the USB interrupt.

NVIC_EnableIRQ(USB0_IRQn); // enable USB0 interrupts
/* now connect */
USBD_API->hw->Connect(g_hUsb, 1);

After connection the DFU class executes through callback functions setup during initialization. The application could also have a task waiting for download to complete and execute the newly downloaded image after completion.

Installing IRQ handlers

USBD ROM stack implements the interrupt handler for USB device controller and invokes the USBD ROM stack routines according to the event type. Hence application should connect this handler to the application vector table. This is done by calling USBD_HW_API_T::ISR from the USB IRQ handler as shown in the code snippet below. The USBD ROM handle passed to the ISR() routine is the one obtained in previous step.

void USB0_IRQHandler(void)
{
USBD_API->hw->ISR(g_hUsb);
}

Implementing callback

Subsections: