Various cosmetic and whitespace fixes.
This commit is contained in:
@@ -44,20 +44,20 @@ static struct {
|
|||||||
} prog;
|
} prog;
|
||||||
|
|
||||||
const struct usb_device_descriptor dev = {
|
const struct usb_device_descriptor dev = {
|
||||||
.bLength = USB_DT_DEVICE_SIZE,
|
.bLength = USB_DT_DEVICE_SIZE,
|
||||||
.bDescriptorType = USB_DT_DEVICE,
|
.bDescriptorType = USB_DT_DEVICE,
|
||||||
.bcdUSB = 0x0200,
|
.bcdUSB = 0x0200,
|
||||||
.bDeviceClass = 0,
|
.bDeviceClass = 0,
|
||||||
.bDeviceSubClass = 0,
|
.bDeviceSubClass = 0,
|
||||||
.bDeviceProtocol = 0,
|
.bDeviceProtocol = 0,
|
||||||
.bMaxPacketSize0 = 64,
|
.bMaxPacketSize0 = 64,
|
||||||
.idVendor = 0x0483,
|
.idVendor = 0x0483,
|
||||||
.idProduct = 0xDF11,
|
.idProduct = 0xDF11,
|
||||||
.bcdDevice = 0x0200,
|
.bcdDevice = 0x0200,
|
||||||
.iManufacturer = 1,
|
.iManufacturer = 1,
|
||||||
.iProduct = 2,
|
.iProduct = 2,
|
||||||
.iSerialNumber = 3,
|
.iSerialNumber = 3,
|
||||||
.bNumConfigurations = 1,
|
.bNumConfigurations = 1,
|
||||||
};
|
};
|
||||||
|
|
||||||
const struct usb_dfu_descriptor dfu_function = {
|
const struct usb_dfu_descriptor dfu_function = {
|
||||||
@@ -110,23 +110,21 @@ static const char *usb_strings[] = {
|
|||||||
"Black Sphere Technologies",
|
"Black Sphere Technologies",
|
||||||
"DFU Demo",
|
"DFU Demo",
|
||||||
"DEMO",
|
"DEMO",
|
||||||
/* This string is used by ST Microelectronics' DfuSe utility */
|
/* This string is used by ST Microelectronics' DfuSe utility. */
|
||||||
"@Internal Flash /0x08000000/8*001Ka,56*001Kg"
|
"@Internal Flash /0x08000000/8*001Ka,56*001Kg"
|
||||||
};
|
};
|
||||||
|
|
||||||
static u8 usbdfu_getstatus(u32 *bwPollTimeout)
|
static u8 usbdfu_getstatus(u32 *bwPollTimeout)
|
||||||
{
|
{
|
||||||
switch(usbdfu_state) {
|
switch (usbdfu_state) {
|
||||||
case STATE_DFU_DNLOAD_SYNC:
|
case STATE_DFU_DNLOAD_SYNC:
|
||||||
usbdfu_state = STATE_DFU_DNBUSY;
|
usbdfu_state = STATE_DFU_DNBUSY;
|
||||||
*bwPollTimeout = 100;
|
*bwPollTimeout = 100;
|
||||||
return DFU_STATUS_OK;
|
return DFU_STATUS_OK;
|
||||||
|
|
||||||
case STATE_DFU_MANIFEST_SYNC:
|
case STATE_DFU_MANIFEST_SYNC:
|
||||||
/* Device will reset when read is complete */
|
/* Device will reset when read is complete. */
|
||||||
usbdfu_state = STATE_DFU_MANIFEST;
|
usbdfu_state = STATE_DFU_MANIFEST;
|
||||||
return DFU_STATUS_OK;
|
return DFU_STATUS_OK;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return DFU_STATUS_OK;
|
return DFU_STATUS_OK;
|
||||||
}
|
}
|
||||||
@@ -137,37 +135,32 @@ static void usbdfu_getstatus_complete(struct usb_setup_data *req)
|
|||||||
int i;
|
int i;
|
||||||
(void)req;
|
(void)req;
|
||||||
|
|
||||||
switch(usbdfu_state) {
|
switch (usbdfu_state) {
|
||||||
case STATE_DFU_DNBUSY:
|
case STATE_DFU_DNBUSY:
|
||||||
|
|
||||||
flash_unlock();
|
flash_unlock();
|
||||||
if(prog.blocknum == 0) {
|
if (prog.blocknum == 0) {
|
||||||
switch(prog.buf[0]) {
|
switch (prog.buf[0]) {
|
||||||
case CMD_ERASE:
|
case CMD_ERASE:
|
||||||
flash_erase_page(*(u32*)(prog.buf+1));
|
flash_erase_page(*(u32 *)(prog.buf + 1));
|
||||||
case CMD_SETADDR:
|
case CMD_SETADDR:
|
||||||
prog.addr = *(u32*)(prog.buf+1);
|
prog.addr = *(u32 *)(prog.buf + 1);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
u32 baseaddr = prog.addr +
|
u32 baseaddr = prog.addr + ((prog.blocknum - 2) *
|
||||||
((prog.blocknum - 2) *
|
dfu_function.wTransferSize);
|
||||||
dfu_function.wTransferSize);
|
for (i = 0; i < prog.len; i += 2)
|
||||||
for(i = 0; i < prog.len; i += 2)
|
|
||||||
flash_program_half_word(baseaddr + i,
|
flash_program_half_word(baseaddr + i,
|
||||||
*(u16*)(prog.buf+i));
|
*(u16 *)(prog.buf + i));
|
||||||
}
|
}
|
||||||
flash_lock();
|
flash_lock();
|
||||||
|
|
||||||
/* We jump straight to dfuDNLOAD-IDLE,
|
/* Jump straight to dfuDNLOAD-IDLE, skipping dfuDNLOAD-SYNC. */
|
||||||
* skipping dfuDNLOAD-SYNC
|
|
||||||
*/
|
|
||||||
usbdfu_state = STATE_DFU_DNLOAD_IDLE;
|
usbdfu_state = STATE_DFU_DNLOAD_IDLE;
|
||||||
return;
|
return;
|
||||||
|
|
||||||
case STATE_DFU_MANIFEST:
|
case STATE_DFU_MANIFEST:
|
||||||
/* USB device must detach, we just reset... */
|
/* USB device must detach, we just reset... */
|
||||||
scb_reset_system();
|
scb_reset_system();
|
||||||
return; /* Will never return */
|
return; /* Will never return. */
|
||||||
default:
|
default:
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -176,17 +169,16 @@ static void usbdfu_getstatus_complete(struct usb_setup_data *req)
|
|||||||
static int usbdfu_control_request(struct usb_setup_data *req, u8 **buf,
|
static int usbdfu_control_request(struct usb_setup_data *req, u8 **buf,
|
||||||
u16 *len, void (**complete)(struct usb_setup_data *req))
|
u16 *len, void (**complete)(struct usb_setup_data *req))
|
||||||
{
|
{
|
||||||
|
if ((req->bmRequestType & 0x7F) != 0x21)
|
||||||
|
return 0; /* Only accept class request. */
|
||||||
|
|
||||||
if((req->bmRequestType & 0x7F) != 0x21)
|
switch (req->bRequest) {
|
||||||
return 0; /* Only accept class request */
|
|
||||||
|
|
||||||
switch(req->bRequest) {
|
|
||||||
case DFU_DNLOAD:
|
case DFU_DNLOAD:
|
||||||
if((len == NULL) || (*len == 0)) {
|
if ((len == NULL) || (*len == 0)) {
|
||||||
usbdfu_state = STATE_DFU_MANIFEST_SYNC;
|
usbdfu_state = STATE_DFU_MANIFEST_SYNC;
|
||||||
return 1;
|
return 1;
|
||||||
} else {
|
} else {
|
||||||
/* Copy download data for use on GET_STATUS */
|
/* Copy download data for use on GET_STATUS. */
|
||||||
prog.blocknum = req->wValue;
|
prog.blocknum = req->wValue;
|
||||||
prog.len = *len;
|
prog.len = *len;
|
||||||
memcpy(prog.buf, *buf, *len);
|
memcpy(prog.buf, *buf, *len);
|
||||||
@@ -194,34 +186,31 @@ static int usbdfu_control_request(struct usb_setup_data *req, u8 **buf,
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
case DFU_CLRSTATUS:
|
case DFU_CLRSTATUS:
|
||||||
/* Clear error and return to dfuIDLE */
|
/* Clear error and return to dfuIDLE. */
|
||||||
if(usbdfu_state == STATE_DFU_ERROR)
|
if (usbdfu_state == STATE_DFU_ERROR)
|
||||||
usbdfu_state = STATE_DFU_IDLE;
|
usbdfu_state = STATE_DFU_IDLE;
|
||||||
return 1;
|
return 1;
|
||||||
case DFU_ABORT:
|
case DFU_ABORT:
|
||||||
/* Abort returns to dfuIDLE state */
|
/* Abort returns to dfuIDLE state. */
|
||||||
usbdfu_state = STATE_DFU_IDLE;
|
usbdfu_state = STATE_DFU_IDLE;
|
||||||
return 1;
|
return 1;
|
||||||
case DFU_UPLOAD:
|
case DFU_UPLOAD:
|
||||||
/* Upload not supported for now */
|
/* Upload not supported for now. */
|
||||||
return 0;
|
return 0;
|
||||||
case DFU_GETSTATUS: {
|
case DFU_GETSTATUS: {
|
||||||
u32 bwPollTimeout = 0; /* 24-bit integer in DFU class spec */
|
u32 bwPollTimeout = 0; /* 24-bit integer in DFU class spec */
|
||||||
|
|
||||||
(*buf)[0] = usbdfu_getstatus(&bwPollTimeout);
|
(*buf)[0] = usbdfu_getstatus(&bwPollTimeout);
|
||||||
(*buf)[1] = bwPollTimeout & 0xFF;
|
(*buf)[1] = bwPollTimeout & 0xFF;
|
||||||
(*buf)[2] = (bwPollTimeout >> 8) & 0xFF;
|
(*buf)[2] = (bwPollTimeout >> 8) & 0xFF;
|
||||||
(*buf)[3] = (bwPollTimeout >> 16) & 0xFF;
|
(*buf)[3] = (bwPollTimeout >> 16) & 0xFF;
|
||||||
(*buf)[4] = usbdfu_state;
|
(*buf)[4] = usbdfu_state;
|
||||||
(*buf)[5] = 0; /* iString not used here */
|
(*buf)[5] = 0; /* iString not used here */
|
||||||
*len = 6;
|
*len = 6;
|
||||||
|
|
||||||
*complete = usbdfu_getstatus_complete;
|
*complete = usbdfu_getstatus_complete;
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
case DFU_GETSTATE:
|
case DFU_GETSTATE:
|
||||||
/* Return state with no state transision */
|
/* Return state with no state transision. */
|
||||||
*buf[0] = usbdfu_state;
|
*buf[0] = usbdfu_state;
|
||||||
*len = 1;
|
*len = 1;
|
||||||
return 1;
|
return 1;
|
||||||
@@ -233,25 +222,27 @@ static int usbdfu_control_request(struct usb_setup_data *req, u8 **buf,
|
|||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN);
|
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN);
|
||||||
if(!gpio_get(GPIOA, GPIO10)) {
|
|
||||||
/* Boot the application if it's valid */
|
if (!gpio_get(GPIOA, GPIO10)) {
|
||||||
if((*(volatile u32*)APP_ADDRESS & 0x2FFE0000) == 0x20000000) {
|
/* Boot the application if it's valid. */
|
||||||
/* Set vector table base address */
|
if ((*(volatile u32 *)APP_ADDRESS & 0x2FFE0000) == 0x20000000) {
|
||||||
|
/* Set vector table base address. */
|
||||||
SCB_VTOR = APP_ADDRESS & 0xFFFF;
|
SCB_VTOR = APP_ADDRESS & 0xFFFF;
|
||||||
/* Initialise master stack pointer */
|
/* Initialise master stack pointer. */
|
||||||
asm volatile ("msr msp, %0"::"g"
|
asm volatile("msr msp, %0"::"g"
|
||||||
(*(volatile u32*)APP_ADDRESS));
|
(*(volatile u32 *)APP_ADDRESS));
|
||||||
/* Jump to application */
|
/* Jump to application. */
|
||||||
(*(void(**)())(APP_ADDRESS + 4))();
|
(*(void (**)())(APP_ADDRESS + 4))();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
rcc_clock_setup_in_hsi_out_48mhz();
|
rcc_clock_setup_in_hsi_out_48mhz();
|
||||||
|
|
||||||
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN);
|
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN);
|
||||||
|
|
||||||
gpio_set(GPIOC, GPIO2);
|
gpio_set(GPIOC, GPIO2);
|
||||||
gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO2);
|
gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ,
|
||||||
|
GPIO_CNF_OUTPUT_PUSHPULL, GPIO2);
|
||||||
|
|
||||||
usbd_init(&stm32f107_usb_driver, &dev, &config, usb_strings);
|
usbd_init(&stm32f107_usb_driver, &dev, &config, usb_strings);
|
||||||
usbd_set_control_buffer_size(sizeof(usbd_control_buffer));
|
usbd_set_control_buffer_size(sizeof(usbd_control_buffer));
|
||||||
|
|||||||
@@ -44,20 +44,20 @@ static struct {
|
|||||||
} prog;
|
} prog;
|
||||||
|
|
||||||
const struct usb_device_descriptor dev = {
|
const struct usb_device_descriptor dev = {
|
||||||
.bLength = USB_DT_DEVICE_SIZE,
|
.bLength = USB_DT_DEVICE_SIZE,
|
||||||
.bDescriptorType = USB_DT_DEVICE,
|
.bDescriptorType = USB_DT_DEVICE,
|
||||||
.bcdUSB = 0x0200,
|
.bcdUSB = 0x0200,
|
||||||
.bDeviceClass = 0,
|
.bDeviceClass = 0,
|
||||||
.bDeviceSubClass = 0,
|
.bDeviceSubClass = 0,
|
||||||
.bDeviceProtocol = 0,
|
.bDeviceProtocol = 0,
|
||||||
.bMaxPacketSize0 = 64,
|
.bMaxPacketSize0 = 64,
|
||||||
.idVendor = 0x0483,
|
.idVendor = 0x0483,
|
||||||
.idProduct = 0xDF11,
|
.idProduct = 0xDF11,
|
||||||
.bcdDevice = 0x0200,
|
.bcdDevice = 0x0200,
|
||||||
.iManufacturer = 1,
|
.iManufacturer = 1,
|
||||||
.iProduct = 2,
|
.iProduct = 2,
|
||||||
.iSerialNumber = 3,
|
.iSerialNumber = 3,
|
||||||
.bNumConfigurations = 1,
|
.bNumConfigurations = 1,
|
||||||
};
|
};
|
||||||
|
|
||||||
const struct usb_dfu_descriptor dfu_function = {
|
const struct usb_dfu_descriptor dfu_function = {
|
||||||
@@ -110,23 +110,21 @@ static const char *usb_strings[] = {
|
|||||||
"Black Sphere Technologies",
|
"Black Sphere Technologies",
|
||||||
"DFU Demo",
|
"DFU Demo",
|
||||||
"DEMO",
|
"DEMO",
|
||||||
/* This string is used by ST Microelectronics' DfuSe utility */
|
/* This string is used by ST Microelectronics' DfuSe utility. */
|
||||||
"@Internal Flash /0x08000000/8*001Ka,56*001Kg"
|
"@Internal Flash /0x08000000/8*001Ka,56*001Kg"
|
||||||
};
|
};
|
||||||
|
|
||||||
static u8 usbdfu_getstatus(u32 *bwPollTimeout)
|
static u8 usbdfu_getstatus(u32 *bwPollTimeout)
|
||||||
{
|
{
|
||||||
switch(usbdfu_state) {
|
switch (usbdfu_state) {
|
||||||
case STATE_DFU_DNLOAD_SYNC:
|
case STATE_DFU_DNLOAD_SYNC:
|
||||||
usbdfu_state = STATE_DFU_DNBUSY;
|
usbdfu_state = STATE_DFU_DNBUSY;
|
||||||
*bwPollTimeout = 100;
|
*bwPollTimeout = 100;
|
||||||
return DFU_STATUS_OK;
|
return DFU_STATUS_OK;
|
||||||
|
|
||||||
case STATE_DFU_MANIFEST_SYNC:
|
case STATE_DFU_MANIFEST_SYNC:
|
||||||
/* Device will reset when read is complete */
|
/* Device will reset when read is complete. */
|
||||||
usbdfu_state = STATE_DFU_MANIFEST;
|
usbdfu_state = STATE_DFU_MANIFEST;
|
||||||
return DFU_STATUS_OK;
|
return DFU_STATUS_OK;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return DFU_STATUS_OK;
|
return DFU_STATUS_OK;
|
||||||
}
|
}
|
||||||
@@ -137,37 +135,32 @@ static void usbdfu_getstatus_complete(struct usb_setup_data *req)
|
|||||||
int i;
|
int i;
|
||||||
(void)req;
|
(void)req;
|
||||||
|
|
||||||
switch(usbdfu_state) {
|
switch (usbdfu_state) {
|
||||||
case STATE_DFU_DNBUSY:
|
case STATE_DFU_DNBUSY:
|
||||||
|
|
||||||
flash_unlock();
|
flash_unlock();
|
||||||
if(prog.blocknum == 0) {
|
if (prog.blocknum == 0) {
|
||||||
switch(prog.buf[0]) {
|
switch (prog.buf[0]) {
|
||||||
case CMD_ERASE:
|
case CMD_ERASE:
|
||||||
flash_erase_page(*(u32*)(prog.buf+1));
|
flash_erase_page(*(u32 *)(prog.buf + 1));
|
||||||
case CMD_SETADDR:
|
case CMD_SETADDR:
|
||||||
prog.addr = *(u32*)(prog.buf+1);
|
prog.addr = *(u32 *)(prog.buf + 1);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
u32 baseaddr = prog.addr +
|
u32 baseaddr = prog.addr + ((prog.blocknum - 2) *
|
||||||
((prog.blocknum - 2) *
|
dfu_function.wTransferSize);
|
||||||
dfu_function.wTransferSize);
|
for (i = 0; i < prog.len; i += 2)
|
||||||
for(i = 0; i < prog.len; i += 2)
|
|
||||||
flash_program_half_word(baseaddr + i,
|
flash_program_half_word(baseaddr + i,
|
||||||
*(u16*)(prog.buf+i));
|
*(u16 *)(prog.buf + i));
|
||||||
}
|
}
|
||||||
flash_lock();
|
flash_lock();
|
||||||
|
|
||||||
/* We jump straight to dfuDNLOAD-IDLE,
|
/* Jump straight to dfuDNLOAD-IDLE, skipping dfuDNLOAD-SYNC. */
|
||||||
* skipping dfuDNLOAD-SYNC
|
|
||||||
*/
|
|
||||||
usbdfu_state = STATE_DFU_DNLOAD_IDLE;
|
usbdfu_state = STATE_DFU_DNLOAD_IDLE;
|
||||||
return;
|
return;
|
||||||
|
|
||||||
case STATE_DFU_MANIFEST:
|
case STATE_DFU_MANIFEST:
|
||||||
/* USB device must detach, we just reset... */
|
/* USB device must detach, we just reset... */
|
||||||
scb_reset_system();
|
scb_reset_system();
|
||||||
return; /* Will never return */
|
return; /* Will never return. */
|
||||||
default:
|
default:
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -176,17 +169,16 @@ static void usbdfu_getstatus_complete(struct usb_setup_data *req)
|
|||||||
static int usbdfu_control_request(struct usb_setup_data *req, u8 **buf,
|
static int usbdfu_control_request(struct usb_setup_data *req, u8 **buf,
|
||||||
u16 *len, void (**complete)(struct usb_setup_data *req))
|
u16 *len, void (**complete)(struct usb_setup_data *req))
|
||||||
{
|
{
|
||||||
|
if ((req->bmRequestType & 0x7F) != 0x21)
|
||||||
|
return 0; /* Only accept class request. */
|
||||||
|
|
||||||
if((req->bmRequestType & 0x7F) != 0x21)
|
switch (req->bRequest) {
|
||||||
return 0; /* Only accept class request */
|
|
||||||
|
|
||||||
switch(req->bRequest) {
|
|
||||||
case DFU_DNLOAD:
|
case DFU_DNLOAD:
|
||||||
if((len == NULL) || (*len == 0)) {
|
if ((len == NULL) || (*len == 0)) {
|
||||||
usbdfu_state = STATE_DFU_MANIFEST_SYNC;
|
usbdfu_state = STATE_DFU_MANIFEST_SYNC;
|
||||||
return 1;
|
return 1;
|
||||||
} else {
|
} else {
|
||||||
/* Copy download data for use on GET_STATUS */
|
/* Copy download data for use on GET_STATUS. */
|
||||||
prog.blocknum = req->wValue;
|
prog.blocknum = req->wValue;
|
||||||
prog.len = *len;
|
prog.len = *len;
|
||||||
memcpy(prog.buf, *buf, *len);
|
memcpy(prog.buf, *buf, *len);
|
||||||
@@ -194,34 +186,31 @@ static int usbdfu_control_request(struct usb_setup_data *req, u8 **buf,
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
case DFU_CLRSTATUS:
|
case DFU_CLRSTATUS:
|
||||||
/* Clear error and return to dfuIDLE */
|
/* Clear error and return to dfuIDLE. */
|
||||||
if(usbdfu_state == STATE_DFU_ERROR)
|
if (usbdfu_state == STATE_DFU_ERROR)
|
||||||
usbdfu_state = STATE_DFU_IDLE;
|
usbdfu_state = STATE_DFU_IDLE;
|
||||||
return 1;
|
return 1;
|
||||||
case DFU_ABORT:
|
case DFU_ABORT:
|
||||||
/* Abort returns to dfuIDLE state */
|
/* Abort returns to dfuIDLE state. */
|
||||||
usbdfu_state = STATE_DFU_IDLE;
|
usbdfu_state = STATE_DFU_IDLE;
|
||||||
return 1;
|
return 1;
|
||||||
case DFU_UPLOAD:
|
case DFU_UPLOAD:
|
||||||
/* Upload not supported for now */
|
/* Upload not supported for now. */
|
||||||
return 0;
|
return 0;
|
||||||
case DFU_GETSTATUS: {
|
case DFU_GETSTATUS: {
|
||||||
u32 bwPollTimeout = 0; /* 24-bit integer in DFU class spec */
|
u32 bwPollTimeout = 0; /* 24-bit integer in DFU class spec */
|
||||||
|
|
||||||
(*buf)[0] = usbdfu_getstatus(&bwPollTimeout);
|
(*buf)[0] = usbdfu_getstatus(&bwPollTimeout);
|
||||||
(*buf)[1] = bwPollTimeout & 0xFF;
|
(*buf)[1] = bwPollTimeout & 0xFF;
|
||||||
(*buf)[2] = (bwPollTimeout >> 8) & 0xFF;
|
(*buf)[2] = (bwPollTimeout >> 8) & 0xFF;
|
||||||
(*buf)[3] = (bwPollTimeout >> 16) & 0xFF;
|
(*buf)[3] = (bwPollTimeout >> 16) & 0xFF;
|
||||||
(*buf)[4] = usbdfu_state;
|
(*buf)[4] = usbdfu_state;
|
||||||
(*buf)[5] = 0; /* iString not used here */
|
(*buf)[5] = 0; /* iString not used here */
|
||||||
*len = 6;
|
*len = 6;
|
||||||
|
|
||||||
*complete = usbdfu_getstatus_complete;
|
*complete = usbdfu_getstatus_complete;
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
case DFU_GETSTATE:
|
case DFU_GETSTATE:
|
||||||
/* Return state with no state transision */
|
/* Return state with no state transision. */
|
||||||
*buf[0] = usbdfu_state;
|
*buf[0] = usbdfu_state;
|
||||||
*len = 1;
|
*len = 1;
|
||||||
return 1;
|
return 1;
|
||||||
@@ -233,20 +222,21 @@ static int usbdfu_control_request(struct usb_setup_data *req, u8 **buf,
|
|||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN);
|
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN);
|
||||||
if(!gpio_get(GPIOA, GPIO10)) {
|
|
||||||
/* Boot the application if it's valid */
|
if (!gpio_get(GPIOA, GPIO10)) {
|
||||||
if((*(volatile u32*)APP_ADDRESS & 0x2FFE0000) == 0x20000000) {
|
/* Boot the application if it's valid. */
|
||||||
/* Set vector table base address */
|
if ((*(volatile u32 *)APP_ADDRESS & 0x2FFE0000) == 0x20000000) {
|
||||||
|
/* Set vector table base address. */
|
||||||
SCB_VTOR = APP_ADDRESS & 0xFFFF;
|
SCB_VTOR = APP_ADDRESS & 0xFFFF;
|
||||||
/* Initialise master stack pointer */
|
/* Initialise master stack pointer. */
|
||||||
asm volatile ("msr msp, %0"::"g"
|
asm volatile("msr msp, %0"::"g"
|
||||||
(*(volatile u32*)APP_ADDRESS));
|
(*(volatile u32 *)APP_ADDRESS));
|
||||||
/* Jump to application */
|
/* Jump to application. */
|
||||||
(*(void(**)())(APP_ADDRESS + 4))();
|
(*(void (**)())(APP_ADDRESS + 4))();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
rcc_clock_setup_in_hsi_out_48mhz();
|
rcc_clock_setup_in_hsi_out_48mhz();
|
||||||
|
|
||||||
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN);
|
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN);
|
||||||
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_AFIOEN);
|
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_AFIOEN);
|
||||||
@@ -263,7 +253,7 @@ int main(void)
|
|||||||
|
|
||||||
gpio_set(GPIOA, GPIO15);
|
gpio_set(GPIOA, GPIO15);
|
||||||
gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ,
|
gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ,
|
||||||
GPIO_CNF_OUTPUT_PUSHPULL, GPIO15);
|
GPIO_CNF_OUTPUT_PUSHPULL, GPIO15);
|
||||||
|
|
||||||
while (1)
|
while (1)
|
||||||
usbd_poll();
|
usbd_poll();
|
||||||
|
|||||||
@@ -116,7 +116,7 @@ static const char *usb_strings[] = {
|
|||||||
|
|
||||||
static u8 usbdfu_getstatus(u32 *bwPollTimeout)
|
static u8 usbdfu_getstatus(u32 *bwPollTimeout)
|
||||||
{
|
{
|
||||||
switch(usbdfu_state) {
|
switch (usbdfu_state) {
|
||||||
case STATE_DFU_DNLOAD_SYNC:
|
case STATE_DFU_DNLOAD_SYNC:
|
||||||
usbdfu_state = STATE_DFU_DNBUSY;
|
usbdfu_state = STATE_DFU_DNBUSY;
|
||||||
*bwPollTimeout = 100;
|
*bwPollTimeout = 100;
|
||||||
@@ -139,7 +139,7 @@ static void usbdfu_getstatus_complete(struct usb_setup_data *req)
|
|||||||
case STATE_DFU_DNBUSY:
|
case STATE_DFU_DNBUSY:
|
||||||
flash_unlock();
|
flash_unlock();
|
||||||
if (prog.blocknum == 0) {
|
if (prog.blocknum == 0) {
|
||||||
switch(prog.buf[0]) {
|
switch (prog.buf[0]) {
|
||||||
case CMD_ERASE:
|
case CMD_ERASE:
|
||||||
flash_erase_page(*(u32 *)(prog.buf + 1));
|
flash_erase_page(*(u32 *)(prog.buf + 1));
|
||||||
case CMD_SETADDR:
|
case CMD_SETADDR:
|
||||||
@@ -169,10 +169,10 @@ static void usbdfu_getstatus_complete(struct usb_setup_data *req)
|
|||||||
static int usbdfu_control_request(struct usb_setup_data *req, u8 **buf,
|
static int usbdfu_control_request(struct usb_setup_data *req, u8 **buf,
|
||||||
u16 *len, void (**complete)(struct usb_setup_data *req))
|
u16 *len, void (**complete)(struct usb_setup_data *req))
|
||||||
{
|
{
|
||||||
if ((req->bmRequestType & 0x7f) != 0x21)
|
if ((req->bmRequestType & 0x7F) != 0x21)
|
||||||
return 0; /* Only accept class request. */
|
return 0; /* Only accept class request. */
|
||||||
|
|
||||||
switch(req->bRequest) {
|
switch (req->bRequest) {
|
||||||
case DFU_DNLOAD:
|
case DFU_DNLOAD:
|
||||||
if ((len == NULL) || (*len == 0)) {
|
if ((len == NULL) || (*len == 0)) {
|
||||||
usbdfu_state = STATE_DFU_MANIFEST_SYNC;
|
usbdfu_state = STATE_DFU_MANIFEST_SYNC;
|
||||||
@@ -187,7 +187,7 @@ static int usbdfu_control_request(struct usb_setup_data *req, u8 **buf,
|
|||||||
}
|
}
|
||||||
case DFU_CLRSTATUS:
|
case DFU_CLRSTATUS:
|
||||||
/* Clear error and return to dfuIDLE. */
|
/* Clear error and return to dfuIDLE. */
|
||||||
if(usbdfu_state == STATE_DFU_ERROR)
|
if (usbdfu_state == STATE_DFU_ERROR)
|
||||||
usbdfu_state = STATE_DFU_IDLE;
|
usbdfu_state = STATE_DFU_IDLE;
|
||||||
return 1;
|
return 1;
|
||||||
case DFU_ABORT:
|
case DFU_ABORT:
|
||||||
@@ -199,7 +199,6 @@ static int usbdfu_control_request(struct usb_setup_data *req, u8 **buf,
|
|||||||
return 0;
|
return 0;
|
||||||
case DFU_GETSTATUS: {
|
case DFU_GETSTATUS: {
|
||||||
u32 bwPollTimeout = 0; /* 24-bit integer in DFU class spec */
|
u32 bwPollTimeout = 0; /* 24-bit integer in DFU class spec */
|
||||||
|
|
||||||
(*buf)[0] = usbdfu_getstatus(&bwPollTimeout);
|
(*buf)[0] = usbdfu_getstatus(&bwPollTimeout);
|
||||||
(*buf)[1] = bwPollTimeout & 0xFF;
|
(*buf)[1] = bwPollTimeout & 0xFF;
|
||||||
(*buf)[2] = (bwPollTimeout >> 8) & 0xFF;
|
(*buf)[2] = (bwPollTimeout >> 8) & 0xFF;
|
||||||
@@ -207,9 +206,7 @@ static int usbdfu_control_request(struct usb_setup_data *req, u8 **buf,
|
|||||||
(*buf)[4] = usbdfu_state;
|
(*buf)[4] = usbdfu_state;
|
||||||
(*buf)[5] = 0; /* iString not used here */
|
(*buf)[5] = 0; /* iString not used here */
|
||||||
*len = 6;
|
*len = 6;
|
||||||
|
|
||||||
*complete = usbdfu_getstatus_complete;
|
*complete = usbdfu_getstatus_complete;
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
case DFU_GETSTATE:
|
case DFU_GETSTATE:
|
||||||
@@ -228,14 +225,14 @@ int main(void)
|
|||||||
|
|
||||||
if (!gpio_get(GPIOA, GPIO10)) {
|
if (!gpio_get(GPIOA, GPIO10)) {
|
||||||
/* Boot the application if it's valid. */
|
/* Boot the application if it's valid. */
|
||||||
if ((*(volatile u32*)APP_ADDRESS & 0x2FFE0000) == 0x20000000) {
|
if ((*(volatile u32 *)APP_ADDRESS & 0x2FFE0000) == 0x20000000) {
|
||||||
/* Set vector table base address. */
|
/* Set vector table base address. */
|
||||||
SCB_VTOR = APP_ADDRESS & 0xFFFF;
|
SCB_VTOR = APP_ADDRESS & 0xFFFF;
|
||||||
/* Initialise master stack pointer. */
|
/* Initialise master stack pointer. */
|
||||||
asm volatile ("msr msp, %0"::"g"
|
asm volatile("msr msp, %0"::"g"
|
||||||
(*(volatile u32 *)APP_ADDRESS));
|
(*(volatile u32 *)APP_ADDRESS));
|
||||||
/* Jump to application. */
|
/* Jump to application. */
|
||||||
(*(void(**)())(APP_ADDRESS + 4))();
|
(*(void (**)())(APP_ADDRESS + 4))();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -33,23 +33,23 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
const struct usb_device_descriptor dev = {
|
const struct usb_device_descriptor dev = {
|
||||||
.bLength = USB_DT_DEVICE_SIZE,
|
.bLength = USB_DT_DEVICE_SIZE,
|
||||||
.bDescriptorType = USB_DT_DEVICE,
|
.bDescriptorType = USB_DT_DEVICE,
|
||||||
.bcdUSB = 0x0200,
|
.bcdUSB = 0x0200,
|
||||||
.bDeviceClass = 0,
|
.bDeviceClass = 0,
|
||||||
.bDeviceSubClass = 0,
|
.bDeviceSubClass = 0,
|
||||||
.bDeviceProtocol = 0,
|
.bDeviceProtocol = 0,
|
||||||
.bMaxPacketSize0 = 64,
|
.bMaxPacketSize0 = 64,
|
||||||
.idVendor = 0x0483,
|
.idVendor = 0x0483,
|
||||||
.idProduct = 0x5710,
|
.idProduct = 0x5710,
|
||||||
.bcdDevice = 0x0200,
|
.bcdDevice = 0x0200,
|
||||||
.iManufacturer = 1,
|
.iManufacturer = 1,
|
||||||
.iProduct = 2,
|
.iProduct = 2,
|
||||||
.iSerialNumber = 3,
|
.iSerialNumber = 3,
|
||||||
.bNumConfigurations = 1,
|
.bNumConfigurations = 1,
|
||||||
};
|
};
|
||||||
|
|
||||||
/* I have no idea what this means. I haven't read the HID spec. */
|
/* I have no idea what this means. I haven't read the HID spec. */
|
||||||
static const u8 hid_report_descriptor[] = {
|
static const u8 hid_report_descriptor[] = {
|
||||||
0x05, 0x01, 0x09, 0x02, 0xA1, 0x01, 0x09, 0x01,
|
0x05, 0x01, 0x09, 0x02, 0xA1, 0x01, 0x09, 0x01,
|
||||||
0xA1, 0x00, 0x05, 0x09, 0x19, 0x01, 0x29, 0x03,
|
0xA1, 0x00, 0x05, 0x09, 0x19, 0x01, 0x29, 0x03,
|
||||||
@@ -60,7 +60,7 @@ static const u8 hid_report_descriptor[] = {
|
|||||||
0x81, 0x06, 0xC0, 0x09, 0x3c, 0x05, 0xff, 0x09,
|
0x81, 0x06, 0xC0, 0x09, 0x3c, 0x05, 0xff, 0x09,
|
||||||
0x01, 0x15, 0x00, 0x25, 0x01, 0x75, 0x01, 0x95,
|
0x01, 0x15, 0x00, 0x25, 0x01, 0x75, 0x01, 0x95,
|
||||||
0x02, 0xb1, 0x22, 0x75, 0x06, 0x95, 0x01, 0xb1,
|
0x02, 0xb1, 0x22, 0x75, 0x06, 0x95, 0x01, 0xb1,
|
||||||
0x01, 0xc0
|
0x01, 0xc0,
|
||||||
};
|
};
|
||||||
|
|
||||||
static const struct {
|
static const struct {
|
||||||
@@ -80,7 +80,7 @@ static const struct {
|
|||||||
.hid_report = {
|
.hid_report = {
|
||||||
.bReportDescriptorType = USB_DT_REPORT,
|
.bReportDescriptorType = USB_DT_REPORT,
|
||||||
.wDescriptorLength = sizeof(hid_report_descriptor),
|
.wDescriptorLength = sizeof(hid_report_descriptor),
|
||||||
}
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
const struct usb_endpoint_descriptor hid_endpoint = {
|
const struct usb_endpoint_descriptor hid_endpoint = {
|
||||||
@@ -174,13 +174,13 @@ static int hid_control_request(struct usb_setup_data *req, u8 **buf, u16 *len,
|
|||||||
{
|
{
|
||||||
(void)complete;
|
(void)complete;
|
||||||
|
|
||||||
if((req->bmRequestType != 0x81) ||
|
if ((req->bmRequestType != 0x81) ||
|
||||||
(req->bRequest != USB_REQ_GET_DESCRIPTOR) ||
|
(req->bRequest != USB_REQ_GET_DESCRIPTOR) ||
|
||||||
(req->wValue != 0x2200))
|
(req->wValue != 0x2200))
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
/* Handle the HID report descriptor */
|
/* Handle the HID report descriptor. */
|
||||||
*buf = (u8*)hid_report_descriptor;
|
*buf = (u8 *)hid_report_descriptor;
|
||||||
*len = sizeof(hid_report_descriptor);
|
*len = sizeof(hid_report_descriptor);
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
@@ -204,8 +204,8 @@ static int dfu_control_request(struct usb_setup_data *req, u8 **buf, u16 *len,
|
|||||||
(void)buf;
|
(void)buf;
|
||||||
(void)len;
|
(void)len;
|
||||||
|
|
||||||
if((req->bmRequestType != 0x21) || (req->bRequest != DFU_DETACH))
|
if ((req->bmRequestType != 0x21) || (req->bRequest != DFU_DETACH))
|
||||||
return 0; /* Only accept class request */
|
return 0; /* Only accept class request. */
|
||||||
|
|
||||||
*complete = dfu_detach_complete;
|
*complete = dfu_detach_complete;
|
||||||
|
|
||||||
@@ -238,19 +238,22 @@ static void hid_set_config(u16 wValue)
|
|||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
rcc_clock_setup_in_hsi_out_48mhz();
|
int i;
|
||||||
|
|
||||||
|
rcc_clock_setup_in_hsi_out_48mhz();
|
||||||
|
|
||||||
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN);
|
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN);
|
||||||
|
|
||||||
gpio_set(GPIOC, GPIO11);
|
gpio_set(GPIOC, GPIO11);
|
||||||
gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_2_MHZ,
|
gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_2_MHZ,
|
||||||
GPIO_CNF_OUTPUT_PUSHPULL, GPIO11);
|
GPIO_CNF_OUTPUT_PUSHPULL, GPIO11);
|
||||||
|
|
||||||
usbd_init(&stm32f103_usb_driver, &dev, &config, usb_strings);
|
usbd_init(&stm32f103_usb_driver, &dev, &config, usb_strings);
|
||||||
usbd_register_set_config_callback(hid_set_config);
|
usbd_register_set_config_callback(hid_set_config);
|
||||||
|
|
||||||
{int i; for (i=0;i<0x80000;i++);}
|
for (i = 0; i < 0x80000; i++)
|
||||||
|
__asm__("nop");
|
||||||
|
|
||||||
gpio_clear(GPIOC, GPIO11);
|
gpio_clear(GPIOC, GPIO11);
|
||||||
|
|
||||||
while (1)
|
while (1)
|
||||||
@@ -265,8 +268,10 @@ void sys_tick_handler(void)
|
|||||||
|
|
||||||
buf[1] = dir;
|
buf[1] = dir;
|
||||||
x += dir;
|
x += dir;
|
||||||
if(x > 30) dir = -dir;
|
if (x > 30)
|
||||||
if(x < -30) dir = -dir;
|
dir = -dir;
|
||||||
|
if (x < -30)
|
||||||
|
dir = -dir;
|
||||||
|
|
||||||
usbd_ep_write_packet(0x81, buf, 4);
|
usbd_ep_write_packet(0x81, buf, 4);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user