⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cd_class.c

📁 最新版IAR FOR ARM(EWARM)5.11中的代码例子
💻 C
📖 第 1 页 / 共 2 页
字号:
/*************************************************************************
 *
 *    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];

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
  TIM_CounterCmd(TIM1, TIM_CLEAR);
  if(BreakCntr)
  {
    --BreakCntr;
  }
  else
  {
    CDC_BreakCallBack(NULL);
    // Disable TIM0 counter
    TIM_CounterCmd(TIM1, TIM_STOP);
  }
#endif // CDC_DEVICE_SUPPORT_BREAK > 0
  // Clear TIM1 flag OC1
  TIM_ClearFlag(TIM1,TIM_FLAG_OC1);
}

/*************************************************************************
 * 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;
#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
  SCU_APBPeriphClockConfig(__TIM01, ENABLE);
  // Release TIM1 reset
  SCU_APBPeriphReset(__TIM01,DISABLE);

  // Timer 1
  // TIM Configuration in Output Compare Timing Mode period 1ms
  TIM_InitStructure.TIM_Mode = TIM_OCM_CHANNEL_1;   // OUTPUT COMPARE CHANNEL 1 Mode
  TIM_InitStructure.TIM_OC1_Modes = TIM_TIMING;     // OCMP1 pin is disabled
  TIM_InitStructure.TIM_Clock_Source = TIM_CLK_APB; // assign PCLK to TIM_Clk
  TIM_InitStructure.TIM_Prescaler = 48 - 1;         // 1us resolution
  TIM_InitStructure.TIM_Pulse_Length_1 = 1000;       // 1 ms period
  TIM_Init(TIM1, &TIM_InitStructure);

  // VIC configuration
  VIC_Config(TIM1_ITLine, VIC_IRQ, IntrPriority);
  VIC_ITCmd(TIM1_ITLine, ENABLE);

#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 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;
  }
}

/*************************************************************************
 * 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))
      {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -