2024-04-16 22:11:30 +08:00
|
|
|
/*
|
2026-02-05 20:39:38 +08:00
|
|
|
* Copyright (c) 2022 ~ 2026, sakumisu
|
2024-04-16 22:11:30 +08:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
*/
|
2022-09-18 12:00:44 +08:00
|
|
|
#include "usbd_core.h"
|
|
|
|
|
#include "usbd_dfu.h"
|
|
|
|
|
|
2022-10-20 21:22:08 +08:00
|
|
|
#define USBD_VID 0x0483
|
2026-02-05 20:39:38 +08:00
|
|
|
#define USBD_PID 0xdf11
|
2022-10-20 21:22:08 +08:00
|
|
|
#define USBD_MAX_POWER 100
|
2022-09-18 12:00:44 +08:00
|
|
|
#define USBD_LANGID_STRING 1033
|
|
|
|
|
|
2026-02-05 20:39:38 +08:00
|
|
|
#define USB_CONFIG_SIZE (9 + DFU_DESCRIPTOR_LEN)
|
2022-09-18 12:00:44 +08:00
|
|
|
|
2026-02-05 20:39:38 +08:00
|
|
|
#define FLASH_DESC_STR "@Internal Flash /0x08000000/16*128Kg"
|
2022-09-18 12:00:44 +08:00
|
|
|
|
2025-02-01 19:13:44 +08:00
|
|
|
static const uint8_t device_descriptor[] = {
|
|
|
|
|
USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0x00, 0x00, 0x00, USBD_VID, USBD_PID, 0x0200, 0x01)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static const uint8_t config_descriptor[] = {
|
|
|
|
|
USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_SIZE, 0x01, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
|
2026-02-05 20:39:38 +08:00
|
|
|
DFU_DESCRIPTOR_INIT(4)
|
2025-02-01 19:13:44 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static const uint8_t device_quality_descriptor[] = {
|
|
|
|
|
///////////////////////////////////////
|
|
|
|
|
/// device qualifier descriptor
|
|
|
|
|
///////////////////////////////////////
|
|
|
|
|
0x0a,
|
|
|
|
|
USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
|
|
|
|
|
0x00,
|
|
|
|
|
0x02,
|
|
|
|
|
0x00,
|
|
|
|
|
0x00,
|
|
|
|
|
0x00,
|
|
|
|
|
0x40,
|
|
|
|
|
0x00,
|
|
|
|
|
0x00,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static const char *string_descriptors[] = {
|
|
|
|
|
(const char[]){ 0x09, 0x04 }, /* Langid */
|
|
|
|
|
"CherryUSB", /* Manufacturer */
|
|
|
|
|
"CherryUSB DFU DEMO", /* Product */
|
|
|
|
|
"2022123456", /* Serial Number */
|
2026-02-05 20:39:38 +08:00
|
|
|
FLASH_DESC_STR
|
2025-02-01 19:13:44 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static const uint8_t *device_descriptor_callback(uint8_t speed)
|
|
|
|
|
{
|
|
|
|
|
return device_descriptor;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const uint8_t *config_descriptor_callback(uint8_t speed)
|
|
|
|
|
{
|
|
|
|
|
return config_descriptor;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const uint8_t *device_quality_descriptor_callback(uint8_t speed)
|
|
|
|
|
{
|
|
|
|
|
return device_quality_descriptor;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const char *string_descriptor_callback(uint8_t speed, uint8_t index)
|
|
|
|
|
{
|
2026-02-05 20:39:38 +08:00
|
|
|
if (index >= (sizeof(string_descriptors) / sizeof(char *))) {
|
2025-02-01 19:13:44 +08:00
|
|
|
return NULL;
|
|
|
|
|
}
|
2026-02-05 20:39:38 +08:00
|
|
|
|
2025-02-01 19:13:44 +08:00
|
|
|
return string_descriptors[index];
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-05 20:39:38 +08:00
|
|
|
const struct usb_descriptor dfu_descriptor = {
|
2025-02-01 19:13:44 +08:00
|
|
|
.device_descriptor_callback = device_descriptor_callback,
|
|
|
|
|
.config_descriptor_callback = config_descriptor_callback,
|
|
|
|
|
.device_quality_descriptor_callback = device_quality_descriptor_callback,
|
|
|
|
|
.string_descriptor_callback = string_descriptor_callback
|
|
|
|
|
};
|
2022-09-18 12:00:44 +08:00
|
|
|
|
2024-02-18 19:36:20 +08:00
|
|
|
static void usbd_event_handler(uint8_t busid, uint8_t event)
|
2022-09-18 12:00:44 +08:00
|
|
|
{
|
2023-06-21 21:26:25 +08:00
|
|
|
switch (event) {
|
|
|
|
|
case USBD_EVENT_RESET:
|
|
|
|
|
break;
|
|
|
|
|
case USBD_EVENT_CONNECTED:
|
|
|
|
|
break;
|
|
|
|
|
case USBD_EVENT_DISCONNECTED:
|
|
|
|
|
break;
|
|
|
|
|
case USBD_EVENT_RESUME:
|
|
|
|
|
break;
|
|
|
|
|
case USBD_EVENT_SUSPEND:
|
|
|
|
|
break;
|
|
|
|
|
case USBD_EVENT_CONFIGURED:
|
|
|
|
|
break;
|
|
|
|
|
case USBD_EVENT_SET_REMOTE_WAKEUP:
|
|
|
|
|
break;
|
|
|
|
|
case USBD_EVENT_CLR_REMOTE_WAKEUP:
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
2022-09-18 12:00:44 +08:00
|
|
|
}
|
|
|
|
|
|
2022-10-20 21:22:08 +08:00
|
|
|
struct usbd_interface intf0;
|
|
|
|
|
|
2026-02-05 20:39:38 +08:00
|
|
|
void dfu_init(uint8_t busid, uintptr_t reg_base)
|
2022-09-18 12:00:44 +08:00
|
|
|
{
|
2026-02-05 20:39:38 +08:00
|
|
|
usbd_desc_register(busid, &dfu_descriptor);
|
2026-01-24 17:39:42 +08:00
|
|
|
|
2024-02-18 19:36:20 +08:00
|
|
|
usbd_add_interface(busid, usbd_dfu_init_intf(&intf0));
|
|
|
|
|
usbd_initialize(busid, reg_base, usbd_event_handler);
|
2022-09-18 12:00:44 +08:00
|
|
|
}
|
2026-02-05 20:39:38 +08:00
|
|
|
|
|
|
|
|
volatile uint32_t flash_start_address;
|
|
|
|
|
|
|
|
|
|
void usbd_dfu_begin_load(void)
|
|
|
|
|
{
|
|
|
|
|
flash_start_address = 0x08000000;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void usbd_dfu_end_load(void)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void usbd_dfu_reset(void)
|
|
|
|
|
{
|
|
|
|
|
//NVIC_SystemReset();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int usbd_dfu_write(uint16_t value, const uint8_t *data, uint16_t length)
|
|
|
|
|
{
|
|
|
|
|
//usb_hexdump(data, length);
|
|
|
|
|
|
|
|
|
|
// patch for stm32 special command
|
|
|
|
|
#if 1
|
|
|
|
|
if (value == 0) {
|
|
|
|
|
if (data[0] == DFU_SPECIAL_CMD_SET_ADDRESS_POINTER) {
|
|
|
|
|
memcpy((uint8_t *)&flash_start_address, data, 4);
|
|
|
|
|
} else if (data[0] == DFU_SPECIAL_CMD_ERASE) {
|
|
|
|
|
memcpy((uint8_t *)&flash_start_address, data, 4);
|
|
|
|
|
}
|
|
|
|
|
} else if (value > 1) {
|
|
|
|
|
uint32_t addr = (value - 2) * CONFIG_USBDEV_REQUEST_BUFFER_LEN + flash_start_address;
|
|
|
|
|
}
|
|
|
|
|
#else
|
|
|
|
|
flash_start_address += length;
|
|
|
|
|
#endif
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int usbd_dfu_read(uint16_t value, const uint8_t *data, uint16_t length, uint16_t *actual_length)
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|