📄 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.8 at90usb162-hidgen-1_0_1 $Id: usb_task.c,v 1.8 2007/06/13 12:58:51 arobert 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"
#include "modules/usb/device_chap9/usb_device_task.h"
#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 process.
*
* The function calls the coresponding usb mode initialization function
*
* @param none
*
* @return none
*/
void usb_task_init(void)
{
#if (USE_USB_PADS_REGULATOR==ENABLE) // Otherwise assume USB PADs regulator is not used
Usb_enable_regulator();
#endif
usb_device_task_init();
#if (USB_REMOTE_WAKEUP == ENABLED)
usb_remote_wup_feature = DISABLED;
#endif
}
/**
* @brief Entry point of the USB mamnagement
*
* The function calls the coresponding usb management function.
*
* @param none
*
* @return none
*/
void usb_task(void)
{
usb_device_task();
}
//! @brief USB interrupt subroutine
//!
//! This function is called each time a USB interrupt occurs.
//! The following USB DEVICE events are taken in charge:
//! - Start Of Frame
//! - Suspend
//! - Wake-Up
//! - Resume
//! - Reset
//!
//! 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
//!
//! @param none
//!
//! @return none
#ifdef AVRGCC
ISR(USB_GEN_vect)
#else
#pragma vector = USB_GENERAL_vect
__interrupt void usb_general_interrupt()
#endif
{
// - Device start of frame received
if (Is_usb_sof() && Is_sof_interrupt_enabled())
{
Usb_ack_sof();
Usb_sof_action();
}
// - Device Suspend event (no more USB activity detected)
if (Is_usb_suspend() && Is_suspend_interrupt_enabled())
{
Usb_enable_wake_up_interrupt();
Usb_ack_wake_up(); // clear wake up to detect next event
Usb_freeze_clock();
Usb_send_event(EVT_USB_SUSPEND);
Usb_suspend_action();
Usb_ack_suspend(); // must be executed last (after Usb_suspend_action()) to allow upstream resume
}
// - Wake up event (USB activity detected): Used to resume
if (Is_usb_wake_up() && Is_swake_up_interrupt_enabled())
{
Usb_unfreeze_clock();
Usb_ack_wake_up();
Usb_disable_wake_up_interrupt();
Usb_wake_up_action();
Usb_send_event(EVT_USB_WAKE_UP);
}
// - Resume state bus detection
if (Is_usb_resume() && Is_resume_interrupt_enabled())
{
Usb_disable_wake_up_interrupt();
Usb_ack_resume();
Usb_disable_resume_interrupt();
Usb_resume_action();
Usb_send_event(EVT_USB_RESUME);
}
// - USB bus reset detection
if (Is_usb_reset()&& Is_reset_interrupt_enabled())
{
#if (USB_REMOTE_WAKEUP == ENABLED)
usb_remote_wup_feature = DISABLED;
#endif
Usb_ack_reset();
usb_init_device();
Usb_reset_action();
Usb_send_event(EVT_USB_RESET);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -