00001
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065 #include "compiler.h"
00066 #include "intc.h"
00067 #ifdef FREERTOS_USED
00068 #include "FreeRTOS.h"
00069 #include "task.h"
00070 #include "semphr.h"
00071 #endif
00072 #include "conf_usb.h"
00073 #include "usb_drv.h"
00074 #include "usb_task.h"
00075
00076 #if USB_DEVICE_FEATURE == ENABLED
00077 #include "usb_descriptors.h"
00078 #include "usb_device_task.h"
00079 #endif
00080
00081 #if USB_HOST_FEATURE == ENABLED
00082 #include "usb_host_task.h"
00083 #endif
00084
00085 #if UC3C
00086 #include "pm_uc3c.h"
00087 #else
00088 #include "pm.h"
00089 #endif
00090
00091
00092
00093
00094
00095
00096
00106 volatile U16 g_usb_event = 0;
00107 #if (USB_HOST_FEATURE == ENABLED) && (USB_DEVICE_FEATURE == ENABLED) && (USB_HIGH_SPEED_SUPPORT==ENABLED)
00108 static U8 private_sof_counter_HS = 0;
00109 #endif
00110
00111
00112 #if USB_DEVICE_FEATURE == ENABLED
00113
00119 extern volatile Bool usb_connected;
00120
00121 #ifdef FREERTOS_USED
00123 extern xTaskHandle usb_device_tsk;
00124 #endif
00125
00126 #endif
00127
00128
00129 #if USB_HOST_FEATURE == ENABLED
00130
00131 static const char log_device_disconnected[] = "Device disconnected\n";
00132
00138 volatile U32 private_sof_counter;
00139
00140 #if USB_HOST_PIPE_INTERRUPT_TRANSFER == ENABLE
00141 extern volatile Bool g_sav_int_sof_enable;
00142 extern volatile S_pipe_int it_pipe_str[MAX_PEP_NB];
00143 #endif
00144
00145 #ifdef FREERTOS_USED
00147 extern xTaskHandle usb_host_tsk;
00148 #endif
00149
00150 #endif
00151
00152
00153 #if USB_DEVICE_FEATURE == ENABLED && USB_HOST_FEATURE == ENABLED
00154
00155 static const char log_pin_id_changed[] = "Pin Id changed\n";
00156
00161 volatile U8 g_usb_mode = USB_MODE_UNDEFINED;
00162 static volatile U8 g_old_usb_mode;
00163
00164 #endif
00165
00166
00167 #ifdef FREERTOS_USED
00169 static xSemaphoreHandle usb_tsk_semphr = NULL;
00170 #endif
00171
00172
00173
00174
00175 #ifdef FREERTOS_USED
00176
00177 #if (defined __GNUC__)
00178 __attribute__((__noinline__))
00179 #endif
00180 static portBASE_TYPE usb_general_interrupt_non_naked(void);
00181
00182
00189 #if (defined __GNUC__)
00190 __attribute__((__naked__))
00191 #elif __ICCAVR32__
00192 #pragma shadow_registers = full
00193 #endif
00194 static void usb_general_interrupt(void)
00195 {
00196 portENTER_SWITCHING_ISR();
00197 usb_general_interrupt_non_naked();
00198 portEXIT_SWITCHING_ISR();
00199 }
00200
00201 #else
00202
00203 #if (defined __GNUC__)
00204 __attribute__((__interrupt__))
00205 #elif (defined __ICCAVR32__)
00206 __interrupt
00207 #endif
00208 static void usb_general_interrupt(void);
00209
00210 #endif // FREERTOS_USED
00211
00212
00217 void usb_task_init(void)
00218 {
00219 #ifdef FREERTOS_USED
00220
00221 vSemaphoreCreateBinary(usb_tsk_semphr);
00222
00223 xTaskCreate(usb_task,
00224 configTSK_USB_NAME,
00225 configTSK_USB_STACK_SIZE,
00226 NULL,
00227 configTSK_USB_PRIORITY,
00228 NULL);
00229 }
00230
00231
00232 void usb_task(void *pvParameters)
00233 {
00234 #endif // FREERTOS_USED
00235
00236
00237 Disable_global_interrupt();
00238 INTC_register_interrupt((__int_handler)&usb_general_interrupt, AVR32_USBB_IRQ, USB_INT_LEVEL);
00239 Enable_global_interrupt();
00240
00241 #ifdef FREERTOS_USED
00242 while (TRUE)
00243 {
00244
00245 while (!xSemaphoreTake(usb_tsk_semphr, portMAX_DELAY));
00246
00247 #endif // FREERTOS_USED
00248
00249 #if USB_DEVICE_FEATURE == ENABLED && USB_HOST_FEATURE == ENABLED
00250 #ifdef FREERTOS_USED
00251 if (usb_device_tsk) vTaskDelete(usb_device_tsk), usb_device_tsk = NULL;
00252 if (usb_host_tsk) vTaskDelete(usb_host_tsk), usb_host_tsk = NULL;
00253 #endif
00254 Usb_input_id_pin();
00255 Usb_enable_id_pin();
00256 if (Is_usb_id_device())
00257 {
00258 g_usb_mode = USB_MODE_DEVICE;
00259 usb_device_task_init();
00260 }
00261 else
00262 {
00263 private_sof_counter = 0;
00264 g_usb_mode = USB_MODE_HOST;
00265 usb_host_task_init();
00266 }
00267 g_old_usb_mode = g_usb_mode;
00268 Usb_raise_id_transition();
00269 Usb_enable_id_interrupt();
00270 Enable_global_interrupt();
00271
00272
00273
00274 #elif USB_DEVICE_FEATURE == ENABLED
00275 #ifdef FREERTOS_USED
00276 if (usb_device_tsk) vTaskDelete(usb_device_tsk), usb_device_tsk = NULL;
00277 #endif
00278 Usb_force_device_mode();
00279 usb_device_task_init();
00280
00281
00282
00283 #elif USB_HOST_FEATURE == ENABLED
00284 #ifdef FREERTOS_USED
00285 if (usb_host_tsk) vTaskDelete(usb_host_tsk), usb_host_tsk = NULL;
00286 #endif
00287 private_sof_counter = 0;
00288 Usb_force_host_mode();
00289 usb_host_task_init();
00290
00291
00292
00293 #else
00294 #error At least one of USB_DEVICE_FEATURE and USB_HOST_FEATURE must be enabled
00295 #endif
00296
00297 #ifdef FREERTOS_USED
00298 }
00299 #endif
00300 }
00301
00302
00307 #ifndef FREERTOS_USED
00308 void usb_task(void)
00309 {
00310
00311 #if USB_DEVICE_FEATURE == ENABLED && USB_HOST_FEATURE == ENABLED
00312 if (g_old_usb_mode != g_usb_mode)
00313 {
00314 if (Is_usb_id_device())
00315 {
00316 usb_device_task_init();
00317 }else{
00318 private_sof_counter = 0;
00319 usb_host_task_init();
00320 }
00321 g_old_usb_mode = g_usb_mode;
00322 Usb_enable_id_interrupt();
00323 Enable_global_interrupt();
00324 }
00325
00326
00327 switch (g_old_usb_mode)
00328 {
00329 case USB_MODE_DEVICE:
00330 usb_device_task();
00331 break;
00332 case USB_MODE_HOST:
00333 usb_host_task();
00334 break;
00335 case USB_MODE_UNDEFINED:
00336 default:
00337 break;
00338 }
00339
00340
00341
00342 #elif USB_DEVICE_FEATURE == ENABLED
00343 usb_device_task();
00344
00345
00346
00347 #elif USB_HOST_FEATURE == ENABLED
00348 usb_host_task();
00349
00350
00351
00352 #else
00353 #error At least one of USB_DEVICE_FEATURE and USB_HOST_FEATURE must be enabled
00354 #endif
00355
00356 }
00357 #endif
00358
00359
00393 #ifdef FREERTOS_USED
00394
00395 #if (defined __GNUC__)
00396 __attribute__((__noinline__))
00397 #elif (defined __ICCAVR32__)
00398 #pragma optimize = no_inline
00399 #endif
00400 static portBASE_TYPE usb_general_interrupt_non_naked(void)
00401
00402 #else
00403
00404 #if (defined __GNUC__)
00405 __attribute__((__interrupt__))
00406 #elif (defined __ICCAVR32__)
00407 __interrupt
00408 #endif
00409 static void usb_general_interrupt(void)
00410
00411 #endif
00412 {
00413 #ifdef FREERTOS_USED
00414 portBASE_TYPE task_woken = pdFALSE;
00415 #endif
00416 #if USB_HOST_FEATURE == ENABLED && USB_HOST_PIPE_INTERRUPT_TRANSFER == ENABLE
00417 U8 i;
00418 #endif
00419
00420
00421 #if USB_DEVICE_FEATURE == ENABLED && USB_HOST_FEATURE == ENABLED
00422
00423 if (Is_usb_id_transition() && Is_usb_id_interrupt_enabled())
00424 {
00425 g_usb_mode = (Is_usb_id_device()) ? USB_MODE_DEVICE : USB_MODE_HOST;
00426 Usb_ack_id_transition();
00427 if (g_usb_mode != g_old_usb_mode)
00428 {
00429
00430 if (g_old_usb_mode == USB_MODE_DEVICE)
00431 {
00432 if (usb_connected)
00433 {
00434
00435 usb_connected = FALSE;
00436 usb_configuration_nb = 0;
00437 Usb_vbus_off_action();
00438 }
00439 }
00440
00441 else if (Is_host_attached())
00442 {
00443
00444 device_state = DEVICE_UNATTACHED;
00445 Host_device_disconnection_action();
00446 }
00447 LOG_STR(log_pin_id_changed);
00448 Usb_send_event((Is_usb_device()) ? EVT_USB_DEVICE_FUNCTION :
00449 EVT_USB_HOST_FUNCTION);
00450 Usb_id_transition_action();
00452
00453 #if ID_PIN_CHANGE_GENERATE_RESET == ENABLE
00454
00455 Usb_disable();
00456 Usb_disable_otg_pad();
00457 #ifdef FREERTOS_USED
00458
00459 taskENTER_CRITICAL();
00460 xSemaphoreGiveFromISR(usb_tsk_semphr, &task_woken);
00461 taskEXIT_CRITICAL();
00462 #else
00463
00464 #endif
00465 #endif
00466 }
00467 }
00468 #endif // End DEVICE/HOST FEATURE MODE
00469
00470
00471 #if USB_DEVICE_FEATURE == ENABLED
00472 #if USB_HOST_FEATURE == ENABLED
00473
00474
00475
00476 if (Is_usb_device())
00477 #endif
00478 {
00479
00480 if (Is_usb_vbus_transition() && Is_usb_vbus_interrupt_enabled())
00481 {
00482 Usb_ack_vbus_transition();
00483 if (Is_usb_vbus_high())
00484 {
00485 usb_start_device();
00486 Usb_send_event(EVT_USB_POWERED);
00487 Usb_vbus_on_action();
00488 }
00489 else
00490 {
00491 Usb_unfreeze_clock();
00492 Usb_detach();
00493 usb_connected = FALSE;
00494 usb_configuration_nb = 0;
00495 Usb_send_event(EVT_USB_UNPOWERED);
00496 Usb_vbus_off_action();
00497 #ifdef FREERTOS_USED
00498
00499 taskENTER_CRITICAL();
00500 xSemaphoreGiveFromISR(usb_tsk_semphr, &task_woken);
00501 taskEXIT_CRITICAL();
00502 #endif
00503 }
00504 }
00505
00506 if (Is_usb_sof() && Is_usb_sof_interrupt_enabled())
00507 {
00508 Usb_ack_sof();
00509 Usb_sof_action();
00510 }
00511
00512 if (Is_usb_suspend() && Is_usb_suspend_interrupt_enabled())
00513 {
00514 Usb_ack_suspend();
00515 Usb_enable_wake_up_interrupt();
00516 (void)Is_usb_wake_up_interrupt_enabled();
00517 Usb_freeze_clock();
00518 Usb_send_event(EVT_USB_SUSPEND);
00519 Usb_suspend_action();
00520 }
00521
00522 if (Is_usb_wake_up() && Is_usb_wake_up_interrupt_enabled())
00523 {
00524 Usb_unfreeze_clock();
00525 (void)Is_usb_clock_frozen();
00526 Usb_ack_wake_up();
00527 Usb_disable_wake_up_interrupt();
00528 Usb_wake_up_action();
00529 Usb_send_event(EVT_USB_WAKE_UP);
00530 }
00531
00532 if (Is_usb_resume() && Is_usb_resume_interrupt_enabled())
00533 {
00534 Usb_disable_wake_up_interrupt();
00535 Usb_ack_resume();
00536 Usb_disable_resume_interrupt();
00537 Usb_resume_action();
00538 Usb_send_event(EVT_USB_RESUME);
00539 }
00540
00541 if (Is_usb_reset() && Is_usb_reset_interrupt_enabled())
00542 {
00543 Usb_ack_reset();
00544 usb_init_device();
00545 Usb_reset_action();
00546 Usb_send_event(EVT_USB_RESET);
00547 }
00548 }
00549 #endif // End DEVICE FEATURE MODE
00550
00551
00552 #if USB_HOST_FEATURE == ENABLED
00553 #if USB_DEVICE_FEATURE == ENABLED
00554
00555
00556
00557 else
00558 #endif
00559 {
00560
00561 if (Is_host_device_disconnection() && Is_host_device_disconnection_interrupt_enabled())
00562 {
00563 host_disable_all_pipes();
00564 Host_ack_device_disconnection();
00565 #if USB_HOST_PIPE_INTERRUPT_TRANSFER == ENABLE
00566 reset_it_pipe_str();
00567 #endif
00568 device_state = DEVICE_UNATTACHED;
00569 LOG_STR(log_device_disconnected);
00570 Usb_send_event(EVT_HOST_DISCONNECTION);
00571 Host_device_disconnection_action();
00572 #ifdef FREERTOS_USED
00573
00574 taskENTER_CRITICAL();
00575 xSemaphoreGiveFromISR(usb_tsk_semphr, &task_woken);
00576 taskEXIT_CRITICAL();
00577 #endif
00578 }
00579
00580 if (Is_host_device_connection() && Is_host_device_connection_interrupt_enabled())
00581 {
00582 Host_ack_device_connection();
00583 host_disable_all_pipes();
00584 Host_device_connection_action();
00585 }
00586
00587 if (Is_host_sof() && Is_host_sof_interrupt_enabled())
00588 {
00589 Host_ack_sof();
00590 Usb_send_event(EVT_HOST_SOF);
00591 #if (USB_HIGH_SPEED_SUPPORT==ENABLED)
00592 if( Is_usb_full_speed_mode() )
00593 {
00594 private_sof_counter++;
00595 }else{
00596 private_sof_counter_HS++;
00597 if( 0 == (private_sof_counter_HS%8) )
00598 {
00599 private_sof_counter++;
00600 }
00601 }
00602 #else
00603 private_sof_counter++;
00604 #endif
00605
00606 #if USB_HOST_PIPE_INTERRUPT_TRANSFER == ENABLE && TIMEOUT_DELAY_ENABLE == ENABLE
00607 if (private_sof_counter >= 250)
00608 {
00609 private_sof_counter = 0;
00610 for (i = 0; i < MAX_PEP_NB; i++)
00611 {
00612 if (it_pipe_str[i].enable &&
00613 ++it_pipe_str[i].timeout > TIMEOUT_DELAY && Host_get_pipe_type(i) != TYPE_INTERRUPT)
00614 {
00615 it_pipe_str[i].enable = FALSE;
00616 it_pipe_str[i].status = PIPE_DELAY_TIMEOUT;
00617 Host_reset_pipe(i);
00618 if (!is_any_interrupt_pipe_active() && !g_sav_int_sof_enable)
00619 {
00620 Host_disable_sof_interrupt();
00621 }
00622 it_pipe_str[i].handler(PIPE_DELAY_TIMEOUT, it_pipe_str[i].nb_byte_processed);
00623 }
00624 }
00625 }
00626 #endif
00627 Host_sof_action();
00628 }
00629
00630 if (Is_host_hwup() && Is_host_hwup_interrupt_enabled())
00631 {
00632
00634
00635
00636 Usb_unfreeze_clock();
00637 (void)Is_usb_clock_frozen();
00638 Host_disable_hwup_interrupt();
00639 Host_ack_hwup();
00640 Usb_send_event(EVT_HOST_HWUP);
00641 Host_hwup_action();
00642 }
00643 #if USB_HOST_PIPE_INTERRUPT_TRANSFER == ENABLE
00644
00645 while ((i = Host_get_interrupt_pipe_number()) < MAX_PEP_NB) usb_pipe_interrupt(i);
00646 #endif
00647 }
00648 #endif // End HOST FEATURE MODE
00649
00650 #ifdef FREERTOS_USED
00651 return task_woken;
00652 #endif
00653 }
00654
00655
00656 #if USB_DEVICE_FEATURE == ENABLED
00657 void usb_suspend_action(void)
00658 {
00659 volatile avr32_pm_t *pm = &AVR32_PM;
00660 pm->AWEN.usb_waken = 1;
00661 SLEEP(AVR32_PM_SMODE_STATIC);
00662 pm->AWEN.usb_waken = 0;
00663 }
00664 #endif
00665
00666
00667 #if USB_HOST_FEATURE == ENABLED
00668 void host_suspend_action(void)
00669 {
00670 Enable_global_interrupt();
00672
00673 }
00674
00675 U32 host_get_timeout( void )
00676 {
00677 return private_sof_counter;
00678 }
00679
00680 #endif