00001
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 #include "config.h"
00044 #include "conf_usb.h"
00045 #include "usb_drv.h"
00046
00047
00048
00049
00050
00051 #if (USB_DEVICE_FEATURE==DISABLED && USB_HOST_FEATURE==DISABLED)
00052 #error at least one of USB_DEVICE_FEATURE or USB_HOST_FEATURE should be unabled
00053 #endif
00054
00055 #if (USB_DEVICE_FEATURE == ENABLED)
00056
00067 U8 usb_config_ep(U8 config0, U8 config1)
00068 {
00069 Usb_enable_endpoint();
00070 UECFG0X = config0;
00071 UECFG1X = (UECFG1X & (1<<ALLOC)) | config1;
00072 Usb_allocate_memory();
00073 return (Is_endpoint_configured());
00074 }
00075
00087 U8 usb_select_enpoint_interrupt(void)
00088 {
00089 U8 interrupt_flags;
00090 U8 ep_num;
00091
00092 ep_num = 0;
00093 interrupt_flags = Usb_interrupt_flags();
00094
00095 while(ep_num < MAX_EP_NB)
00096 {
00097 if (interrupt_flags & 1)
00098 {
00099 return (ep_num);
00100 }
00101 else
00102 {
00103 ep_num++;
00104 interrupt_flags = interrupt_flags >> 1;
00105 }
00106 }
00107 return 0;
00108 }
00109
00130 U8 usb_send_packet(U8 ep_num, U8* tbuf, U8 data_length)
00131 {
00132 U8 remaining_length;
00133
00134 remaining_length = data_length;
00135 Usb_select_endpoint(ep_num);
00136 while(Is_usb_write_enabled() && (0 != remaining_length))
00137 {
00138 Usb_write_byte(*tbuf);
00139 remaining_length--;
00140 tbuf++;
00141 }
00142 return remaining_length;
00143 }
00144
00165 U8 usb_read_packet(U8 ep_num, U8* rbuf, U8 data_length)
00166 {
00167 U8 remaining_length;
00168
00169 remaining_length = data_length;
00170 Usb_select_endpoint(ep_num);
00171
00172 while(Is_usb_read_enabled() && (0 != remaining_length))
00173 {
00174 *rbuf = Usb_read_byte();
00175 remaining_length--;
00176 rbuf++;
00177 }
00178 return remaining_length;
00179 }
00180
00191 void usb_halt_endpoint (U8 ep_num)
00192 {
00193 Usb_select_endpoint(ep_num);
00194 Usb_enable_stall_handshake();
00195 }
00196
00207 U8 usb_init_device (void)
00208 {
00209 Usb_select_device();
00210 if(Is_usb_id_device())
00211 {
00212 Usb_select_endpoint(EP_CONTROL);
00213 if(!Is_usb_endpoint_enabled())
00214 {
00215 #if (USB_LOW_SPEED_DEVICE==DISABLE)
00216 return usb_configure_endpoint(EP_CONTROL, \
00217 TYPE_CONTROL, \
00218 DIRECTION_OUT, \
00219 SIZE_64, \
00220 ONE_BANK, \
00221 NYET_DISABLED);
00222 #else
00223 return usb_configure_endpoint(EP_CONTROL, \
00224 TYPE_CONTROL, \
00225 DIRECTION_OUT, \
00226 SIZE_8, \
00227 ONE_BANK, \
00228 NYET_DISABLED);
00229 #endif
00230 }
00231 }
00232 return FALSE;
00233 }
00234
00235 #endif
00236
00240
00241 #if (USB_HOST_FEATURE == ENABLED)
00242
00252 U8 host_config_pipe(U8 config0, U8 config1)
00253 {
00254 Host_enable_pipe();
00255 UPCFG0X = config0;
00256 UPCFG1X = config1;
00257 Host_allocate_memory();
00258 return (Is_pipe_configured());
00259 }
00260
00268 U8 host_determine_pipe_size(U16 size)
00269 {
00270 if(size <= 8 ) {return (SIZE_8 );}
00271 else if(size <= 16 ) {return (SIZE_16 );}
00272 else if(size <= 32 ) {return (SIZE_32 );}
00273 else if(size <= 64 ) {return (SIZE_64 );}
00274 else if(size <= 128) {return (SIZE_128 );}
00275 else if(size <= 256) {return (SIZE_256 );}
00276 else if(size <= 512) {return (SIZE_512 );}
00277 else {return (SIZE_1024);}
00278
00279 }
00280
00288 void host_disable_all_pipe(void)
00289 {
00290 U8 i;
00291 for (i=0;i<7;i++)
00292 {
00293 Host_reset_pipe(i);
00294 Host_select_pipe(i);
00295 Host_unallocate_memory();
00296 Host_disable_pipe();
00297 }
00298 }
00299
00309 U8 usb_get_nb_pipe_interrupt(void)
00310 {
00311 U8 interrupt_flags;
00312 U8 i;
00313
00314 interrupt_flags = Host_get_pipe_interrupt();
00315 for(i=0;i< MAX_EP_NB;i++)
00316 {
00317 if (interrupt_flags & (1<<i))
00318 {
00319 return (i);
00320 }
00321 }
00322
00323 return MAX_EP_NB+1;
00324 }
00325
00326
00327 #endif // USB_HOST_FEATURE == ENABLED
00328
00329