文章目录
- 前言
- 分析
- 端点 0
- USBHS_UIS_TOKEN_OUT
- 端点 2
- USBHS_UIS_TOKEN_OUT
- USBHS_UIS_TOKEN_IN
前言
虚拟串口,端口 3 单向上报,端口 2 双向收发。
分析
端点 0
USBHS_UIS_TOKEN_OUT
设置串口参数:
- 判断 USBHS_SetupReqCode == CDC_SET_LINE_CODING;
- 波特率:USBHS_EP0_Buf 前四个字节,小端模式;
- 停止位:USBHS_EP0_Buf 第五个字节(0:1 停止位;1:1.5 停止位;2:2 停止位);
- 校验位:USBHS_EP0_Buf 第六个字节(0:None;1:Odd;2:Even;3:Mark;4:Space);
- 数据位:USBHS_EP0_Buf 第七个字节();
/* Non-standard request end-point 0 Data download */
if( USBHS_SetupReqCode == CDC_SET_LINE_CODING )
{
/* Save relevant parameters such as serial port baud rate */
/* The downlinked data is processed in the endpoint 0 OUT packet, the 7 bytes of the downlink are, in order
4 bytes: baud rate value: lowest baud rate byte, next lowest baud rate byte, next highest baud rate byte, highest baud rate byte.
1 byte: number of stop bits (0: 1 stop bit; 1: 1.5 stop bit; 2: 2 stop bits).
1 byte: number of parity bits (0: None; 1: Odd; 2: Even; 3: Mark; 4: Space).
1 byte: number of data bits (5,6,7,8,16); */
Uart.Com_Cfg[ 0 ] = USBHS_EP0_Buf[ 0 ];
Uart.Com_Cfg[ 1 ] = USBHS_EP0_Buf[ 1 ];
Uart.Com_Cfg[ 2 ] = USBHS_EP0_Buf[ 2 ];
Uart.Com_Cfg[ 3 ] = USBHS_EP0_Buf[ 3 ];
Uart.Com_Cfg[ 4 ] = USBHS_EP0_Buf[ 4 ];
Uart.Com_Cfg[ 5 ] = USBHS_EP0_Buf[ 5 ];
Uart.Com_Cfg[ 6 ] = USBHS_EP0_Buf[ 6 ];
Uart.Com_Cfg[ 7 ] = DEF_UARTx_RX_TIMEOUT;
/* save bauds */
baudrate = USBHS_EP0_Buf[ 0 ];
baudrate += ((uint32_t)USBHS_EP0_Buf[ 1 ] << 8 );
baudrate += ((uint32_t)USBHS_EP0_Buf[ 2 ] << 16 );
baudrate += ((uint32_t)USBHS_EP0_Buf[ 3 ] << 24 );
Uart.Com_Cfg[ 7 ] = Uart.Rx_TimeOutMax;
/* Uart usb init */
UART_USB_Init( );
}
端点 2
USBHS_UIS_TOKEN_OUT
USBHSD->UEP2_RX_CTRL ^= USBHS_UEP_R_TOG_DATA1;
/* Record related information & Switch DMA Address*/
Uart.Tx_PackLen[ Uart.Tx_LoadNum ] = USBHSD->RX_LEN;
Uart.Tx_LoadNum++;
USBHSD->UEP2_RX_DMA = (uint32_t)(uint8_t *)&UART2_Tx_Buf[ ( Uart.Tx_LoadNum * DEF_USB_HS_PACK_LEN ) ];
if( Uart.Tx_LoadNum >= DEF_UARTx_TX_BUF_NUM_MAX )
{
Uart.Tx_LoadNum = 0x00;
USBHSD->UEP2_RX_DMA = (uint32_t)(uint8_t *)&UART2_Tx_Buf[ 0 ];
}
Uart.Tx_RemainNum++;
/* Determine if the downlink needs to be paused */
if( Uart.Tx_RemainNum >= ( DEF_UARTx_TX_BUF_NUM_MAX - 2 ) )
{
USBHSD->UEP2_RX_CTRL &= ~USBHS_UEP_R_RES_MASK;
USBHSD->UEP2_RX_CTRL |= USBHS_UEP_R_RES_NAK;
Uart.USB_Down_StopFlag = 0x01;
}
USBHS_UIS_TOKEN_IN
Uart.USB_Up_IngFlag = 0x00;