sc8-gat-stand/gat_stand_fw/user/periph/usb/cdc.h

90 lines
4.3 KiB
C

/*
* cdc.h
*
* Created on: Nov 6, 2024
* Author: true
*/
#ifndef USER_USB_CDC_H_
#define USER_USB_CDC_H_
#ifdef __cplusplus
extern "C" {
#endif
#include <stdio.h>
#include <string.h>
// CDC buffer
#define DEF_CDC_SEND_HOST_BUF_LEN ( 2 * 512 ) // device to host buffer size
#define DEF_CDC_FROM_HOST_BUF_LEN ( 2 * 512 ) // host to device buffer size
#define DEF_USB_FS_PACKET_LEN 64 // USB full speed mode packet size for CDC data
#define DEF_CDC_FROM_HOST_BUF_NUM_MAX (DEF_CDC_FROM_HOST_BUF_LEN / DEF_USB_FS_PACKET_LEN) // Serial x transmit buffer size
// CDC default settings
#define DEF_CDC_BAUDRATE 115200 /* Default baud rate for serial port */
#define DEF_CDC_STOPBIT 0 /* Default stop bit for serial port */
#define DEF_CDC_PARITY 0 /* Default parity bit for serial port */
#define DEF_CDC_DATABIT 8 /* Default data bit for serial port */
typedef struct __attribute__((packed)) _UART_CTL
{
uint16_t send_host_ptr; // data location for data being added to the buffer
uint16_t send_host_idx; // data location for data being sent over USB
volatile uint16_t send_host_count; // amount of data to send
uint16_t from_host_ptr;
uint16_t from_host_idx;
uint16_t from_host_count;
volatile uint16_t Tx_LoadNum; /* Serial x data send buffer load number */
volatile uint16_t Tx_DealNum; /* Serial x data send buffer processing number */
volatile uint16_t from_host_remain; /* Serial x data send buffer remaining unprocessed number */
volatile uint16_t Tx_PackLen[DEF_CDC_FROM_HOST_BUF_NUM_MAX]; /* The current packet length of the serial x data send buffer */
uint8_t from_host_flag; /* Serial x data send status */
uint8_t pad1;
uint16_t Tx_CurPackLen; /* The current packet length sent by serial port x */
uint16_t Tx_CurPackPtr; /* Pointer to the packet currently being sent by serial port x */
uint8_t usb_send_host_flag; /* Serial xUSB packet being uploaded flag */
uint8_t usb_send_host_zero_flag; /* Serial xUSB data needs to upload 0-length packet flag */
uint8_t usb_from_host_stop_flag; /* Serial xUSB packet stop down flag */
uint8_t pad2;
uint8_t cdc_cfg[ 8 ]; /* Serial x parameter configuration (default baud rate is 115200, 1 stop bit, no parity, 8 data bits) */
uint8_t usb_send_flag; /* Serial x interrupt upload status */
uint8_t pad3;
uint16_t USB_Int_UpTimeCount; /* Serial x interrupt upload timing */
} CDC_CTL, *PCDC_CTL;
/***********************************************************************************************************************/
/* Constant, variable extents */
/* The following are serial port transmit and receive related variables and buffers */
extern volatile CDC_CTL Cdc; /* Serial x control related structure */
extern __attribute__((aligned(4))) uint8_t cdc_from[DEF_CDC_FROM_HOST_BUF_LEN];
extern __attribute__((aligned(4))) uint8_t cdc_send[DEF_CDC_SEND_HOST_BUF_LEN];
/***********************************************************************************************************************/
void CDC_USB_Init(void);
void cdc_from_host_process(void);
void cdc_send_host_process(void);
int usb_cdc_puts(char *buf, int cnt);
int usb_cdc_gets(char *buf, int cnt);
#ifdef __cplusplus
}
#endif
#endif /* USER_USB_CDC_H_ */