00001
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
00044
00045
00046
00047
00048
00049
00050
00051 #include "conf_usb.h"
00052
00053
00054 #if USB_HOST_FEATURE == ENABLED
00055
00056 #ifdef FREERTOS_USED
00057 #include "FreeRTOS.h"
00058 #include "task.h"
00059 #endif
00060 #include "usb_drv.h"
00061 #include "usb_task.h"
00062 #include "usb_host_enum.h"
00063 #include "usb_host_task.h"
00064
00065
00066
00067
00068
00069
00070
00071 static const char log_device_connected[] = "Device connected\n";
00072 static const char log_unsupported_device[] = "Unsupported device\n";
00073 static const char log_device_enumerated[] = "Device enumerated\n";
00074 static const char log_usb_suspended[] = "USB suspended\n";
00075 static const char log_usb_resumed[] = "USB resumed\n";
00076
00077 #if USB_HOST_PIPE_INTERRUPT_TRANSFER == ENABLE
00078 volatile Bool g_sav_int_sof_enable;
00079 volatile S_pipe_int it_pipe_str[MAX_PEP_NB];
00080 #endif
00081
00098 volatile U8 device_state;
00099
00101 volatile S_usb_setup_data usb_request;
00102
00111 U8 data_stage[SIZEOF_DATA_STAGE];
00112
00113 volatile U8 device_status;
00114
00115 volatile Bool request_resume;
00116
00118 static U16 sof_cnt;
00119
00120 #ifdef FREERTOS_USED
00122 xTaskHandle usb_host_tsk = NULL;
00123 #endif
00124
00125
00126
00127
00133 void usb_host_task_init(void)
00134 {
00136
00137
00138 Disable_global_interrupt();
00139 Usb_disable();
00140 (void)Is_usb_enabled();
00141 Enable_global_interrupt();
00142 Usb_disable_otg_pad();
00143 Usb_enable_otg_pad();
00144 Usb_enable();
00145 Usb_unfreeze_clock();
00146 (void)Is_usb_clock_frozen();
00147 #if USB_VBOF_ACTIVE_LEVEL == HIGH
00148 Usb_set_vbof_active_high();
00149 #else // USB_VBOF_ACTIVE_LEVEL == LOW
00150 Usb_set_vbof_active_low();
00151 #endif
00152 Usb_output_vbof_pin();
00153 Usb_disable_vbus_hw_control();
00154 Host_enable_device_disconnection_interrupt();
00155 #if USB_HOST_PIPE_INTERRUPT_TRANSFER == ENABLE
00156 reset_it_pipe_str();
00157 #endif
00158 device_state = DEVICE_UNATTACHED;
00159 Host_clear_device_status();
00160 Host_ack_request_resume();
00161 sof_cnt = 0;
00162
00163 #ifdef FREERTOS_USED
00164 xTaskCreate(usb_host_task,
00165 configTSK_USB_HST_NAME,
00166 configTSK_USB_HST_STACK_SIZE,
00167 NULL,
00168 configTSK_USB_HST_PRIORITY,
00169 &usb_host_tsk);
00170 #endif // FREERTOS_USED
00171 }
00172
00173
00188 #ifdef FREERTOS_USED
00189 void usb_host_task(void *pvParameters)
00190 #else
00191 void usb_host_task(void)
00192 #endif
00193 {
00194 static Bool sav_int_sof_enable;
00195 U8 pipe;
00196
00197 #ifdef FREERTOS_USED
00198 portTickType xLastWakeTime;
00199
00200 xLastWakeTime = xTaskGetTickCount();
00201 while (TRUE)
00202 {
00203 vTaskDelayUntil(&xLastWakeTime, configTSK_USB_HST_PERIOD);
00204
00205 #endif // FREERTOS_USED
00206 switch (device_state)
00207 {
00208
00209
00210
00211
00212
00213
00214 case DEVICE_UNATTACHED:
00215 nb_interface_supported = 0;
00216 Host_clear_device_status();
00217 Usb_clear_all_event();
00218 Host_disable_sof();
00219 host_disable_all_pipes();
00220 Usb_enable_vbus();
00221
00222 if (Is_usb_vbus_high()) device_state = DEVICE_ATTACHED;
00223 break;
00224
00225
00226
00227
00228
00229
00230
00231 case DEVICE_ATTACHED:
00232 if (Is_host_device_connection())
00233 {
00234 Usb_ack_bconnection_error_interrupt();
00235 Usb_ack_vbus_error_interrupt();
00236 Host_ack_device_connection();
00237
00238
00239 Host_enable_device_disconnection_interrupt();
00240 Enable_global_interrupt();
00241 Host_clear_device_status();
00242 Host_enable_sof();
00243 Host_enable_sof_interrupt();
00244 sof_cnt = 0;
00245 while (sof_cnt < 100)
00246 {
00247 if (Is_usb_event(EVT_HOST_SOF)) Usb_ack_event(EVT_HOST_SOF), sof_cnt++;
00248 if (Is_host_emergency_exit() || Is_usb_bconnection_error_interrupt()) goto device_attached_error;
00249 }
00250 Disable_global_interrupt();
00251 Host_disable_device_disconnection_interrupt();
00252 Host_send_reset();
00253 (void)Is_host_sending_reset();
00254 Enable_global_interrupt();
00255 Usb_ack_event(EVT_HOST_SOF);
00256
00257 while (Is_host_sending_reset())
00258 {
00259
00260 if (Is_host_device_disconnection())
00261 {
00262
00263 Host_stop_sending_reset();
00264 }
00265 }
00266 Host_ack_reset_sent();
00267 if (!Is_host_device_disconnection())
00268 {
00269
00270
00271 for (sof_cnt = 0; sof_cnt < 0xFFFF; sof_cnt++)
00272 {
00273
00274 if (Is_usb_event(EVT_HOST_SOF) && Is_host_device_disconnection())
00275 {
00276 Host_ack_device_connection();
00277 Host_ack_device_disconnection();
00278 break;
00279 }
00280 }
00281 }
00282 Host_enable_device_disconnection_interrupt();
00283 sof_cnt = 0;
00284 while (sof_cnt < 100)
00285 {
00286 if (Is_usb_event(EVT_HOST_SOF)) Usb_ack_event(EVT_HOST_SOF), sof_cnt++;
00287 if (Is_host_emergency_exit() || Is_usb_bconnection_error_interrupt()) goto device_attached_error;
00288 }
00289 device_state = DEVICE_POWERED;
00290 LOG_STR(log_device_connected);
00291 Host_device_connection_action();
00292 sof_cnt = 0;
00293 }
00294 device_attached_error:
00295
00296 if (Is_usb_bconnection_error_interrupt() || Is_usb_vbus_error_interrupt() || Is_usb_vbus_low())
00297 {
00298 device_state = DEVICE_UNATTACHED;
00299 Usb_ack_bconnection_error_interrupt();
00300 Usb_ack_vbus_error_interrupt();
00301 Host_disable_sof();
00302 }
00303 break;
00304
00305
00306
00307
00308
00309
00310
00311 case DEVICE_POWERED:
00312 if (Is_usb_event(EVT_HOST_SOF))
00313 {
00314 Usb_ack_event(EVT_HOST_SOF);
00315 if (sof_cnt++ >= 100)
00316 {
00317 Host_enable_pipe(P_CONTROL);
00318 (void)Host_configure_pipe(P_CONTROL,
00319 0,
00320 EP_CONTROL,
00321 TYPE_CONTROL,
00322 TOKEN_SETUP,
00323 8,
00324 SINGLE_BANK);
00325 device_state = DEVICE_DEFAULT;
00326 }
00327 }
00328 break;
00329
00330
00331
00332
00333
00334
00335
00336
00337 case DEVICE_DEFAULT:
00338
00339 if (host_get_device_descriptor_incomplete() == CONTROL_GOOD)
00340 {
00341 sof_cnt = 0;
00342 while (sof_cnt < 20)
00343 {
00344 if (Is_usb_event(EVT_HOST_SOF)) Usb_ack_event(EVT_HOST_SOF), sof_cnt++;
00345 if (Is_host_emergency_exit() || Is_usb_bconnection_error_interrupt()) break;
00346 }
00347 Disable_global_interrupt();
00348 Host_disable_device_disconnection_interrupt();
00349 Host_send_reset();
00350 (void)Is_host_sending_reset();
00351 Enable_global_interrupt();
00352 Usb_ack_event(EVT_HOST_SOF);
00353
00354 while (Is_host_sending_reset())
00355 {
00356
00357 if (Is_host_device_disconnection())
00358 {
00359
00360 Host_stop_sending_reset();
00361 }
00362 }
00363 Host_ack_reset_sent();
00364 if (!Is_host_device_disconnection())
00365 {
00366
00367
00368 for (sof_cnt = 0; sof_cnt < 0xFFFF; sof_cnt++)
00369 {
00370
00371 if (Is_usb_event(EVT_HOST_SOF) && Is_host_device_disconnection())
00372 {
00373 Host_ack_device_connection();
00374 Host_ack_device_disconnection();
00375 break;
00376 }
00377 }
00378 }
00379 Host_enable_device_disconnection_interrupt();
00380 sof_cnt = 0;
00381 while (sof_cnt < 200)
00382 {
00383 if (Is_usb_event(EVT_HOST_SOF)) Usb_ack_event(EVT_HOST_SOF), sof_cnt++;
00384 if (Is_host_emergency_exit() || Is_usb_bconnection_error_interrupt()) break;
00385 }
00386 Host_disable_pipe(P_CONTROL);
00387 Host_unallocate_memory(P_CONTROL);
00388 Host_enable_pipe(P_CONTROL);
00389
00390 (void)Host_configure_pipe(P_CONTROL,
00391 0,
00392 EP_CONTROL,
00393 TYPE_CONTROL,
00394 TOKEN_SETUP,
00395 data_stage[OFFSET_FIELD_MAXPACKETSIZE],
00396 SINGLE_BANK);
00397
00398 if (host_set_address(DEVICE_ADDRESS) == CONTROL_GOOD)
00399 {
00400 for (pipe = 0; pipe < MAX_PEP_NB; pipe++)
00401 Host_configure_address(pipe, DEVICE_ADDRESS);
00402 device_state = DEVICE_ADDRESSED;
00403 }
00404 else device_state = DEVICE_ERROR;
00405 }
00406 else device_state = DEVICE_ERROR;
00407 break;
00408
00409
00410
00411
00412
00413
00414 case DEVICE_ADDRESSED:
00415 if (host_get_device_descriptor() == CONTROL_GOOD)
00416 {
00417
00418 if (host_check_VID_PID())
00419 {
00420 Host_set_device_supported();
00421 Host_device_supported_action();
00422 device_state = DEVICE_CONFIGURED;
00423 }
00424 else
00425 {
00426 #if HOST_STRICT_VID_PID_TABLE == ENABLE
00427 device_state = DEVICE_ERROR;
00428 LOG_STR(log_unsupported_device);
00429 #else
00430 device_state = DEVICE_CONFIGURED;
00431 #endif
00432 Host_device_not_supported_action();
00433 }
00434 }
00435 else device_state = DEVICE_ERROR;
00436 break;
00437
00438
00439
00440
00441
00442
00443
00444
00445 case DEVICE_CONFIGURED:
00446 if (host_get_configuration_descriptor(0) == CONTROL_GOOD)
00447 {
00448 if (host_check_class())
00449 {
00450 #if HOST_AUTO_CFG_ENDPOINT == DISABLE
00451 User_configure_endpoint();
00452 Host_set_configured();
00453 #endif
00454 if (Is_host_configured())
00455 {
00456 if (host_set_configuration(1) == CONTROL_GOOD)
00457 {
00458
00459
00460 device_state = DEVICE_READY;
00461
00462 Host_enable_device_disconnection_interrupt();
00463
00464
00465 #if HOST_CONTINUOUS_SOF_INTERRUPT == DISABLE
00466 Disable_global_interrupt();
00467 Host_disable_sof_interrupt();
00468 (void)Is_host_sof_interrupt_enabled();
00469 Enable_global_interrupt();
00470 #endif
00471 Host_new_device_connection_action();
00472 Enable_global_interrupt();
00473 LOG_STR(log_device_enumerated);
00474 }
00475 else device_state = DEVICE_ERROR;
00476 }
00477 }
00478 else
00479 {
00480 device_state = DEVICE_ERROR;
00481 LOG_STR(log_unsupported_device);
00482 Host_device_class_not_supported_action();
00483 }
00484 }
00485 else device_state = DEVICE_ERROR;
00486 break;
00487
00488
00489
00490
00491
00492
00493
00494 case DEVICE_READY:
00495 break;
00496
00497
00498
00499
00500
00501
00502
00503 case DEVICE_ERROR:
00504 #if HOST_ERROR_RESTART == ENABLE
00505 device_state = DEVICE_UNATTACHED;
00506 #endif
00507 Host_device_error_action();
00508 break;
00509
00510
00511
00512
00513
00514
00515
00516 case DEVICE_SUSPENDED:
00517 if (Is_device_supports_remote_wakeup())
00518 {
00519 host_set_feature_remote_wakeup();
00520 }
00521 LOG_STR(log_usb_suspended);
00522 sav_int_sof_enable = Is_host_sof_interrupt_enabled();
00523 Disable_global_interrupt();
00524 Host_disable_sof_interrupt();
00525 (void)Is_host_sof_interrupt_enabled();
00526 Enable_global_interrupt();
00527 Host_ack_sof();
00528 Host_disable_sof();
00529 Host_ack_hwup();
00530 Host_enable_hwup_interrupt();
00531
00532 (void)Is_host_hwup_interrupt_enabled();
00533 Usb_freeze_clock();
00535
00536 Host_suspend_action();
00537 device_state = DEVICE_WAIT_RESUME;
00538 break;
00539
00540
00541
00542
00543
00544
00545
00546
00547 case DEVICE_WAIT_RESUME:
00548 if (Is_usb_event(EVT_HOST_HWUP) || Is_host_request_resume())
00549
00550 {
00551 if (Is_host_request_resume())
00552 {
00553
00555
00556
00557 Usb_unfreeze_clock();
00558 (void)Is_usb_clock_frozen();
00559 Disable_global_interrupt();
00560 Host_disable_hwup_interrupt();
00561 (void)Is_host_hwup_interrupt_enabled();
00562 Enable_global_interrupt();
00563 Host_ack_hwup();
00564 }
00565 Host_enable_sof();
00566 Host_send_resume();
00567 while (!Is_host_down_stream_resume());
00568 Host_ack_remote_wakeup();
00569 Host_ack_request_resume();
00570 Host_ack_down_stream_resume();
00571 Usb_ack_event(EVT_HOST_HWUP);
00572 if (sav_int_sof_enable) Host_enable_sof_interrupt();
00573 device_state = DEVICE_READY;
00574 LOG_STR(log_usb_resumed);
00575 }
00576 break;
00577
00578
00579
00580
00581
00582
00583
00584 default:
00585 device_state = DEVICE_UNATTACHED;
00586 break;
00587 }
00588 #ifdef FREERTOS_USED
00589 }
00590 #endif
00591 }
00592
00593
00594
00595
00608 Status_t host_send_data(U8 pipe, U16 nb_data, const void *ptr_buf)
00609 {
00610 Status_t status = PIPE_GOOD;
00611 Bool sav_int_sof_enable;
00612 Bool sav_glob_int_en;
00613 U8 nak_timeout;
00614 #if NAK_TIMEOUT_ENABLE == ENABLE
00615 U16 cpt_nak;
00616 #endif
00617
00618 sav_int_sof_enable = Is_host_sof_interrupt_enabled();
00619 Host_enable_sof_interrupt();
00620 Host_configure_pipe_token(pipe, TOKEN_OUT);
00621 Host_ack_out_ready(pipe);
00622 Host_unfreeze_pipe(pipe);
00623 while (nb_data)
00624 {
00625
00626 Host_reset_pipe_fifo_access(pipe);
00627 nb_data = host_write_p_txpacket(pipe, ptr_buf, nb_data, &ptr_buf);
00628 private_sof_counter = 0;
00629 #if NAK_TIMEOUT_ENABLE == ENABLE
00630 cpt_nak = 0;
00631 #endif
00632 nak_timeout = 0;
00633 Host_ack_out_ready_send(pipe);
00634 while (!Is_host_out_ready(pipe))
00635 {
00636 if (Is_host_emergency_exit())
00637 {
00638 status = PIPE_DELAY_TIMEOUT;
00639 Host_reset_pipe(pipe);
00640 goto host_send_data_end;
00641 }
00642 #if TIMEOUT_DELAY_ENABLE == ENABLE
00643 if (private_sof_counter >= 250)
00644 {
00645 private_sof_counter = 0;
00646 if (nak_timeout++ >= TIMEOUT_DELAY)
00647 {
00648 status = PIPE_DELAY_TIMEOUT;
00649 Host_reset_pipe(pipe);
00650 goto host_send_data_end;
00651 }
00652 }
00653 #endif
00654 if (Is_host_pipe_error(pipe))
00655 {
00656 status = Host_error_status(pipe);
00657 Host_ack_all_errors(pipe);
00658 goto host_send_data_end;
00659 }
00660 if (Is_host_stall(pipe))
00661 {
00662 status = PIPE_STALL;
00663 Host_ack_stall(pipe);
00664 goto host_send_data_end;
00665 }
00666 #if NAK_TIMEOUT_ENABLE == ENABLE
00667 if (Is_host_nak_received(pipe))
00668 {
00669 Host_ack_nak_received(pipe);
00670 if (cpt_nak++ > NAK_SEND_TIMEOUT)
00671 {
00672 status = PIPE_NAK_TIMEOUT;
00673 Host_reset_pipe(pipe);
00674 goto host_send_data_end;
00675 }
00676 }
00677 #endif
00678 }
00679
00680 Host_ack_out_ready(pipe);
00681 }
00682 while (Host_nb_busy_bank(pipe));
00683 host_send_data_end:
00684 Host_freeze_pipe(pipe);
00685
00686 if (!sav_int_sof_enable)
00687 {
00688 if ((sav_glob_int_en = Is_global_interrupt_enabled())) Disable_global_interrupt();
00689 Host_disable_sof_interrupt();
00690 (void)Is_host_sof_interrupt_enabled();
00691 if (sav_glob_int_en) Enable_global_interrupt();
00692 }
00693
00694
00695 return status;
00696 }
00697
00698
00713 Status_t host_get_data(U8 pipe, U16 *nb_data, void *ptr_buf)
00714 {
00715 Status_t status = PIPE_GOOD;
00716 Bool sav_int_sof_enable;
00717 Bool sav_glob_int_en;
00718 U8 nak_timeout;
00719 U16 n, i;
00720 #if NAK_TIMEOUT_ENABLE == ENABLE
00721 U16 cpt_nak;
00722 #endif
00723
00724 n = *nb_data;
00725 sav_int_sof_enable = Is_host_sof_interrupt_enabled();
00726 Host_enable_sof_interrupt();
00727 Host_enable_continuous_in_mode(pipe);
00728 Host_configure_pipe_token(pipe, TOKEN_IN);
00729 Host_ack_in_received(pipe);
00730 while (n)
00731 {
00732 Host_free_in(pipe);
00733 Host_unfreeze_pipe(pipe);
00734 private_sof_counter = 0;
00735 nak_timeout = 0;
00736 #if NAK_TIMEOUT_ENABLE == ENABLE
00737 cpt_nak = 0;
00738 #endif
00739 while (!Is_host_in_received(pipe))
00740 {
00741 if (Is_host_emergency_exit())
00742 {
00743 status = PIPE_DELAY_TIMEOUT;
00744 Host_reset_pipe(pipe);
00745 goto host_get_data_end;
00746 }
00747 #if TIMEOUT_DELAY_ENABLE == ENABLE
00748 if (private_sof_counter >= 250)
00749 {
00750 private_sof_counter = 0;
00751 if (nak_timeout++ >= TIMEOUT_DELAY)
00752 {
00753 status = PIPE_DELAY_TIMEOUT;
00754 Host_reset_pipe(pipe);
00755 goto host_get_data_end;
00756 }
00757 }
00758 #endif
00759 if (Is_host_pipe_error(pipe))
00760 {
00761 status = Host_error_status(pipe);
00762 Host_ack_all_errors(pipe);
00763 goto host_get_data_end;
00764 }
00765 if (Is_host_stall(pipe))
00766 {
00767 status = PIPE_STALL;
00768 Host_reset_pipe(pipe);
00769 Host_ack_stall(pipe);
00770 goto host_get_data_end;
00771 }
00772 #if NAK_TIMEOUT_ENABLE == ENABLE
00773 if (Is_host_nak_received(pipe))
00774 {
00775 Host_ack_nak_received(pipe);
00776 if (cpt_nak++ > NAK_RECEIVE_TIMEOUT)
00777 {
00778 status = PIPE_NAK_TIMEOUT;
00779 Host_reset_pipe(pipe);
00780 goto host_get_data_end;
00781 }
00782 }
00783 #endif
00784 }
00785 Host_freeze_pipe(pipe);
00786 Host_reset_pipe_fifo_access(pipe);
00787 i = Host_get_pipe_size(pipe) - Host_byte_count(pipe);
00788 if (!ptr_buf)
00789 {
00790 if (Host_byte_count(pipe) > n)
00791 {
00792 n = 0;
00794 }
00795 else
00796 {
00797 n -= Host_byte_count(pipe);
00798 if (i)
00799 {
00800 *nb_data -= n;
00801 n = 0;
00802 }
00803 }
00804 }
00805 else
00806 {
00807 n = host_read_p_rxpacket(pipe, ptr_buf, n, &ptr_buf);
00808 if (Host_byte_count(pipe))
00809 {
00811 }
00812 else if (i)
00813 {
00814 *nb_data -= n;
00815 n = 0;
00816 }
00817 }
00818 Host_ack_in_received(pipe);
00819
00820
00821
00822
00823
00824
00825 if (Is_usb_low_speed_mode())
00826 {
00827 Usb_ack_event(EVT_HOST_SOF);
00828 sav_int_sof_enable = Is_host_sof_interrupt_enabled();
00829 if ((sav_glob_int_en = Is_global_interrupt_enabled())) Disable_global_interrupt();
00830 Host_ack_sof();
00831 (void)Is_host_sof_interrupt_enabled();
00832 if (sav_glob_int_en) Enable_global_interrupt();
00833 Host_enable_sof_interrupt();
00834 while (!Is_usb_event(EVT_HOST_SOF))
00835 {
00836 if (Is_host_emergency_exit())
00837 {
00838 status = PIPE_DELAY_TIMEOUT;
00839 Host_reset_pipe(pipe);
00840 goto host_get_data_end;
00841 }
00842 }
00843 if (!sav_int_sof_enable)
00844 {
00845 if ((sav_glob_int_en = Is_global_interrupt_enabled())) Disable_global_interrupt();
00846 Host_disable_sof_interrupt();
00847 (void)Is_host_sof_interrupt_enabled();
00848 if (sav_glob_int_en) Enable_global_interrupt();
00849 }
00850 }
00851 }
00852 host_get_data_end:
00853 Host_freeze_pipe(pipe);
00854
00855 if (!sav_int_sof_enable)
00856 {
00857 if ((sav_glob_int_en = Is_global_interrupt_enabled())) Disable_global_interrupt();
00858 Host_disable_sof_interrupt();
00859 (void)Is_host_sof_interrupt_enabled();
00860 if (sav_glob_int_en) Enable_global_interrupt();
00861 }
00862
00863
00864 return status;
00865 }
00866
00867
00868
00869
00870 #if USB_HOST_PIPE_INTERRUPT_TRANSFER == ENABLE
00871
00872 void reset_it_pipe_str(void)
00873 {
00874 U8 i;
00875
00876 for (i = 0; i < MAX_PEP_NB; i++)
00877 {
00878 it_pipe_str[i].enable = FALSE;
00879 it_pipe_str[i].timeout = 0;
00880 }
00881 }
00882
00883
00884 Bool is_any_interrupt_pipe_active(void)
00885 {
00886 U8 i;
00887
00888 for (i = 0; i < MAX_PEP_NB; i++)
00889 {
00890 if (it_pipe_str[i].enable) return TRUE;
00891 }
00892
00893 return FALSE;
00894 }
00895
00896
00907 Bool host_send_data_interrupt(U8 pipe, U16 nb_data, const void *ptr_buf, Pipe_handler *handler)
00908 {
00909 Bool sav_glob_int_en;
00910
00911 if (it_pipe_str[pipe].enable) return FALSE;
00912
00913 if (!is_any_interrupt_pipe_active())
00914 {
00915 g_sav_int_sof_enable = Is_host_sof_interrupt_enabled();
00916 Host_enable_sof_interrupt();
00917 }
00918 it_pipe_str[pipe].enable = TRUE;
00919 it_pipe_str[pipe].nb_byte_to_process = nb_data;
00920 it_pipe_str[pipe].nb_byte_processed = 0;
00921 it_pipe_str[pipe].ptr_buf = (void *)ptr_buf;
00922 it_pipe_str[pipe].handler = handler;
00923 it_pipe_str[pipe].timeout = 0;
00924 it_pipe_str[pipe].nak_timeout = NAK_SEND_TIMEOUT;
00925
00926 if ((sav_glob_int_en = Is_global_interrupt_enabled())) Disable_global_interrupt();
00927 Host_reset_pipe(pipe);
00928 (void)Is_host_resetting_pipe(pipe);
00929 if (sav_glob_int_en) Enable_global_interrupt();
00930 Host_configure_pipe_token(pipe, TOKEN_OUT);
00931 Host_ack_out_ready(pipe);
00932 Host_unfreeze_pipe(pipe);
00933
00934 Host_reset_pipe_fifo_access(pipe);
00935 it_pipe_str[pipe].nb_byte_on_going = nb_data - host_write_p_txpacket(pipe, ptr_buf, nb_data, NULL);
00936 private_sof_counter = 0;
00937 it_pipe_str[pipe].timeout = 0;
00938 if ((sav_glob_int_en = Is_global_interrupt_enabled())) Disable_global_interrupt();
00939 Host_ack_out_ready(pipe);
00940 Host_ack_stall(pipe);
00941 Host_ack_nak_received(pipe);
00942 (void)Is_host_nak_received(pipe);
00943 if (sav_glob_int_en) Enable_global_interrupt();
00944
00945 Host_enable_stall_interrupt(pipe);
00946 Host_enable_pipe_error_interrupt(pipe);
00947 #if NAK_TIMEOUT_ENABLE == ENABLE
00948 Host_enable_nak_received_interrupt(pipe);
00949 #endif
00950 Host_enable_out_ready_interrupt(pipe);
00951 Host_enable_pipe_interrupt(pipe);
00952
00953 Host_send_out(pipe);
00954
00955 return TRUE;
00956 }
00957
00958
00971 Bool host_get_data_interrupt(U8 pipe, U16 nb_data, void *ptr_buf, Pipe_handler *handler)
00972 {
00973 Bool sav_glob_int_en;
00974
00975 if (it_pipe_str[pipe].enable) return FALSE;
00976
00977 if (!is_any_interrupt_pipe_active())
00978 {
00979 g_sav_int_sof_enable = Is_host_sof_interrupt_enabled();
00980 Host_enable_sof_interrupt();
00981 }
00982 it_pipe_str[pipe].enable = TRUE;
00983 it_pipe_str[pipe].nb_byte_to_process = nb_data;
00984 it_pipe_str[pipe].nb_byte_processed = 0;
00985 it_pipe_str[pipe].ptr_buf = ptr_buf;
00986 it_pipe_str[pipe].handler = handler;
00987 it_pipe_str[pipe].timeout = 0;
00988 it_pipe_str[pipe].nak_timeout = NAK_RECEIVE_TIMEOUT;
00989
00990 private_sof_counter = 0;
00991 if ((sav_glob_int_en = Is_global_interrupt_enabled())) Disable_global_interrupt();
00992 Host_reset_pipe(pipe);
00993 Host_ack_stall(pipe);
00994 Host_ack_nak_received(pipe);
00995 (void)Is_host_nak_received(pipe);
00996 if (sav_glob_int_en) Enable_global_interrupt();
00997
00998 Host_enable_stall_interrupt(pipe);
00999 #if NAK_TIMEOUT_ENABLE == ENABLE
01000 Host_enable_nak_received_interrupt(pipe);
01001 #endif
01002 Host_enable_pipe_error_interrupt(pipe);
01003 Host_enable_in_received_interrupt(pipe);
01004 Host_enable_pipe_interrupt(pipe);
01005
01006 Host_enable_continuous_in_mode(pipe);
01007 Host_configure_pipe_token(pipe, TOKEN_IN);
01008 Host_ack_in_received(pipe);
01009 Host_unfreeze_pipe(pipe);
01010
01011 return TRUE;
01012 }
01013
01014
01018 void usb_pipe_interrupt(U8 pipe)
01019 {
01020 void *ptr_buf;
01021 U16 n, i;
01022 Bool callback = FALSE;
01023
01024
01025
01026 if (Is_host_pipe_error(pipe))
01027 {
01028 it_pipe_str[pipe].status = Host_error_status(pipe);
01029 it_pipe_str[pipe].enable = FALSE;
01030 Host_reset_pipe(pipe);
01031 Host_ack_all_errors(pipe);
01032 callback = TRUE;
01033 goto usb_pipe_interrupt_end;
01034 }
01035
01036 if (Is_host_stall(pipe))
01037 {
01038 it_pipe_str[pipe].status = PIPE_STALL;
01039 it_pipe_str[pipe].enable = FALSE;
01040 Host_reset_pipe(pipe);
01041 callback = TRUE;
01042 goto usb_pipe_interrupt_end;
01043 }
01044
01045 #if NAK_TIMEOUT_ENABLE == ENABLE
01046 if (Is_host_nak_received(pipe))
01047 {
01048 Host_ack_nak_received(pipe);
01049
01050 if (!--it_pipe_str[pipe].nak_timeout && Host_get_pipe_type(pipe) != TYPE_INTERRUPT)
01051 {
01052 it_pipe_str[pipe].status = PIPE_NAK_TIMEOUT;
01053 it_pipe_str[pipe].enable = FALSE;
01054 Host_reset_pipe(pipe);
01055 callback = TRUE;
01056 goto usb_pipe_interrupt_end;
01057 }
01058 }
01059 #endif
01060
01061 if (Is_host_in_received(pipe))
01062 {
01063 ptr_buf = (U8 *)it_pipe_str[pipe].ptr_buf + it_pipe_str[pipe].nb_byte_processed;
01064 n = it_pipe_str[pipe].nb_byte_to_process - it_pipe_str[pipe].nb_byte_processed;
01065 Host_freeze_pipe(pipe);
01066 Host_reset_pipe_fifo_access(pipe);
01067 i = Host_get_pipe_size(pipe) - Host_byte_count(pipe);
01068 n = host_read_p_rxpacket(pipe, ptr_buf, n, NULL);
01069 it_pipe_str[pipe].nb_byte_processed = it_pipe_str[pipe].nb_byte_to_process - n;
01070 if (Host_byte_count(pipe))
01071 {
01073 }
01074 else if (i)
01075 {
01076 n = 0;
01077 }
01078 Host_ack_in_received(pipe);
01079 if (n)
01080 {
01081 Host_free_in(pipe);
01082 Host_unfreeze_pipe(pipe);
01083 private_sof_counter = 0;
01084 it_pipe_str[pipe].timeout = 0;
01085 it_pipe_str[pipe].nak_timeout = NAK_RECEIVE_TIMEOUT;
01086 }
01087 else
01088 {
01089 it_pipe_str[pipe].enable = FALSE;
01090 it_pipe_str[pipe].status = PIPE_GOOD;
01091 Host_reset_pipe(pipe);
01092 callback = TRUE;
01093 }
01094 }
01095
01096 if (Is_host_out_ready(pipe))
01097 {
01098 Host_ack_out_ready(pipe);
01099 it_pipe_str[pipe].nb_byte_processed += it_pipe_str[pipe].nb_byte_on_going;
01100 it_pipe_str[pipe].nb_byte_on_going = 0;
01101 ptr_buf = (U8 *)it_pipe_str[pipe].ptr_buf + it_pipe_str[pipe].nb_byte_processed;
01102 n = it_pipe_str[pipe].nb_byte_to_process - it_pipe_str[pipe].nb_byte_processed;
01103 if (n)
01104 {
01105 Host_unfreeze_pipe(pipe);
01106
01107 Host_reset_pipe_fifo_access(pipe);
01108 it_pipe_str[pipe].nb_byte_on_going = n - host_write_p_txpacket(pipe, ptr_buf, n, NULL);
01109 private_sof_counter = 0;
01110 it_pipe_str[pipe].timeout = 0;
01111 it_pipe_str[pipe].nak_timeout = NAK_SEND_TIMEOUT;
01112 Host_send_out(pipe);
01113 }
01114 else
01115 {
01116 it_pipe_str[pipe].enable = FALSE;
01117 it_pipe_str[pipe].status = PIPE_GOOD;
01118 Host_reset_pipe(pipe);
01119 callback = TRUE;
01120 }
01121 }
01122
01123 usb_pipe_interrupt_end:
01124 if (!is_any_interrupt_pipe_active() && !g_sav_int_sof_enable)
01125 {
01126 Host_disable_sof_interrupt();
01127 }
01128 if (callback)
01129 {
01130 it_pipe_str[pipe].handler(it_pipe_str[pipe].status, it_pipe_str[pipe].nb_byte_processed);
01131 }
01132 }
01133
01134 #endif // USB_HOST_PIPE_INTERRUPT_TRANSFER == ENABLE
01135
01136
01137 #endif // USB_HOST_FEATURE == ENABLED