driver/usb/usb.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /** | ||
| 2 | ****************************************************************************** | ||
| 3 | * @file : usb.c | ||
| 4 | * @brief : Universal Serial Driver Interface | ||
| 5 | * @author : Sun Devil Rocketry Firmware Team | ||
| 6 | * | ||
| 7 | * @note Hardware Assumptions: | ||
| 8 | * Target: STM32H733 (A0010, Rev 3 PCB) | ||
| 9 | * Legacy: STM32H750xx boards using UART-to-USB bridge (A0002, L0002, L0005) | ||
| 10 | * | ||
| 11 | * For Native USB CDC (USE_USB_CDC_FS): | ||
| 12 | * - USB_OTG_HS enabled as Internal FS Phy (Device Only) | ||
| 13 | * - USB_DEVICE Middleware set to Communication Device Class (CDC) | ||
| 14 | * - USB clock must be exactly 48 MHz | ||
| 15 | * - VBUS sensing handled via USB_DETECT GPIO (configured as Input | ||
| 16 | * with Internal Pull-Down to prevent floating when unplugged) | ||
| 17 | * | ||
| 18 | * For Legacy UART: | ||
| 19 | * - Standard UART TX/RX pins configured | ||
| 20 | * - Baud rate: 921600 | ||
| 21 | * | ||
| 22 | * Usage Example (Terminal Echo): | ||
| 23 | * | ||
| 24 | * #include "usb.h" | ||
| 25 | * | ||
| 26 | * int main(void) { | ||
| 27 | * #ifdef USE_USB_CDC_FS | ||
| 28 | * usb_init(); | ||
| 29 | * #else | ||
| 30 | * usb_init(&huart3); | ||
| 31 | * #endif | ||
| 32 | * | ||
| 33 | * uint8_t rx_byte; | ||
| 34 | * while (1) { | ||
| 35 | * if (usb_receive(&rx_byte, 1, 10) == USB_OK) { | ||
| 36 | * usb_transmit(&rx_byte, 1, 10); | ||
| 37 | * } | ||
| 38 | * } | ||
| 39 | * } | ||
| 40 | ****************************************************************************** | ||
| 41 | * @attention | ||
| 42 | * Copyright (c) 2026 Sun Devil Rocketry. All rights reserved. | ||
| 43 | * This software is licensed under terms that can be found in the LICENSE | ||
| 44 | * file in the root directory of this software component. | ||
| 45 | * If no LICENSE file comes with this software, it is covered under the | ||
| 46 | * BSD-3-Clause (https://opensource.org/license/bsd-3-clause). | ||
| 47 | ****************************************************************************** | ||
| 48 | */ | ||
| 49 | |||
| 50 | /*------------------------------------------------------------------------------ | ||
| 51 | Standard Includes | ||
| 52 | ------------------------------------------------------------------------------*/ | ||
| 53 | #include <stdbool.h> | ||
| 54 | #include <stddef.h> | ||
| 55 | |||
| 56 | |||
| 57 | /*------------------------------------------------------------------------------ | ||
| 58 | MCU Pins | ||
| 59 | ------------------------------------------------------------------------------*/ | ||
| 60 | #if defined( FLIGHT_COMPUTER ) | ||
| 61 | #include "sdr_pin_defines_A0002.h" | ||
| 62 | #elif defined( ENGINE_CONTROLLER ) | ||
| 63 | #include "sdr_pin_defines_L0002.h" | ||
| 64 | #elif defined( VALVE_CONTROLLER ) | ||
| 65 | #include "sdr_pin_defines_L0005.h" | ||
| 66 | #elif defined( GROUND_STATION ) | ||
| 67 | #include "sdr_pin_defines_A0005.h" | ||
| 68 | #elif defined( FLIGHT_COMPUTER_LITE ) | ||
| 69 | #include "sdr_pin_defines_A0007.h" | ||
| 70 | #elif defined( A0010 ) | ||
| 71 | #include "sdr_pin_defines_A0010.h" | ||
| 72 | #endif | ||
| 73 | |||
| 74 | |||
| 75 | /*------------------------------------------------------------------------------ | ||
| 76 | Project Includes | ||
| 77 | ------------------------------------------------------------------------------*/ | ||
| 78 | #include "main.h" | ||
| 79 | #include "usb.h" | ||
| 80 | |||
| 81 | #ifdef USE_USB_CDC_FS | ||
| 82 | #include "usb_device.h" | ||
| 83 | #include "usbd_cdc_if.h" | ||
| 84 | #endif /* USE_USB_CDC_FS */ | ||
| 85 | |||
| 86 | |||
| 87 | /*------------------------------------------------------------------------------ | ||
| 88 | Preprocessor Directives | ||
| 89 | ------------------------------------------------------------------------------*/ | ||
| 90 | #ifdef USE_USB_CDC_FS | ||
| 91 | #define USB_FLUSH_RX_TIMEOUT_MS 10 /* Max wait to drain receive buffer */ | ||
| 92 | #define USB_FLUSH_TX_TIMEOUT_MS 10 /* Max wait for TX completion */ | ||
| 93 | #endif /* USE_USB_CDC_FS */ | ||
| 94 | |||
| 95 | |||
| 96 | /*------------------------------------------------------------------------------ | ||
| 97 | Global Variables | ||
| 98 | ------------------------------------------------------------------------------*/ | ||
| 99 | |||
| 100 | |||
| 101 | /*------------------------------------------------------------------------------ | ||
| 102 | Procedures | ||
| 103 | ------------------------------------------------------------------------------*/ | ||
| 104 | |||
| 105 | /* Initializes the USB or UART driver based on build configuration */ | ||
| 106 | ✗ | USB_STATUS usb_init(void) | |
| 107 | { /* ==================== API implementation =================== */ | ||
| 108 | |||
| 109 | #ifdef USE_USB_CDC_FS | ||
| 110 | /* Initialize Native USB CDC Middleware */ | ||
| 111 | MX_USB_DEVICE_Init(); | ||
| 112 | return USB_OK; | ||
| 113 | #else | ||
| 114 | /* For legacy mode, the global USB_HUART is initialized in main.c. | ||
| 115 | We can verify CubeMX assigned the base address. */ | ||
| 116 | ✗ | if (USB_HUART.Instance == NULL) { | |
| 117 | ✗ | return USB_FAIL; | |
| 118 | } | ||
| 119 | return USB_OK; | ||
| 120 | #endif | ||
| 121 | |||
| 122 | } /* usb_init */ | ||
| 123 | |||
| 124 | #ifdef USE_USB_CDC_FS | ||
| 125 | #include "usb_cdc.c" | ||
| 126 | #else | ||
| 127 | #include "usb_uart.c" | ||
| 128 | #endif /* USE_USB_CDC_FS */ | ||
| 129 | |||
| 130 | |||
| 131 | /******************************************************************************* | ||
| 132 | * END OF usb.c * | ||
| 133 | *******************************************************************************/ | ||
| 134 |