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

usb_drv.c File Reference

Copyright (c) 2004 This file contains the USB driver routines. More...

#include "config.h"
#include "usb_drv.h"

Go to the source code of this file.

Functions

U8 usb_config_ep (U8 config0, U8 config1)
 usb_configure_endpoint.

U8 usb_select_enpoint_interrupt (void)
 usb_select_endpoint_interrupt.

U8 usb_send_packet (U8 ep_num, U8 *tbuf, U8 data_length)
 usb_send_packet.

U8 usb_read_packet (U8 ep_num, U8 *rbuf, U8 data_length)
 usb_read_packet.

U8 usb_init_device (void)
 usb_init_device.

void usb_generate_clock (void)
 Usb_configure_clock.


Detailed Description

Copyright (c) 2004 This file contains the USB driver routines.

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 driver routines.

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

Todo:
Bug:

Definition in file usb_drv.c.


Function Documentation

U8 usb_config_ep U8  config0,
U8  config1
 

usb_configure_endpoint.

This function configures an endpoint with the selected type.

Warning:
Code:xx bytes (function code length)
Parameters:
uc_data_receive Number of the endpoint to configure.
i_length Type to configure.
Returns:
Is_endpoint_configured.
The possible types are: CONTROL BULK_IN BULK_OUT INTERRUPT_IN INTERRUPT_OUT ISOCHRONOUS_IN ISOCHRONOUS_OUT

EXAMPLE: usb_configure_endpoint(EP_CONTROL,TYPE_CONTROL, DIRECTION_OUT, SIZE_64, ONE_BANK, NYET_DISABLED);

NOTE: The endpoint 0 shall always be configured in CONTROL type.

Definition at line 52 of file usb_drv.c.

References MSK_EPEN, TRUE, U8, Usb_allocate_memory, Usb_enable_endpoint, Usb_set_cfg0, and Usb_set_cfg1.

00053 { 00054 Usb_enable_endpoint(); 00055 Usb_set_cfg0(config0); // UECFG0X = config0; 00056 Usb_set_cfg1(config1);// UECFG1X = (UECFG1X & MSK_ALLOC) | config1; 00057 UEPCONX = MSK_EPEN | (config0>>6) | ((config0 & 0x01)<<2); 00058 Usb_allocate_memory(); 00059 return (TRUE); 00060 }

U8 usb_select_enpoint_interrupt void   ) 
 

usb_select_endpoint_interrupt.

This function select the endpoint where an event occurs and returns the number of this endpoint. If no event occurs on the endpoints, this function returns 0.

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

Definition at line 74 of file usb_drv.c.

References ep_num, U8, and Usb_interrupt_flags.

00075 { 00076 U8 interrupt_flags; 00077 U8 ep_num; 00078 00079 ep_num = 0; 00080 interrupt_flags = Usb_interrupt_flags(); 00081 00082 while(ep_num < 9) 00083 { 00084 if (interrupt_flags & 1) 00085 { 00086 return (ep_num); 00087 } 00088 else 00089 { 00090 ep_num++; 00091 interrupt_flags = interrupt_flags >> 1; 00092 } 00093 } 00094 return 0; 00095 }

U8 usb_send_packet U8  ep_num,
U8 tbuf,
U8  data_length
 

usb_send_packet.

This function moves the data pointed by tbuf to the selected endpoint fifo and sends it through the USB.

Warning:
Code:xx bytes (function code length)
Parameters:
ep_num number of the addressed endpoint
*tbuf address of the first data to send
data_length number of bytes to send
Returns:
address of the next U8 to send.
Example: usb_send_packet(3,&first_data,0x20); // send packet on the endpoint #3 while(!(Usb_tx_complete)); // wait packet ACK'ed by the Host Usb_clear_tx_complete(); // acknowledge the transmit

Note: tbuf is incremented of 'data_length'.

Definition at line 118 of file usb_drv.c.

References ep_num, Is_usb_write_enabled, U8, Usb_select_endpoint, Usb_send_control_in, and Usb_write_byte.

00119 { 00120 U8 remaining_length; 00121 00122 remaining_length = data_length; 00123 Usb_select_endpoint(ep_num); 00124 while(Is_usb_write_enabled() && (0 != remaining_length)) 00125 { 00126 Usb_write_byte(*tbuf); 00127 remaining_length--; 00128 tbuf++; 00129 } 00130 Usb_send_control_in(); 00131 return remaining_length; 00132 }

U8 usb_read_packet U8  ep_num,
U8 rbuf,
U8  data_length
 

usb_read_packet.

This function moves the data stored in the selected endpoint fifo to the address specified by *rbuf.

Warning:
Code:xx bytes (function code length)
Parameters:
ep_num number of the addressed endpoint
*rbuf aaddress of the first data to write with the USB data
data_length number of bytes to read
Returns:
address of the next U8 to send.
Example: while(!(Usb_rx_complete)); // wait new packet received usb_read_packet(4,&first_data,usb_get_nb_byte); // read packet from ep 4 Usb_clear_rx(); // acknowledge the transmit

Note: rbuf is incremented of 'data_length'.

Definition at line 155 of file usb_drv.c.

References ep_num, Is_usb_read_enabled, U8, Usb_read_byte, and Usb_select_endpoint.

Referenced by usb_read_continuous().

00156 { 00157 U8 remaining_length; 00158 00159 remaining_length = data_length; 00160 Usb_select_endpoint(ep_num); 00161 00162 while(Is_usb_read_enabled() && (0 != remaining_length)) 00163 { 00164 *rbuf = Usb_read_byte(); 00165 remaining_length--; 00166 rbuf++; 00167 } 00168 return remaining_length; 00169 }

U8 usb_init_device void   ) 
 

usb_init_device.

This function initializes the USB device controller and configures the Default Control Endpoint.

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

Definition at line 200 of file usb_drv.c.

References DIRECTION_OUT, EP_CONTROL, NYET_DISABLED, ONE_BANK, SIZE_64, TRUE, TYPE_CONTROL, U8, and usb_configure_endpoint.

Referenced by usb_start_device().

00201 { 00202 usb_configure_endpoint(EP_CONTROL, \ 00203 TYPE_CONTROL, \ 00204 DIRECTION_OUT, \ 00205 SIZE_64, \ 00206 ONE_BANK, \ 00207 NYET_DISABLED); 00208 return TRUE; 00209 }

void usb_generate_clock void   ) 
 

Usb_configure_clock.

This function configure the PLL to generate the 48MHz clock required by the USB controller, considering the FOSC defined in the "config.h" file.

Warning:
Code:xx bytes (function code length)
Parameters:
none The possible value for FOSC are : 3000 ( 3MHz) 6000 ( 6MHz) 8000 ( 8MHz) 12000 (12MHz) 16000 (16MHz) 18000 (18MHz) 20000 (20MHz) 24000 (24MHz) 32000 (32MHz) 40000 (40MHz) 48000 (48MHz) 0000 (frequency auto-dectection)
Returns:
none

Definition at line 235 of file usb_drv.c.

References MSK_SOFINT, MSK_T0X2, MSK_X2, PLL_12MHz, PLL_16MHz, PLL_18MHz, PLL_20MHz, PLL_24MHz, PLL_32MHz, PLL_3MHz, PLL_40MHz, PLL_4MHz, PLL_6MHz, PLL_8MHz, Pll_enable, Pll_set_div, Pll_stop, and Uchar.

Referenced by usb_start_device().

00236 { 00237 #if FOSC == 3000 00238 Pll_set_div(PLL_3MHz); 00239 Pll_enable(); 00240 #endif 00241 00242 #if FOSC == 4000 00243 Pll_set_div(PLL_4MHz); 00244 Pll_enable(); 00245 #endif 00246 00247 #if FOSC == 6000 00248 Pll_set_div(PLL_6MHz); 00249 Pll_enable(); 00250 #endif 00251 00252 #if FOSC == 8000 00253 Pll_set_div(PLL_8MHz); 00254 Pll_enable(); 00255 #endif 00256 00257 #if FOSC == 12000 00258 Pll_set_div(PLL_12MHz); 00259 Pll_enable(); 00260 #endif 00261 00262 #if FOSC == 16000 00263 Pll_set_div(PLL_16MHz); 00264 Pll_enable(); 00265 #endif 00266 00267 #if FOSC == 18000 00268 Pll_set_div(PLL_18MHz); 00269 Pll_enable(); 00270 #endif 00271 00272 #if FOSC == 20000 00273 Pll_set_div(PLL_20MHz); 00274 Pll_enable(); 00275 #endif 00276 00277 #if FOSC == 24000 00278 Pll_set_div(PLL_24MHz); 00279 Pll_enable(); 00280 #endif 00281 00282 #if FOSC == 32000 00283 Pll_set_div(PLL_32MHz); 00284 Pll_enable(); 00285 #endif 00286 00287 #if FOSC == 40000 00288 Pll_set_div(PLL_40MHz); 00289 Pll_enable(); 00290 #endif 00291 00292 #if FOSC == 48000 00293 Usb_set_EXT48(); 00294 #endif 00295 00296 #if FOSC == 0000 /* frequency Auto-detection */ 00297 00298 Uchar i; 00299 Uchar reload; 00300 00301 PLLCON = 0x00; 00302 00303 if (CKCON0 & MSK_X2) 00304 { 00305 CKCON0 |= MSK_T0X2; 00306 reload = 6; 00307 } 00308 else 00309 { 00310 CKCON0 &= ~MSK_T0X2; 00311 reload = 9; 00312 } 00313 00314 TMOD = 0x01; /* put Timer 0 in mode 1 */ 00315 USBINT = 0x00; 00316 i=reload; 00317 00318 while (!(USBINT & MSK_SOFINT)) /* Do until Start Of Frame detection */ 00319 { 00320 if (i==9) 00321 { 00322 Pll_stop(); /* external 48 MHz supposed */ 00323 Usb_set_EXT48(); 00324 } 00325 else 00326 { 00327 Usb_clear_EXT48(); /* PLL output supposed */ 00328 Pll_set_div(pll_value[i]); /* configure PLL */ 00329 Pll_enable(); 00330 } 00331 TH0 = TH0_value[i]; /* Run Timer 0 */ 00332 TL0 = TL0_value[i]; 00333 TCON |= 0x10; 00334 while(((TCON & 0x20) != 0x20)); /* Wait Timer 0 Overflow */ 00335 TCON &= ~(0x30); 00336 if (i==0) 00337 { 00338 i=reload; 00339 } 00340 else 00341 { 00342 i--; 00343 } 00344 } 00345 00346 TH0 = 0; /* Reset Timer 0 Registers */ 00347 TL0 = 0; 00348 TCON = 0; 00349 TMOD = 0; 00350 #endif 00351 }


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