📄 usb_task.c
字号:
//!
//! @file usb_task.c
//!
//! Copyright (c) 2004
//!
//! Please read file license.txt for copyright notice.
//!
//! @brief This file manages the USB controller.
//!
//! The USB task checks the income of new requests from the USB Host.
//! When a Setup request occurs, this task will launch the processing
//! of this setup contained in the usb_enum.c file.
//! Other class specific requests are also processed in this file.
//! This file manages all the USB events:
//! Suspend / Resume / Reset / Start Of Frame / Wake Up / Vbus events
//!
//! @version 1.10.2.2 (c5131-usb-generic-1_2_0)
//!
//! @todo
//! @bug
//!/
//_____ I N C L U D E S ___________________________________________________
#include "config.h"
#include "conf\conf_usb.h"
#include "usb_task.h"
#include "lib_mcu\usb\usb_drv.h"
#include "modules\usb\usb_enum.h"
#include "intrins.h"
//_____ M A C R O S ________________________________________________________
//_____ D E F I N I T I O N S ______________________________________________
//!
//! Public : (bit) usb_connected
//! usb_connected is set to TRUE when VBUS has been detected
//! usb_connected is set to FALSE otherwise
//!/
volatile bit usb_connected;
U8 xdata sof_counter;
volatile U8 data toggle_next_time;
extern _MEM_TYPE_SLOW_ U8 usb_configuration_nb;
volatile bit usb_continuous_mode;
U8 send_free = 0;
S_hid_set_control SetControlBytes;
//_____ D E C L A R A T I O N S ____________________________________________
//!
//! @brief This function initializes the USB the associated variables.
//!
//! This function enables the USB controller and init the USB interrupts.
//! The aim is to allow the USB connection detection in order to send
//! the appropriate USB event to the operating mode manager.
//!
//! @warning Code:?? bytes (function code length)
//!
//! @param none
//!
//! @return none
//!
//!/
void usb_task_init(void)
{
Set_page_usb();
Usb_enable(); // enable the USB controller
usb_start_device();
sof_counter = 0;
usb_connected = TRUE;
toggle_next_time = 0;
usb_continuous_mode = FALSE;
}
//!
//! @brief This function initializes the USB device controller
//!
//! This function enables the USB controller and init the USB interrupts.
//! The aim is to allow the USB connection detection in order to send
//! the appropriate USB event to the operating mode manager.
//!
//! @warning Code:?? bytes (function code length)
//!
//! @param none
//!
//! @return none
//!
//!/
void usb_start_device (void)
{
U16 c;
usb_generate_clock();
Set_page_usb();
Usb_unfreeze_clock();
Usb_set_speed(); // for speed analysis
Usb_ack_suspend();
Usb_detach();
for (c=0; c<1000; c++);
Usb_attach();
Usb_enable_suspend_interrupt();
Usb_enable_resume_interrupt();
Usb_enable_reset_interrupt();
Usb_enable_wake_up_interrupt();
Usb_enable_sof_interrupt();
Enable_usb_interrupt();
Enable_interrupt();
usb_init_device(); // configure the USB controller EP0
Usb_attach();
}
//! @brief Entry point of the USB mamnagement
//!
//! This function is the entry point of the USB management. Each USB
//! event is checked here in order to launch the appropriate action.
//! If a Setup request occurs on the Default Control Endpoint,
//! the usb_process_request() function is call in the usb_enum.c file
//! If a new USB mass storage Command Block Wrapper (CBW) occurs,
//! this one will be decoded and the SCSI command will be taken in charge
//! by the scsi decoder.
//!
//! @warning Code:?? bytes (function code length)
//!
//! @param none
//!
//! @return none
void usb_task(void)
{
if (!usb_connected) /* power down mode when USB not connected */
{
Pll_stop(); /* disable PLL before going in power down mode */
Enable_interrupt();
// Shutdown board ressources
Led_all_off();
Disable_ale();
if (!usb_connected)
{
Usb_suspend_action(); // see in conf_usb.h
_nop_();
}
}
else //USB Connected
{
// USB MANAGEMENT
Set_page_usb ();
Usb_select_endpoint(EP_CONTROL);
if (Is_usb_receive_setup())
{
usb_process_request();
if(SetControlBytes.type==0) { usb_continuous_mode = FALSE; }
if(SetControlBytes.type==1) { usb_continuous_mode = TRUE ; }
}
//!
//! this part of the code can be modify following the application
//!
if(usb_continuous_mode == TRUE)
{
Usb_select_endpoint(EP_HID_IN);
if(Is_usb_in_ready())
{
send_free = 1;
Usb_ack_in_ready();
}
}
//!
//! end of modification
//!
}
}
//! @brief USB interrupt process
//!
//! This function is called each time a USB interrupt occurs.
//! The following USB events are taken in charge:
//! - Suspend
//! - Wake-Up
//! - Reset
//! For each event, the user can launch an action by completing
//! the associate define
//!
//! @warning Code:?? bytes (function code length)
//!
//! @param none
//!
//! @return none
void usb_interrupt(void) interrupt IRQ_USB
{
if (Is_usb_sof())
{
Usb_ack_sof();
sof_counter++;
if (usb_continuous_mode == FALSE)
{
if (sof_counter == 0x40)
{
Switch_ale();
sof_counter = 0;
}
}
else
{
if (sof_counter == 0xFF)
{
Switch_ale();
}
}
return;
}
if (Is_usb_suspend())
{
Usb_ack_suspend();
Usb_freeze_clock();
usb_connected = FALSE;
}
if (Is_usb_wake_up())
{
if (!usb_connected) /* reconfigure PLL when exiting power down mode */
{
Pll_enable();
}
Usb_unfreeze_clock();
Enable_ale();
Usb_ack_suspend();
Usb_ack_wake_up();
usb_connected = TRUE;
}
if (Is_usb_reset())
{
Usb_ack_reset();
usb_enum_var_init();
Usb_enable_suspend_interrupt();
Usb_enable_wake_up_interrupt();
Usb_enable_sof_interrupt();
usb_connected = TRUE;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -