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

📄 usb_cdc.c

📁 USB 通信,使用的是Freescale公司的CMX协议栈
💻 C
📖 第 1 页 / 共 2 页
字号:
/****************************************************************************
 *
 *            Copyright (c) 2006 by CMX Systems, Inc.
 *
 * This software is copyrighted by and is the sole property of
 * CMX.  All rights, title, ownership, or other interests
 * in the software remain the property of CMX.  This
 * software may only be used in accordance with the corresponding
 * license agreement.  Any unauthorized use, duplication, transmission,
 * distribution, or disclosure of this software is expressly forbidden.
 *
 * This Copyright notice may not be removed or modified without prior
 * written consent of CMX.
 *
 * CMX reserves the right to modify this software without notice.
 *
 * CMX Systems, Inc.
 * 12276 San Jose Blvd. #511
 * Jacksonville, FL 32223
 * USA
 *
 * Tel:  (904) 880-1840
 * Fax:  (904) 880-1632
 * http: www.cmx.com
 * email: cmx@cmx.com
 *
 ***************************************************************************/
#include "mcf5222x_reg.h"
#include "usb.h"
#include "usb_cdc.h"
#include "uart.h"
/****************************************************************************
 ************************** Macro definitions *******************************
 ***************************************************************************/
/* Mandatory class specific requests. */
#define CDCRQ_SEND_ENCAPSULATED_COMMAND 0x0
#define CDCRQ_GET_ENCAPSULATED_RESPONSE 0x1

/* Optional class specific requests. Windows usbser.sys depends on these. */
#define CDCRQ_SET_LINE_CODING           0x20
#define CDCRQ_GET_LINE_CODING           0x21
#define CDCRQ_SET_CONTROL_LINE_STATE    0x22

/* Optional not implemented class specific requests.
#define CDCRQ_SET_COMM_FEATURE          0x2
#define CDCRQ_GET_COMM_FEATURE          0x3
#define CDCRQ_CLEAR_COMM_FEATURE        0x4
#define CDCRQ_SEND_BREAK                0x23
*/

#define TX_EP_NO  2
#define RX_EP_NO  3

/* This macro will evaluate to an array inicializer list. It will set the
   content of a line coding structure to the defined values. */
#define FILL_LINE_CODING(bps, stop, parity, data_bits) \
  (bps) & 0xff, ((bps)>>8) & 0xff, ((bps)>>16) & 0xff, ((bps)>>24) & 0xff\
  , (hcc_u8)(stop), (hcc_u8)(parity), (hcc_u8)(data_bits)
/****************************************************************************
 ************************** Type definitions ********************************
 ***************************************************************************/

/****************************************************************************
 ************************** Function predefinitions. ************************
 ***************************************************************************/
callback_state_t got_line_coding(void);
void cdc_reset_event(void);

/****************************************************************************
 ************************** Global variables ********************************
 ***************************************************************************/
hcc_u8 new_line_coding;

/****************************************************************************
 ************************** Module variables ********************************
 ***************************************************************************/
/* Buffer long enouh to hold one USB frame. (Length equals to rx packet
   size. */
//static hcc_u32 rx_buffer[32/4];
static hcc_u32 rx_buffer[512/4];
//static hcc_u8 rx_length;
//static hcc_u8 rx_ndx;
static hcc_u16 rx_length;
static hcc_u16 rx_ndx;
/* Buffer long enouh to hold one USB frame. (Length equals to tx packet
   size. */
static hcc_u8 *cur_tx_buffer;   
/*static hcc_u32 tx_buffer1[32/4];
static hcc_u32 tx_buffer2[32/4];*/
static hcc_u32 tx_buffer1[512/4];
static hcc_u32 tx_buffer2[512/4];
//static hcc_u8 tx_ndx;
static hcc_u16 tx_ndx;

static hcc_u8 line_coding[7] = {
  FILL_LINE_CODING(19200, 0, 0, 8) /* Default is 19200 BPS and 8N1 format. */
};

/****************************************************************************
 ************************** Function definitions ****************************
 ***************************************************************************/

/*****************************************************************************
 * USB callback function. Is called by the USB driver if an USB error event
 * occurs.
 ****************************************************************************/
void usb_bus_error_event(void)
{
  /* empty */
}

/*****************************************************************************
 * USB callback function. Is called by the USB driver if an USB suspend event
 * occurs.
 ****************************************************************************/
void usb_suspend_event(void)
{
  /* empty */
}

/*****************************************************************************
 * USB callback function. Is called by the USB driver if an USB wakeup event
 * occurs.
 ****************************************************************************/
void usb_wakeup_event(void)
{
  /* empty */
}

/*****************************************************************************
 * USB callback function. Is called by the USB driver if an USB reset event
 * occurs.
 ****************************************************************************/
void usb_reset_event(void)
{
  /* empty */
}

/*****************************************************************************
 * USB callback function. Is called by the USB driver when the line coding
 * has been changed by the host.
 ****************************************************************************/
callback_state_t got_line_coding(void)
{
  new_line_coding=1;
  return(clbst_ok);
}
/*****************************************************************************
 * USB callback function. Is called by the USB driver if a non standard
 * request is received from the HOST. This callback extends the known
 * requests by masstorage related ones.
 ****************************************************************************/
callback_state_t usb_ep0_callback(void)
{
  hcc_u8 *pdata=usb_get_rx_pptr(0);
  
  callback_state_t r=clbst_error;

  /* A request to the command interface. */
  if (STP_INDEX(pdata) == CMD_IFC_INDEX)
  {
    switch(STP_REQU_TYPE(pdata))
    {
    /* Class specific in request. */
    case ((1<<7) | (1<<5) | 1):
      /* Host wants to get a descriptor */
      switch (STP_REQUEST(pdata))
      {
      case CDCRQ_GET_LINE_CODING:
        usb_send(0, (void *) 0, (void *)&line_coding, 7, STP_LENGTH(pdata));
        r=clbst_in;
        break;
      case CDCRQ_GET_ENCAPSULATED_RESPONSE:
      default:
        break;
      }
      break;
    /* Class specific out request. */
    case ((0<<7) | (1<<5) | 1):
      switch (STP_REQUEST(pdata))
      {
      case CDCRQ_SET_LINE_CODING:
        usb_receive(0, got_line_coding, (void *)&line_coding, 7, 7);
        r=clbst_out;
        break;
      case CDCRQ_SET_CONTROL_LINE_STATE:
        r=clbst_out;
        break;
      case CDCRQ_SEND_ENCAPSULATED_COMMAND:
      default:
        break;
      }
      break;
    default:
      break;
    }
  }
  return(r);
}

/*****************************************************************************
* Name:
*    cdc_getch
* In:
*    N/A
*
* Out:
*    N/A
*
* Description:
*    Get the next character from rx_buffer.
*
* Assumptions:
*    Is only called if cdc_input_ready returns true.
*****************************************************************************/
char cdc_getch(void)
{
  /* Return the next character, and advance read index. */
  return((char)((hcc_u8*)rx_buffer)[rx_ndx++]);
}

/*****************************************************************************
* Name:
*    cdc_input_ready
* In:
*    N/A
*
* Out:
*    N/A
*
* Description:
*    Will return one, if the rx_buffer contains any unread characters.
*
* Assumptions:
*    This function is called periodicaly. (Otherwise usb_receive call will
*    not bee made, and no data will ge received over the USB.)

⌨️ 快捷键说明

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