72 lines
1.9 KiB
C
72 lines
1.9 KiB
C
/* Includes ----------------------------------------------------------------- */
|
|
#include "bsp/usb.h"
|
|
|
|
#include <stdarg.h>
|
|
#include <stdbool.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
#include "bsp/delay.h"
|
|
#include "usbd_cdc_if.h"
|
|
|
|
/* Private define ----------------------------------------------------------- */
|
|
/* Private macro ------------------------------------------------------------ */
|
|
/* Private typedef ---------------------------------------------------------- */
|
|
/* Private variables -------------------------------------------------------- */
|
|
/* Must set to NULL explicitly. */
|
|
osThreadId_t gbsp_usb_alert = NULL;
|
|
uint8_t usb_rx_buf[BSP_USB_MAX_RX_LEN];
|
|
uint8_t usb_tx_buf[BSP_USB_MAX_TX_LEN];
|
|
|
|
/* Private function -------------------------------------------------------- */
|
|
static int8_t BSP_USB_Transmit(uint8_t *buffer, uint16_t len) {
|
|
uint8_t retry = 0;
|
|
do {
|
|
if (CDC_Transmit_HS(buffer, len) != USBD_OK) {
|
|
retry++;
|
|
BSP_Delay(10);
|
|
} else {
|
|
break;
|
|
}
|
|
} while (retry < 3);
|
|
return BSP_OK;
|
|
}
|
|
|
|
/* Exported functions ------------------------------------------------------- */
|
|
int8_t BSP_USB_ReadyReceive(osThreadId_t alert) {
|
|
if (alert == NULL) return BSP_ERR_NULL;
|
|
|
|
gbsp_usb_alert = alert;
|
|
CDC_ReadyReceive();
|
|
return BSP_OK;
|
|
}
|
|
|
|
char BSP_USB_ReadChar(void) {
|
|
static uint16_t read_idx = 0;
|
|
char ch = 0;
|
|
if (usb_rx_buf[read_idx] != 0) {
|
|
ch = usb_rx_buf[read_idx];
|
|
usb_rx_buf[read_idx] = 0; // 清空已读
|
|
read_idx++;
|
|
if (read_idx >= BSP_USB_MAX_RX_LEN) read_idx = 0;
|
|
}
|
|
return ch;
|
|
}
|
|
|
|
int8_t BSP_USB_Printf(const char *fmt, ...) {
|
|
va_list ap;
|
|
uint16_t len = 0;
|
|
|
|
va_start(ap, fmt);
|
|
len =
|
|
(uint16_t)vsnprintf((char *)usb_tx_buf, BSP_USB_MAX_TX_LEN - 1, fmt, ap);
|
|
va_end(ap);
|
|
|
|
if (len > 0) {
|
|
BSP_USB_Transmit(usb_tx_buf, len);
|
|
return BSP_OK;
|
|
} else {
|
|
return BSP_ERR_NULL;
|
|
}
|
|
}
|