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

📄 usb.c

📁 TI公司的CCS一些常用的函数库
💻 C
📖 第 1 页 / 共 5 页
字号:
   usb_ep0_rx_buffer[0]=0;
   usb_ep0_rx_buffer[1]=0;

   usb_put_packet(0,usb_ep0_rx_buffer,2,DATA1);
}

/**************************************************************
/* usb_Get_Endpoint_Status()
/*
/* Input: usb_ep0_rx_buffer[4] == wIndex, endpoint direction bit.
/* Output: Stall status put into usb_ep0_rx_buffer[], usb_ep0_rx_buffer sent to host.
/*
/* Summary: Handles Get_Status request where recipient is endpoint.
/*
/* Part of usb_isr_tok_setup_dne()
/***************************************************************/
void usb_Get_Endpoint_Status() {
   int8 endpoint;
   int1 direction=0;

   debug(debug_txb,"GES ");

   if (bit_test(usb_ep0_rx_buffer[4],7)) {direction=1;}    //direction = 0 == OUT or CONTROL; direction = 1 == IN;
   endpoint=usb_ep0_rx_buffer[4] & 0x0F;
   usb_ep0_rx_buffer[0]=0;
   usb_ep0_rx_buffer[1]=0;

   if (endpoint < USB_MAX_ENDPOINTS) {
      if (usb_endpoint_stalled(endpoint,direction)) {
         usb_ep0_rx_buffer[0]=1;
      }
      usb_put_packet(0,usb_ep0_rx_buffer,2,DATA1);
   }
   else {usb_wrongstate();}
}

/**************************************************************
/* usb_Set_Address_Token()
/*
/* Input: usb_ep0_rx_buffer[2] == wValue, new device address
/* Output: 0 len buffer sent back to host.
/*
/* Summary: Set_Address handler.  National part handles this differently then PIC16C7x5.
/* Note: Firmware must respond within a few milli-seconds.
/*
/* Part of usb_isr_tok_setup_dne()
/***************************************************************/
void usb_Set_Address_Token() {
   debug(debug_txb,"SA ");

   USB_dev_req=SET_ADDRESS; //currently processing set_address request
   USB_address_pending=usb_ep0_rx_buffer[2];


   #ifdef __USBN__
   USB_dev_req=NONE; //currently processing set_address request
   usb_set_address(USB_address_pending);
	USB_Curr_Config=0;	// make sure current configuration is 0
//   if (USB_address_pending) {usb_set_enumerated(1);} else {usb_set_enumerated(0);}
   #endif

   usb_put_packet(0,usb_ep0_rx_buffer,0,DATA1);  //send 0len packet
}

/**************************************************************
/* usb_finish_set_address()
/*
/* Input: USB_address_pending holds the address we were asked to set to.
/*
/* Summary: Sets the address.
/*
/* This code should only be run on the PIC16C7x5, and not the National part.
/*
/* Part of usb_isr_tok_setup_dne()
/***************************************************************/
 void usb_finish_set_address() {
   debug(debug_txb,"FSA ");
	USB_Curr_Config=0;	// make sure current configuration is 0

   #ifdef __PIC__
	USB_dev_req=NONE;  // no request pending
   usb_set_address(USB_address_pending);
   #endif
}

/**************************************************************
/* usb_Clear_Device_Feature()
/*
/* Input: usb_ep0_rx_buffer[2] == wValue, feature to clear
/* Output: 0 len packet sent back to host.
/*
/* Summary: Handles the Clear_Feature request where the recipient is Device.  Only
/*          feature valid for device is remote wakeup.
/*
/* Part of usb_isr_tok_setup_dne()
/***************************************************************/
void usb_Clear_Device_Feature() {
   debug(debug_txb,"CDF ");
   if (usb_ep0_rx_buffer[2] == 1) {
      bit_clear(USB_status_device,1); //set device remote wakeup
      usb_put_packet(0,usb_ep0_rx_buffer,0,DATA1); //send 0len packet
   }
   else {usb_wrongstate();}
}

/**************************************************************
/* usb_Clear_Endpoint_Feature()
/*
/* Input: usb_ep0_rx_buffer[2] == wValue, feature to clear
/*        usb_ep0_rx_buffer[4] == wIndex, direction of endpoint.
/* Output: 0 len packet sent back to host.
/*
/* Summary: Handles the Clear_Feature request where the recipient is endpoint.
/*          Only endpoint feature is Endpoint halt.
/*
/* Part of usb_isr_tok_setup_dne()
/***************************************************************/
void usb_Clear_Endpoint_Feature() {
   int endpoint;
   int1 direction=0;

   debug(debug_txb,"CEF ");

   if (bit_test(usb_ep0_rx_buffer[4],7)) {direction=1;}
   endpoint=usb_ep0_rx_buffer[4] & 0x0F;

   if (endpoint  < USB_MAX_ENDPOINTS) {
      usb_unstall_ep(endpoint,direction);
      usb_put_packet(0,usb_ep0_rx_buffer,0,DATA1);
   }
   else {usb_wrongstate();}
}

/**************************************************************
/* usb_Clear_Interface_Feature()
/*
/* Summary: Handles the Clear_Feature request where the recipient is interface.
/*          This is a valid request, but there are no valid features on the interface in 1.1.  Don't do anything.
/*          (Should we send a Wrong-State (IDLE))?
/*
/* Part of usb_isr_tok_setup_dne()
/***************************************************************/
void usb_Clear_Interface_Feature() {
   debug(debug_txb,"CIF ");
}

/**************************************************************
/* usb_Set_Device_Feature()
/*
/* Input:  usb_ep0_rx_buffer[2] == wValue, feature to set.
/* Output: 0 len packet sent to host.
/*
/* Summary: Set_Feature request handler where recipient is device.
/*          Only feature valid for device feature is Device Remote wakeup.
/*
/* Part of usb_isr_tok_setup_dne()
/***************************************************************/
void usb_Set_Device_Feature() {
   debug(debug_txb,"SDF ");

   if (usb_ep0_rx_buffer[2]==1) {
      bit_set(USB_status_device,1);
      usb_put_packet(0,usb_ep0_rx_buffer,0,DATA1);
   }
   else {usb_wrongstate();}
}

/**************************************************************
/* usb_Set_Feature()
/*
/* Input:  usb_ep0_rx_buffer[2] == wValue, feature to set.
/* Output: 0 len packet sent to host.
/*
/* Summary: Set_Feature request handler where recipient is device.
/*          Only feature valid for device feature is Device Remote wakeup.
/*
/*          WHY DOES THIS LOOK LIKE usb_Set_Device_Feature()???
/*
/* Part of usb_isr_tok_setup_dne()
/***************************************************************/
void usb_Set_Feature() {
   debug(debug_txb,"SF ");
   if (usb_ep0_rx_buffer[2]==1) {
      bit_set(USB_status_device,1); //set device remote wakeup
      usb_put_packet(0,usb_ep0_rx_buffer,0,DATA1); //prepare 0len packet
   }
   else {usb_wrongstate();}
}

/**************************************************************
/* usb_Set_Endpoint_Feature()
/*
/* Input:  usb_ep0_rx_buffer[2] == wValue, feature to set.
/*         usb_ep0_rx_buffer[4] == wIndex, direction of endpoint
/* Output: 0 len packet sent to host.
/*
/* Summary: Set_Feature request handler where recipient is endpoint.
/*          Only endpoint feature is Endpoint halt.  This function will halt
/*          the endpoint that is requested.  The endpoint will remain halted until a reset or
/*          a Clear_Feature on the endpoint.  When the endpoint is halted it will not send or receive data.
/*
/* Part of usb_isr_tok_setup_dne()
/***************************************************************/
void usb_Set_Endpoint_Feature() {
   int endpoint;
   int1 direction=0;
   debug(debug_txb,"SEF ");

   if (bit_test(usb_ep0_rx_buffer[4],7)) {direction=1;}
   endpoint=usb_ep0_rx_buffer[4] & 0x0F;

   if (endpoint != 0) { //should we let them stall ep0?  don't think so
      if (endpoint < USB_MAX_ENDPOINTS) {
         usb_stall_ep(endpoint,direction);
         usb_put_packet(0,usb_ep0_rx_buffer,0,DATA1);
      }
      else {usb_wrongstate();}
   }
}

/**************************************************************
/* usb_Set_Interface_Feature()
/*
/* Output: Since this is an invalid request, we wend a Wrong-State (IDLE) to host.
/*
/* Summary: Handles the Set_Feature request where the recipient is interface.
/*          There are no valid features on the interface in 1.1.
/*
/* Part of usb_isr_tok_setup_dne()
/***************************************************************/
void usb_Set_Interface_Feature() {
   debug(debug_txb,"SIF ");
   usb_wrongstate(); //invalid request
}

/**************************************************************
/* usb_Get_Configuration_Token()
/*
/* Output: Current configuration in use is put into usb_ep0_rx_buffer[] and sent to host.
/*
/* Summary: Get_Configuration handler.  Host asks us what configuration we are using, and we respond
/*          indicating the configuration in use:
/*             Default State    - undefined
/*             Addressed State  - returns 0
/*             Configured state - returns current configured state.
/*
/* Part of usb_isr_tok_setup_dne()
/***************************************************************/
void usb_Get_Configuration_Token() {
   debug(debug_txb,"GC ");
   usb_ep0_rx_buffer[0]=USB_Curr_Config;
   usb_put_packet(0,usb_ep0_rx_buffer,1,DATA1);
}

/**************************************************************
/* usb_Set_Configuration_Token()
/*
/* Input: usb_ep0_rx_buffer[2] == wValue, configuration to set to.
/* Output: if a valid configuration, we send a 0 len packet.  If invalid, we send a wrong-state (IDLE).
/*
/* Summary: Set_Configuration handler.
/*
/* Part of usb_isr_tok_setup_dne()
/***************************************************************/
void usb_Set_Configuration_Token() {
   debug(debug_txb,"SC ");

   if (usb_ep0_rx_buffer[2] > USB_NUM_CONFIGURATIONS) {
      usb_wrongstate();
   }
   else {  // if config <= num configs, request appears valid
      usb_set_configured(usb_ep0_rx_buffer[2]);
      usb_put_packet(0,usb_ep0_rx_buffer,0,DATA1);; //send 0len packet
   }
}

/**************************************************************
/* usb_Get_Interface()
/*
/* Input: usb_ep0_rx_buffer[4] == wIndex, interface to get
/* Output: The current interface number.
/*
/* Summary: Get_Interface request handler.  For devices with configurations that include multiple interfaces,
/*          the host requests the current interface number.  Only valid if device is configured.
/*
/* Part of usb_isr_tok_setup_dne()
/***************************************************************/
void usb_Get_Interface() {
   int interface;
   debug(debug_txb,"GI ");

   interface=usb_ep0_rx_buffer[4];

   if ( (USB_Curr_Config) && (interface < USB_NUM_INTERFACES) ) {   //book says only supports configed state
      usb_ep0_rx_buffer[0]=USB_Interface[interface];//our new outgoing byte
      usb_put_packet(0,usb_ep0_rx_buffer,1,DATA1);; //send byte back
   }
   else {
      usb_wrongstate();
   }
}

/**************************************************************
/* usb_Set_Interface()
/*
/* Input: usb_ep0_rx_buffer[2] == wValue, value to set interface to
/*        usb_ep0_rx_buffer[4] == wIndex, interface to set.
/* Returns: If a valid request, we send a 0 len packet to host as a reply.
/*
/* Summary: Set_Interface request handler.  Configure desired interface to use a specified alt. setting.
/*          Only valid when configured.
/*
/* Part of usb_isr_tok_setup_dne()
/***************************************************************/
void usb_Set_Interface() {
   int interface;

   debug(debug_txb,"SI ");

   if (USB_Curr_Config) { //if configured state
      interface=usb_ep0_rx_buffer[4];
      USB_Interface[interface]=usb_ep0_rx_buffer[2];
      usb_put_packet(0,usb_ep0_rx_buffer,0,DATA1); //send 0len packet
   }
   else { //not configured
      usb_wrongstate();
   }
}

////////////////////////////////////////////////////////////////////////////
///
/// The following functions retrieve data from constant arrays.  This may
/// look un-optimized, but remember that you can't create a pointer to
/// a constant array.
///
///////////////////////////////////////////////////////////////////////////

/**************************************************************
/* get_next_string_character()
/*
/* Returns: next character from string, or 0xFF when no more characters to pull.
/*
/* Summary: string_x are constant arrays that hold the string descriptors.  Since it's a constant
/*          array we cant create a pointer to it, therefore we have to do it one character at a time like this.
/*          string holds the string we are working on, String_ptr holds our current position on the string, and
/*          and the constant STRINGx has the length of the string.  Used in response to a Get_Descriptor request.
/*
/* Part of usb_isr_tok_setup_dne()
/***************************************************************/
#inline
int get_next_string_character() {
   int ret;

   switch (string) {
   #ifdef USB_STRING_0_LEN
      case 0: ret=USB_STRING_0[String_ptr++]; if (String_ptr>=USB_S

⌨️ 快捷键说明

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