diff --git a/README.md b/README.md index 558ae10..a5abd8d 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,67 @@ This implements a basic serial echo using the TinyUSB implementation of a CDC dr - Make sure you do not put extra device classes that aren't implemented in your code (like mass storage) otherwise it will not work! I've only included the minimum required for CDC to work. - TinyUSB seems to be very unforgiving for newcomers like me, since there are big pitfalls (previous dotpoint) and no obvious way of debugging it using only SWD (My board does not have SWO or secondary UART for their debugging messages since I have used every pin on it), but I will say once it is set up it is way better than the builtin STM32 HAL USB middleware, which sucks because of stuff like needing a delay between CDC transmits, since it does not buffer each transmit. +## Instructions + +- Enable USB device (but not the builtin STM32 HAL USB CDC middleware) +- Enable the USB/CAN high priority and low priority interrupts +- Copy the `usb_descriptors.c` `usb_descriptors.h` `tusb_config.h` files to your project +- Copy the `cdc_task` function into `main.c`. Call `tusb_init()` after the devices have been initialized in user code 2, then call `tud_task()` and `cdc_task()` in the main loop. Optionally copy the `renumerate_usb` function and call it before `tusb_init` but after `MX_USB_PCD_Init` to have the device renumerate to the computer whenever it is reset or re-flashed. +- Add the interrupt handler to the USB/CAN global interrupts in `stm32f3xx_it.c`. Either return early or use an `#if 0` macro to comment out the autogenerated USB IRQ handler (which should not be called). + +```c +/** + * @brief This function handles CAN TX and USB high priority interrupts. + */ +void USB_HP_CAN_TX_IRQHandler(void) +{ + /* USER CODE BEGIN USB_HP_CAN_TX_IRQn 0 */ + tud_int_handler(BOARD_TUD_RHPORT); +#if 0 + /* USER CODE END USB_HP_CAN_TX_IRQn 0 */ + HAL_PCD_IRQHandler(&hpcd_USB_FS); + /* USER CODE BEGIN USB_HP_CAN_TX_IRQn 1 */ +#endif + /* USER CODE END USB_HP_CAN_TX_IRQn 1 */ +} + +/** + * @brief This function handles CAN RX0 and USB low priority interrupts. + */ +void USB_LP_CAN_RX0_IRQHandler(void) +{ + /* USER CODE BEGIN USB_LP_CAN_RX0_IRQn 0 */ + tud_int_handler(BOARD_TUD_RHPORT); +#if 0 + /* USER CODE END USB_LP_CAN_RX0_IRQn 0 */ + HAL_PCD_IRQHandler(&hpcd_USB_FS); + /* USER CODE BEGIN USB_LP_CAN_RX0_IRQn 1 */ +#endif + /* USER CODE END USB_LP_CAN_RX0_IRQn 1 */ +} +``` + +- Change parameters in `usb_descriptors.c` to match your application. + +```c +#define USB_VID 0xCaff +#define USB_BCD 0x0200 + +(...) + +// array of pointer to string descriptors +char const *string_desc_arr[] = { + (const char[]){0x09, 0x04}, // 0: is supported language is English (0x0409) + "Peterus", // 1: Manufacturer + "Neptunium", // 2: Product + NULL, // 3: Serials will use unique ID if possible + "Neptunium CDC", // 4: CDC Interface + "Neptunium MSC", // 5: MSC Interface +}; +``` + +Note on Windows the "Friendly Name" shown in Device Manager is determined by the driver, not these descriptors. + ## License MIT License diff --git a/Src/usb_descriptors.c b/Src/usb_descriptors.c index 6b30301..6526db8 100644 --- a/Src/usb_descriptors.c +++ b/Src/usb_descriptors.c @@ -245,7 +245,6 @@ char const *string_desc_arr[] = { "Neptunium", // 2: Product NULL, // 3: Serials will use unique ID if possible "Neptunium CDC", // 4: CDC Interface - "Neptunium MSC", // 5: MSC Interface }; #define DESC_STR_SIZE 32 // bytes