From 9da4b55529e3c9cb34be47ee23391c1dfd4dec5a Mon Sep 17 00:00:00 2001 From: root Date: Wed, 10 Mar 2021 10:17:23 +0000 Subject: update libopencm3 to fix bugs in usb implementation --- app/cdcacm.c | 23 ++++++++++++----------- app/dfu.c | 24 +++++++++++------------- app/i2c_hw.c | 2 +- app/main.c | 2 +- app/project.h | 2 +- app/prototypes.h | 4 ++-- app/serial_over_dp.ld | 2 +- app/usb.c | 29 +++++++++++++++++++---------- boot/usbdfu.c | 30 ++++++++++++++---------------- boot/usbdfu.ld | 2 +- libopencm3 | 2 +- 11 files changed, 64 insertions(+), 58 deletions(-) diff --git a/app/cdcacm.c b/app/cdcacm.c index f6a1b0f..0b4a4e3 100644 --- a/app/cdcacm.c +++ b/app/cdcacm.c @@ -122,35 +122,36 @@ const struct usb_iface_assoc_descriptor cdc_iface_assoc = { -int cdcacm_control_request (usbd_device *usbd_dev, - struct usb_setup_data *req, - uint8_t **buf, - uint16_t *len, - usbd_control_complete_callback *complete) -{ +enum usbd_request_return_codes +cdcacm_control_request (usbd_device *usbd_dev, + struct usb_setup_data *req, + uint8_t **buf, + uint16_t *len, + usbd_control_complete_callback *complete) { (void) complete; (void) buf; (void) usbd_dev; - switch (req->bRequest) { + switch (req->bRequest) + { case USB_CDC_REQ_SET_CONTROL_LINE_STATE: { /* * This Linux cdc_acm driver requires this to be implemented * even though it's optional in the CDC spec, and we don't * advertise it in the ACM functional descriptor. */ - return 1; + return USBD_REQ_HANDLED; } case USB_CDC_REQ_SET_LINE_CODING: if (*len < sizeof (struct usb_cdc_line_coding)) - return 0; + return USBD_REQ_NOTSUPP; - return 1; + return USBD_REQ_HANDLED; } - return 0; + return USBD_REQ_NEXT_CALLBACK; } diff --git a/app/dfu.c b/app/dfu.c index b0715be..29a2428 100644 --- a/app/dfu.c +++ b/app/dfu.c @@ -36,7 +36,7 @@ const struct usb_iface_assoc_descriptor dfu_iface_assoc = { }; -static int +static void dfu_detach_complete (usbd_device *usbd_dev, struct usb_setup_data *req) { (void) req; @@ -44,23 +44,21 @@ dfu_detach_complete (usbd_device *usbd_dev, struct usb_setup_data *req) dfu_flag = 0xfee1dead; scb_reset_core(); - return 1; } -int +enum usbd_request_return_codes dfu_control_request (usbd_device *usbd_dev, struct usb_setup_data *req, uint8_t **buf, uint16_t *len, - usbd_control_complete_callback *complete) -{ + usbd_control_complete_callback *complete) { (void) buf; (void) len; (void) usbd_dev; - if ((req->bmRequestType & 0x7F) != 0x21) { - return 0; /* Only accept class request. */ - } + if ((req->bmRequestType & 0x7F) != 0x21) + return USBD_REQ_NEXT_CALLBACK; - switch (req->bRequest) { + switch (req->bRequest) + { case DFU_GETSTATUS: { (*buf) [0] = DFU_STATUS_OK; (*buf) [1] = 0; @@ -69,20 +67,20 @@ dfu_control_request (usbd_device *usbd_dev, struct usb_setup_data *req, (*buf) [4] = STATE_APP_IDLE; (*buf) [5] = 0; /* iString not used here */ *len = 6; - return 1; + return USBD_REQ_HANDLED; } case DFU_GETSTATE: /* Return state with no state transision. */ *buf[0] = STATE_APP_IDLE; *len = 1; - return 1; + return USBD_REQ_HANDLED; case DFU_DETACH: *complete = dfu_detach_complete; - return 1; + return USBD_REQ_HANDLED; } - return 0; + return USBD_REQ_NEXT_CALLBACK; } diff --git a/app/i2c_hw.c b/app/i2c_hw.c index 9b77009..3efb862 100644 --- a/app/i2c_hw.c +++ b/app/i2c_hw.c @@ -89,7 +89,7 @@ i2c_hw_init (void) gpio_set_mode (SDA_BANK, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_OPENDRAIN, SDA_GPIO); - i2c_set_clock_frequency (I2C, I2C_CR2_FREQ_36MHZ); + i2c_set_clock_frequency (I2C, 36); i2c_set_standard_mode (I2C); i2c_set_ccr (I2C, 0x200); /* t_high=t_high=CCR * Tpclk1 */ i2c_set_trise (I2C, 0x0b); diff --git a/app/main.c b/app/main.c index 2ac458f..b6b5039 100644 --- a/app/main.c +++ b/app/main.c @@ -7,7 +7,7 @@ int main (void) { /*set up pll */ - rcc_clock_setup_in_hse_8mhz_out_72mhz(); + rcc_clock_setup_pll(&rcc_hse_configs[RCC_CLOCK_HSE8_72MHZ]); /*turn on clocks to periferals */ rcc_periph_clock_enable (RCC_GPIOA); rcc_periph_clock_enable (RCC_GPIOB); diff --git a/app/project.h b/app/project.h index f4b113b..28b9ab5 100644 --- a/app/project.h +++ b/app/project.h @@ -2,8 +2,8 @@ #include #include #include -#include #include +#include #include #include #include diff --git a/app/prototypes.h b/app/prototypes.h index a432df4..9417a76 100644 --- a/app/prototypes.h +++ b/app/prototypes.h @@ -6,7 +6,7 @@ extern ring_t cdcacm_tx_ring; extern const struct usb_interface_descriptor comm_iface; extern const struct usb_interface_descriptor data_iface; extern const struct usb_iface_assoc_descriptor cdc_iface_assoc; -extern int cdcacm_control_request(usbd_device *usbd_dev, struct usb_setup_data *req, uint8_t **buf, uint16_t *len, usbd_control_complete_callback *complete); +extern enum usbd_request_return_codes cdcacm_control_request(usbd_device *usbd_dev, struct usb_setup_data *req, uint8_t **buf, uint16_t *len, usbd_control_complete_callback *complete); extern void cdcacm_tick(void); extern int cdcacm_write(char *ptr, int len); extern void cdcacm_set_config(usbd_device *usbd_dev, uint16_t wValue); @@ -15,7 +15,7 @@ extern void cdcacm_rings_init(void); extern const struct usb_dfu_descriptor dfu_function; extern const struct usb_interface_descriptor dfu_iface; extern const struct usb_iface_assoc_descriptor dfu_iface_assoc; -extern int dfu_control_request(usbd_device *usbd_dev, struct usb_setup_data *req, uint8_t **buf, uint16_t *len, usbd_control_complete_callback *complete); +extern enum usbd_request_return_codes dfu_control_request(usbd_device *usbd_dev, struct usb_setup_data *req, uint8_t **buf, uint16_t *len, usbd_control_complete_callback *complete); /* i2c_bb.c */ extern void i2c_bb_init(void); extern void i2c_bb_poll(void); diff --git a/app/serial_over_dp.ld b/app/serial_over_dp.ld index a2d5a9c..20cbff9 100644 --- a/app/serial_over_dp.ld +++ b/app/serial_over_dp.ld @@ -27,7 +27,7 @@ MEMORY } /* Include the common ld script. */ -INCLUDE libopencm3_stm32f1.ld +INCLUDE cortex-m-generic.ld dfu_shared_location = ORIGIN(ram) + LENGTH(ram) - 1024; diff --git a/app/usb.c b/app/usb.c index 8023ea3..277b881 100644 --- a/app/usb.c +++ b/app/usb.c @@ -60,6 +60,7 @@ static const struct usb_config_descriptor config = { .bMaxPower = 0x32, .interface = ifaces, }; +#define N_USB_STRINGS (sizeof(usb_strings)/sizeof(usb_strings[0])) static const char *usb_strings[] = { VENDOR_NAME, /*1*/ @@ -69,7 +70,6 @@ static const char *usb_strings[] = { "DFU interface", /*5*/ "Debug interface", /*6*/ }; -#define N_USB_STRINGS (sizeof(usb_strings)/sizeof(usb_strings[0])) void usb_hp_can_tx_isr (void) { @@ -81,16 +81,25 @@ void usb_lp_can_rx0_isr (void) usbd_poll (usb_device); } -static int control_request (usbd_device *usbd_dev, - struct usb_setup_data *req, - uint8_t **buf, - uint16_t *len, - usbd_control_complete_callback *complete) +static enum usbd_request_return_codes control_request (usbd_device *usbd_dev, + struct usb_setup_data *req, + uint8_t **buf, + uint16_t *len, + usbd_control_complete_callback *complete) { - if (dfu_control_request (usbd_dev, req, buf, len, complete)) - return 1; + enum usbd_request_return_codes ret; + + ret = dfu_control_request (usbd_dev, req, buf, len, complete); + + if (ret != USBD_REQ_NEXT_CALLBACK) + return ret; + + ret = cdcacm_control_request (usbd_dev, req, buf, len, complete); + + if (ret != USBD_REQ_NEXT_CALLBACK) + return ret; - return cdcacm_control_request (usbd_dev, req, buf, len, complete); + return USBD_REQ_NOTSUPP; } static void set_config (usbd_device *usbd_dev, uint16_t wValue) @@ -119,7 +128,7 @@ void usb_init (void) gpio_set_mode (GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO13); - usb_device = usbd_init (&stm32f103_usb_driver, + usb_device = usbd_init ( &st_usbfs_v1_usb_driver, &dev, &config, usb_strings, diff --git a/boot/usbdfu.c b/boot/usbdfu.c index 9287f57..e547e4b 100644 --- a/boot/usbdfu.c +++ b/boot/usbdfu.c @@ -151,7 +151,7 @@ static uint8_t usbdfu_getstatus (usbd_device *usbd_dev, uint32_t *bwPollTimeout) } } -static int usbdfu_getstatus_complete (usbd_device *usbd_dev, struct usb_setup_data *req) +static void usbdfu_getstatus_complete (usbd_device *usbd_dev, struct usb_setup_data *req) { int i; (void)req; @@ -189,40 +189,38 @@ static int usbdfu_getstatus_complete (usbd_device *usbd_dev, struct usb_setup_da /* Jump straight to dfuDNLOAD-IDLE, skipping dfuDNLOAD-SYNC. */ usbdfu_state = STATE_DFU_DNLOAD_IDLE; - return 0; + return; case STATE_DFU_MANIFEST: /* USB device must detach, we just reset... */ scb_reset_system(); - return 0; /* Will never return. */ + return; /* Will never return. */ default: ; } - return 0; } -static int usbdfu_control_request (usbd_device *usbd_dev, struct usb_setup_data *req, uint8_t **buf, +static enum usbd_request_return_codes usbdfu_control_request (usbd_device *usbd_dev, struct usb_setup_data *req, uint8_t **buf, uint16_t *len, usbd_control_complete_callback *complete) { if ((req->bmRequestType & 0x7F) != 0x21) - return 0; /* Only accept class request. */ + return USBD_REQ_NEXT_CALLBACK; switch (req->bRequest) { case DFU_DNLOAD: if ((len == NULL) || (*len == 0)) { usbdfu_state = STATE_DFU_MANIFEST_SYNC; - return 1; } else { /* Copy download data for use on GET_STATUS. */ prog.blocknum = req->wValue; prog.len = *len; memcpy (prog.buf, *buf, *len); usbdfu_state = STATE_DFU_DNLOAD_SYNC; - return 1; } + return USBD_REQ_HANDLED; case DFU_CLRSTATUS: @@ -230,16 +228,16 @@ static int usbdfu_control_request (usbd_device *usbd_dev, struct usb_setup_data if (usbdfu_state == STATE_DFU_ERROR) usbdfu_state = STATE_DFU_IDLE; - return 1; + return USBD_REQ_HANDLED; case DFU_ABORT: /* Abort returns to dfuIDLE state. */ usbdfu_state = STATE_DFU_IDLE; - return 1; + return USBD_REQ_HANDLED; case DFU_UPLOAD: /* Upload not supported for now. */ - return 0; + return USBD_REQ_NOTSUPP; case DFU_GETSTATUS: { uint32_t bwPollTimeout = 0; /* 24-bit integer in DFU class spec */ @@ -251,17 +249,17 @@ static int usbdfu_control_request (usbd_device *usbd_dev, struct usb_setup_data (*buf)[5] = 0; /* iString not used here */ *len = 6; *complete = usbdfu_getstatus_complete; - return 1; + return USBD_REQ_HANDLED; } case DFU_GETSTATE: /* Return state with no state transision. */ *buf[0] = usbdfu_state; *len = 1; - return 1; + return USBD_REQ_HANDLED; } - return 0; + return USBD_REQ_NEXT_CALLBACK; } static void usb_reset (void) @@ -316,7 +314,7 @@ int main (void) dfu_flag = 0; - rcc_clock_setup_in_hsi_out_48mhz(); + rcc_clock_setup_pll(&rcc_hsi_configs[RCC_CLOCK_HSI_48MHZ]); rcc_periph_clock_enable (RCC_GPIOC); @@ -325,7 +323,7 @@ int main (void) gpio_set (GPIOC, GPIO11); - usbd_dev = usbd_init (&stm32f103_usb_driver, &dev, &config, usb_strings, N_USB_STRINGS, usbd_control_buffer, sizeof (usbd_control_buffer)); + usbd_dev = usbd_init (&st_usbfs_v1_usb_driver, &dev, &config, usb_strings, N_USB_STRINGS, usbd_control_buffer, sizeof (usbd_control_buffer)); usbd_register_set_config_callback (usbd_dev, usb_set_config); diff --git a/boot/usbdfu.ld b/boot/usbdfu.ld index 8680dc9..498bcea 100644 --- a/boot/usbdfu.ld +++ b/boot/usbdfu.ld @@ -27,7 +27,7 @@ MEMORY } /* Include the common ld script. */ -INCLUDE libopencm3_stm32f1.ld +INCLUDE cortex-m-generic.ld dfu_shared_location = ORIGIN(ram) + LENGTH(ram) - 1024; diff --git a/libopencm3 b/libopencm3 index 5c73d60..a9cc695 160000 --- a/libopencm3 +++ b/libopencm3 @@ -1 +1 @@ -Subproject commit 5c73d601763dd140bff020be8d6d06f03c2bea78 +Subproject commit a9cc6953817e0de1cad06662ce58a81607cd796b -- cgit v1.2.3