nt9856x/rtos/code/application/source/cardv/SrcCode/UIApp/UsbCdc/UIAppUsbCdc.c
2023-03-28 15:07:53 +08:00

185 lines
4.0 KiB
C
Executable File

#include "PrjInc.h"
#include <console.h>
#include "USBCDC.h"
///////////////////////////////////////////////////////////////////////////////
#define __MODULE__ UIAppUsbCdc
#define __DBGLVL__ 2 // 0=FATAL, 1=ERR, 2=WRN, 3=UNIT, 4=FUNC, 5=IND, 6=MSG, 7=VALUE, 8=USER
#define __DBGFLT__ "*" //*=All, [mark]=CustomClass
#include <kwrap/debug.h>
///////////////////////////////////////////////////////////////////////////////
#define CDC_LINEENCODING_OneStopBit 0
#define CDC_LINEENCODING_OneAndHalfStopBits 1
#define CDC_LINEENCODING_TwoStopBits 2
#define CDC_PARITY_None 0
#define CDC_PARITY_Odd 1
#define CDC_PARITY_Even 2
#define CDC_PARITY_Mark 3
#define CDC_PARITY_Space 4
_ALIGNED(4) const static UINT8 gCDCManuStrDesc[] =
{
USB_VENDER_DESC_STRING_LEN*2+2, // size of String Descriptor = 6 bytes
0x03, // 03: String Descriptor type
USB_VENDER_DESC_STRING
};
_ALIGNED(4) const static UINT8 gCDCProdStrDesc[] =
{
USB_PRODUCT_DESC_STRING_LEN*2+2, // size of String Descriptor = 6 bytes
0x03, // 03: String Descriptor type
USB_PRODUCT_DESC_STRING
};
_ALIGNED(16) UINT8 gCDCInquiryData[36] =
{
0x00, 0x80, 0x05, 0x02, 0x20, 0x00, 0x00, 0x00,
//Vendor identification,
USB_VENDER_STRING,/*'\0',*/
USB_PRODUCT_STRING, '\0',
USB_PRODUCT_REVISION
};
#define TERMNULL 0 // NULL
#define BACKSPACE 8 // back space
#define NEWLINE 10 // new line
#define UART_ENTER 13
static void CDC_hook(void)
{
}
static void CDC_unhook(void)
{
CDC_AbortRead();
}
static void CDC_puts(const char* s)
{
UINT32 DataSize = 0;
if (s == NULL || *s == TERMNULL) {
return;
}
DataSize = strlen(s);
CDC_AsyncWriteData((UINT8 *)s, DataSize);
CDC_AsyncWaitWriteDone();
}
static int CDC_gets(char* s, int len)
{
UINT32 TempSize = len;
int uiStringLen = 0;
while ((int)uiStringLen < len) {
if (E_OK != CDC_ReadData((UINT8 *)s, &TempSize)) {
break;
}
if (*s == BACKSPACE) {
if (uiStringLen) {
uiStringLen--;
s--;
TempSize = len - uiStringLen;
}
} else if ((*s == NEWLINE) || (*s == UART_ENTER)) {
CDC_puts("\n");
*s = TERMNULL;
break;
} else {
uiStringLen += TempSize;
s += TempSize;
TempSize = len - uiStringLen;
}
}
return uiStringLen;
}
CONSOLE CON_CDC = {CDC_hook, CDC_unhook, CDC_puts, CDC_gets};
static BOOL CdcPstnReqCB(CDC_PSTN_REQUEST Code, UINT8 *pData, UINT32 *pDataLen)
{
BOOL bSupported = TRUE;
CDCLineCoding LineCoding;
switch (Code)
{
case REQ_GET_LINE_CODING:
DBG_DUMP("Get Line Coding\r\n");
LineCoding.uiBaudRateBPS = 115200;
LineCoding.uiCharFormat = CDC_LINEENCODING_OneStopBit;
LineCoding.uiParityType = CDC_PARITY_None;
LineCoding.uiDataBits = 8;
*pDataLen = sizeof(LineCoding);
memcpy(pData, &LineCoding, *pDataLen);
break;
case REQ_SET_LINE_CODING:
DBG_DUMP("Set Line Coding\r\n");
if (*pDataLen == sizeof(LineCoding)) {
memcpy(&LineCoding, pData, *pDataLen);
} else {
bSupported = FALSE;
}
break;
case REQ_SET_CONTROL_LINE_STATE:
DBG_DUMP("Control Line State = 0x%X\r\n", *(UINT16 *)pData);
// debug console test
if (*(UINT16 *)pData & 0x3) { // console ready
// set console to cdc
console_set_curr(&CON_CDC);
}
break;
case REQ_SEND_BREAK:
DBG_DUMP("Send Break = 0x%X\r\n", *(UINT16 *)pData);
break;
default:
bSupported = FALSE;
break;
}
return bSupported;
}
CONSOLE gDefaultConsole = {0};
static BOOL gCdcOpen = FALSE;
void UIApp_CdcOpen(void)
{
USB_CDC_INFO CDCInfo = {0};
if (gCdcOpen == FALSE) {
CDCInfo.VID = 0x0603;
CDCInfo.PID = 0x8613;
CDCInfo.pManuStringDesc = (USB_STRING_DESC *)gCDCManuStrDesc;
CDCInfo.pProdStringDesc = (USB_STRING_DESC *)gCDCProdStrDesc;
CDCInfo.pSerialStringDesc = NULL;
console_get_curr(&gDefaultConsole);
CDC_SetConfig(CDC_CONFIG_PSTN_REQUEST_CB, (UINT32)CdcPstnReqCB);
CDC_SetConfig(CDC_CONFIG_ASYNC_WRITE_MODE, (UINT32)CDC_ASYNC_WRITE_DEBUG_MODE);
if (CDC_Open(&CDCInfo) != E_OK) {
DBG_ERR("Error open USB CDC task\r\n");
} else {
gCdcOpen = TRUE;
}
}
}
void UIApp_CdcClose(void)
{
if (gCdcOpen) {
console_set_curr(&gDefaultConsole);
CDC_Close();
gCdcOpen = FALSE;
}
}