📄 cd_class.c
字号:
/*************************************************************************
*
* Used with ICCARM and AARM.
*
* (c) Copyright IAR Systems 2007
*
* File name : cd_class.c
* Description : Communication device class module
*
* History :
* 1. Date : February 5, 2007
* Author : Stanimir Bonev
* Description : Create
*
* $Revision: 18137 $
**************************************************************************/
#define CD_CLASS_GLOBAL
#include "cd_class.h"
#pragma data_alignment=4
CDC_LineCoding_t CDC_LineCoding;
#if CDC_DEVICE_SUPPORT_LINE_CODING > 0
volatile Int32U LineCodingDelta;
#endif // CDC_DEVICE_SUPPORT_LINE_CODING > 0
#pragma data_alignment=4
CDC_LineState_t CDC_LineState;
#if CDC_DEVICE_SUPPORT_LINE_STATE > 0
volatile Int32U SerialStateDelta;
volatile Int32U LineStateDelta;
#endif // CDC_DEVICE_SUPPORT_LINE_STATE > 0
#if CDC_DEVICE_SUPPORT_BREAK > 0
volatile Int32U BreakCntr;
#endif // CDC_DEVICE_SUPPORT_BREAK > 0
#pragma data_alignment=4
UsbSetupPacket_t CdcReqPacket;
volatile Int32U CDC_ReceiveIndx;
Int32U CDC_ReceiveIndxHold;
Int32U CDC_TransmitSize;
volatile Boolean USB_CDC_OutEpBufferNotEmpty;
Int8U * pCDC_TransmitUserBuffer;
Int8U CDC_ReceiveBuffer[CommOutEpMaxSize];
Int8U CDC_TransmitBuffer[CommInEpMaxSize];
volatile Boolean CDC_Configure;
/*************************************************************************
* Function Name: UsbCdcInit
* Parameters: LPC_VicIrqSlots_t TimIrqSlot
*
* Return: none
*
* Description: USB communication device class init
*
*************************************************************************/
void UsbCdcInit (Int32U TimIrqSlot)
{
// Init CD Class variables
CDC_Configure = FALSE;
CDC_LineCoding.dwDTERate = CDC_DATA_RATE;
CDC_LineCoding.bDataBits = CDC_DATA_BITS;
CDC_LineCoding.bParityType = CDC_PARITY;
CDC_LineCoding.bCharFormat = CDC_STOP_BITS;
#if CDC_DEVICE_SUPPORT_LINE_CODING > 0
// Update the line coding
LineCodingDelta = TRUE;
#endif // CDC_DEVICE_SUPPORT_LINE_CODING > 0
CDC_LineState.DTR_State = CDC_LINE_DTR;
CDC_LineState.RTS_State = CDC_LINE_RTS;
#if CDC_DEVICE_SUPPORT_LINE_STATE > 0
// Update the line state
LineStateDelta = TRUE;
SerialStateDelta = FALSE;
#endif // CDC_DEVICE_SUPPORT_LINE_STATE > 0
#if CDC_DEVICE_SUPPORT_BREAK > 0
BreakCntr = 0;
// Init Break timer resolution 1 ms
// Init Time0
PCONP_bit.PCTIM0 = 1; // Enable TMR0 clk
T0TCR_bit.CE = 0; // counting disable
T0TCR_bit.CR = 1; // set reset
T0TCR_bit.CR = 0; // release reset
T0CTCR_bit.CTM = 0; // Timer Mode: every rising PCLK edge
T0MCR_bit.MR0I = 1; // Enable Interrupt on MR0
T0MCR_bit.MR0R = 1; // Enable reset on MR0
T0MCR_bit.MR0S = 0; // Disable stop on MR0
// Init Break timer resolution 1 ms
T0PR = ((SYS_GetFpclk(TIMER0_PCLK_OFFSET))/1000)-1;
// init timer 0 interrupt
T0IR_bit.MR0INT = 1; // clear pending interrupt
VIC_SetVectoredIRQ(CDC_BreakCallBack,TimIrqSlot,VIC_TIMER0);
VICINTENABLE |= 1UL << VIC_TIMER0;
#endif // CDC_DEVICE_SUPPORT_BREAK > 0
CDC_ReceiveIndx = \
CDC_ReceiveIndxHold = \
CDC_TransmitSize = 0;
USB_CDC_OutEpBufferNotEmpty = FALSE;
pCDC_TransmitUserBuffer = NULL;
// Registered The Class Request
USB_UserFuncRegistering(UsbCdcRequest,UsbUserClass);
// Registered User Ep0 Data receive
USB_UserFuncRegistering(UsbCdcData,UsbClassEp0OutPacket);
}
/*************************************************************************
* Function Name: UsbCdcConfigure
* Parameters: void * pArg
*
* Return: void *
*
* Description: USB communication device class configure
*
*************************************************************************/
void * UsbCdcConfigure (void * pArg)
{
UsbDevCtrl_t * pUsbDevCtrl = (UsbDevCtrl_t *)pArg;
if(pUsbDevCtrl == NULL)
{
if(UsbCoreReq(UsbCoreReqConfiquration) != 0)
{
CDC_Configure = FALSE;
// disable all class EPs
USB_RealizeEp((USB_Endpoint_t)CommOutEp,1,0,FALSE);
USB_UserFuncRegistering(NULL,CommOutEp);
USB_RealizeEp((USB_Endpoint_t)CommInEp,1,0,FALSE);
USB_UserFuncRegistering(NULL,CommInEp);
USB_RealizeEp((USB_Endpoint_t)ReportEp,1,0,FALSE);
USB_UserFuncRegistering(NULL,ReportEp);
}
}
else
{
// Init variables
CDC_ReceiveIndx = \
CDC_ReceiveIndxHold = \
CDC_TransmitSize = 0;
USB_CDC_OutEpBufferNotEmpty = FALSE;
#if CDC_DEVICE_SUPPORT_BREAK > 0
BreakCntr = 0;
#endif // CDC_DEVICE_SUPPORT_BREAK > 0
// enable all class EPs
USB_UserFuncRegistering(UsbCdcInHadler,CommOutEp);
USB_RealizeEp((USB_Endpoint_t)CommOutEp,1,CommOutEpMaxSize,TRUE);
USB_UserFuncRegistering(UsbCdcOutHadler,CommInEp);
USB_RealizeEp((USB_Endpoint_t)CommInEp,1,CommInEpMaxSize,TRUE);
#if CDC_DEVICE_SUPPORT_LINE_STATE > 0
USB_UserFuncRegistering(UsbCdcReportHadler,ReportEp);
#else
USB_UserFuncRegistering(NULL,ReportEp);
#endif // CDC_DEVICE_SUPPORT_LINE_STATE > 0
USB_RealizeEp((USB_Endpoint_t)ReportEp,0,ReportEpMaxSize,TRUE);
CDC_Configure = TRUE;
}
return(NULL);
}
#if CDC_DEVICE_SUPPORT_LINE_STATE > 0
/*************************************************************************
* Function Name: UsbCdcReportHadler
* Parameters: void * pArg
*
* Return: void *
*
* Description: USB Communication Device Class Report (In) EP handler
*
*************************************************************************/
void * UsbCdcReportHadler (void *Arg)
{
SerialStateDelta = FALSE;
return(NULL);
}
#endif // CDC_DEVICE_SUPPORT_LINE_STATE > 0
/*************************************************************************
* Function Name: UsbCdcInHadler
* Parameters: void * pArg
*
* Return: void *
*
* Description: USB Communication Device Class Out EP handler
*
*************************************************************************/
void * UsbCdcInHadler (void *Arg)
{
Int32U CurrWriteSize;
if(CDC_ReceiveIndx == 0)
{
CurrWriteSize = sizeof(CDC_ReceiveBuffer);
USB_EpRead((USB_Endpoint_t)CommOutEp,
CDC_ReceiveBuffer,
&CurrWriteSize);
#ifdef CDC_OUT_EP_SING_BUFFERED
USB_EpValidate((USB_Endpoint_t)CommOutEp);
#endif // CDC_OUT_EP_SING_BUFFERED
CDC_ReceiveIndx = CurrWriteSize;
}
else
{
USB_CDC_OutEpBufferNotEmpty = TRUE;
}
return(NULL);
}
/*************************************************************************
* Function Name: UsbCdcOutHadler
* Parameters: void * pArg
*
* Return: void *
*
* Description: USB Communication Device Class In EP handler
*
*************************************************************************/
void * UsbCdcOutHadler (void *Arg)
{
Int32U CurrWriteSize;
if(CDC_TransmitSize != 0)
{
CurrWriteSize = CDC_TransmitSize;
USB_EpWrite((USB_Endpoint_t)CommInEp,pCDC_TransmitUserBuffer,&CurrWriteSize);
CDC_TransmitSize -= CurrWriteSize;
pCDC_TransmitUserBuffer += CurrWriteSize;
}
else
{
pCDC_TransmitUserBuffer = NULL;
}
return(NULL);
}
/*************************************************************************
* Function Name: UsbCdcRequest
* Parameters: void * pArg
*
* Return: void *
*
* Description: The class requests processing
*
*************************************************************************/
void * UsbCdcRequest (void * pArg)
{
UsbEpCtrl_t * pCdcReqCtrl = (UsbEpCtrl_t *) pArg;
CdcReqPacket = *(UsbSetupPacket_t *)pCdcReqCtrl->pData;
// Validate Request
if (CdcReqPacket.mRequestType.Recipient == UsbRecipientInterface)
{
switch (CdcReqPacket.bRequest)
{
case SET_LINE_CODING:
if ((CdcReqPacket.wValue.Word == 0) &&
(CdcReqPacket.wIndex.Word == 0))
{
pCdcReqCtrl->Counter = MIN(sizeof(CDC_LineCoding_t),
((CdcReqPacket.wLength.Hi << 8) + CdcReqPacket.wLength.Lo));
pCdcReqCtrl->pData = (pInt8U)&CDC_LineCoding;
return((void*)UsbPass);
}
break;
case GET_LINE_CODING:
if ((CdcReqPacket.wValue.Word == 0) &&
(CdcReqPacket.wIndex.Word == 0))
{
pCdcReqCtrl->Counter = sizeof(CDC_LineCoding_t);
pCdcReqCtrl->pData = (pInt8U)&CDC_LineCoding;
return((void*)UsbPass);
}
break;
case SET_CONTROL_LINE_STATE:
if ((CdcReqPacket.wLength.Word == 0) &&
(CdcReqPacket.wIndex.Word == 0))
{
CDC_LineState.DTR_State = ((CdcReqPacket.wValue.Lo & 1) != 0);
CDC_LineState.RTS_State = ((CdcReqPacket.wValue.Lo & 2) != 0);
#if CDC_DEVICE_SUPPORT_LINE_STATE > 0
LineStateDelta = TRUE;
#endif // CDC_DEVICE_SUPPORT_LINE_STATE > 0
return((void*)UsbPass);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -