cd_class.c
来自「最新版IAR FOR ARM(EWARM)5.11中的代码例子」· C语言 代码 · 共 651 行 · 第 1/2 页
C
651 行
/*************************************************************************
*
* Used with ICCARM and AARM.
*
* (c) Copyright IAR Systems 2006
*
* File name : cd_class.c
* Description : Communication device class module
*
* History :
* 1. Date : June 28, 2006
* Author : Stanimir Bonev
* Description : Create
*
* $Revision: 16170 $
**************************************************************************/
#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;
Int32U CDC_ReceiveIndx, CDC_ReceiveIndxHold;
Int32U CDC_TransmitSize;
volatile Boolean USB_CDC_OutEpBufferNotEmpty;
pInt8U volatile pCDC_TransmitUserBuffer;
#pragma data_alignment=4
Int8U CDC_ReceiveBuffer[CommOutEpMaxSize];
#pragma data_alignment=4
Int8U CDC_TransmitBuffer[CommInEpMaxSize];
volatile Boolean CDC_Configure;
/*************************************************************************
* Function Name: Tim1Handler
* Parameters: void
*
* Return: none
*
* Description: Timer 1 interrupt handler
*
*************************************************************************/
void Tim1Handler(void)
{
#if CDC_DEVICE_SUPPORT_BREAK > 0
if(BreakCntr)
{
--BreakCntr;
}
else
{
CDC_BreakCallBack(NULL);
// Disable TIM1 counter
TIM_Cmd(TIM1, DISABLE);
}
#endif // CDC_DEVICE_SUPPORT_BREAK > 0
// Clear the Update pending Bit
TIM_ClearITPendingBit(TIM1, TIM_IT_Update);
}
/*************************************************************************
* Function Name: UsbCdcInit
* Parameters: Int32U IntrPriority
*
* Return: none
*
* Description: USB communication device class init
*
*************************************************************************/
void UsbCdcInit (Int32U IntrPriority)
{
#if CDC_DEVICE_SUPPORT_BREAK > 0
TIM_InitTypeDef TIM_InitStructure;
EIC_IRQInitTypeDef EIC_IRQInitStructure;
MRCC_ClocksTypeDef MRCC_Clocks;
#endif // CDC_DEVICE_SUPPORT_BREAK > 0
// 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
// Enable TIM1 clocks
MRCC_PeripheralClockConfig(MRCC_Peripheral_TIM1, ENABLE);
// Release TIM1 reset
MRCC_PeripheralSWResetConfig(MRCC_Peripheral_TIM1,DISABLE);
// Calculate counter value
MRCC_GetClocksStatus(&MRCC_Clocks);
TIM_InitStructure.TIM_Period = (MRCC_Clocks.CKTIM_Frequency / 1000);
TIM_InitStructure.TIM_Prescaler = 0;
TIM_InitStructure.TIM_Mode = TIM_Mode_OCTiming;
TIM_InitStructure.TIM_ClockSource = TIM_ClockSource_Internal;
TIM_InitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_InitStructure.TIM_Channel = TIM_Channel_2;
TIM_Init(TIM1, &TIM_InitStructure);
// Enable and configure the priority of the TIM1 Update IRQ Channel
EIC_IRQInitStructure.EIC_IRQChannel = TIM1_UP_IRQChannel;
EIC_IRQInitStructure.EIC_IRQChannelPriority = IntrPriority;
EIC_IRQInitStructure.EIC_IRQChannelCmd = ENABLE;
EIC_IRQInit(&EIC_IRQInitStructure);
#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 the function for a data receive of the class request
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)
{
CDC_Configure = FALSE;
if(UsbCoreReq(UsbCoreReqConfiquration) != 0)
{
// disable all class EPs
USB_RealizeEp((USB_Endpoint_t)CommOutEp,0,EP_BULK_SING_BUFF,EP_SLOT2);
USB_UserFuncRegistering(NULL,CommOutEp);
USB_RealizeEp((USB_Endpoint_t)CommInEp,0,EP_BULK_SING_BUFF,EP_SLOT2);
USB_UserFuncRegistering(NULL,CommInEp);
USB_RealizeEp((USB_Endpoint_t)ReportEp,0,EP_INTERRUPT,EP_SLOT1);
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((UserFunc_t)UsbCdcInHadler,CommOutEp);
USB_RealizeEp((USB_Endpoint_t)CommOutEp,CommOutEpMaxSize,EP_BULK_SING_BUFF,EP_SLOT2);
USB_UserFuncRegistering((UserFunc_t)UsbCdcOutHadler,CommInEp);
USB_RealizeEp((USB_Endpoint_t)CommInEp,CommInEpMaxSize,EP_BULK_SING_BUFF,EP_SLOT2);
#if CDC_DEVICE_SUPPORT_LINE_STATE > 0
USB_UserFuncRegistering((UserFunc_t)UsbCdcReportHadler,ReportEp);
USB_RealizeEp((USB_Endpoint_t)ReportEp,ReportEpMaxSize,EP_INTERRUPT,EP_SLOT1);
#else
USB_UserFuncRegistering(NULL,ReportEp);
USB_RealizeEp((USB_Endpoint_t)ReportEp,ReportEpMaxSize,EP_INTERRUPT,EP_SLOT1);
#endif // CDC_DEVICE_SUPPORT_LINE_STATE > 0
CDC_Configure = TRUE;
}
return(NULL);
}
#if CDC_DEVICE_SUPPORT_LINE_STATE > 0
/*************************************************************************
* Function Name: UsbCdcReportHadler
* Parameters: void * pArg
*
* Return: none
*
* Description: USB Communication Device Class Report (In) EP handler
*
*************************************************************************/
void UsbCdcReportHadler (void *Arg)
{
SerialStateDelta = FALSE;
}
#endif // CDC_DEVICE_SUPPORT_LINE_STATE > 0
/*************************************************************************
* Function Name: UsbCdcInHadler
* Parameters: void * pArg
*
* Return: none
*
* Description: USB Communication Device Class Out EP handler
*
*************************************************************************/
void UsbCdcInHadler (void *Arg)
{
if(CDC_ReceiveIndx == 0)
{
CDC_ReceiveIndx = CommOutEpMaxSize;
USB_EpRead((USB_Endpoint_t)CommOutEp,
(pInt8U)CDC_ReceiveBuffer,
&CDC_ReceiveIndx);
USB_EpValidate((USB_Endpoint_t)CommOutEp,
TRUE);
}
else
{
USB_CDC_OutEpBufferNotEmpty = TRUE;
}
}
/*************************************************************************
* Function Name: UsbCdcOutHadler
* Parameters: void * pArg
*
* Return: none
*
* Description: USB Communication Device Class In EP handler
*
*************************************************************************/
void UsbCdcOutHadler (void *Arg)
{
Int32U Counter, CurrWriteSize;
if(CDC_TransmitSize != 0)
{
CurrWriteSize = MIN(CDC_TransmitSize,CommInEpMaxSize);
for(Counter = 0; Counter < CurrWriteSize; ++Counter)
{
CDC_TransmitBuffer[Counter] = *pCDC_TransmitUserBuffer++;
}
CDC_TransmitSize -= CurrWriteSize;
USB_EpWrite((USB_Endpoint_t)CommInEp,CDC_TransmitBuffer,&CurrWriteSize);
}
else
{
pCDC_TransmitUserBuffer = 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) &&
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?