00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "config.h"
00020 #include "conf_usb.h"
00021 #include "usb_drv.h"
00022
00023
00024
00025
00026
00027 #if (USB_DEVICE_FEATURE==DISABLED && USB_HOST_FEATURE==DISABLED)
00028 #error at least one of USB_DEVICE_FEATURE or USB_HOST_FEATURE should be unabled
00029 #endif
00030
00031 #if (USB_DEVICE_FEATURE == ENABLED)
00032
00043 U8 usb_config_ep(U8 config0, U8 config1)
00044 {
00045 Usb_enable_endpoint();
00046 UECFG0X = config0;
00047 UECFG1X = (UECFG1X & (1<<ALLOC)) | config1;
00048 Usb_allocate_memory();
00049 return (Is_endpoint_configured());
00050 }
00051
00063 U8 usb_select_enpoint_interrupt(void)
00064 {
00065 U8 interrupt_flags;
00066 U8 ep_num;
00067
00068 ep_num = 0;
00069 interrupt_flags = Usb_interrupt_flags();
00070
00071 while(ep_num < 9)
00072 {
00073 if (interrupt_flags & 1)
00074 {
00075 return (ep_num);
00076 }
00077 else
00078 {
00079 ep_num++;
00080 interrupt_flags = interrupt_flags >> 1;
00081 }
00082 }
00083 return 0;
00084 }
00085
00106 U8 usb_send_packet(U8 ep_num, U8* tbuf, U8 data_length)
00107 {
00108 U8 remaining_length;
00109
00110 remaining_length = data_length;
00111 Usb_select_endpoint(ep_num);
00112 while(Is_usb_write_enabled() && (0 != remaining_length))
00113 {
00114 Usb_write_byte(*tbuf);
00115 remaining_length--;
00116 tbuf++;
00117 }
00118 return remaining_length;
00119 }
00120
00141 U8 usb_read_packet(U8 ep_num, U8* rbuf, U8 data_length)
00142 {
00143 U8 remaining_length;
00144
00145 remaining_length = data_length;
00146 Usb_select_endpoint(ep_num);
00147
00148 while(Is_usb_read_enabled() && (0 != remaining_length))
00149 {
00150 *rbuf = Usb_read_byte();
00151 remaining_length--;
00152 rbuf++;
00153 }
00154 return remaining_length;
00155 }
00156
00167 void usb_halt_endpoint (U8 ep_num)
00168 {
00169 Usb_select_endpoint(ep_num);
00170 Usb_enable_stall_handshake();
00171 }
00172
00183 U8 usb_init_device (void)
00184 {
00185 Usb_select_device();
00186 if(Is_usb_id_device())
00187 {
00188 Usb_select_endpoint(EP_CONTROL);
00189 if(!Is_usb_endpoint_enabled())
00190 {
00191 return usb_configure_endpoint(EP_CONTROL, \
00192 TYPE_CONTROL, \
00193 DIRECTION_OUT, \
00194 SIZE_64, \
00195 ONE_BANK, \
00196 NYET_DISABLED);
00197 }
00198 }
00199 return FALSE;
00200 }
00201
00202 #endif
00203
00207
00208 #if (USB_HOST_FEATURE == ENABLED)
00209
00219 U8 host_config_pipe(U8 config0, U8 config1)
00220 {
00221 Host_enable_pipe();
00222 UPCFG0X = config0;
00223 UPCFG1X = config1;
00224 Host_allocate_memory();
00225 return (Is_pipe_configured());
00226 }
00227
00235 U8 host_determine_pipe_size(U16 size)
00236 {
00237 if(size <= 8 ) {return (SIZE_8 );}
00238 else if(size <= 16 ) {return (SIZE_16 );}
00239 else if(size <= 32 ) {return (SIZE_32 );}
00240 else if(size <= 64 ) {return (SIZE_64 );}
00241 else if(size <= 128) {return (SIZE_128 );}
00242 else if(size <= 256) {return (SIZE_256 );}
00243 else if(size <= 512) {return (SIZE_512 );}
00244 else {return (SIZE_1024);}
00245
00246 }
00247
00255 void host_disable_all_pipe(void)
00256 {
00257 U8 i;
00258 for (i=0;i<7;i++)
00259 {
00260 Host_reset_pipe(i);
00261 Host_select_pipe(i);
00262 Host_unallocate_memory();
00263 Host_disable_pipe();
00264 }
00265
00266 }
00267
00268
00269
00270 #endif // USB_HOST_FEATURE == ENABLED
00271
00272