📄 usbacm_adap.c
字号:
if(g_UsbACM.acm_owner!=USB_ACM_OWNER_UART)
return;
#endif
USB2UART_ClrRxBuffer(0);
}
/************************************************************
UART driver functions
*************************************************************/
/* open USB2UART port , actually port is no use because only support one USB port */
static kal_bool USB2UART_open(UART_PORT port, module_type owner)
{
#ifndef __USB_ENABLE__
ASSERT(0);
#endif
#ifdef __DSPIRDBG__
if(g_UsbACM.acm_owner!=USB_ACM_OWNER_UART)
{
EXT_ASSERT(0, g_UsbACM.acm_owner, owner, 0);
}
#endif
USB2UARTPort.initialized = KAL_TRUE;
USB2UARTPort.ownerid = owner;
g_UsbACM.send_Txilm = KAL_FALSE;
g_UsbACM.send_Rxilm = KAL_TRUE;
if( (USB2UARTPort.RingBuffers.rx_buffer==NULL) || (USB2UARTPort.RingBuffers.tx_buffer==NULL) ||
(USB2UARTPort.RingBuffers.txISR_buffer==NULL) )
EXT_ASSERT(0, (kal_uint32)USB2UARTPort.RingBuffers.rx_buffer, (kal_uint32)USB2UARTPort.RingBuffers.tx_buffer,
(kal_uint32)USB2UARTPort.RingBuffers.txISR_buffer);
Buf_init(&(USB2UARTPort.Rx_Buffer),(kal_uint8 *)(USB2UARTPort.RingBuffers.rx_buffer),
g_UsbACM.acm_param->rx_ringbuff_size);
Buf_init(&(USB2UARTPort.Tx_Buffer),(kal_uint8 *)(USB2UARTPort.RingBuffers.tx_buffer),
g_UsbACM.acm_param->tx_ringbuff_size);
Buf_init(&(USB2UARTPort.Tx_Buffer_ISR),(kal_uint8 *)(USB2UARTPort.RingBuffers.txISR_buffer),
g_UsbACM.acm_param->txisr_ringbuff_size);
if ((gUsbDevice.device_type == USB_CDC_ACM)&&(gUsbDevice.nDevState==DEVSTATE_CONFIG))
{
/* Only out EP needs to be enabled since IN EP will use DMA polling*/
USB_InEPEn(g_UsbACM.txpipe->byEP, KAL_TRUE);
USB_OutEPEn(g_UsbACM.rxpipe->byEP, KAL_FALSE);
USB_Set_UnMask_Irq(KAL_TRUE);
}
return KAL_TRUE;
}
/* close USB2UART port */
static void USB2UART_close(UART_PORT port)
{
#ifndef __USB_ENABLE__
ASSERT(0);
#endif
#ifdef __DSPIRDBG__
if(g_UsbACM.acm_owner!=USB_ACM_OWNER_UART)
{
EXT_ASSERT(0, g_UsbACM.acm_owner, USB2UARTPort.ownerid, 0);
}
#endif
g_UsbACM.send_Txilm = KAL_FALSE;
USB2UARTPort.initialized = KAL_FALSE;
USB2UARTPort.ownerid = MOD_DRV_HISR;
if ((gUsbDevice.device_type == USB_CDC_ACM)&&(gUsbDevice.nDevState==DEVSTATE_CONFIG))
{
/* disable endpoint interrupt */
USB_InEPDis(g_UsbACM.txpipe->byEP, KAL_TRUE);
USB_OutEPDis(g_UsbACM.rxpipe->byEP, KAL_FALSE);
}
Buf_init(&(USB2UARTPort.Rx_Buffer),(kal_uint8 *)(USB2UARTPort.RingBuffers.rx_buffer),
g_UsbACM.acm_param->rx_ringbuff_size);
Buf_init(&(USB2UARTPort.Tx_Buffer),(kal_uint8 *)(USB2UARTPort.RingBuffers.tx_buffer),
g_UsbACM.acm_param->tx_ringbuff_size);
Buf_init(&(USB2UARTPort.Tx_Buffer_ISR),(kal_uint8 *)(USB2UARTPort.RingBuffers.txISR_buffer),
g_UsbACM.acm_param->txisr_ringbuff_size);
USB2UART_ConfigEscape(port, 0xff, 0);
}
/* clear rx buffer */
static void USB2UART_ClrRxBuffer(UART_PORT port)
{
kal_uint32 savedMask;
#ifdef __DSPIRDBG__
if(g_UsbACM.acm_owner!=USB_ACM_OWNER_UART)
{
EXT_ASSERT(0, g_UsbACM.acm_owner, USB2UARTPort.ownerid, 0);
}
#endif
/* clear hardware fifo if current status is CDC ACM*/
if ( (gUsbDevice.device_type == USB_CDC_ACM) && (USB2UARTPort.initialized == KAL_TRUE)
&&(gUsbDevice.nDevState==DEVSTATE_CONFIG) )
{
/* clear rx fifo*/
USB_Acm_Rx_ClrFifo();
/* clear interrupt mask variable */
USB_Set_UnMask_Irq(KAL_TRUE);
}
savedMask = SaveAndSetIRQMask();
/* clear ring buffer*/
USB2UARTPort.Rx_Buffer.Write = 0;
USB2UARTPort.Rx_Buffer.Read = 0;
g_UsbACM.send_Rxilm = KAL_TRUE;
RestoreIRQMask(savedMask);
}
/* get available bytes in rx buffer */
static kal_uint16 USB2UART_GetRxAvail(UART_PORT port)
{
kal_uint16 real_count;
kal_uint32 savedMask;
#ifdef __DSPIRDBG__
if(g_UsbACM.acm_owner!=USB_ACM_OWNER_UART)
{
EXT_ASSERT(0, g_UsbACM.acm_owner, USB2UARTPort.ownerid, 0);
}
#endif
savedMask = SaveAndSetIRQMask();
Buf_GetBytesAvail(&(USB2UARTPort.Rx_Buffer),real_count);
RestoreIRQMask(savedMask);
return real_count;
}
/* get bytes from rx buffer, parameter status shows escape and break status
return value is the actually get bytes */
static kal_uint16 USB2UART_GetBytes(UART_PORT port, kal_uint8 *Buffaddr, kal_uint16 Length,
kal_uint8 *status, module_type ownerid)
{
kal_uint16 real_count,index;
kal_uint16 RoomLeft;
kal_uint32 savedMask;
#ifdef __DSPIRDBG__
if(g_UsbACM.acm_owner!=USB_ACM_OWNER_UART)
{
EXT_ASSERT(0, g_UsbACM.acm_owner, USB2UARTPort.ownerid, 0);
}
#endif
if(ownerid!=USB2UARTPort.ownerid)
{
#ifdef __PRODUCTION_RELEASE__
return 0;
#else /* __PRODUCTION_RELEASE__ */
EXT_ASSERT( 0, (kal_uint32) ownerid, 0, 0);
#endif /* __PRODUCTION_RELEASE__ */
}
/* return directly if not match condition */
if ( (gUsbDevice.device_type != USB_CDC_ACM) ||
(USB2UARTPort.initialized == KAL_FALSE) || (gUsbDevice.nDevState!=DEVSTATE_CONFIG))
return 0;
/* determine real data count */
/* Note that the area to determine send_Rxilm must also contain in critical section.
Otherwise if USB HISR activated before send_Rxilm setting as true,
this message will be lost */
savedMask = SaveAndSetIRQMask();
Buf_GetBytesAvail(&(USB2UARTPort.Rx_Buffer),real_count);
if ( real_count >= Length)
real_count = Length;
else
g_UsbACM.send_Rxilm = KAL_TRUE; /*After this time get byte, buffer will be empty */
RestoreIRQMask(savedMask);
/* pop data from ring buffer to caller buffer */
for (index = 0; index < real_count; index++)
{
Buf_Pop(&(USB2UARTPort.Rx_Buffer),*(Buffaddr+index));
}
/* update status */
if (status != NULL)
{
*status = 0;
if (USB2UARTPort.EscFound)
{
*status |= UART_STAT_EscDet;
USB2UARTPort.EscFound = KAL_FALSE;
}
if (USB2UARTPort.breakDet)
{
*status |= UART_STAT_Break;
USB2UARTPort.breakDet = KAL_FALSE;
}
}
IRQMask(IRQ_USB_CODE);
Buf_GetRoomLeft(&(USB2UARTPort.Rx_Buffer),RoomLeft);
/* only unmask IRQ if ring buffer room left >= 64 */
if(RoomLeft >= USB_EP_BULK_MAXP)
{
USB_Set_UnMask_Irq(KAL_TRUE);
IRQUnmask(IRQ_USB_CODE);
}
return real_count;
}
/* clear tx buffer */
static void USB2UART_ClrTxBuffer(UART_PORT port)
{
kal_uint32 savedMask;
#ifdef __DSPIRDBG__
if(g_UsbACM.acm_owner!=USB_ACM_OWNER_UART)
{
EXT_ASSERT(0, g_UsbACM.acm_owner, USB2UARTPort.ownerid, 0);
}
#endif
/* stop dma channel if current status is CDC ACM*/
if ( (gUsbDevice.device_type == USB_CDC_ACM) && (USB2UARTPort.initialized == KAL_TRUE)
&&(gUsbDevice.nDevState==DEVSTATE_CONFIG))
{
USB_Stop_DMA_Channel(g_UsbACM.txpipe->byEP);
}
savedMask = SaveAndSetIRQMask();
USB2UARTPort.Tx_Buffer.Write = 0;
USB2UARTPort.Tx_Buffer.Read = 0;
g_UsbACM.send_Txilm = KAL_TRUE;
RestoreIRQMask(savedMask);
}
/*get the left bytes for tx buffer*/
static kal_uint16 USB2UART_GetTxRoomLeft(UART_PORT port)
{
kal_uint16 real_count;
kal_uint32 savedMask;
#ifdef __DSPIRDBG__
if(g_UsbACM.acm_owner!=USB_ACM_OWNER_UART)
{
EXT_ASSERT(0, g_UsbACM.acm_owner, USB2UARTPort.ownerid, 0);
}
#endif
savedMask = SaveAndSetIRQMask();
Buf_GetRoomLeft(&(USB2UARTPort.Tx_Buffer),real_count);
RestoreIRQMask(savedMask);
return real_count;
}
/*put bytes to tx buffer, return value is the actually put out bytes*/
static kal_uint16 USB2UART_PutBytes(UART_PORT port, kal_uint8 *Buffaddr, kal_uint16 Length, module_type ownerid)
{
kal_uint16 real_count,index;
kal_uint32 savedMask;
#ifdef __DSPIRDBG__
if(g_UsbACM.acm_owner!=USB_ACM_OWNER_UART)
{
EXT_ASSERT(0, g_UsbACM.acm_owner, USB2UARTPort.ownerid, 0);
}
#endif
if(ownerid!=USB2UARTPort.ownerid)
{
#ifdef __PRODUCTION_RELEASE__
return 0;
#else /* __PRODUCTION_RELEASE__ */
EXT_ASSERT( 0, (kal_uint32) ownerid, 0, 0);
#endif /* __PRODUCTION_RELEASE__ */
}
/* return directly if not match condition */
if ( (gUsbDevice.device_type != USB_CDC_ACM) ||
(USB2UARTPort.initialized == KAL_FALSE) || (gUsbDevice.nDevState!=DEVSTATE_CONFIG))
{
if((gUsbDevice.device_type == USB_CDC_ACM) && (USB2UARTPort.initialized==KAL_TRUE)
&&(gUsbDevice.nDevState!=DEVSTATE_CONFIG))
{
g_UsbACM.config_send_Txilm = KAL_TRUE; /* for PC set config later then can issue the first message */
}
return 0;
}
/* The same issue as USB2UART_GetBytes()
The area to determine send_Txilm must also contain in critical section.
Otherwise if DMA callback activated before send_Txilm setting as true,
this message will be lost */
savedMask = SaveAndSetIRQMask();
Buf_GetRoomLeft(&(USB2UARTPort.Tx_Buffer),real_count);
/* determine real sent data count */
if (real_count > Length)
real_count = Length;
else
{
g_UsbACM.send_Txilm = KAL_TRUE; /*After this time put bytes, buffer will be full */
g_UsbACM.config_send_Txilm = KAL_TRUE; /* if be reseted, then it can isseu the message waited for*/
}
RestoreIRQMask(savedMask);
/* push data from caller buffer to ring buffer */
for (index = 0; index < real_count; index++)
{
Buf_Push(&(USB2UARTPort.Tx_Buffer),*(Buffaddr+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);
return real_count;
}
/*put bytes to tx buffer, return value is the actually put out bytes*/
static kal_uint16 USB2UART_SendData(UART_PORT port, kal_uint8 *Buffaddr, kal_uint16 Length,
kal_uint8 mode,kal_uint8 escape_char , 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
if(ownerid!=USB2UARTPort.ownerid)
{
#ifdef __PRODUCTION_RELEASE__
return 0;
#else /* __PRODUCTION_RELEASE__ */
EXT_ASSERT( 0, (kal_uint32) ownerid, 0, 0);
#endif /* __PRODUCTION_RELEASE__ */
}
/* caller handle special character already, so do not need to handle it */
return USB2UART_PutBytes(port,Buffaddr,Length,ownerid);
}
/*get the left bytes for tx buffer*/
static kal_uint16 USB2UART_GetTxISRRoomLeft(UART_PORT port)
{
kal_uint16 real_count;
kal_uint32 savedMask;
#ifdef __DSPIRDBG__
if(g_UsbACM.acm_owner!=USB_ACM_OWNER_UART)
{
EXT_ASSERT(0, g_UsbACM.acm_owner, USB2UARTPort.ownerid, 0);
}
#endif
savedMask = SaveAndSetIRQMask();
Buf_GetRoomLeft(&(USB2UARTPort.Tx_Buffer_ISR),real_count);
RestoreIRQMask(savedMask);
return real_count;
}
/*put bytes to tx buffer, return value is the actually put out bytes*/
static kal_uint16 USB2UART_PutISRBytes(UART_PORT port, kal_uint8 *Buffaddr, kal_uint16 Length, module_type ownerid)
{
kal_uint16 real_count,index;
kal_uint32 savedMask;
#ifdef __DSPIRDBG__
if(g_UsbACM.acm_owner!=USB_ACM_OWNER_UART)
{
EXT_ASSERT(0, g_UsbACM.acm_owner, USB2UARTPort.ownerid, 0);
}
#endif
if ( (gUsbDevice.device_type != USB_CDC_ACM) ||
(USB2UARTPort.initialized == KAL_FALSE) || (gUsbDevice.nDevState!=DEVSTATE_CONFIG))
return 0;
savedMask = SaveAndSetIRQMask();
Buf_GetRoomLeft(&(USB2UARTPort.Tx_Buffer_ISR),real_count);
RestoreIRQMask(savedMask);
/* determine real sent data count */
if (real_count > Length)
real_count = Length;
/* push data from caller buffer to ring buffer */
for (index = 0; index < real_count; index++)
{
Buf_Push(&(USB2UARTPort.Tx_Buffer_ISR),*(Buffaddr+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);
return real_count;
}
/* put bytes to tx buffer, handle the special character in this function(add escape character)
return value is the actually put out bytes*/
static kal_uint16 USB2UART_SendISRData(UART_PORT port, kal_uint8 *Buffaddr, kal_uint16 Length,
kal_uint8 mode, kal_uint8 escape_char, module_type ownerid)
{
kal_int16 real_count, index;
kal_uint8 data;
kal_uint32 savedMask;
#ifdef __DSPIRDBG__
if(g_UsbACM.acm_owner!=USB_ACM_OWNER_UART)
{
EXT_ASSERT(0, g_UsbACM.acm_owner, USB2UARTPort.ownerid, 0);
}
#endif
if ( (gUsbDevice.device_type != USB_CDC_ACM) ||
(USB2UARTPort.initialized == KAL_FALSE) || (gUsbDevice.nDevState!=DEVSTATE_CONFIG))
return 0;
if (mode == 0)
{
real_count = USB2UART_PutISRBytes(port,Buffaddr,Length,ownerid);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -