driver/usb/usb_uart.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /** | ||
| 2 | ****************************************************************************** | ||
| 3 | * @file : usb_uart.c | ||
| 4 | * @brief : Legacy UART-to-USB Serial Implementation | ||
| 5 | * @author : Sun Devil Rocketry Firmware Team | ||
| 6 | * | ||
| 7 | * @note This file is conditionally included by usb.c when USE_USB_CDC_FS is | ||
| 8 | * NOT defined. It provides the UART-backed implementation of the USB | ||
| 9 | * driver API for legacy boards using a UART-to-USB bridge. | ||
| 10 | * | ||
| 11 | * Do NOT compile this file directly. It is included as a translation | ||
| 12 | * unit by usb.c based on the active build configuration. | ||
| 13 | * | ||
| 14 | * Legacy board support: | ||
| 15 | * - A0002 (Flight Computer) → UART-to-USB bridge | ||
| 16 | * - L0002 (Engine Controller) → UART-to-USB bridge | ||
| 17 | * - L0005 (Valve Controller) → UART-to-USB bridge | ||
| 18 | * - A0005 (Ground Station) → UART-to-USB bridge | ||
| 19 | * - A0007 (Flight Computer Lite) → UART-to-USB bridge | ||
| 20 | * | ||
| 21 | ****************************************************************************** | ||
| 22 | * @attention | ||
| 23 | * Copyright (c) 2026 Sun Devil Rocketry. All rights reserved. | ||
| 24 | * This software is licensed under terms that can be found in the LICENSE | ||
| 25 | * file in the root directory of this software component. | ||
| 26 | * If no LICENSE file comes with this software, it is covered under the | ||
| 27 | * BSD-3-Clause (https://opensource.org/license/bsd-3-clause). | ||
| 28 | ****************************************************************************** | ||
| 29 | */ | ||
| 30 | |||
| 31 | |||
| 32 | /* Transmits a specified number of bytes over USB */ | ||
| 33 | 8 | USB_STATUS usb_transmit | |
| 34 | ( | ||
| 35 | void* tx_data_ptr , | ||
| 36 | size_t tx_data_size , | ||
| 37 | uint32_t timeout | ||
| 38 | ) | ||
| 39 | { /* ==================== API implementation =================== */ | ||
| 40 | |||
| 41 | 8 | HAL_StatusTypeDef usb_status; | |
| 42 | |||
| 43 | /* Transmit bytes over UART */ | ||
| 44 | 8 | usb_status = HAL_UART_Transmit( &( USB_HUART ), | |
| 45 | tx_data_ptr , | ||
| 46 | tx_data_size , | ||
| 47 | timeout ); | ||
| 48 | |||
| 49 | /* Return HAL status */ | ||
| 50 |
1/3✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
|
8 | switch ( usb_status ) { |
| 51 | case HAL_TIMEOUT: | ||
| 52 | { | ||
| 53 | return USB_TIMEOUT; | ||
| 54 | 8 | break; | |
| 55 | } | ||
| 56 | 8 | case HAL_OK: | |
| 57 | { | ||
| 58 | 8 | return USB_OK; | |
| 59 | ✗ | break; | |
| 60 | } | ||
| 61 | ✗ | default: | |
| 62 | { | ||
| 63 | ✗ | return USB_FAIL; | |
| 64 | 8 | break; | |
| 65 | } | ||
| 66 | } | ||
| 67 | |||
| 68 | } /* usb_transmit */ | ||
| 69 | |||
| 70 | |||
| 71 | /* Receives a specified number of bytes from the USB port */ | ||
| 72 | 774 | USB_STATUS usb_receive | |
| 73 | ( | ||
| 74 | void* rx_data_ptr , | ||
| 75 | size_t rx_data_size , | ||
| 76 | uint32_t timeout | ||
| 77 | ) | ||
| 78 | { /* ==================== API implementation =================== */ | ||
| 79 | |||
| 80 | 774 | HAL_StatusTypeDef usb_status; | |
| 81 | |||
| 82 | /* Receive bytes over UART */ | ||
| 83 | 774 | usb_status = HAL_UART_Receive( &( USB_HUART ), | |
| 84 | rx_data_ptr , | ||
| 85 | rx_data_size , | ||
| 86 | timeout ); | ||
| 87 | |||
| 88 | /* Return HAL status */ | ||
| 89 |
1/3✓ Branch 0 taken 773 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
|
773 | switch ( usb_status ) { |
| 90 | case HAL_TIMEOUT: | ||
| 91 | { | ||
| 92 | return USB_TIMEOUT; | ||
| 93 | 773 | break; | |
| 94 | } | ||
| 95 | 773 | case HAL_OK: | |
| 96 | { | ||
| 97 | 773 | return USB_OK; | |
| 98 | ✗ | break; | |
| 99 | } | ||
| 100 | ✗ | default: | |
| 101 | { | ||
| 102 | ✗ | return USB_FAIL; | |
| 103 | 773 | break; | |
| 104 | } | ||
| 105 | } | ||
| 106 | |||
| 107 | } /* usb_receive */ | ||
| 108 | |||
| 109 | |||
| 110 | /* Transmits a specified number of bytes over USB */ | ||
| 111 | ✗ | USB_STATUS usb_transmit_IT | |
| 112 | ( | ||
| 113 | void* tx_data_ptr , /* Data to be sent */ | ||
| 114 | size_t tx_data_size /* Size of transmit data */ | ||
| 115 | ) | ||
| 116 | { | ||
| 117 | /*------------------------------------------------------------------------------ | ||
| 118 | Local Variables | ||
| 119 | ------------------------------------------------------------------------------*/ | ||
| 120 | ✗ | HAL_StatusTypeDef usb_status; | |
| 121 | |||
| 122 | |||
| 123 | /*------------------------------------------------------------------------------ | ||
| 124 | API Function Implementation | ||
| 125 | ------------------------------------------------------------------------------*/ | ||
| 126 | |||
| 127 | /* Transmit byte */ | ||
| 128 | ✗ | usb_status = HAL_UART_Transmit_IT( &( USB_HUART ), | |
| 129 | tx_data_ptr , | ||
| 130 | tx_data_size ); | ||
| 131 | |||
| 132 | /* Return HAL status */ | ||
| 133 | ✗ | if ( usb_status != HAL_OK ) | |
| 134 | { | ||
| 135 | return usb_status; | ||
| 136 | } | ||
| 137 | else | ||
| 138 | { | ||
| 139 | ✗ | return USB_OK; | |
| 140 | } | ||
| 141 | |||
| 142 | } /* usb_transmit_IT */ | ||
| 143 | |||
| 144 | |||
| 145 | /* Receive bytes without blocking. */ | ||
| 146 | ✗ | USB_STATUS usb_receive_IT | |
| 147 | ( | ||
| 148 | void* rx_data_ptr , /* Buffer to export data to */ | ||
| 149 | size_t rx_data_size /* Size of the data to be received */ | ||
| 150 | ) | ||
| 151 | { | ||
| 152 | /*------------------------------------------------------------------------------ | ||
| 153 | Local Variables | ||
| 154 | ------------------------------------------------------------------------------*/ | ||
| 155 | ✗ | HAL_StatusTypeDef usb_status; | |
| 156 | |||
| 157 | |||
| 158 | /*------------------------------------------------------------------------------ | ||
| 159 | API Function Implementation | ||
| 160 | ------------------------------------------------------------------------------*/ | ||
| 161 | |||
| 162 | /* Transmit byte */ | ||
| 163 | ✗ | usb_status = HAL_UART_Receive_IT( &( USB_HUART ), | |
| 164 | rx_data_ptr , | ||
| 165 | rx_data_size ); | ||
| 166 | |||
| 167 | /* Return HAL status */ | ||
| 168 | ✗ | switch ( usb_status ) | |
| 169 | { | ||
| 170 | case HAL_TIMEOUT: | ||
| 171 | { | ||
| 172 | return USB_TIMEOUT; | ||
| 173 | ✗ | break; | |
| 174 | } | ||
| 175 | ✗ | case HAL_OK: | |
| 176 | { | ||
| 177 | ✗ | return USB_OK; | |
| 178 | ✗ | break; | |
| 179 | } | ||
| 180 | ✗ | default: | |
| 181 | { | ||
| 182 | ✗ | return USB_FAIL; | |
| 183 | ✗ | break; | |
| 184 | } | ||
| 185 | } | ||
| 186 | |||
| 187 | } /* usb_receive_IT */ | ||
| 188 | |||
| 189 | |||
| 190 | /* Detects an active USB connection via VBUS GPIO */ | ||
| 191 | #if defined( A0002_REV2 ) || \ | ||
| 192 | defined( FLIGHT_COMPUTER_LITE ) || \ | ||
| 193 | defined( L0002_REV5 ) || \ | ||
| 194 | defined( L0005_REV3 ) | ||
| 195 | 771 | bool usb_detect( void ) | |
| 196 | { /* ==================== API implementation =================== */ | ||
| 197 | /* Read voltage on USB detect pin and compare against GPIO Enum */ | ||
| 198 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 771 times.
|
771 | if (HAL_GPIO_ReadPin(USB_DETECT_GPIO_PORT, USB_DETECT_PIN) == GPIO_PIN_SET) { |
| 199 | return true; | ||
| 200 | } else { | ||
| 201 | ✗ | return false; | |
| 202 | } | ||
| 203 | } /* usb_detect */ | ||
| 204 | #endif /* A0002_REV2 || FLIGHT_COMPUTER_LITE || L0002_REV5 || L0005_REV3 */ | ||
| 205 | |||
| 206 | |||
| 207 | /* Flushes pending data from the receive buffer */ | ||
| 208 | ✗ | void usb_flush( void ) | |
| 209 | { /* ==================== API implementation =================== */ | ||
| 210 | |||
| 211 | /* Cycle receive register until no data remains */ | ||
| 212 | ✗ | while ( __HAL_UART_GET_FLAG( &( USB_HUART ), UART_FLAG_RXNE ) ){ | |
| 213 | ✗ | __HAL_UART_FLUSH_DRREGISTER( &( USB_HUART ) ); | |
| 214 | } | ||
| 215 | |||
| 216 | ✗ | } /* usb_flush */ | |
| 217 | |||
| 218 | |||
| 219 | /******************************************************************************* | ||
| 220 | * END OF usb_uart.c * | ||
| 221 | *******************************************************************************/ | ||
| 222 |