📄 usbacm_adap.c
字号:
savedMask = SaveAndSetIRQMask();
Buf_GetRoomLeft(&(USB2UARTPort.Tx_Buffer_ISR),roomleft);
RestoreIRQMask(savedMask);
data = *(Buffaddr+index);
/* if the character is special character, translate it. PC has the ability to distinguish it*/
if (data == USB2UARTPort.DCB.xonChar)
{
if ( roomleft >= 2 )
{
Buf_Push(&(USB2UARTPort.Tx_Buffer_ISR),escape_char);
Buf_Push(&(USB2UARTPort.Tx_Buffer_ISR),0x01);
}
else
{
break;
}
}
else if (data == USB2UARTPort.DCB.xoffChar)
{
if ( roomleft >= 2 )
{
Buf_Push(&(USB2UARTPort.Tx_Buffer_ISR),escape_char);
Buf_Push(&(USB2UARTPort.Tx_Buffer_ISR),0x02);
}
else
{
break;
}
}
else if (data == escape_char)
{
if ( roomleft >= 2 )
{
Buf_Push(&(USB2UARTPort.Tx_Buffer_ISR),escape_char);
Buf_Push(&(USB2UARTPort.Tx_Buffer_ISR),0x03);
}
else
{
break;
}
}
else
{
if (roomleft)
{
Buf_Push(&(USB2UARTPort.Tx_Buffer_ISR),*(Buffaddr+index));
}
else
{
break;
}
}
}
real_count = index;
}
savedMask = SaveAndSetIRQMask();
/* in case usb is plug out just before this critical section*/
if(gUsbDevice.device_type == USB_CDC_ACM)
{
if(USB_DMA_Get_Run_Status(g_UsbACM.txpipe->byEP)==KAL_FALSE)
{
USB2UART_DMATransmit();
}
}
RestoreIRQMask(savedMask);
#ifndef __PRODUCTION_RELEASE__
if(ownerid != USB2UARTPort.ownerid)
{
EXT_ASSERT( 0, (kal_uint32) ownerid, (kal_uint32)USB2UARTPort.ownerid, 0);
}
// kal_prompt_trace(MOD_USB, "SendISR %d", real_count);
#endif /* __PRODUCTION_RELEASE__ */
return real_count;
}
/* set owner for UART port */
static void USB2UART_setowner(UART_PORT port, kal_uint8 owner)
{
#ifdef __DSPIRDBG__
if(g_UsbACM.acm_owner!=USB_ACM_OWNER_UART)
{
EXT_ASSERT(0, g_UsbACM.acm_owner, owner, 0);
}
#endif
#ifndef __PRODUCTION_RELEASE__
kal_prompt_trace(MOD_USB, "setowner\n");
#endif /* __PRODUCTION_RELEASE__ */
USB2UARTPort.ownerid = owner;
}
/* get current UART owner id */
static module_type USB2UART_GetOwnerID(UART_PORT port)
{
return USB2UARTPort.ownerid;
}
/* config escape charater and guard time */
static void USB2UART_ConfigEscape(UART_PORT port, kal_uint8 EscChar, kal_uint16 ESCGuardtime, module_type ownerid)
{
#ifdef __DSPIRDBG__
if(g_UsbACM.acm_owner!=USB_ACM_OWNER_UART)
{
EXT_ASSERT(0, g_UsbACM.acm_owner, USB2UARTPort.ownerid, 0);
}
#endif
#ifndef __PRODUCTION_RELEASE__
if(ownerid != USB2UARTPort.ownerid)
{
EXT_ASSERT( 0, (kal_uint32) ownerid, 0, 0);
}
#endif /* __PRODUCTION_RELEASE__ */
USB2UARTPort.ESCDet.EscChar = EscChar;
USB2UARTPort.ESCDet.GuardTime = ESCGuardtime;
if (USB2UARTPort.ESCDet.GuardTime)
{
USB2UARTPort.Rec_state = UART_RecNormal;
GPTI_StartItem(USB2UARTPort.handle, (USB2UARTPort.ESCDet.GuardTime/10),
USB2UART_Timeout, &USB2UARTPort);
}
}
static void USB2UART_SetDCBConfig(UART_PORT port, UARTDCBStruct *UART_Config, module_type ownerid)
{
#ifdef __DSPIRDBG__
if(g_UsbACM.acm_owner!=USB_ACM_OWNER_UART)
{
EXT_ASSERT(0, g_UsbACM.acm_owner, USB2UARTPort.ownerid, 0);
}
#endif
#ifndef __PRODUCTION_RELEASE__
if(ownerid != USB2UARTPort.ownerid)
{
EXT_ASSERT( 0, (kal_uint32) ownerid, 0, 0);
}
#endif /* __PRODUCTION_RELEASE__ */
kal_mem_cpy(&USB2UARTPort.DCB, UART_Config, sizeof(UARTDCBStruct));
}
static void USB2UART_Purge(UART_PORT port, UART_buffer dir, module_type ownerid)
{
#ifdef __DSPIRDBG__
if(g_UsbACM.acm_owner!=USB_ACM_OWNER_UART)
{
EXT_ASSERT(0, g_UsbACM.acm_owner, USB2UARTPort.ownerid, 0);
}
#endif
#ifndef __PRODUCTION_RELEASE__
if(ownerid != USB2UARTPort.ownerid)
{
EXT_ASSERT( 0, (kal_uint32) ownerid, 0, 0);
}
#endif /* __PRODUCTION_RELEASE__ */
}
static void USB2UART_SetFlowCtrl(UART_PORT port, kal_bool XON, module_type ownerid)
{
#ifdef __DSPIRDBG__
if(g_UsbACM.acm_owner!=USB_ACM_OWNER_UART)
{
EXT_ASSERT(0, g_UsbACM.acm_owner, USB2UARTPort.ownerid, 0);
}
#endif
#ifndef __PRODUCTION_RELEASE__
if(ownerid != USB2UARTPort.ownerid)
{
EXT_ASSERT( 0, (kal_uint32) ownerid, 0, 0);
}
#endif /* __PRODUCTION_RELEASE__ */
}
static void USB2UART_CtrlDCD(UART_PORT port, IO_level SDCD, module_type ownerid)
{
#ifdef __DSPIRDBG__
if(g_UsbACM.acm_owner!=USB_ACM_OWNER_UART)
{
EXT_ASSERT(0, g_UsbACM.acm_owner, USB2UARTPort.ownerid, 0);
}
#endif
#ifndef __PRODUCTION_RELEASE__
if(ownerid != USB2UARTPort.ownerid)
{
EXT_ASSERT( 0, (kal_uint32) ownerid, 0, 0);
}
#endif /* __PRODUCTION_RELEASE__ */
}
static void USB2UART_CtrlBreak(UART_PORT port, IO_level SBREAK, module_type ownerid)
{
#ifdef __DSPIRDBG__
if(g_UsbACM.acm_owner!=USB_ACM_OWNER_UART)
{
EXT_ASSERT(0, g_UsbACM.acm_owner, USB2UARTPort.ownerid, 0);
}
#endif
#ifndef __PRODUCTION_RELEASE__
if(ownerid != USB2UARTPort.ownerid)
{
EXT_ASSERT( 0, (kal_uint32) ownerid, 0, 0);
}
#endif /* __PRODUCTION_RELEASE__ */
}
static void USB2UART_SetBaudRate(UART_PORT port, UART_baudrate baudrate, module_type ownerid)
{
#ifdef __DSPIRDBG__
if(g_UsbACM.acm_owner!=USB_ACM_OWNER_UART)
{
EXT_ASSERT(0, g_UsbACM.acm_owner, USB2UARTPort.ownerid, 0);
}
#endif
#ifndef __PRODUCTION_RELEASE__
if(ownerid != USB2UARTPort.ownerid)
{
EXT_ASSERT( 0, (kal_uint32) ownerid, 0, 0);
}
#endif /* __PRODUCTION_RELEASE__ */
}
static void USB2UART_SetAutoBaud_Div(UART_PORT port, module_type ownerid)
{
#ifdef __DSPIRDBG__
if(g_UsbACM.acm_owner!=USB_ACM_OWNER_UART)
{
EXT_ASSERT(0, g_UsbACM.acm_owner, USB2UARTPort.ownerid, 0);
}
#endif
#ifndef __PRODUCTION_RELEASE__
if(ownerid != USB2UARTPort.ownerid)
{
EXT_ASSERT( 0, (kal_uint32) ownerid, 0, 0);
}
#endif /* __PRODUCTION_RELEASE__ */
}
static void USB2UART_Register_TX_Callback(UART_PORT port, module_type ownerid, UART_TX_FUNC func)
{
#ifdef __DSPIRDBG__
if(g_UsbACM.acm_owner!=USB_ACM_OWNER_UART)
{
EXT_ASSERT(0, g_UsbACM.acm_owner, USB2UARTPort.ownerid, 0);
}
#endif
#ifndef __PRODUCTION_RELEASE__
if(ownerid != USB2UARTPort.ownerid)
{
EXT_ASSERT( 0, (kal_uint32) ownerid, 0, 0);
}
#endif /* __PRODUCTION_RELEASE__ */
USB2UARTPort.tx_cb = func;
}
static void USB2UART_Register_RX_Callback(UART_PORT port, module_type ownerid, UART_RX_FUNC func)
{
#ifdef __DSPIRDBG__
if(g_UsbACM.acm_owner!=USB_ACM_OWNER_UART)
{
EXT_ASSERT(0, g_UsbACM.acm_owner, USB2UARTPort.ownerid, 0);
}
#endif
#ifndef __PRODUCTION_RELEASE__
if(ownerid != USB2UARTPort.ownerid)
{
EXT_ASSERT( 0, (kal_uint32) ownerid, 0, 0);
}
#endif /* __PRODUCTION_RELEASE__ */
USB2UARTPort.rx_cb = func;
}
/* This function is only used for retrive exception log*/
static kal_uint8 USB2UART_GetUARTByte(UART_PORT port)
{
kal_uint32 real_count = 0;
kal_uint32 RoomLeft = 0;
kal_uint8 data;
#ifdef __DSPIRDBG__
if(g_UsbACM.acm_owner!=USB_ACM_OWNER_UART)
{
EXT_ASSERT(0, g_UsbACM.acm_owner, USB2UARTPort.ownerid, 0);
}
#endif
/* This function can only be called after exception*/
if(INT_Exception_Enter == 0)
ASSERT(0);
if ((gUsbDevice.device_type != USB_CDC_ACM)||(USB2UARTPort.initialized == KAL_FALSE))
{
/* This function should only be called when assertion, if not CDC_ACM type, trap in while loop*/
while(1);
}
Buf_GetBytesAvail(&(USB2UARTPort.Rx_Buffer),real_count);
while(real_count==0)
{
/* polling received data*/
USB2UART_Polling_Recv_Data();
Buf_GetBytesAvail(&(USB2UARTPort.Rx_Buffer),real_count);
}
Buf_Pop(&(USB2UARTPort.Rx_Buffer),data); /* one byte at a time */
/* set unmask flag if room left is larger than USB_EP_BULK_MAXP */
Buf_GetRoomLeft(&(USB2UARTPort.Rx_Buffer),RoomLeft);
/* only unmask IRQ if ring buffer romm left >= 64 */
if(RoomLeft >= USB_EP_BULK_MAXP)
{
USB_Set_UnMask_Irq(KAL_TRUE);
}
return data;
}
/* This function is only used for retrive exception log*/
static void USB2UART_PutUARTByte(UART_PORT port, kal_uint8 data)
{
#ifdef __DSPIRDBG__
if(g_UsbACM.acm_owner!=USB_ACM_OWNER_UART)
{
EXT_ASSERT(0, g_UsbACM.acm_owner, USB2UARTPort.ownerid, 0);
}
#endif
/* This function can only be called after exception*/
if(INT_Exception_Enter == 0)
ASSERT(0);
if ((gUsbDevice.device_type != USB_CDC_ACM)||(USB2UARTPort.initialized == KAL_FALSE))
{
/* This function should only be called when assertion, if not CDC_ACM type, return it directly*/
return;
}
/* flush previous data first*/
USB2UART_Polling_Flush_Transmit_Data();
/* put data to tx queue*/
/* push data from caller buffer to ring buffer */
Buf_Push(&(USB2UARTPort.Tx_Buffer), data);
/* flush the tx queue*/
USB2UART_Polling_Flush_Transmit_Data();
}
/* This function is only used for retrive exception log*/
static void USB2UART_PutUARTBytes(UART_PORT port, kal_uint8 *Buffaddr, kal_uint16 Length)
{
kal_uint32 index;
kal_uint16 data_length = 0;
kal_uint16 offset = 0;
#ifdef __DSPIRDBG__
if(g_UsbACM.acm_owner!=USB_ACM_OWNER_UART)
{
EXT_ASSERT(0, g_UsbACM.acm_owner, USB2UARTPort.ownerid, 0);
}
#endif
/* This function can only be called after exception*/
if(INT_Exception_Enter == 0)
ASSERT(0);
if ( (gUsbDevice.device_type != USB_CDC_ACM)||(USB2UARTPort.initialized == KAL_FALSE))
{
/* This function should only be called when assertion, if not CDC_ACM type, return it directly*/
return;
}
/* flush previous data first*/
USB2UART_Polling_Flush_Transmit_Data();
/* If data length is larger than TX ring buffer, seperate to send as seceral times*/
do
{
if((Length - offset) > g_UsbACM.acm_param->tx_ringbuff_size)
{
data_length = g_UsbACM.acm_param->tx_ringbuff_size;
}
else
{
data_length = Length - offset;
}
/* put data to tx queue*/
/* push data from caller buffer to ring buffer */
for (index = 0; index < data_length; index++)
{
Buf_Push(&(USB2UARTPort.Tx_Buffer),*(Buffaddr+offset+index));
}
offset+=data_length;
/* flush the tx queue*/
USB2UART_Polling_Flush_Transmit_Data();
}while(offset<Length);
}
static void USB2UART_ReadDCBConfig(UART_PORT port, UARTDCBStruct *DCB)
{
#ifdef __DSPIRDBG__
if(g_UsbACM.acm_owner != USB_ACM_OWNER_UART)
{
EXT_ASSERT(0, g_UsbACM.acm_owner, USB2UARTPort.ownerid, 0);
}
#endif
kal_mem_cpy(DCB, &USB2UARTPort.DCB, sizeof(UARTDCBStruct));
}
static void USB2UART_CtrlRI (UART_PORT port, IO_level SRI, module_type ownerid)
{
#ifdef __DSPIRDBG__
if(g_UsbACM.acm_owner != USB_ACM_OWNER_UART)
{
EXT_ASSERT(0, g_UsbACM.acm_owner, USB2UARTPort.ownerid, 0);
}
#endif
#ifndef __PRODUCTION_RELEASE__
if(ownerid != USB2UARTPort.ownerid)
{
EXT_ASSERT( 0, (kal_uint32) ownerid, 0, 0);
}
#endif /* __PRODUCTION_RELEASE__ */
}
static void USB2UART_CtrlDTR (UART_PORT port, IO_level SDTR, module_type ownerid)
{
#ifdef __DSPIRDBG__
if(g_UsbACM.acm_owner != USB_ACM_OWNER_UART)
{
EXT_ASSERT(0, g_UsbACM.acm_owner, USB2UARTPort.ownerid, 0);
}
#endif
#ifndef __PRODUCTION_RELEASE__
if(ownerid != USB2UARTPort.ownerid)
{
EXT_ASSERT( 0, (kal_uint32) ownerid, 0, 0);
}
#endif /* __PRODUCTION_RELEASE__ */
}
static void USB2UART_ReadHWStatus(UART_PORT port, IO_level *SDSR, IO_level *SCTS)
{
#ifdef __DSPIRDBG__
if(g_UsbACM.acm_owner!=USB_ACM_OWNER_UART)
{
EXT_ASSERT(0, g_UsbACM.acm_owner, USB2UARTPort.ownerid, 0);
}
#endif
kal_mem_set(SDSR, 0, sizeof(IO_level));
kal_mem_set(SCTS, 0, sizeof(IO_level));
}
static const UartDriver_strcut USB2UartDriver =
{
USB2UART_open,
USB2UART_close,
USB2UART_GetBytes,
USB2UART_PutBytes,
USB2UART_GetRxAvail,
USB2UART_GetTxRoomLeft,
USB2UART_PutISRBytes,
USB2UART_GetTxISRRoomLeft,
USB2UART_Purge,
USB2UART_setowner,
USB2UART_SetFlowCtrl,
USB2UART_ConfigEscape,
USB2UART_SetDCBConfig,
USB2UART_CtrlDCD,
USB2UART_CtrlBreak,
USB2UART_ClrRxBuffer,
USB2UART_ClrTxBuffer,
USB2UART_SetBaudRate,
USB2UART_SendISRData,
USB2UART_SendData,
USB2UART_GetOwnerID,
USB2UART_SetAutoBaud_Div,
USB2UART_Register_TX_Callback,
USB2UART_Register_RX_Callback,
/* Add for retrive exception log */
USB2UART_GetUARTByte,
USB2UART_PutUARTByte,
USB2UART_PutUARTBytes,
USB2UART_ReadDCBConfig,
USB2UART_CtrlRI,
USB2UART_CtrlDTR,
USB2UART_ReadHWStatus
};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -