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

📄 usb_user_enum.c

📁 用AT89C5131A开发USB的框架很好用
💻 C
字号:
//! @file usb_user_enum.c
//!
//! Copyright (c) 2004
//!
//! Use of this program is subject to Atmel's End User License Agreement.
//! Please read file license.txt for copyright notice.
//!
//! @brief user call-back functions
//!
//! This file contains the user call-back functions corresponding to the
//! application:
//! MASS STORAGE DEVICE
//!
//! @version 1.4.2.5 (c5131-usb-generic-1_2_0)
//!
//! @todo
//! @bug


//_____ I N C L U D E S ____________________________________________________

#include "config.h"
#include "conf\conf_usb.h"
#include "lib_mcu\usb\usb_drv.h"
#include "modules\usb\usb_enum.h"
#include "usb_user_configuration.h"
#include "usb_user_enum.h"
#include "usb_task.h"

//_____ M A C R O S ________________________________________________________

//_____ D E F I N I T I O N ________________________________________________

//_____ P R I V A T E   D E C L A R A T I O N ______________________________

static  U8   hid_idle_duration;
extern  U8   send_free;

extern _MEM_TYPE_SLOW_ U8   *pbuffer;
extern _MEM_TYPE_SLOW_ U8   data_to_transfer;

extern S_hid_set_control    SetControlBytes;
extern void    usb_get_descriptor(   void);
extern bit     reset_to_bootloader;
extern code U8 hid_report[];


//_____ D E C L A R A T I O N ______________________________________________

//! usb_user_read_request(type, request);
//!
//! This function is called by the standard usb read request function when
//! the Usb request is not supported. This function returns TRUE when the
//! request is processed. This function returns FALSE if the request is not
//! supported. In this case, a STALL handshake will be automatically 
//! sent by the standard usb read request function.
//!
//! @warning Code:xx bytes (function code length)
//!
//! @param none
//!
//! @return none
//!
Bool usb_user_read_request(U8 type, U8 request)
{
U8* pointer; 
U8  c;

  type = type;            // FIXME (added for warnings)
  switch (request)
  {
    case HID_SET_REPORT:
         Usb_ack_receive_setup();
         while(!Is_usb_receive_out() );
         SetControlBytes.arrived=1;
         pointer = &(SetControlBytes.type);
         for (c=HID_CONTROL_DATA_LENGTH; c!=0; c--)
         {
            *pointer = Usb_read_byte();
             pointer ++;
         }
         if (SetControlBytes.type == 0x55)
         {
            if (SetControlBytes.direction == 0xAA)
            {
                if (SetControlBytes.length == 0x55AA)
                {
                    reset_to_bootloader = TRUE;
                }
            }
         }
         Usb_ack_receive_out();
         Usb_send_control_in();                          /* send a ZLP for STATUS phase */
         while(!(Is_usb_in_ready()));
         Usb_ack_in_ready();
         return TRUE;
         break;

    case HID_SET_IDLE:
         ACC = Usb_read_byte();
         hid_idle_duration = Usb_read_byte();      /* wValue contains the duration */
         Usb_ack_receive_setup();

         Usb_send_control_in();                          /* send a ZLP for STATUS phase */
         while(!(Is_usb_in_ready()));
         Usb_ack_in_ready();
         return TRUE;
         break;

    case HID_GET_IDLE:
         Usb_ack_receive_setup();
         Usb_set_DIR();
         Usb_write_byte(hid_idle_duration);
         Usb_send_control_in();                          /* send a ZLP for STATUS phase */
         while(!(Is_usb_in_ready()));
         Usb_ack_in_ready();
  
         while (!(Is_usb_receive_out()));
         Usb_ack_receive_out();
         return TRUE;
         break;
    case GET_DESCRIPTOR:
         if (type == 0x81) { usb_get_descriptor(); }
         return TRUE;
         break;
    default:
         return FALSE;
         break;
  }
  return FALSE;
}


//! usb_user_get_descriptor.
//!
//! This function returns the size and the pointer on a user information
//! structure
//!
//! @warning Code:xx bytes (function code length)
//!
//! @param none
//!
//! @return none
//!
Bool usb_user_get_descriptor(U8 type, U8 string)
{
  switch (type)
  {
    case STRING_DESCRIPTOR:
      if (string == 0)
      {
        data_to_transfer = sizeof (usb_user_language_id);
        pbuffer          = &usb_user_language_id.bLength;
        return TRUE;
      }
      else
      if (string == 1)
      {
        data_to_transfer = sizeof (usb_user_manufacturer_string_descriptor);
        pbuffer          = &usb_user_manufacturer_string_descriptor.bLength;
        return TRUE;
      }
      else
      if (string == 2)
      {
        data_to_transfer = sizeof (usb_user_product_string_descriptor);
        pbuffer          = &usb_user_product_string_descriptor.bLength;
        return TRUE;
      }
      else
      if (string == 3)
      {
        data_to_transfer = sizeof (usb_user_serial_number);
        pbuffer          = &usb_user_serial_number.bLength;
        return TRUE;
      }
      else
      {
        return FALSE;
      }
      break;
    case HID_REPORT_DESCRIPTOR:
      data_to_transfer = SIZE_OF_REPORT;
      pbuffer          = hid_report;
      return TRUE;
      break;
    case HID_DESCRIPTOR:
      data_to_transfer = sizeof(usb_conf_desc_hid_fs.hid);
      pbuffer          = &(usb_conf_desc_hid_fs.hid.bLength);
      return TRUE;
      break;    
    default:
      return FALSE;
      break;
  }
}


//! usb_user_endpoint_init.
//!
//! This function configures the endpoints.
//!
//! @warning Code:xx bytes (function code length)
//!
//! @param none
//!
//! @return none
//!
void usb_user_endpoint_init(U8 conf_nb)
{
  U8 ep_size;

  conf_nb = conf_nb;                    // FIXME (added for warnings)

  if(Is_usb_high_speed()) { ep_size = SIZE_512; } else { ep_size = SIZE_64; }

  usb_configure_endpoint(EP_HID_IN,     \
                         TYPE_INTERRUPT,\
                         DIRECTION_IN,  \
                         ep_size,       \
                         TWO_BANKS,     \
                         NYET_ENABLED);
  usb_configure_endpoint(EP_HID_OUT,    \
                         TYPE_INTERRUPT,\
                         DIRECTION_OUT, \
                         ep_size,       \
                         TWO_BANKS,     \
                         NYET_ENABLED);
  send_free = 1;
}

⌨️ 快捷键说明

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