usb_task.c File Reference

This file manages the USB task either device/host or both. More...

#include "config.h"
#include "conf_usb.h"
#include "usb_task.h"
#include "lib_mcu/usb/usb_drv.h"
#include "usb_descriptors.h"
#include "lib_mcu/power/power_drv.h"
#include "lib_mcu/wdt/wdt_drv.h"
#include "lib_mcu/pll/pll_drv.h"
#include "modules/usb/host_chap9/usb_host_task.h"
#include "modules/usb/device_chap9/usb_device_task.h"

Include dependency graph for usb_task.c:

Go to the source code of this file.

Defines

#define LOG_STR_CODE(str)

Functions

void usb_task_init (void)
 This function initializes the USB proces.
void usb_task (void)
 Entry point of the USB mamnagement.
__interrupt void usb_general_interrupt ()
 USB interrupt subroutine.
void suspend_action (void)
void host_suspend_action (void)

Variables

U8 g_sav_int_sof_enable
volatile U16 g_usb_event = 0
bit usb_connected
U8 usb_configuration_nb
volatile U8 private_sof_counter = 0
volatile S_pipe_int it_pipe_str [MAX_EP_NB]
U8 g_usb_mode = 0x00
U8 g_old_usb_mode


Detailed Description

This file manages the USB task either device/host or both.

,v

Copyright (c) 2006 Atmel.

Please read file license.txt for copyright notice.

The USB task selects the correct USB task (usb_device task or usb_host task to be executed depending on the current mode available.

According to USB_DEVICE_FEATURE and USB_HOST_FEATURE value (located in conf_usb.h file) The usb_task can be configured to support USB DEVICE mode or USB Host mode or both for a dual role device application.

This module also contains the general USB interrupt subroutine. This subroutine is used to detect asynchronous USB events.

Note:

Version:
1.28 at90usb128-demo-hidgen-std-2_0_0
Id
usb_task.c,v 1.28 2006/07/03 11:55:11 rletendu Exp
Todo:
Bug:
/

Definition in file usb_task.c.


Define Documentation

#define LOG_STR_CODE ( str   ) 

Definition at line 63 of file usb_task.c.

Referenced by usb_general_interrupt(), and usb_task().


Function Documentation

__interrupt void usb_general_interrupt (  ) 

USB interrupt subroutine.

This function is called each time a USB interrupt occurs. The following USB DEVICE events are taken in charge:

The following USB HOST events are taken in charge:

For each event, the user can launch an action by completing the associate define (See conf_usb.h file to add action upon events)

Note: Only interrupts events that are enabled are processed

Parameters:
none 
Returns:
none

Definition at line 286 of file usb_task.c.

References DISABLE, ENABLE, EVT_HOST_DISCONNECTION, EVT_HOST_HWUP, EVT_HOST_SOF, EVT_USB_DEVICE_FUNCTION, EVT_USB_HOST_FUNCTION, EVT_USB_POWERED, EVT_USB_RESET, EVT_USB_RESUME, EVT_USB_SUSPEND, EVT_USB_UNPOWERED, EVT_USB_WAKE_UP, FALSE, g_old_usb_mode, g_sav_int_sof_enable, g_usb_mode, Host_ack_device_connection, Host_ack_device_disconnection, Host_ack_hwup, Host_ack_sof, Host_device_connection_action, Host_device_disconnection_action, host_disable_all_pipe(), Host_disable_hwup_interrupt, Host_disable_sof_interrupt, Host_get_pipe_type, Host_get_selected_pipe, Host_hwup_action, Host_select_pipe, Host_sof_action, Is_device_connection, Is_device_disconnection, Is_host_device_connection_interrupt_enabled, Is_host_device_disconnection_interrupt_enabled, Is_host_hwup, Is_host_hwup_interrupt_enabled, Is_host_sof, Is_host_sof_interrupt_enabled, Is_reset_interrupt_enabled, Is_resume_interrupt_enabled, Is_sof_interrupt_enabled, Is_suspend_interrupt_enabled, Is_swake_up_interrupt_enabled, Is_usb_id_device, Is_usb_id_interrupt_enabled, Is_usb_id_transition, Is_usb_reset, Is_usb_resume, Is_usb_sof, Is_usb_suspend, Is_usb_vbus_high, Is_usb_vbus_interrupt_enabled, Is_usb_vbus_transition, Is_usb_wake_up, it_pipe_str, LOG_STR_CODE, MAX_EP_NB, private_sof_counter, TRUE, TYPE_INTERRUPT, Usb_ack_id_transition, Usb_ack_reset, Usb_ack_resume, Usb_ack_sof, Usb_ack_suspend, Usb_ack_vbus_transition, Usb_ack_wake_up, Usb_attach, usb_configuration_nb, usb_connected, Usb_disable_resume_interrupt, Usb_disable_wake_up_interrupt, Usb_enable_reset_interrupt, Usb_enable_wake_up_interrupt, Usb_freeze_clock, Usb_id_transition_action, usb_init_device(), USB_MODE_DEVICE, USB_MODE_HOST, Usb_reset_action, Usb_resume_action, Usb_send_event, Usb_sof_action, usb_start_device(), Usb_suspend_action, Usb_unfreeze_clock, Usb_vbus_off_action, Usb_vbus_on_action, Usb_wake_up_action, Wait_pll_ready, Wdt_change_16ms, and Wdt_change_enable.

00288 {
00289    #if (USB_HOST_PIPE_INTERRUPT_TRANSFER == ENABLE)
00290    U8 i;
00291    U8 save_pipe_nb;
00292    #endif
00293 // ---------- DEVICE events management -----------------------------------
00294 #if (USB_DEVICE_FEATURE == ENABLED)
00295   //- VBUS state detection
00296    if (Is_usb_vbus_transition() && Is_usb_vbus_interrupt_enabled())
00297    {
00298       Usb_ack_vbus_transition();
00299       if (Is_usb_vbus_high())
00300       {
00301          usb_connected = TRUE;
00302          Usb_vbus_on_action();
00303          Usb_send_event(EVT_USB_POWERED);
00304          Usb_enable_reset_interrupt();
00305          usb_start_device();
00306          Usb_attach();
00307       }
00308       else
00309       {
00310          Usb_vbus_off_action();
00311          usb_connected = FALSE;
00312          usb_configuration_nb = 0;
00313          Usb_send_event(EVT_USB_UNPOWERED);
00314       }
00315    }
00316   // - Device start of frame received
00317    if (Is_usb_sof() && Is_sof_interrupt_enabled())
00318    {
00319       Usb_ack_sof();
00320       Usb_sof_action();
00321    }
00322   // - Device Suspend event (no more USB activity detected)
00323    if (Is_usb_suspend() && Is_suspend_interrupt_enabled())
00324    {
00325       Usb_ack_suspend();
00326       Usb_enable_wake_up_interrupt();
00327       Usb_ack_wake_up();                 // clear wake up to detect next event
00328       Usb_freeze_clock();
00329       Usb_send_event(EVT_USB_SUSPEND);
00330       Usb_suspend_action();
00331    }
00332   // - Wake up event (USB activity detected): Used to resume
00333    if (Is_usb_wake_up() && Is_swake_up_interrupt_enabled())
00334    {
00335       Usb_unfreeze_clock();
00336       Usb_ack_wake_up();
00337       Usb_disable_wake_up_interrupt();
00338       Usb_wake_up_action();
00339       Usb_send_event(EVT_USB_WAKE_UP);
00340    }
00341   // - Resume state bus detection
00342    if (Is_usb_resume() && Is_resume_interrupt_enabled())
00343    {
00344       Usb_disable_wake_up_interrupt();
00345       Usb_ack_resume();
00346       Usb_disable_resume_interrupt();
00347       Usb_resume_action();
00348       Usb_send_event(EVT_USB_RESUME);
00349    }
00350   // - USB bus reset detection
00351    if (Is_usb_reset()&& Is_reset_interrupt_enabled())
00352    {
00353       Usb_ack_reset();
00354       usb_init_device();
00355       Usb_reset_action();
00356       Usb_send_event(EVT_USB_RESET);
00357    }
00358 #endif// End DEVICE FEATURE MODE
00359 
00360 // ---------- HOST events management -----------------------------------
00361 #if (USB_HOST_FEATURE == ENABLED && USB_DEVICE_FEATURE == ENABLED)
00362   // - ID pin change detection
00363    if(Is_usb_id_transition()&&Is_usb_id_interrupt_enabled())
00364    {
00365       if(Is_usb_id_device())
00366       { g_usb_mode=USB_MODE_DEVICE;}
00367       else
00368       { g_usb_mode=USB_MODE_HOST;}
00369       Usb_ack_id_transition();
00370       if( g_usb_mode != g_old_usb_mode) // Basic Debounce
00371       {
00372          if(Is_usb_id_device()) // Going to device mode
00373          {
00374             Usb_send_event(EVT_USB_DEVICE_FUNCTION);
00375          }
00376          else                   // Going to host mode
00377          {
00378             Usb_send_event(EVT_USB_HOST_FUNCTION);
00379          }
00380          Usb_id_transition_action();
00381          LOG_STR_CODE(log_id_change);
00382          #if ( ID_PIN_CHANGE_GENERATE_RESET == ENABLE)
00383         // Hot ID transition generates wdt reset
00384             #ifndef  AVRGCC
00385                Wdt_change_16ms(); while(1);
00386             #else
00387                Wdt_change_enable(); while(1);
00388             #endif
00389          #endif
00390       }
00391    }
00392 #endif
00393 #if (USB_HOST_FEATURE == ENABLED)
00394   // - The device has been disconnected
00395    if(Is_device_disconnection() && Is_host_device_disconnection_interrupt_enabled())
00396    {
00397       host_disable_all_pipe();
00398       Host_ack_device_disconnection();
00399       device_state=DEVICE_DISCONNECTED;
00400       Usb_send_event(EVT_HOST_DISCONNECTION);
00401       LOG_STR_CODE(log_device_disconnect);
00402       Host_device_disconnection_action();
00403    }
00404   // - Device connection
00405    if(Is_device_connection() && Is_host_device_connection_interrupt_enabled())
00406    {
00407       Host_ack_device_connection();
00408       host_disable_all_pipe();
00409       Host_device_connection_action();
00410    }
00411   // - Host Start of frame has been sent
00412    if (Is_host_sof() && Is_host_sof_interrupt_enabled())
00413    {
00414       Host_ack_sof();
00415       Usb_send_event(EVT_HOST_SOF);
00416       private_sof_counter++;
00417 
00418       // delay timeout management for interrupt tranfer mode in host mode
00419       #if ((USB_HOST_PIPE_INTERRUPT_TRANSFER==ENABLE) && (TIMEOUT_DELAY_ENABLE==ENABLE))
00420       if (private_sof_counter>=250)   // Count 1/4 sec
00421       {
00422          private_sof_counter=0;
00423          for(i=0;i<MAX_EP_NB;i++)
00424          {
00425             if(it_pipe_str[i].enable==ENABLE)
00426             {
00427                save_pipe_nb=Host_get_selected_pipe();
00428                Host_select_pipe(i);
00429                if((++it_pipe_str[i].timeout>TIMEOUT_DELAY) && (Host_get_pipe_type()!=TYPE_INTERRUPT))
00430                {
00431                   it_pipe_str[i].enable=DISABLE;
00432                   it_pipe_str[i].status=PIPE_DELAY_TIMEOUT;
00433                   Host_stop_pipe_interrupt(i);
00434                   if (is_any_interrupt_pipe_active()==FALSE)    // If no more transfer is armed
00435                   {
00436                      if (g_sav_int_sof_enable==FALSE)
00437                      {
00438                         Host_disable_sof_interrupt();
00439                      }
00440                   }
00441                   it_pipe_str[i].handle(PIPE_DELAY_TIMEOUT,it_pipe_str[i].nb_byte_processed);
00442                }
00443                Host_select_pipe(save_pipe_nb);
00444             }
00445          }
00446       }
00447       #endif  // (USB_HOST_PIPE_INTERRUPT_TRANSFER==ENABLE) && (TIMEOUT_DELAY_ENABLE==ENABLE))
00448       Host_sof_action();
00449    }
00450   // - Host Wake-up has been received
00451    if (Is_host_hwup() && Is_host_hwup_interrupt_enabled())
00452    {
00453       Host_disable_hwup_interrupt();  // Wake up interrupt should be disable host is now wake up !
00454       // CAUTION HWUP can be cleared only when USB clock is active (not frozen)!
00455       Pll_start_auto();               // First Restart the PLL for USB operation
00456       Wait_pll_ready();               // Get sure pll is lock
00457       Usb_unfreeze_clock();           // Enable clock on USB interface
00458       Host_ack_hwup();                // Clear HWUP interrupt flag
00459       Usb_send_event(EVT_HOST_HWUP);  // Send software event
00460       Host_hwup_action();             // Map custom action
00461    }
00462 #endif // End HOST FEATURE MODE
00463 }

Here is the call graph for this function:

void suspend_action ( void   ) 

Definition at line 466 of file usb_task.c.

References Enter_power_down_mode.

00467 {
00468    Enable_interrupt();
00469    Enter_power_down_mode();
00470 }


Variable Documentation

U8 g_sav_int_sof_enable

Referenced by usb_general_interrupt().

bit usb_connected

Public : (bit) usb_connected usb_connected is set to TRUE when VBUS has been detected usb_connected is set to FALSE otherwise /

Definition at line 46 of file usb_device_task.c.

volatile S_pipe_int it_pipe_str[MAX_EP_NB]

Referenced by usb_general_interrupt().

U8 g_old_usb_mode

Definition at line 125 of file usb_task.c.

Referenced by usb_general_interrupt(), usb_task(), and usb_task_init().


Generated on Fri Jan 26 17:33:07 2007 for Atmel by  doxygen 1.5.1-p1