00001
00015 #include "pic_usb.h"
00016 #include "memory.h"
00017 #include "config.h"
00018 #include "pic_usb_buffer_mgt.h"
00019
00020 #ifdef USB_DEBUG
00021 #include "pic_serial.h"
00022 #endif
00023
00024
00025
00026
00027 usb_state_type usb_state = st_POWERED;
00028
00029 setup_data_packet usb_sdp;
00030
00031 uns8 usb_address;
00032 control_mode_type control_mode;
00033 uns16 delivery_bytes_to_send,
00034 delivery_bytes_max_send,
00035 delivery_bytes_sent;
00036 uns8 *delivery_ptr;
00037 uns8 delivery_buffer_size;
00038 uns8 *delivery_buffer;
00039 buffer_descriptor *delivery_bd;
00040
00041 usb_status_type usb_status;
00042
00043 void usb_configure_endpoints() {
00044
00045 #ifdef USB_DEBUG
00046 serial_print_str("Config eps ");
00047 #endif
00048 #ifdef USB_EP1
00049 set_bit (uep1, EPHSHK);
00050 #ifdef USB_EP1_OUT_SIZE
00051 set_bit(uep1, EPOUTEN);
00052 #else
00053 clear_bit(uep1, EPOUTEN);
00054 #endif
00055 #ifdef USB_EP1_IN_SIZE
00056 set_bit(uep1, EPINEN);
00057 #else
00058 clear_bit(uep1, EPINEN);
00059 #endif
00060 set_bit (uep1, EPCONDIS);
00061
00062
00063 #ifdef USB_EP1_IN_SIZE
00064 set_bit(bd1in.stat, DTS);
00065 clear_bit(bd1in.stat, KEN);
00066 clear_bit(bd1in.stat, INCDIS);
00067 clear_bit(bd1in.stat, DTSEN);
00068 clear_bit(bd1in.stat, BSTALL);
00069 clear_bit(bd1in.stat, BC9);
00070 clear_bit(bd1in.stat, BC8);
00071
00072 clear_bit(bd1in.stat, UOWN);
00073 #endif
00074
00075 #ifdef USB_EP1_OUT_SIZE
00076 bd1out.count = USB_EP1_OUT_SIZE;
00077 bd1out.addr = USB_EP1_OUT_ADDR;
00078
00079 clear_bit(bd1out.stat, DTS);
00080 clear_bit(bd1out.stat, KEN);
00081 clear_bit(bd1out.stat, INCDIS);
00082 clear_bit(bd1out.stat, DTSEN);
00083 clear_bit(bd1out.stat, BSTALL);
00084 clear_bit(bd1out.stat, BC9);
00085 clear_bit(bd1out.stat, BC8);
00086 set_bit (bd1out.stat, UOWN);
00087
00088 #endif
00089
00090
00091 #endif
00092
00093 #ifdef USB_EP2
00094 set_bit (uep2, EPHSHK);
00095 #ifdef USB_EP2_OUT_SIZE
00096 set_bit(uep2, EPOUTEN);
00097 #else
00098 clear_bit(uep2, EPOUTEN);
00099 #endif
00100 #ifdef USB_EP2_IN_SIZE
00101 set_bit(uep2, EPINEN);
00102 #else
00103 clear_bit(uep2, EPINEN);
00104 #endif
00105 set_bit (uep2, EPCONDIS);
00106
00107
00108 #ifdef USB_EP2_IN_SIZE
00109
00110 set_bit(bd2in.stat, DTS);
00111 clear_bit(bd2in.stat, KEN);
00112 clear_bit(bd2in.stat, INCDIS);
00113 clear_bit(bd2in.stat, DTSEN);
00114 clear_bit(bd2in.stat, BSTALL);
00115 clear_bit(bd2in.stat, BC9);
00116 clear_bit(bd2in.stat, BC8);
00117
00118 clear_bit(bd2in.stat, UOWN);
00119 #endif
00120
00121 #ifdef USB_EP2_OUT_SIZE
00122 bd2out.count = USB_EP2_OUT_SIZE;
00123 bd2out.addr = USB_EP2_OUT_ADDR;
00124
00125 clear_bit(bd2out.stat, DTS);
00126 clear_bit(bd2out.stat, KEN);
00127 clear_bit(bd2out.stat, INCDIS);
00128 clear_bit(bd2out.stat, DTSEN);
00129 clear_bit(bd2out.stat, BSTALL);
00130 clear_bit(bd2out.stat, BC9);
00131 clear_bit(bd2out.stat, BC8);
00132 set_bit (bd2out.stat, UOWN);
00133 #endif
00134 #endif
00135
00136 #ifdef USB_EP3
00137 set_bit (uep3, EPHSHK);
00138 #ifdef USB_EP3_OUT_SIZE
00139 set_bit(uep3, EPOUTEN);
00140 #else
00141 clear_bit(uep3, EPOUTEN);
00142 #endif
00143 #ifdef USB_EP3_IN_SIZE
00144 set_bit(uep3, EPINEN);
00145 #else
00146 clear_bit(uep3, EPINEN);
00147 #endif
00148 set_bit (uep3, EPCONDIS);
00149
00150 #ifdef USB_EP3_IN_SIZE
00151 set_bit(bd3in.stat, DTS);
00152 clear_bit(bd3in.stat, KEN);
00153 clear_bit(bd3in.stat, INCDIS);
00154 clear_bit(bd3in.stat, DTSEN);
00155 clear_bit(bd3in.stat, BSTALL);
00156 clear_bit(bd3in.stat, BC9);
00157 clear_bit(bd3in.stat, BC8);
00158
00159 clear_bit(bd3in.stat, UOWN);
00160 #endif
00161
00162 #ifdef USB_EP3_OUT_SIZE
00163 bd3out.count = USB_EP3_OUT_SIZE;
00164 bd3out.addr = USB_EP3_OUT_ADDR;
00165
00166 clear_bit(bd3out.stat, DTS);
00167 clear_bit(bd3out.stat, KEN);
00168 clear_bit(bd3out.stat, INCDIS);
00169 clear_bit(bd3out.stat, DTSEN);
00170 clear_bit(bd3out.stat, BSTALL);
00171 clear_bit(bd3out.stat, BC9);
00172 clear_bit(bd3out.stat, BC8);
00173 set_bit (bd3out.stat, UOWN);
00174 #endif
00175
00176 #endif
00177
00178 }
00179
00180 void usb_stall_ep0() {
00181 set_bit(bd0in.stat, BSTALL);
00182 set_bit(bd0in.stat, UOWN);
00183 set_bit(bd0out.stat, BSTALL);
00184 set_bit(bd0out.stat, UOWN);
00185
00186 }
00187
00188 void usb_send_data(uns8 ep, uns8 *data, uns8 send_count, bit first) {
00189 uns8 count;
00190 buffer_descriptor *bd;
00191 uns8 *buffer;
00192
00193
00194
00195
00196 buffer = ep_in_buffer_location[ep];
00197
00198 bd = ep_in_bd_location[ep];
00199
00200 if (test_bit(bd->stat, UOWN)) {
00201 #ifdef USB_DEBUG
00202 serial_print_str(" don't own it! ");
00203 #endif
00204 return;
00205 }
00206
00207 count = 0;
00208 while ((count < send_count)) {
00209 buffer[count] = data[count];
00210 count++;
00211 }
00212
00213
00214 bd->count = count;
00215 bd->addr = (uns16)buffer;
00216 if (first) {
00217 clear_bit(bd->stat, DTS);
00218 }
00219
00220 toggle_bit(bd->stat, DTS);
00221 clear_bit(bd->stat, KEN);
00222 clear_bit(bd->stat, INCDIS);
00223 set_bit (bd->stat, DTSEN);
00224 clear_bit(bd->stat, BSTALL);
00225 clear_bit(bd->stat, BC9);
00226 clear_bit(bd->stat, BC8);
00227
00228 set_bit (bd->stat, UOWN);
00229 }
00230
00231
00232
00233 void usb_send_data_chunk() {
00234
00235 uns8 count;
00236
00237 if (test_bit(bd0in.stat, UOWN)) {
00238 #ifdef USB_DEBUG
00239 serial_print_str(" !don't own it! ");
00240 #endif
00241 return;
00242 }
00243
00244 count = 0;
00245 while ((count < delivery_buffer_size) &&
00246 (delivery_bytes_sent < delivery_bytes_to_send) &&
00247 (delivery_bytes_sent < delivery_bytes_max_send)) {
00248 delivery_buffer[count] = *delivery_ptr;
00249 delivery_ptr++;
00250 delivery_bytes_sent++;
00251 count++;
00252 }
00253
00254 if ((count < delivery_buffer_size) &&
00255 ((delivery_bytes_sent == delivery_bytes_max_send) ||
00256 (delivery_bytes_sent == delivery_bytes_to_send))) {
00257 control_mode = cm_CTRL_READ_AWAITING_STATUS;
00258 }
00259
00260
00261 bd0in.count = count;
00262 bd0in.addr = (uns16)&buffer_0_in;
00263 toggle_bit(bd0in.stat, DTS);
00264 clear_bit(bd0in.stat, KEN);
00265 clear_bit(bd0in.stat, INCDIS);
00266 set_bit (bd0in.stat, DTSEN);
00267 clear_bit(bd0in.stat, BSTALL);
00268 clear_bit(bd0in.stat, BC9);
00269 clear_bit(bd0in.stat, BC8);
00270
00271 set_bit (bd0in.stat, UOWN);
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285 }
00286
00287
00288 void usb_send_empty_data_pkt() {
00289 delivery_buffer_size = USB_EP0_IN_SIZE;
00290 delivery_bd = &bd0in;
00291 delivery_buffer = &buffer_0_in;
00292 delivery_bytes_sent = 0;
00293 delivery_bytes_to_send = 0;
00294 delivery_bytes_max_send = 0;
00295 delivery_ptr = (uns8 *) 0;
00296 clear_bit(bd0in.stat, DTS);
00297 usb_send_data_chunk();
00298 }
00299
00300 uns8 buffer_byte;
00301
00302 void usb_send_one_byte(uns8 data) {
00303 delivery_buffer_size = USB_EP0_IN_SIZE;
00304 delivery_bytes_sent = 0;
00305 delivery_bytes_to_send = 1;
00306 delivery_bytes_max_send = 0;
00307 buffer_byte = data;
00308 delivery_ptr = (uns8 *) &buffer_byte;
00309 delivery_bd = &bd0in;
00310 delivery_buffer = &buffer_0_in;
00311
00312 clear_bit(bd0in.stat, DTS);
00313 usb_send_data_chunk();
00314 }
00315
00316 void usb_prime_ep0_out() {
00317 #ifdef USB_DEBUG
00318 serial_putc('P');
00319 #endif
00320 bd0out.count = USB_EP0_OUT_SIZE;
00321 bd0out.addr = USB_EP0_OUT_ADDR;
00322
00323 set_bit(bd0out.stat, DTS);
00324 clear_bit(bd0out.stat, KEN);
00325 clear_bit(bd0out.stat, INCDIS);
00326
00327 set_bit (bd0out.stat, DTSEN);
00328 clear_bit(bd0out.stat, BSTALL);
00329 clear_bit(bd0out.stat, BC9);
00330 clear_bit(bd0out.stat, BC8);
00331
00332 set_bit (bd0out.stat, UOWN);
00333 }
00334
00335 void usb_handle_standard_request(setup_data_packet sdp) {
00336
00337 switch (sdp.bRequest) {
00338 case req_Get_Descriptor:
00339 #ifdef USB_DEBUG
00340 serial_print_str(" GD: ");
00341 #endif
00342 uns8 descriptor_type = sdp.wValue >> 8;
00343 uns8 descriptor_num = sdp.wValue & 0xff;
00344 #ifdef USB_DEBUG
00345 serial_print_int(descriptor_type);
00346 #endif
00347 usb_get_descriptor_callback(descriptor_type, descriptor_num, &delivery_ptr, &delivery_bytes_to_send);
00348 if (delivery_ptr != 0) {
00349 control_mode = cm_CTRL_READ_DATA_STAGE;
00350 delivery_bytes_max_send = sdp.wLength;
00351 delivery_bytes_sent = 0;
00352 delivery_buffer_size = USB_EP0_IN_SIZE;
00353 delivery_bd = &bd0in;
00354 delivery_buffer = (uns8 *)USB_EP0_IN_ADDR;
00355 clear_bit(bd0in.stat, DTS);
00356 usb_send_data_chunk();
00357 } else {
00358 #ifdef USB_DEBUG
00359 serial_print_str(" <stall> ");
00360 #endif
00361 usb_stall_ep0();
00362 }
00363
00364 break;
00365 case req_Set_Address:
00366 usb_address = sdp.wValue & 0xff;
00367 #ifdef USB_DEBUG
00368 serial_print_str(" SA:");
00369 serial_print_int_hex(usb_address);
00370 #endif
00371 usb_status = us_SET_ADDRESS;
00372
00373
00374 usb_send_status_ack();
00375 control_mode = cm_CTRL_WRITE_SENDING_STATUS;
00376 break;
00377 case req_Set_Configuration:
00378 #ifdef USB_DEBUG
00379 serial_print_str(" SC: ");
00380 #endif
00381
00382
00383 control_mode = cm_CTRL_WRITE_SENDING_STATUS;
00384 usb_send_status_ack();
00385
00386 usb_state = st_CONFIGURED;
00387
00388
00389 usb_configure_endpoints();
00390
00391 #ifdef USB_CALLBACK_ON_DEVICE_CONFIGURED
00392 usb_device_configured_callback();
00393 #endif
00394 break;
00395 case req_Get_Interface:
00396 #ifdef USB_DEBUG
00397 serial_print_str(" GI ");
00398 #endif
00399 control_mode = cm_CTRL_READ_DATA_STAGE;
00400 usb_send_one_byte(1);
00401 case req_Get_Status:
00402
00403 #ifdef USB_DEBUG
00404 serial_print_str(" GS ");
00405 #endif
00406
00407 #ifdef USB_SELF_POWERED
00408 usb_send_one_byte(1);
00409 #else
00410 usb_send_one_byte(0);
00411 #endif
00412 break;
00413 default:
00414 #ifdef USB_DEBUG
00415 serial_print_str(" ??SR ");
00416 serial_print_int(sdp.bRequest);
00417 #endif
00418 break;
00419
00420 }
00421 }
00422
00423
00424 void usb_handle_transaction() {
00425
00426 uns8 end_point, pid;
00427
00428 end_point = ustat >> 3;
00429
00430 if (test_bit(ustat, DIR)) {
00431 #ifdef USB_DEBUG
00432 serial_print_str("\nDI ");
00433 #endif
00434 pid = (bd0in.stat >> 2) & 0x0f;
00435
00436 } else {
00437 pid = (bd0out.stat >> 2) & 0x0f;
00438 #ifdef USB_DEBUG
00439 serial_print_str("\nDO ");
00440 #endif
00441 #ifdef USB_DEBUG_HIGH
00442 serial_print_int(bd0out.count);
00443 serial_print_spc();
00444 #endif
00445
00446 }
00447 #ifdef USB_DEBUG
00448 if (end_point != 0) {
00449 serial_putc('E');
00450 serial_print_int_hex(end_point);
00451 serial_print_spc();
00452 }
00453 #endif
00454
00455 if (end_point == 0) {
00456 #ifdef USB_DEBUG_HIGH
00457 serial_print_str(" bytes: ");
00458 serial_print_int(bd0out.count);
00459 #endif
00460 if (pid == pid_SETUP) {
00461 memcpy( (void*)&usb_sdp, (void *)&buffer_0_out, 8);
00462 usb_prime_ep0_out();
00463 #ifdef USB_DEBUG
00464 serial_print_str(" Setup ");
00465 serial_print_int_hex(usb_sdp.bmRequestType);
00466 serial_putc(' ');
00467 if (test_bit(usb_sdp.bmRequestType, DATA_STAGE_DIR)) {
00468 serial_print_str(" Data stage=IN ");
00469 } else {
00470 serial_print_str(" Data stage=OUT/NO ");
00471 }
00472 #endif
00473
00474
00475
00476
00477
00478 if (!test_bit(usb_sdp.bmRequestType, REQUEST_TYPE1) &&
00479 !test_bit(usb_sdp.bmRequestType, REQUEST_TYPE0)) {
00480
00481 if ((usb_sdp.bmRequestType & 0b00011111) == 0) {
00482
00483 } else if ((usb_sdp.bmRequestType & 0b00011111) == 1) {
00484
00485 } else if ((usb_sdp.bmRequestType & 0b00011111) == 0b00011) {
00486
00487 }
00488 #ifdef USB_DEBUG
00489 serial_print_str(" std ");
00490 #endif
00491 usb_handle_standard_request(usb_sdp);
00492 } else if (!test_bit(usb_sdp.bmRequestType, REQUEST_TYPE1) &&
00493 test_bit(usb_sdp.bmRequestType, REQUEST_TYPE0)) {
00494 #ifdef USB_DEBUG
00495 serial_print_str(" class ");
00496 #endif
00497 #ifdef USB_CALLBACK_ON_CLASS_CTRL
00498 usb_handle_class_request_callback(usb_sdp);
00499 #endif
00500 } else {
00501
00502
00503
00504 #ifdef USB_DEBUG
00505 serial_print_str(" ??req t=");
00506 serial_print_int(usb_sdp.bmRequestType);
00507 serial_putc(' ');
00508 #endif
00509 }
00510 clear_bit(ucon, PKTDIS);
00511
00512 } else if (pid == pid_IN) {
00513 #ifdef USB_DEBUG
00514 serial_print_str(" IN ");
00515 #endif
00516 if (control_mode == cm_CTRL_READ_DATA_STAGE) {
00517
00518 usb_send_data_chunk();
00519 } else if (control_mode == cm_CTRL_WRITE_SENDING_STATUS) {
00520
00521 control_mode = cm_IDLE;
00522 if (usb_status == us_SET_ADDRESS) {
00523
00524 usb_state = st_ADDRESS;
00525 uaddr = usb_address;
00526
00527
00528 usb_status = us_IDLE;
00529 }
00530 #ifdef USB_DEBUG
00531 serial_print_str(" ----\n");
00532 #endif
00533 } else if (control_mode == cm_CTRL_READ_AWAITING_STATUS) {
00534
00535 #ifdef USB_DEBUG
00536
00537 #endif
00538 nop();
00539 } else if (control_mode == cm_CTRL_READ_DATA_STAGE_CLASS) {
00540 #ifdef USB_DEBUG
00541 serial_print_str(" ctrl read data stage class - more to come? ");
00542 #endif
00543
00544 #ifdef USB_CALLBACK_ON_CLASS_CTRL
00545 usb_handle_class_ctrl_read_callback();
00546 #else
00547 nop();
00548 #endif
00549 } else {
00550 #ifdef USB_DEBUG
00551 serial_print_str(" ?? cm=");
00552 serial_print_int((uns8)control_mode);
00553 serial_print_spc();
00554 #else
00555 nop();
00556 #endif
00557
00558 }
00559 } else if (pid == pid_ACK) {
00560 #ifdef USB_DEBUG
00561 serial_print_str("****A\n");
00562 #endif
00563 if (control_mode == cm_CTRL_READ_DATA_STAGE) {
00564 #ifdef USB_DEBUG
00565 serial_print_str(" &2 ");
00566 #endif
00567 usb_send_data_chunk();
00568 } else if (control_mode == cm_CTRL_READ_DATA_STAGE_CLASS) {
00569 #ifdef USB_CALLBACK_ON_CLASS_CTRL
00570 usb_handle_class_ctrl_read_callback();
00571 #else
00572 nop();
00573 #endif
00574 } else if (control_mode == cm_CTRL_WRITE_SENDING_STATUS) {
00575 #ifdef USB_DEBUG
00576 serial_print_str(" st sent ");
00577 #endif
00578 control_mode = cm_IDLE;
00579 }
00580
00581 } else if (pid == pid_OUT) {
00582
00583
00584 if (control_mode == cm_CTRL_READ_AWAITING_STATUS) {
00585 #ifdef USB_DEBUG
00586 serial_print_str(" ----\n");
00587 #endif
00588 control_mode = cm_IDLE;
00589 } else if (control_mode == cm_CTRL_WRITE_DATA_STAGE_CLASS) {
00590 #ifdef USB_CALLBACK_ON_CLASS_CTRL
00591 usb_handle_class_ctrl_write_callback((uns8 *)&buffer_0_out, bd0out.count);
00592
00593
00594 #else
00595 nop();
00596 #endif
00597 } else if (control_mode == cm_CTRL_READ_DATA_STAGE) {
00598
00599 control_mode = cm_IDLE;
00600 } else {
00601
00602 #ifdef USB_DEBUG
00603 serial_print_str("??unk pid_OUT ");
00604 serial_print_int((uns8)control_mode);
00605 #endif
00606 }
00607 usb_prime_ep0_out();
00608
00609 } else {
00610 #ifdef USB_DEBUG
00611 serial_print_str(" UKPID = ");
00612 serial_print_int(pid);
00613 #endif
00614 }
00615
00616 } else {
00617 buffer_descriptor *bd;
00618 if (test_bit(ustat, DIR)) {
00619
00620 #ifdef USB_EP_DATA_CALLBACK
00621 bd = ep_in_bd_location[end_point];
00622 usb_ep_data_in_callback(end_point, bd->count);
00623 #else
00624 nop();
00625 #endif
00626 } else {
00627
00628 bd = ep_out_bd_location[end_point];
00629
00630 #ifdef USB_EP_DATA_CALLBACK
00631 usb_ep_data_out_callback(end_point, ep_out_buffer_location[end_point],
00632 bd->count);
00633 #endif
00634
00635 bd->count = ep_out_buffer_size[end_point];
00636
00637
00638
00639 clear_bit(bd->stat, DTS);
00640 clear_bit(bd->stat, KEN);
00641 clear_bit(bd->stat, INCDIS);
00642 clear_bit(bd->stat, DTSEN);
00643 clear_bit(bd->stat, BSTALL);
00644 clear_bit(bd->stat, BC9);
00645 clear_bit(bd->stat, BC8);
00646 set_bit (bd->stat, UOWN);
00647
00648
00649 }
00650 }
00651 }
00652
00653 void usb_handle_reset() {
00654 usb_address = 0;
00655
00656
00657 control_mode = cm_IDLE;
00658 usb_status = us_IDLE;
00659
00660
00661 clear_bit(uir, TRNIF);
00662 clear_bit(uir, TRNIF);
00663 clear_bit(uir, TRNIF);
00664 clear_bit(uir, TRNIF);
00665
00666
00667
00668
00669
00670 bd0out.count = 8;
00671 bd0out.addr = 0x0500;
00672
00673 clear_bit(bd0out.stat, DTS);
00674 clear_bit(bd0out.stat, KEN);
00675 clear_bit(bd0out.stat, INCDIS);
00676 clear_bit (bd0out.stat, DTSEN);
00677 clear_bit(bd0out.stat, BSTALL);
00678 clear_bit(bd0out.stat, BC9);
00679 clear_bit(bd0out.stat, BC8);
00680
00681 set_bit (bd0out.stat, UOWN);
00682
00683
00684
00685 bd0in.count = 8;
00686 bd0in.addr = 0x0508;
00687 clear_bit(bd0in.stat, DTS);
00688 clear_bit(bd0in.stat, KEN);
00689 clear_bit(bd0in.stat, INCDIS);
00690 clear_bit(bd0in.stat, BSTALL);
00691 clear_bit(bd0in.stat, BC9);
00692 clear_bit(bd0in.stat, BC8);
00693
00694 clear_bit(bd0in.stat, UOWN);
00695
00696 #ifdef USB_DEBUG
00697 serial_print_str("\nR ");
00698 serial_print_int_hex(uir);
00699 #endif
00700 }
00701
00702 void usb_handle_stall() {
00703 #ifdef USB_DEBUG
00704 serial_print_str(" U:Stall ");
00705 #endif
00706 }
00707
00708
00709
00710 void usb_handle_isr() {
00711
00712 if (test_bit(pir2, USBIF)) {
00713 clear_bit(pir2, USBIF);
00714
00715 if (test_bit(uir, TRNIF)) {
00716 usb_handle_transaction();
00717 clear_bit(uir, TRNIF);
00718 }
00719
00720 if (test_bit(uir, URSTIF)) {
00721 usb_handle_reset();
00722 clear_bit(uir, URSTIF);
00723 }
00724
00725 if (test_bit(uir, STALLIF)) {
00726 usb_handle_stall();
00727 clear_bit(uir, STALLIF);
00728 }
00729 #ifdef USB_CALLBACK_ON_SOF
00730 if (test_bit(uir, SOFIF)) {
00731 usb_SOF_callback(ufrm);
00732 clear_bit(uir, SOFIF);
00733 }
00734 #endif
00735 }
00736 }
00737
00738 void turn_usb_ints_on() {
00739
00740 set_bit(uie, STALLIE);
00741 set_bit(uie, TRNIE);
00742 set_bit(uie, URSTIE);
00743 set_bit(pie2, USBIE);
00744 #ifdef USB_CALLBACK_ON_SOF
00745 set_bit(uie, SOFIE);
00746 #endif
00747 }
00748
00749 void usb_setup() {
00750
00751 usb_state = st_POWERED;
00752
00753
00754 clear_bit(ucfg, UTRDIS);
00755 set_bit (ucfg, FSEN);
00756 set_bit (ucfg, UPUEN);
00757
00758 clear_bit(ucfg, PPB1);
00759 clear_bit(ucfg, PPB0);
00760
00761
00762
00763
00764
00765
00766
00767 set_bit(uep0, EPHSHK);
00768 set_bit(uep0, EPOUTEN);
00769 set_bit(uep0, EPINEN);
00770 clear_bit(uep0, EPCONDIS);
00771
00772
00773
00774
00775 ep_out_bd_location[0] = &bd0out;
00776 #if USB_HIGHEST_EP >= 1
00777 ep_out_bd_location[1] = &bd1out;
00778 #endif
00779 #if USB_HIGHEST_EP >= 2
00780 ep_out_bd_location[2] = &bd2out;
00781 #endif
00782 #if USB_HIGHEST_EP >= 3
00783 ep_out_bd_location[3] = &bd3out;
00784 #endif
00785 #if USB_HIGHEST_EP >= 4
00786 ep_out_bd_location[4] = &bd4out;
00787 #endif
00788
00789 ep_in_bd_location[0] = &bd0in;
00790 #if USB_HIGHEST_EP >= 1
00791 ep_in_bd_location[1] = &bd1in;
00792 #endif
00793 #if USB_HIGHEST_EP >= 2
00794 ep_in_bd_location[2] = &bd2in;
00795 #endif
00796 #if USB_HIGHEST_EP >= 3
00797 ep_in_bd_location[3] = &bd3in;
00798 #endif
00799 #if USB_HIGHEST_EP >= 4
00800 ep_in_bd_location[4] = &bd4in;
00801 #endif
00802
00803
00804 }
00805
00806
00807 void usb_enable_module() {
00808 uir = 0;
00809 set_bit(ucon, USBEN);
00810 usb_state = st_DEFAULT;
00811 }
00812
00813 usb_state_type usb_get_state() {
00814 return usb_state;
00815 }