From 6a233ff4f58e269536a73d48de102730c0267de9 Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Thu, 15 Dec 2022 14:11:22 -0500 Subject: [PATCH] usb_cdc: Resync USB code with upstream code from Klipper Signed-off-by: Kevin O'Connor --- src/Kconfig | 1 + src/deployer.c | 2 +- src/generic/armcm_reset.c | 1 + src/generic/armcm_reset.h | 6 ++++++ src/generic/armcm_timer.h | 8 ++++++++ src/generic/misc.h | 4 ++-- src/generic/usb_cdc.h | 1 - src/generic/usb_cdc_ep.h | 4 ++-- src/generic/usbstd.h | 6 ++++++ src/lpc176x/internal.h | 1 + src/lpc176x/usbserial.c | 14 +++++++++++--- src/stm32/usbfs.c | 14 ++++++++++---- src/stm32/usbotg.c | 38 ++++++++++++++++++++++++-------------- 13 files changed, 73 insertions(+), 27 deletions(-) create mode 100644 src/generic/armcm_reset.h create mode 100644 src/generic/armcm_timer.h diff --git a/src/Kconfig b/src/Kconfig index a240184..d9bf308 100644 --- a/src/Kconfig +++ b/src/Kconfig @@ -78,6 +78,7 @@ config CANBUS_FILTER bool default y if CANSERIAL +# Support setting gpio state at startup config INITIAL_PINS string "GPIO pins to set on bootloader entry" depends on LOW_LEVEL_OPTIONS diff --git a/src/deployer.c b/src/deployer.c index 27872db..0388df5 100644 --- a/src/deployer.c +++ b/src/deployer.c @@ -6,9 +6,9 @@ #include // memcmp #include "autoconf.h" // CONFIG_BLOCK_SIZE +#include "board/armcm_reset.h" // try_request_canboot #include "board/flash.h" // flash_write_block #include "board/io.h" // readb -#include "board/misc.h" // try_request_canboot #include "deployer.h" // deployer_is_active #include "sched.h" // sched_check_periodic diff --git a/src/generic/armcm_reset.c b/src/generic/armcm_reset.c index 3afbac9..0457e21 100644 --- a/src/generic/armcm_reset.c +++ b/src/generic/armcm_reset.c @@ -5,6 +5,7 @@ // This file may be distributed under the terms of the GNU GPLv3 license. #include "autoconf.h" // CONFIG_FLASH_BOOT_ADDRESS +#include "armcm_reset.h" // try_request_canboot #include "board/internal.h" // NVIC_SystemReset #include "board/irq.h" // irq_disable #include "board/misc.h" // try_request_canboot diff --git a/src/generic/armcm_reset.h b/src/generic/armcm_reset.h new file mode 100644 index 0000000..1627edd --- /dev/null +++ b/src/generic/armcm_reset.h @@ -0,0 +1,6 @@ +#ifndef __GENERIC_ARMCM_RESET_H +#define __GENERIC_ARMCM_RESET_H + +void try_request_canboot(void); + +#endif // armcm_reset.h diff --git a/src/generic/armcm_timer.h b/src/generic/armcm_timer.h new file mode 100644 index 0000000..6d6f5ac --- /dev/null +++ b/src/generic/armcm_timer.h @@ -0,0 +1,8 @@ +#ifndef __GENERIC_ARMCM_TIMER_H +#define __GENERIC_ARMCM_TIMER_H + +#include // uint32_t + +void udelay(uint32_t usecs); + +#endif // armcm_timer.h diff --git a/src/generic/misc.h b/src/generic/misc.h index f1afb66..97d9591 100644 --- a/src/generic/misc.h +++ b/src/generic/misc.h @@ -18,8 +18,6 @@ void application_read_flash(uint32_t address, uint32_t *dest); int application_check_valid(void); void application_jump(void); -void try_request_canboot(void); - void timer_setup(void); uint32_t timer_from_us(uint32_t us); @@ -32,4 +30,6 @@ void *dynmem_end(void); uint16_t crc16_ccitt(uint8_t *buf, uint_fast8_t len); +void bootloader_request(void); + #endif // misc.h diff --git a/src/generic/usb_cdc.h b/src/generic/usb_cdc.h index f955c84..3c92ef1 100644 --- a/src/generic/usb_cdc.h +++ b/src/generic/usb_cdc.h @@ -21,7 +21,6 @@ int_fast8_t usb_send_ep0_progmem(const void *data, uint_fast8_t len); void usb_stall_ep0(void); void usb_set_address(uint_fast8_t addr); void usb_set_configure(void); -void usb_request_bootloader(void); struct usb_string_descriptor *usbserial_get_serialid(void); // usb_cdc.c diff --git a/src/generic/usb_cdc_ep.h b/src/generic/usb_cdc_ep.h index 1ca97a7..f752158 100644 --- a/src/generic/usb_cdc_ep.h +++ b/src/generic/usb_cdc_ep.h @@ -3,9 +3,9 @@ // Default USB endpoint ids enum { - USB_CDC_EP_ACM = 1, + USB_CDC_EP_BULK_IN = 1, USB_CDC_EP_BULK_OUT = 2, - USB_CDC_EP_BULK_IN = 3, + USB_CDC_EP_ACM = 3, }; #endif // usb_cdc_ep.h diff --git a/src/generic/usbstd.h b/src/generic/usbstd.h index 07d0c1c..2cec37d 100644 --- a/src/generic/usbstd.h +++ b/src/generic/usbstd.h @@ -8,6 +8,12 @@ #define USB_DIR_OUT 0 /* to device */ #define USB_DIR_IN 0x80 /* to host */ +#define USB_TYPE_MASK (0x03 << 5) +#define USB_TYPE_STANDARD (0x00 << 5) +#define USB_TYPE_CLASS (0x01 << 5) +#define USB_TYPE_VENDOR (0x02 << 5) +#define USB_TYPE_RESERVED (0x03 << 5) + #define USB_REQ_GET_STATUS 0x00 #define USB_REQ_CLEAR_FEATURE 0x01 #define USB_REQ_SET_FEATURE 0x03 diff --git a/src/lpc176x/internal.h b/src/lpc176x/internal.h index 3adb7e1..2358701 100644 --- a/src/lpc176x/internal.h +++ b/src/lpc176x/internal.h @@ -23,5 +23,6 @@ int is_enabled_pclock(uint32_t pclk); void enable_pclock(uint32_t pclk); uint32_t get_pclock_frequency(uint32_t pclk); void gpio_peripheral(uint32_t gpio, int func, int pullup); +void usb_disconnect(void); #endif // internal.h diff --git a/src/lpc176x/usbserial.c b/src/lpc176x/usbserial.c index 4d251ce..7a54c84 100644 --- a/src/lpc176x/usbserial.c +++ b/src/lpc176x/usbserial.c @@ -5,13 +5,12 @@ // This file may be distributed under the terms of the GNU GPLv3 license. #include // memcpy -#include "autoconf.h" // CONFIG_SMOOTHIEWARE_BOOTLOADER #include "board/armcm_boot.h" // armcm_enable_irq -#include "board/irq.h" // irq_disable +#include "board/armcm_timer.h" // udelay #include "board/misc.h" // timer_read_time -#include "generic/usb_cdc.h" // usb_notify_ep0 #include "byteorder.h" // cpu_to_le32 #include "command.h" // DECL_CONSTANT_STR +#include "generic/usb_cdc.h" // usb_notify_ep0 #include "internal.h" // gpio_peripheral #include "sched.h" // DECL_INIT #include "usb_cdc_ep.h" // USB_CDC_EP_BULK_IN @@ -244,6 +243,15 @@ usb_set_configure(void) usb_irq_enable(); } +// Force a USB disconnect (used during reboot into bootloader) +void +usb_disconnect(void) +{ + sie_cmd_write(SIE_CMD_SET_DEVICE_STATUS, 0); + udelay(5000); +} + + /**************************************************************** * Setup and interrupts ****************************************************************/ diff --git a/src/stm32/usbfs.c b/src/stm32/usbfs.c index 77f74f2..f7559ef 100644 --- a/src/stm32/usbfs.c +++ b/src/stm32/usbfs.c @@ -6,6 +6,7 @@ #include // NULL #include "board/armcm_boot.h" // armcm_enable_irq +#include "board/armcm_timer.h" // udelay #include "board/gpio.h" // gpio_out_setup #include "board/io.h" // writeb #include "board/usb_cdc.h" // usb_notify_ep0 @@ -14,12 +15,12 @@ #include "internal.h" // GPIO #include "sched.h" // DECL_INIT -#if CONFIG_MACH_STM32F103 +#if CONFIG_MACH_STM32F103 || CONFIG_MACH_STM32G4 // Transfer memory is accessed with 32bits, but contains only 16bits of data typedef volatile uint32_t epmword_t; #define WSIZE 2 #define USBx_IRQn USB_LP_IRQn -#elif CONFIG_MACH_STM32F0 +#elif CONFIG_MACH_STM32F0 || CONFIG_MACH_STM32L4 // Transfer memory is accessed with 16bits and contains 16bits of data typedef volatile uint16_t epmword_t; #define WSIZE 2 @@ -28,9 +29,14 @@ // Transfer memory is accessed with 32bits and contains 32bits of data typedef volatile uint32_t epmword_t; #define WSIZE 4 - #define USBx_IRQn USB_UCPD1_2_IRQn + #define USBx_IRQn USB_IRQn +#endif - // The stm32g0 has slightly different register names +// The stm32g0 has slightly different register names +#if CONFIG_MACH_STM32G0 + #if CONFIG_MACH_STM32G0B1 + #define USB_IRQn USB_UCPD1_2_IRQn + #endif #define USB USB_DRD_FS #define USB_PMAADDR USB_DRD_PMAADDR #define USB_EPADDR_FIELD USB_CHEP_ADDR diff --git a/src/stm32/usbotg.c b/src/stm32/usbotg.c index d655110..b61c99c 100644 --- a/src/stm32/usbotg.c +++ b/src/stm32/usbotg.c @@ -15,21 +15,31 @@ #include "sched.h" // DECL_INIT #if CONFIG_STM32_USB_PB14_PB15 -#define USB_PERIPH_BASE USB_OTG_HS_PERIPH_BASE -#define OTG_IRQn OTG_HS_IRQn -#define USBOTGEN RCC_AHB1ENR_USB1OTGHSEN -#define GPIO_D_NEG GPIO('B', 14) -#define GPIO_D_POS GPIO('B', 15) -#define GPIO_FUNC GPIO_FUNCTION(12) -DECL_CONSTANT_STR("RESERVE_PINS_USB1", "PB14,PB15"); + #define IS_OTG_HS 1 + #define GPIO_D_NEG GPIO('B', 14) + #define GPIO_D_POS GPIO('B', 15) + #define GPIO_FUNC GPIO_FUNCTION(12) + DECL_CONSTANT_STR("RESERVE_PINS_USB1", "PB14,PB15"); #else -#define USB_PERIPH_BASE USB_OTG_FS_PERIPH_BASE -#define OTG_IRQn OTG_FS_IRQn -#define USBOTGEN RCC_AHB1ENR_USB2OTGHSEN -#define GPIO_D_NEG GPIO('A', 11) -#define GPIO_D_POS GPIO('A', 12) -#define GPIO_FUNC GPIO_FUNCTION(10) -DECL_CONSTANT_STR("RESERVE_PINS_USB", "PA11,PA12"); + #if CONFIG_MACH_STM32H723 + #define IS_OTG_HS 1 + #else + #define IS_OTG_HS 0 + #endif + #define GPIO_D_NEG GPIO('A', 11) + #define GPIO_D_POS GPIO('A', 12) + #define GPIO_FUNC GPIO_FUNCTION(10) + DECL_CONSTANT_STR("RESERVE_PINS_USB", "PA11,PA12"); +#endif + +#if IS_OTG_HS + #define USB_PERIPH_BASE USB_OTG_HS_PERIPH_BASE + #define OTG_IRQn OTG_HS_IRQn + #define USBOTGEN RCC_AHB1ENR_USB1OTGHSEN +#else + #define USB_PERIPH_BASE USB_OTG_FS_PERIPH_BASE + #define OTG_IRQn OTG_FS_IRQn + #define USBOTGEN RCC_AHB1ENR_USB2OTGHSEN #endif static void