00001
00047 #include "config.h"
00048 #include "conf_usb.h"
00049 #include "usb_drv.h"
00050
00051
00059 uint8_t usb_config_ep(uint8_t config0, uint8_t config1)
00060 {
00061 Usb_enable_endpoint();
00062 UECFG0X = config0;
00063 UECFG1X = (UECFG1X & (1<<ALLOC)) | config1;
00064 Usb_allocate_memory();
00065 return (Is_endpoint_configured());
00066 }
00067
00068
00075 uint8_t usb_select_enpoint_interrupt(void)
00076 {
00077 uint8_t interrupt_flags;
00078 uint8_t ep_num;
00079
00080 ep_num = 0;
00081 interrupt_flags = Usb_interrupt_flags();
00082
00083 while(ep_num < 9){
00084 if (interrupt_flags & 1){
00085 return (ep_num);
00086 }else{
00087 ep_num++;
00088 interrupt_flags = interrupt_flags >> 1;
00089 }
00090 }
00091 return 0;
00092 }
00093
00094
00106 uint8_t usb_send_packet(uint8_t ep_num, uint8_t* tbuf, uint8_t data_length)
00107 {
00108 uint8_t remaining_length;
00109
00110 remaining_length = data_length;
00111 Usb_select_endpoint(ep_num);
00112 while(Is_usb_write_enabled() && (0 != remaining_length)){
00113 Usb_write_byte(*tbuf);
00114 remaining_length--;
00115 tbuf++;
00116 }
00117 return remaining_length;
00118 }
00119
00131 uint8_t usb_read_packet(uint8_t ep_num, uint8_t* rbuf, uint8_t data_length)
00132 {
00133 uint8_t remaining_length;
00134
00135 remaining_length = data_length;
00136 Usb_select_endpoint(ep_num);
00137
00138 while(Is_usb_read_enabled() && (0 != remaining_length)){
00139 *rbuf = Usb_read_byte();
00140 remaining_length--;
00141 rbuf++;
00142 }
00143 return remaining_length;
00144 }
00145
00146
00153 void usb_halt_endpoint (uint8_t ep_num)
00154 {
00155 Usb_select_endpoint(ep_num);
00156 Usb_enable_stall_handshake();
00157 }
00158
00159
00165 uint8_t usb_init_device (void)
00166 {
00167 Usb_select_device();
00168 if(Is_usb_id_device())
00169 {
00170 Usb_select_endpoint(EP_CONTROL);
00171 if(!Is_usb_endpoint_enabled())
00172 {
00173 return usb_configure_endpoint(EP_CONTROL, \
00174 TYPE_CONTROL, \
00175 DIRECTION_OUT, \
00176 SIZE_64, \
00177 ONE_BANK, \
00178 NYET_DISABLED);
00179 }
00180 }
00181 return false;
00182 }
00183