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