?? usb_task.c
字號:
//!
//! @file usb_task.c,v
//!
//! Copyright (c) 2006 Atmel.
//!
//! Please read file license.txt for copyright notice.
//!
//! @brief This file manages the USB task either device/host or both.
//!
//! 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:
//! - The usb_task belongs to the scheduler, the usb_device_task and usb_host do not, they are called
//! from the general usb_task
//! - See conf_usb.h file for more details about the configuration of this module
//!
//!
//! @version 1.28 at90usb128-demo-audio-1_0_2 $Id: usb_task.c,v 1.28 2006/07/03 11:55:11 rletendu Exp $
//!
//! @todo
//! @bug
//!/
//_____ I N C L U D E S ___________________________________________________
#include "config.h"
#include "conf_usb.h"
#include "usb_task.h"
#include "lib_mcu/usb/usb_drv.h"
#if ((USB_DEVICE_FEATURE == ENABLED))
#include "usb_descriptors.h"
#endif
#include "lib_mcu/power/power_drv.h"
#include "lib_mcu/wdt/wdt_drv.h"
#include "lib_mcu/pll/pll_drv.h"
#if ((USB_HOST_FEATURE == ENABLED))
#include "modules/usb/host_chap9/usb_host_task.h"
#if (USB_HOST_PIPE_INTERRUPT_TRANSFER == ENABLE)
extern U8 g_sav_int_sof_enable;
#endif
#endif
#if ((USB_DEVICE_FEATURE == ENABLED))
#include "modules/usb/device_chap9/usb_device_task.h"
#endif
#ifndef USE_USB_PADS_REGULATOR
#error "USE_USB_PADS_REGULATOR" should be defined as ENABLE or DISABLE in conf_usb.h file
#endif
//_____ M A C R O S ________________________________________________________
#ifndef LOG_STR_CODE
#define LOG_STR_CODE(str)
#else
U8 code log_device_disconnect[]="Device Disconnected";
U8 code log_id_change[]="Pin Id Change";
#endif
//_____ D E F I N I T I O N S ______________________________________________
//!
//! Public : U16 g_usb_event
//! usb_connected is used to store USB events detected upon
//! USB general interrupt subroutine
//! Its value is managed by the following macros (See usb_task.h file)
//! Usb_send_event(x)
//! Usb_ack_event(x)
//! Usb_clear_all_event()
//! Is_usb_event(x)
//! Is_not_usb_event(x)
volatile U16 g_usb_event=0;
#if (USB_DEVICE_FEATURE == ENABLED)
//!
//! Public : (bit) usb_connected
//! usb_connected is set to TRUE when VBUS has been detected
//! usb_connected is set to FALSE otherwise
//! Used with USB_DEVICE_FEATURE == ENABLED only
//!/
extern bit usb_connected;
//!
//! Public : (U8) usb_configuration_nb
//! Store the number of the USB configuration used by the USB device
//! when its value is different from zero, it means the device mode is enumerated
//! Used with USB_DEVICE_FEATURE == ENABLED only
//!/
extern U8 usb_configuration_nb;
#endif
#if (USB_HOST_FEATURE == ENABLED)
//!
//! Private : (U8) private_sof_counter
//! Incremented by host SOF interrupt subroutime
//! This counter is used to detect timeout in host requests.
//! It must not be modified by the user application tasks.
//!/
volatile U8 private_sof_counter=0;
#if (USB_HOST_PIPE_INTERRUPT_TRANSFER == ENABLE)
extern volatile S_pipe_int it_pipe_str[MAX_EP_NB];
#endif
#endif
#if ((USB_DEVICE_FEATURE == ENABLED)&& (USB_HOST_FEATURE == ENABLED))
//!
//! Public : (U8) g_usb_mode
//! Used in dual role application (both device/host) to store
//! the current mode the usb controller is operating
//!/
U8 g_usb_mode=USB_MODE_UNDEFINED;
U8 g_old_usb_mode;
#endif
//_____ D E C L A R A T I O N S ____________________________________________
/**
* @brief This function initializes the USB proces.
*
* Depending on the mode supported (HOST/DEVICE/DUAL_ROLE) the function
* calls the coresponding usb mode initialization function
*
* @param none
*
* @return none
*/
void usb_task_init(void)
{
#if (USB_HOST_FEATURE == ENABLED && USB_DEVICE_FEATURE == ENABLED)
U8 delay;
#endif
#if (USE_USB_PADS_REGULATOR==ENABLE) // Otherwise assume USB PADs regulator is not used
Usb_enable_regulator();
#endif
// ---- DUAL ROLE DEVICE USB MODE ---------------------------------------------
#if ((USB_DEVICE_FEATURE == ENABLED)&& (USB_HOST_FEATURE == ENABLED))
Usb_enable_uid_pin();
delay=PORTA;
g_usb_mode=USB_MODE_UNDEFINED;
if(Is_usb_id_device())
{
g_usb_mode=USB_MODE_DEVICE;
usb_device_task_init();
}
else
{
g_usb_mode=USB_MODE_HOST;
Usb_ack_id_transition(); // REQUIRED !!! Startup with ID=0, Ack ID pin transistion (default hwd start up is device mode)
Usb_enable_id_interrupt();
Enable_interrupt();
usb_host_task_init();
}
g_old_usb_mode=g_usb_mode; // Store current usb mode, for mode change detection
// -----------------------------------------------------------------------------
// ---- DEVICE ONLY USB MODE ---------------------------------------------------
#elif ((USB_DEVICE_FEATURE == ENABLED)&& (USB_HOST_FEATURE == DISABLE))
Usb_force_device_mode();
usb_device_task_init();
// -----------------------------------------------------------------------------
// ---- REDUCED HOST ONLY USB MODE ---------------------------------------------
#elif ((USB_DEVICE_FEATURE == DISABLE)&& (USB_HOST_FEATURE == ENABLED))
Usb_force_host_mode();
usb_host_task_init();
#elif ((USB_DEVICE_FEATURE == DISABLE)&& (USB_HOST_FEATURE == DISABLE))
#error at least one of USB_DEVICE_FEATURE or USB_HOST_FEATURE should be enabled
#endif
// -----------------------------------------------------------------------------
}
/**
* @brief Entry point of the USB mamnagement
*
* Depending on the USB mode supported (HOST/DEVICE/DUAL_ROLE) the function
* calls the coresponding usb management function.
*
* @param none
*
* @return none
*/
void usb_task(void)
{
// ---- DUAL ROLE DEVICE USB MODE ---------------------------------------------
#if ((USB_DEVICE_FEATURE == ENABLED)&& (USB_HOST_FEATURE == ENABLED))
if(Is_usb_id_device())
{ g_usb_mode=USB_MODE_DEVICE;}
else
{ g_usb_mode=USB_MODE_HOST;}
// TODO !!! ID pin hot state change
// Preliminary management: HARDWARE RESET !!!
#if ( ID_PIN_CHANGE_GENERATE_RESET == ENABLE)
// Hot ID transition generates wdt reset
if((g_old_usb_mode!=g_usb_mode))
#ifndef AVRGCC
{Wdt_change_16ms(); while(1); LOG_STR_CODE(log_id_change);}
#else
{Wdt_change_enable(); while(1); LOG_STR_CODE(log_id_change);}
#endif
#endif
g_old_usb_mode=g_usb_mode; // Store current usb mode, for mode change detection
// Depending on current usb mode, launch the correct usb task (device or host)
switch(g_usb_mode)
{
case USB_MODE_DEVICE:
usb_device_task();
break;
case USB_MODE_HOST:
usb_host_task();
break;
case USB_MODE_UNDEFINED: // No break !
default:
break;
}
// -----------------------------------------------------------------------------
// ---- DEVICE ONLY USB MODE ---------------------------------------------------
#elif ((USB_DEVICE_FEATURE == ENABLED)&& (USB_HOST_FEATURE == DISABLE))
usb_device_task();
// -----------------------------------------------------------------------------
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -