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

📄 usb_cdc_enum.c

📁 AT89C5131的接口驱动
💻 C
📖 第 1 页 / 共 3 页
字号:
      Usb_set_CONFG();
      Usb_set_usb_configured();
      break;
    default:
      Usb_set_stall_request();
      while (!Usb_stall_sent());
      Usb_clear_stall_request();
      Usb_clear_stalled();
      return;
  }*/
  if (configuration_number <= CONF_NB)
  {
    usb_configuration_nb = configuration_number;
  }
  else
  {
    Usb_set_stall_request();
    while (!Usb_stall_sent());
    Usb_clear_stall_request();
    Usb_clear_stalled();
    return;
  }

  Usb_set_tx_ready();                          /* send a ZLP for STATUS phase */
  while (!Usb_tx_complete());
  Usb_clear_tx_complete();
  usb_ep_init();                            /* endpoints configuration */
}


/*F**************************************************************************
* NAME: usb_get_descriptor
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
*----------------------------------------------------------------------------
* PURPOSE: 
* This function manages the GET_DESCRIPTOR request.
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS: 
*****************************************************************************/
void usb_get_descriptor (void)
{
Uchar   data_to_transfer;
Uint16  wLength;
Uchar   descriptor_type;
Uchar   string_type;

  zlp = FALSE;                              /* no zero length packet */
  string_type = Usb_read_byte();            /* read LSB of wValue */
  descriptor_type = Usb_read_byte();        /* read MSB of wValue */
  switch (descriptor_type)
  {
    case DEVICE:
    {
      data_to_transfer = sizeof (usb_device_descriptor);
      pbuffer = &(usb_device_descriptor.bLength);
      break;
    }

    case CONFIGURATION:
    {
      data_to_transfer = sizeof (usb_configuration);
      pbuffer = &(usb_configuration.cfg.bLength);
      break;
    }

/*    case REPORT:
    {
      data_to_transfer = SIZE_OF_REPORT;
      pbuffer = &(usb_configuration.rep[0]);
      break;
    }

    case HID:
    {
      data_to_transfer = sizeof(usb_configuration.hid);
      pbuffer = &(usb_configuration.hid.bLength);
      break;
    }
    case STRING:
    {
      switch (string_type)
      {
        case LANG_ID:
        {
          data_to_transfer = sizeof (usb_language);
          pbuffer = &(usb_language.bLength);
          break;
        }
        case MAN_INDEX:
        {
          data_to_transfer = sizeof (usb_manufacturer);
          pbuffer = &(usb_manufacturer.bLength);
          break;
        }
        case PROD_INDEX:
        {
          data_to_transfer = sizeof (usb_product);
          pbuffer = &(usb_product.bLength);
          break;
        }
        case SN_INDEX:
        {
          data_to_transfer = sizeof (usb_serial_number);
          pbuffer = &(usb_serial_number.bLength);
          break;
        }
        default:
        {
          Usb_clear_rx_setup();
          Usb_set_stall_request(); 
          while ((!(Usb_stall_sent())) && (Usb_setup_received()));
          Usb_clear_stalled();
          Usb_clear_stall_request();
          Usb_clear_DIR();
          return;
        }
      }
      break;
    }
*/
    default:
    {
      Usb_clear_rx_setup();
      Usb_set_stall_request();
      while ((!(Usb_stall_sent())) && (Usb_setup_received()));
      Usb_clear_stalled();
      Usb_clear_stall_request();
      Usb_clear_DIR();
      return;
    }
  }

  ACC = Usb_read_byte();                    /* don't care of wIndex field */
  ACC = Usb_read_byte();
  ((Uchar*)&wLength)[1] = Usb_read_byte();   /* read wLength */
  ((Uchar*)&wLength)[0] = Usb_read_byte();
  if (wLength > data_to_transfer)
  {
    if ((data_to_transfer % EP_CONTROL_LENGTH) == 0) { zlp = TRUE; }
    else { zlp = FALSE; }                        /* no need of zero length packet */
  }
  else
  {
    data_to_transfer = (Uchar)wLength;       /* send only requested number of data */
  }
  Usb_clear_rx_setup() ;                     /* clear the receive setup flag */
  Usb_set_DIR();                            /* set out on EP0 */

  while (data_to_transfer > EP_CONTROL_LENGTH)
  {
    pbuffer = usb_send_ep0_packet(pbuffer, EP_CONTROL_LENGTH);
    data_to_transfer -= EP_CONTROL_LENGTH;

    while ((!(Usb_rx_complete())) && (!(Usb_tx_complete())));
    Usb_clear_tx_complete();
    if ((Usb_rx_complete()))                /* if cancel from USB Host */
    {
      Usb_clear_tx_ready();
      Usb_clear_rx();
      return;
    }
  }
  /* send last data packet */
  pbuffer = usb_send_ep0_packet(pbuffer, data_to_transfer);
  data_to_transfer = 0;
  while ((!(Usb_rx_complete())) && (!(Usb_tx_complete())));
  Usb_clear_tx_complete();
  if ((Usb_rx_complete()))                  /* if cancel from USB Host */
  {
    Usb_clear_tx_ready();
    Usb_clear_rx();
    return;
  }

  if (zlp == TRUE)
  {
    usb_send_ep0_packet(pbuffer, 0);
    while ((!(Usb_rx_complete())) && (!(Usb_tx_complete())));
    Usb_clear_tx_complete();
    if ((Usb_rx_complete()))              /* if cancel from USB Host */
    {
      Usb_clear_tx_ready();
      Usb_clear_rx();
      return;
    }

  }
  while ((!(Usb_rx_complete())) && (!(Usb_setup_received())));
  if (Usb_setup_received())
  {
    return;
  }

  if (Usb_rx_complete())
  {
    Usb_clear_DIR();                        /* set in on EP0 */
    Usb_clear_rx();
  }
}


/*F**************************************************************************
* NAME: usb_get_configuration
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
*----------------------------------------------------------------------------
* PURPOSE: 
* This function manages the GET_CONFIGURATION request.
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS: 
*****************************************************************************/
void usb_get_configuration (void)
{
  Usb_clear_rx_setup();
  Usb_set_DIR();

/*  if (Usb_get_usb_configured()) { Usb_write_byte(1); }
  else { Usb_write_byte(0); }
*/
  Usb_write_byte(usb_configuration_nb);

  Usb_set_tx_ready();
  while (!(Usb_tx_complete()));
  Usb_clear_tx_complete();
  while (!(Usb_rx_complete()));
  Usb_clear_rx();
  Usb_clear_DIR();
}

/*F**************************************************************************
* NAME: usb_get_interface
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
*----------------------------------------------------------------------------
* PURPOSE: 
* This function manages the GET_INTERFACE request.
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS: 
*****************************************************************************/
void usb_get_interface (void)
{
  Usb_clear_rx_setup();
  Usb_set_DIR();
  Usb_set_stall_request();
  while (!Usb_stall_sent());
  Usb_clear_stall_request();
  Usb_clear_stalled();
  Usb_clear_DIR();
}


/*F**************************************************************************
* NAME: usb_get_status
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
*----------------------------------------------------------------------------
* PURPOSE: 
* This function manages the GET_STATUS request.
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS: 
*****************************************************************************/
void usb_get_status (void)
{
Uchar wIndex;

  ACC = Usb_read_byte();                    /* dummy read */
  ACC = Usb_read_byte();                    /* dummy read */
  wIndex = Usb_read_byte();
  Usb_clear_rx_setup();
  Usb_set_DIR();
  switch(bmRequestType) 
  {
    case REQUEST_DEVICE_STATUS:    Usb_write_byte(SELF_POWERED);  break;
    
    case REQUEST_INTERFACE_STATUS: Usb_write_byte(0x00);          break;
    
    case REQUEST_ENDPOINT_STATUS:  wIndex = wIndex & MSK_EP_DIR;
                                   Usb_write_byte(endpoint_status[wIndex]);
                                   break;
  }
  Usb_write_byte(0x00);

  Usb_set_tx_ready();
  while ((!(Usb_tx_complete())) || (Usb_setup_received()));
  Usb_clear_tx_complete();
  while ((!(Usb_rx_complete())) || (Usb_setup_received()));

⌨️ 快捷键说明

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