# TinyUSB CDC on STM32F302C8 with STM32 HAL minimum configuration This implements a basic serial echo using the TinyUSB implementation of a CDC driver. ## Important Notes - This was designed for a custom board using a **16 MHz** crystal. Change the frequency by using the clock solver to whatever is your board's frequency. - Project was generated using STM32CubeMX. The project is designed to be opened in either vscode using the embedded tools extension or STM32CubeIDE. - I've based it off the notes in the [TinyUSB discussions](https://github.com/hathach/tinyusb/discussions/633) and [this repository](https://github.com/Jo5ta/stm32_freeRTOS_tinyUSB_SCPI/tree/main) - 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 Copyright (c) 2024 PETER TANNER Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.