usb初始化过程
处理器:pic18f2550
编译环境:mplab V8.53 + CCS_PCWHD_4.120 (安装过程如上一篇博客)
1.usb_init();把整个usb初始化都集成到一个函数里边,进入死循环之前调用该函数即可完成对usb接口的初始化工作。
函数声明在:usb_hw_layer.h中
函数体在pic18_usb.c中。
函数体如下:
void usb_init(void)
{ usb_init_cs(); do
{ usb_task(); } while (usb_state != USB_STATE_POWERED); } 2.函数中调用usb_init_cs();其作用是禁止USB,具体的禁止过程由函数usb_detach()完成。
函数声明在:usb_hw_layer.h中 函数体在pic18_usb.c中。
函数体如下:
void usb_init_cs(void)
{ usb_detach(); } 3.函数中调用usb_detach().实现usb功能的禁止。
函数声明在:usb_hw_layer.h中 函数体在pic18_usb.c中。
函数体如下:
void usb_detach(void)
{ UCON = 0; //disable USB hardware UIE = 0; //disable USB interrupts UCFG = __UCFG_VAL_DISABLED__; // set D+/D- to inputs #if defined(__USB_87J50__) set_tris_f(get_tris_f() | 0x18); #elif defined(__USB_K50__) set_tris_a(get_tris_a() | 0x3); #else set_tris_c(get_tris_c() | 0x30); //pin_c4 and pin_c5 ——》程序将进入这里(pic18f2550的usb数据端口) #endif usb_state = USB_STATE_DETACHED; usb_token_reset(); //clear the chapter9 stack //__usb_kbhit_status=0; } 注:
usb_state在下面的代码(枚举)中定义,用于指示usb的状态
//Define the states that the USB interface can be in
enum {USB_STATE_DETACHED=0, USB_STATE_ATTACHED=1, USB_STATE_POWERED=2, USB_STATE_DEFAULT=3, USB_STATE_ADDRESS=4, USB_STATE_CONFIGURED=5} usb_state=0; 注:__UCFG_VAL_DISABLED__在前面声明为:
#if defined(__USB_UCFG_UTRDIS) #define __UCFG_VAL_DISABLED__ __USB_UCFG_UTRDIS #else #define __UCFG_VAL_DISABLED__ 0 #endif 4.函数中调用usb_token_reset(); //clear the chapter9 stack
函数声明在:usb.h中
函数体在:usb.c中
函数体为:
void usb_token_reset(void)
{ unsigned int i; for (i=0;i<USB_MAX_NUM_INTERFACES;i++)
USB_Interface[i] = 0; //reset each interface to default #IF USB_HID_BOOT_PROTOCOL
for (i=0;i<USB_NUM_HID_INTERFACES; i++) hid_protocol[i] = 1; #endif #if USB_CDC_DEVICE
usb_cdc_init(); #endif USB_stack_status.curr_config = 0; //unconfigured device
USB_stack_status.status_device = 1; //previous state. init at none
USB_stack_status.dev_req = NONE; //previous token request state. init at none } 注:NUM_INTERFACES defined with descriptors
USB_Interface在下面的代码中声明: int8 USB_Interface[USB_MAX_NUM_INTERFACES]; //config state for all of our interfaces, NUM_INTERFACES defined with descriptors
USB_stack_status在下面的代码中声明:
TYPE_USB_STACK_STATUS USB_stack_status;
5.usb_init_cs()之后
do
{ usb_task(); } while (usb_state != USB_STATE_POWERED); 调用usb_task();进行USB连接,等待USB连接成功,才跳出循环,完成初始化。
6.调用usb_task();进行USB连接。
函数声明在:usb_hw_layer.h中 函数体在pic18_usb.c中。
函数体为:
void usb_task(void)
{ #if defined(USB_ISR_POLLING) if (interrupt_active(INT_USB)) { usb_isr(); } #endif if (usb_attached())
{ if (UCON_USBEN==0) { debug_usb(debug_putc, "\r\n\nUSB TASK: ATTACH"); usb_attach(); } } else { if (UCON_USBEN==1) { debug_usb(debug_putc, "\r\n\nUSB TASK: DE-ATTACH"); usb_detach(); } } if ((usb_state == USB_STATE_ATTACHED)&&(!UCON_SE0))
{ UIR=0; UIE=0; #if !defined(USB_ISR_POLLING) enable_interrupts(INT_USB); enable_interrupts(GLOBAL); #endif UIE=__USB_UIF_IDLE | __USB_UIF_RESET; //enable IDLE and RESET USB ISR usb_state=USB_STATE_POWERED; debug_usb(debug_putc, "\r\n\nUSB TASK: POWERED"); } } 注:USB.H中关于usb_task()的解释:(USB没有连接的话会尝试重新连接)
//// usb_task() - If usb_init_cs() was used to initiate the USB ////
//// peripheral, usb_task() should then be called periodically //// //// to check the connection sense pin. If the connection //// //// sense pin denotes USB is connected and the USB peripheral //// //// is not attached, this will attach the USB peripheral //// //// so the PC can start the enumeration process (and it //// //// will enable interrupts). If the connection sense pin //// //// denotes USB is not attached and the USB peripheral is //// //// running, this will reset the USB peripheral and wait //// //// for USB to reconnect (and usb_enumerated() will start //// //// returning FALSE). If connection sense macro //// //// (USB_CABLE_IS_ATTACHED) is not defined the usb_task() //// //// assumes that USB is always connected. //// 7.调用usb_detach();连接USB 函数声明在:usb_hw_layer.h中 函数体在pic18_usb.c中。
函数体为:
void usb_attach(void)
{ usb_token_reset(); UCON = 0; UCFG = __UCFG_VAL_ENABLED__; UIE = 0; // Mask all USB interrupts UCON_USBEN = 1; // Enable module & attach to bus usb_state = USB_STATE_ATTACHED; // Defined in usbmmap.c & .h } 至此便完成了USB的初始化。要注意函数之间的调用关系。 |
|