Main Page | Alphabetical List | Data Structures | File List | Data Fields | Globals | Related Pages

usb_enum.c File Reference

Copyright (c) 2004 This file is a template for writing C software programs. More...

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

Go to the source code of this file.

Functions

void usb_get_descriptor (void)
 usb_get_descriptor.

void usb_read_request (void)
void usb_set_address (void)
 usb_set_address.

void usb_set_configuration (void)
 usb_set_configuration.

void usb_clear_feature (void)
 usb_clear_feature.

void usb_set_feature (void)
 usb_set_feature.

void usb_get_status (void)
 usb_get_status.

void usb_get_configuration (void)
 usb_get_configuration.

void usb_enum_var_init (void)
 usb_enum_variable_init.

void usb_process_request (void)
 usb_process_request.


Variables

bdata bit rx_bank
bit zlp
_MEM_TYPE_SLOW_ U8 endpoint_status [7]
_MEM_TYPE_SLOW_ U8pbuffer
_MEM_TYPE_SLOW_ U8 data_to_transfer
U8 bmRequestType
_MEM_TYPE_SLOW_ U8 usb_configuration_nb
bit usb_connected
 Public : (bit) usb_connected usb_connected is set to TRUE when VBUS has been detected usb_connected is set to FALSE otherwise /.

code S_usb_device_descriptor usb_user_device_descriptor
code S_usb_user_configuration_descriptor usb_user_configuration_descriptor


Detailed Description

Copyright (c) 2004 This file is a template for writing C software programs.

Use of this program is subject to Atmel's End User License Agreement. Please read file license.txt for copyright notice.

This file contains the USB endpoint 0 management routines corresponding to the standard enumeration process (refer to chapter 9 of the USB specification. This file calls routines of the usb_user_enum.c file for non-standard request management. The enumeration parameters (descriptor tables) are contained in the usb_user_configuration.c file.

Version:
1.6.2.1 (c5131-usb-generic-1_2_0)

Todo:
Bug:

Definition in file usb_enum.c.


Function Documentation

void usb_get_descriptor void   ) 
 

usb_get_descriptor.

This function manages the GET DESCRIPTOR request. The device descriptor, the configuration descriptor and the device qualifier are supported. All other descriptors must be supported by the usb_user_get_descriptor function. Only 1 configuration is supported.

Warning:
Code:xx bytes (function code length)
Parameters:
none 
Returns:
none

< sizeof (usb_user_device_descriptor);

< sizeof (usb_user_configuration_descriptor);

< don't care of wIndex field

< read wLength

< clear the receive setup flag

< send only requested number of data

< abort from Host

Definition at line 261 of file usb_enum.c.

References CONFIGURATION_DESCRIPTOR, data_to_transfer, DEVICE_DESCRIPTOR, EP_CONTROL_LENGTH, FALSE, Is_usb_read_control_enabled, Is_usb_receive_out, pbuffer, TRUE, U16, U8, Usb_ack_in_ready, Usb_ack_receive_out, Usb_ack_receive_setup, Usb_enable_stall_handshake, Usb_get_conf_desc_length, Usb_get_conf_desc_pointer, Usb_get_dev_desc_length, Usb_get_dev_desc_pointer, Usb_read_byte, Usb_send_control_in, Usb_set_DIR, usb_user_get_descriptor(), Usb_write_byte, and zlp.

Referenced by usb_process_request(), and usb_user_read_request().

00262 { 00263 U16 wLength ; 00264 U8 descriptor_type ; 00265 U8 string_type ; 00266 U8 c; // New 00267 00268 zlp = FALSE; /* no zero length packet */ 00269 string_type = Usb_read_byte(); /* read LSB of wValue */ 00270 descriptor_type = Usb_read_byte(); /* read MSB of wValue */ 00271 00272 switch (descriptor_type) 00273 { 00274 case DEVICE_DESCRIPTOR: 00275 data_to_transfer = Usb_get_dev_desc_length(); 00276 pbuffer = Usb_get_dev_desc_pointer(); 00277 break; 00278 case CONFIGURATION_DESCRIPTOR: 00279 data_to_transfer = Usb_get_conf_desc_length(); 00280 pbuffer = Usb_get_conf_desc_pointer(); 00281 break; 00282 default: 00283 if( usb_user_get_descriptor(descriptor_type, string_type)==FALSE ) 00284 { 00285 Usb_enable_stall_handshake(); 00286 Usb_ack_receive_setup(); 00287 return; 00288 } 00289 break; 00290 } 00291 00292 ACC = Usb_read_byte(); 00293 ACC = Usb_read_byte(); 00294 ((U8*)&wLength)[1] = Usb_read_byte(); 00295 ((U8*)&wLength)[0] = Usb_read_byte(); 00296 Usb_ack_receive_setup() ; 00297 Usb_set_DIR(); /* set out on EP0 */ 00298 00299 if (wLength > data_to_transfer) 00300 { 00301 if ((data_to_transfer % EP_CONTROL_LENGTH) == 0) { zlp = TRUE; } 00302 else { zlp = FALSE;} 00303 } 00304 else 00305 { 00306 data_to_transfer = (U8)wLength; 00307 } 00308 00309 while((data_to_transfer != 0) && (!Is_usb_receive_out())) 00310 { 00311 if (data_to_transfer > EP_CONTROL_LENGTH) { c = EP_CONTROL_LENGTH; } 00312 else { c = data_to_transfer; } 00313 data_to_transfer -= c; 00314 while ( c!= 0 ) 00315 { 00316 Usb_write_byte(*pbuffer); 00317 c--; 00318 pbuffer ++; 00319 } 00320 Usb_send_control_in(); 00321 while((!Is_usb_read_control_enabled()) && (!Is_usb_receive_out())); 00322 Usb_ack_in_ready(); 00323 /* while(!Is_usb_read_control_enabled()); // snd3 like, not compatible with c5131 00324 while(data_to_transfer != 0) 00325 { 00326 Usb_write_byte(*pbuffer); 00327 pbuffer ++; 00328 data_to_transfer --; 00329 } 00330 Usb_send_control_in(); 00331 */ 00332 } 00333 00334 // Usb_send_control_in(); 00335 00336 if(Is_usb_receive_out()) 00337 { 00338 Usb_ack_receive_out(); 00339 return; 00340 } 00341 if(zlp == TRUE) 00342 { 00343 Usb_send_control_in(); 00344 while((!Is_usb_read_control_enabled()) && (!Is_usb_receive_out())); 00345 Usb_ack_in_ready(); 00346 } 00347 00348 while(!Is_usb_receive_out()); 00349 Usb_ack_receive_out(); 00350 }

void usb_read_request void   )  [static]
 

void usb_set_address void   )  [static]
 

usb_set_address.

This function manages the SET ADDRESS request. When complete, the device will filter the requests using the new address.

Warning:
Code:xx bytes (function code length)
Parameters:
none 
Returns:
none

< send a ZLP for STATUS phase

< waits for status phase done < before using the new address

Definition at line 189 of file usb_enum.c.

References Is_usb_in_ready, U8, Usb_ack_in_ready, Usb_ack_receive_setup, Usb_configure_address, Usb_enable_address, Usb_read_byte, and Usb_send_control_in.

Referenced by usb_process_request().

00190 { 00191 U8 addr; 00192 00193 addr = Usb_read_byte(); 00194 Usb_ack_receive_setup(); 00195 00196 Usb_send_control_in(); 00197 while(!Is_usb_in_ready()); 00198 00199 Usb_ack_in_ready(); // for Tiger compatibility 00200 Usb_configure_address(addr); 00201 Usb_enable_address(); 00202 }

void usb_set_configuration void   )  [static]
 

usb_set_configuration.

This function manages the SET CONFIGURATION request. If the selected configuration is valid, this function call the usb_user_endpoint_init() function that will configure the endpoints following the configuration number.

Warning:
Code:xx bytes (function code length)
Parameters:
none 
Returns:
none

< send a ZLP for STATUS phase

< endpoint configuration

< keep that order (set StallRq/clear RxSetup) or a < OUT request following the SETUP may be acknowledged

Definition at line 218 of file usb_enum.c.

References Is_usb_in_ready, NB_CONFIGURATIONS, U8, Usb_ack_in_ready, Usb_ack_receive_setup, usb_configuration_nb, Usb_enable_stall_handshake, Usb_read_byte, Usb_send_control_in, Usb_set_configuration_action, and usb_user_endpoint_init().

Referenced by usb_process_request().

00219 { 00220 U8 configuration_number; 00221 00222 configuration_number = Usb_read_byte(); 00223 00224 if (configuration_number <= NB_CONFIGURATIONS) 00225 { 00226 Usb_ack_receive_setup(); 00227 usb_configuration_nb = configuration_number; 00228 } 00229 else 00230 { 00233 Usb_enable_stall_handshake(); 00234 Usb_ack_receive_setup(); 00235 return; 00236 } 00237 00238 Usb_send_control_in(); 00239 while(!Is_usb_in_ready()); 00240 Usb_ack_in_ready(); 00241 00242 usb_user_endpoint_init(usb_configuration_nb); 00243 Usb_set_configuration_action(); 00244 }

void usb_clear_feature void   )  [static]
 

usb_clear_feature.

This function manages the SET FEATURE request.

Warning:
Code:xx bytes (function code length)
Parameters:
none 
Returns:
none

< dummy read

< keep that order (set StallRq/clear RxSetup) or a < OUT request following the SETUP may be acknowledged

< keep that order (set StallRq/clear RxSetup) or a < OUT request following the SETUP may be acknowledged

Definition at line 505 of file usb_enum.c.

References bmRequestType, endpoint_status, ENDPOINT_TYPE, EP_CONTROL, FEATURE_ENDPOINT_HALT, INTERFACE_TYPE, Is_usb_endpoint_enabled, MSK_EP_DIR, U8, Usb_ack_receive_setup, Usb_disable_stall_handshake, Usb_enable_stall_handshake, Usb_read_byte, Usb_reset_data_toggle, Usb_reset_endpoint, Usb_select_endpoint, Usb_send_control_in, and ZERO_TYPE.

Referenced by usb_process_request().

00506 { 00507 U8 wValue; 00508 U8 wIndex; 00509 00510 if (bmRequestType == ZERO_TYPE) 00511 { 00514 Usb_enable_stall_handshake(); 00515 Usb_ack_receive_setup(); 00516 return; 00517 } 00518 else if (bmRequestType == INTERFACE_TYPE) 00519 { 00522 Usb_enable_stall_handshake(); 00523 Usb_ack_receive_setup(); 00524 return; 00525 } 00526 else if (bmRequestType == ENDPOINT_TYPE) 00527 { 00528 wValue = Usb_read_byte(); 00529 ACC = Usb_read_byte(); 00530 00531 if (wValue == FEATURE_ENDPOINT_HALT) 00532 { 00533 wIndex = (Usb_read_byte() & MSK_EP_DIR); 00534 00535 Usb_select_endpoint(wIndex); 00536 if(Is_usb_endpoint_enabled()) 00537 { 00538 if(wIndex != EP_CONTROL) 00539 { 00540 Usb_disable_stall_handshake(); 00541 Usb_reset_endpoint(wIndex); 00542 Usb_reset_data_toggle(); 00543 } 00544 Usb_select_endpoint(EP_CONTROL); 00545 endpoint_status[wIndex] = 0x00; 00546 Usb_ack_receive_setup(); 00547 Usb_send_control_in(); 00548 } 00549 else 00550 { 00551 Usb_enable_stall_handshake(); 00552 Usb_ack_receive_setup(); 00553 return; 00554 } 00555 } 00556 else 00557 { 00558 Usb_enable_stall_handshake(); 00559 Usb_ack_receive_setup(); 00560 return; 00561 } 00562 } 00563 }

void usb_set_feature void   )  [static]
 

usb_set_feature.

This function manages the SET FEATURE request. The USB test modes are supported by this function.

Warning:
Code:xx bytes (function code length)
Parameters:
none 
Returns:
none

< dummy read

< keep that order (set StallRq/clear RxSetup) or a < OUT request following the SETUP may be acknowledged

Definition at line 440 of file usb_enum.c.

References bmRequestType, endpoint_status, ENDPOINT_TYPE, EP_CONTROL, FEATURE_ENDPOINT_HALT, INTERFACE_TYPE, Is_usb_endpoint_enabled, MSK_EP_DIR, U8, Usb_ack_receive_setup, Usb_enable_stall_handshake, Usb_read_byte, Usb_select_endpoint, and Usb_send_control_in.

Referenced by usb_process_request().

00441 { 00442 U8 wValue; 00443 U8 wIndex; 00444 00445 if (bmRequestType == INTERFACE_TYPE) 00446 { 00449 Usb_enable_stall_handshake(); 00450 Usb_ack_receive_setup(); 00451 return; 00452 } 00453 else if (bmRequestType == ENDPOINT_TYPE) 00454 { 00455 wValue = Usb_read_byte(); 00456 ACC = Usb_read_byte(); 00457 00458 if (wValue == FEATURE_ENDPOINT_HALT) 00459 { 00460 wIndex = (Usb_read_byte() & MSK_EP_DIR); 00461 00462 if (wIndex == EP_CONTROL) 00463 { 00464 Usb_enable_stall_handshake(); 00465 Usb_ack_receive_setup(); 00466 return; 00467 } 00468 00469 Usb_select_endpoint(wIndex); 00470 if(Is_usb_endpoint_enabled()) 00471 { 00472 Usb_enable_stall_handshake(); 00473 Usb_select_endpoint(EP_CONTROL); 00474 endpoint_status[wIndex] = 0x01; 00475 Usb_ack_receive_setup(); 00476 Usb_send_control_in(); 00477 } 00478 else 00479 { 00480 Usb_enable_stall_handshake(); 00481 Usb_ack_receive_setup(); 00482 return; 00483 } 00484 } 00485 else 00486 { 00487 Usb_enable_stall_handshake(); 00488 Usb_ack_receive_setup(); 00489 return; 00490 } 00491 } 00492 }

void usb_get_status void   )  [static]
 

usb_get_status.

This function manages the GET STATUS request. The device, interface or endpoint status is returned.

Warning:
Code:xx bytes (function code length)
Parameters:
none 
Returns:
none

< dummy read

< dummy read

Definition at line 389 of file usb_enum.c.

References bmRequestType, DEVICE_STATUS, endpoint_status, INTERFACE_STATUS, Is_usb_in_ready, Is_usb_receive_out, MSK_EP_DIR, REQUEST_DEVICE_STATUS, REQUEST_ENDPOINT_STATUS, REQUEST_INTERFACE_STATUS, U8, Usb_ack_in_ready, Usb_ack_receive_out, Usb_ack_receive_setup, Usb_enable_stall_handshake, Usb_read_byte, Usb_send_control_in, Usb_set_DIR, and Usb_write_byte.

Referenced by usb_process_request().

00390 { 00391 U8 wIndex; 00392 00393 ACC = Usb_read_byte(); 00394 ACC = Usb_read_byte(); 00395 wIndex = Usb_read_byte(); 00396 00397 switch(bmRequestType) 00398 { 00399 case REQUEST_DEVICE_STATUS: Usb_ack_receive_setup(); 00400 Usb_set_DIR(); 00401 Usb_write_byte(DEVICE_STATUS); 00402 break; 00403 00404 case REQUEST_INTERFACE_STATUS: Usb_ack_receive_setup(); 00405 Usb_set_DIR(); 00406 Usb_write_byte(INTERFACE_STATUS); 00407 break; 00408 00409 case REQUEST_ENDPOINT_STATUS: Usb_ack_receive_setup(); 00410 Usb_set_DIR(); 00411 wIndex = wIndex & MSK_EP_DIR; 00412 Usb_write_byte(endpoint_status[wIndex]); 00413 break; 00414 default: 00415 Usb_enable_stall_handshake(); 00416 Usb_ack_receive_setup(); 00417 return; 00418 } 00419 00420 Usb_write_byte(0x00); 00421 Usb_send_control_in(); 00422 while(!Is_usb_in_ready()); 00423 Usb_ack_in_ready(); 00424 while( !Is_usb_receive_out() ); 00425 Usb_ack_receive_out(); 00426 }

void usb_get_configuration void   )  [static]
 

usb_get_configuration.

This function manages the GET CONFIGURATION request. The current configuration number is returned.

Warning:
Code:xx bytes (function code length)
Parameters:
none 
Returns:
none

Definition at line 364 of file usb_enum.c.

References Is_usb_in_ready, Is_usb_receive_out, Usb_ack_in_ready, Usb_ack_receive_out, Usb_ack_receive_setup, usb_configuration_nb, Usb_send_control_in, Usb_set_DIR, and Usb_write_byte.

Referenced by usb_process_request().

00365 { 00366 Usb_ack_receive_setup(); 00367 Usb_set_DIR(); /* set out on EP0 */ 00368 00369 Usb_write_byte(usb_configuration_nb); 00370 // Usb_ack_in_ready(); 00371 Usb_send_control_in(); 00372 while(!Is_usb_in_ready());// while(!Is_usb_read_control_enabled()); 00373 Usb_ack_in_ready(); 00374 while( !Is_usb_receive_out() ); 00375 Usb_ack_receive_out(); 00376 }

void usb_enum_var_init void   ) 
 

usb_enum_variable_init.

This function initializes the main usb variables:

  • endpoint status
  • connection status

Warning:
Code:xx bytes (function code length)
Parameters:
none 
Returns:
none

< USB is not connected

< Default configuration number is 0

Definition at line 78 of file usb_enum.c.

References endpoint_status, ep_num, FALSE, NB_ENDPOINTS, rx_bank, U8, usb_configuration_nb, and usb_connected.

Referenced by usb_interrupt().

00079 { 00080 U8 ep_num ; 00081 00082 for( ep_num=0 ; ep_num<NB_ENDPOINTS ; ep_num++ ) 00083 { 00084 endpoint_status[ep_num] = 0; 00085 } 00086 usb_connected = FALSE; 00087 usb_configuration_nb = 0 ; 00088 #ifndef NO_SUPPORT_USB_PING_PONG 00089 rx_bank = 0; 00090 #endif 00091 00092 }

void usb_process_request void   ) 
 

usb_process_request.

This function reads the SETUP request sent to the default control endpoint and calls the appropriate function. When exiting of the usb_read_request function, the device is ready to manage the next request.

Warning:
Code:xx bytes (function code length)
Parameters:
none 
Returns:
none NOTE: list of supported requests: GET_DESCRIPTOR GET_CONFIGURATION SET_ADDRESS SET_CONFIGURATION CLEAR_FEATURE SET_FEATURE GET_STATUS

< un-supported request => call to user read request

Definition at line 115 of file usb_enum.c.

References bmRequestType, CLEAR_FEATURE, EP_CONTROL, FALSE, GET_CONFIGURATION, GET_DESCRIPTOR, GET_INTERFACE, GET_STATUS, SET_ADDRESS, SET_CONFIGURATION, SET_DESCRIPTOR, SET_FEATURE, SET_INTERFACE, SYNCH_FRAME, U8, Usb_ack_receive_setup, usb_clear_feature(), Usb_enable_stall_handshake, usb_get_configuration(), usb_get_descriptor(), usb_get_status(), Usb_read_byte, Usb_select_endpoint, usb_set_address(), usb_set_configuration(), usb_set_feature(), and usb_user_read_request().

Referenced by usb_task().

00116 { 00117 U8 bmRequest; 00118 00119 Usb_select_endpoint(EP_CONTROL); 00120 00121 bmRequestType = Usb_read_byte(); 00122 bmRequest = Usb_read_byte(); 00123 00124 switch (bmRequest) 00125 { 00126 case GET_DESCRIPTOR: 00127 if (0x80 == bmRequestType) { usb_get_descriptor(); } 00128 else { usb_user_read_request(bmRequestType, bmRequest); } 00129 break; 00130 00131 case GET_CONFIGURATION: 00132 if (0x80 == bmRequestType) { usb_get_configuration(); } 00133 else { usb_user_read_request(bmRequestType, bmRequest); } 00134 break; 00135 00136 case SET_ADDRESS: 00137 if (0x00 == bmRequestType) { usb_set_address(); } 00138 else { usb_user_read_request(bmRequestType, bmRequest); } 00139 break; 00140 00141 case SET_CONFIGURATION: 00142 if (0x00 == bmRequestType) { usb_set_configuration(); } 00143 else { usb_user_read_request(bmRequestType, bmRequest); } 00144 break; 00145 00146 case CLEAR_FEATURE: 00147 if (0x02 >= bmRequestType) { usb_clear_feature(); } 00148 else { usb_user_read_request(bmRequestType, bmRequest); } 00149 break; 00150 00151 case SET_FEATURE: 00152 if (0x02 >= bmRequestType) { usb_set_feature(); } 00153 else { usb_user_read_request(bmRequestType, bmRequest); } 00154 break; 00155 00156 case GET_STATUS: 00157 if ((0x7F < bmRequestType) & (0x82 >= bmRequestType)) 00158 { usb_get_status(); } 00159 else { usb_user_read_request(bmRequestType, bmRequest); } 00160 break; 00161 00162 case GET_INTERFACE: 00163 case SET_DESCRIPTOR: 00164 case SET_INTERFACE: 00165 case SYNCH_FRAME: 00166 default: 00167 if(usb_user_read_request(bmRequestType, bmRequest) == FALSE) 00168 { 00169 Usb_enable_stall_handshake(); 00170 Usb_ack_receive_setup(); 00171 return; 00172 } 00173 break; 00174 } 00175 }


Variable Documentation

bdata bit rx_bank
 

Definition at line 50 of file usb_enum.c.

Referenced by usb_enum_var_init().

bit zlp [static]
 

Definition at line 52 of file usb_enum.c.

Referenced by usb_get_descriptor().

_MEM_TYPE_SLOW_ U8 endpoint_status[7] [static]
 

Definition at line 53 of file usb_enum.c.

Referenced by usb_clear_feature(), usb_enum_var_init(), usb_get_status(), and usb_set_feature().

_MEM_TYPE_SLOW_ U8* pbuffer
 

Definition at line 54 of file usb_enum.c.

Referenced by usb_get_descriptor(), and usb_user_get_descriptor().

_MEM_TYPE_SLOW_ U8 data_to_transfer
 

Definition at line 55 of file usb_enum.c.

Referenced by usb_get_descriptor(), and usb_user_get_descriptor().

U8 bmRequestType [static]
 

Definition at line 58 of file usb_enum.c.

Referenced by usb_clear_feature(), usb_get_status(), usb_process_request(), and usb_set_feature().

_MEM_TYPE_SLOW_ U8 usb_configuration_nb
 

Definition at line 60 of file usb_enum.c.

Referenced by usb_enum_var_init(), usb_get_configuration(), usb_read_continuous(), usb_set_configuration(), and usb_write_continuous().

bit usb_connected
 

Public : (bit) usb_connected usb_connected is set to TRUE when VBUS has been detected usb_connected is set to FALSE otherwise /.

Definition at line 61 of file usb_enum.c.

Referenced by usb_enum_var_init(), usb_interrupt(), usb_task(), and usb_task_init().

code S_usb_device_descriptor usb_user_device_descriptor
 

Definition at line 62 of file usb_enum.c.

code S_usb_user_configuration_descriptor usb_user_configuration_descriptor
 

Definition at line 63 of file usb_enum.c.


Generated on Mon Apr 10 17:23:31 2006 for Atmel by doxygen 1.3.7