新聞中心

EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > 初涉USB,初學(xué)者USB入門總結(jié)(2) 設(shè)備固件程序

初涉USB,初學(xué)者USB入門總結(jié)(2) 設(shè)備固件程序

作者: 時(shí)間:2016-12-14 來源:網(wǎng)絡(luò) 收藏
為了更好的說明整個(gè)USB啟動(dòng)過程,我們可以用串口實(shí)時(shí)的跟蹤各個(gè)USB中斷。不過這里先不用串口進(jìn)行測(cè)試,只是簡(jiǎn)單的用一組變量記錄過程。測(cè)試程序如下(以下會(huì)有程序的說明):

uchar test[100];//100長(zhǎng)度的變量,記錄過程
uchar conters=0;//記錄計(jì)數(shù)值,
/*------------------------------------------------------------
高校電子聯(lián)盟--肖繼達(dá)
QQ:258347765
-------------------------------------------------------------*/

void EXT_int(void)//USB中斷響應(yīng)函數(shù)
{
/*------------------------------------------------------------
Check interrupt status register to know interrupt
source.
------------------------------------------------------------*/

if(USB_BUSRESET_ASS_INT())
{/* USB bus reset */
/*for USB Rev.1.1
After USB bus reset released, 10msec recoverly time we have.
Follwing request must be processed normally.
*/

CLR_BUS_RESET_STATE();/* USB bus reset status clear */

/*------------------------------------------------------------
Endpoint0 setting
------------------------------------------------------------*/
/* Tx/Rx payload size setting */
/* Rx payload is fixed as 8-byte or 32-byte, therefor the
setting has no meaninig */

SET_PAYLOAD_EPn(EP0RX, device_deor.bMaxPacketSize0);
SET_PAYLOAD_EPn(EP0TX, device_deor.bMaxPacketSize0);
/* Stall bit, the value undefined after reset, cleared */
CLR_STALL_EPn(EP0);

/*------------------------------------------------------------
Misceronous status variable initialization
------------------------------------------------------------*/
usb_status.configuration = NULL;
usb_status.remote_wakeup = 0;
usb_status.address = 0;
usb_status.dvcstate = DEFAULT_STATE;/* Device state :DEFAULT */
usb_status.stall_req = 0;
#ifdef Debug
test[conters]=!;
conters++;
#endif

/*------------------------------------------------------------
Callback to application layer
------------------------------------------------------------*/
(*usb_status.callback)();
}
else if (SUSPENDED_INT())
{/* suspended state */
/* for USB Rev.1.1
Transit to suspended state after detect the USB line has kept idle over 3msec.
After resume detected, end suspend state in 3msec to be able to respond
the host request.
*/
CLR_SUSPENDED_STATE();
#ifdef Debug
test[conters]=@;
conters++;
#endif

}

else if(AWAKE_INT())
{/* Deveice awake state */
/* AWAKE procedure */

CLR_AWAKE_STATE();/* Request clear */
#ifdef Debug
test[conters]=#;
conters++;
#endif

}
else if(USB_BUSRESET_DES_INT())
{/* USB bus reset deassert */
/* Procedure for USB bus reset de-assert */

CLR_BUS_RESET_DES_STATE();/* Request clear */
#ifdef Debug
test[conters]=$;
conters++;
#endif

}

else if(SOF_INT())
{/* SOF interrupt status */
CLR_B_SOF_STATE();
#ifdef Debug
test[conters]=%;
conters++;
#endif
/* SOF interrupt status clear */
}/* SOF interrupt status */

if(SETUP_RDY_INT())
{/* setup ready */
#ifdef Debug
test[conters]=^;
conters++;
#endif

read_Device_Requests();
}

else if(EP1_PKTRDY_INT())
{/* EP1 packet ready */
read_FIFO(EP1);
}
else if(EP2_PKTRDY_INT())
{/* EP2 packet ready */

write_FIFO(EP2);
}

else if(EP0_RXPKTRDY_INT())
{/* EP0 receive packet ready */
read_FIFO(EP0RX);
}
else if(EP0_TXPKTRDY_INT())
{/* EP0 transmit packet ready */
write_FIFO(EP0TX);
}

}
計(jì)錄的結(jié)果在變量查看中顯示如下:

首先我解釋一下,這段程序是我在做USB設(shè)備時(shí)的中斷函數(shù)。主控(就是你往里面寫固件程序的那個(gè)東西)會(huì)在要求設(shè)備進(jìn)行操作時(shí),產(chǎn)生一個(gè)相應(yīng)的中斷(我們可以用中斷的方式,也可以用查詢的方式,中斷的方式的好處就是主機(jī)有需要操作的都會(huì)叫你,而用查詢你必須不斷的問主機(jī)“有事么”,這里采用中斷方式),比如主機(jī)給設(shè)備設(shè)置地址,主機(jī)會(huì)通過固定的通道(point0)發(fā)送一個(gè)‘設(shè)定地址’包,設(shè)備主控接到包后會(huì)產(chǎn)生中斷,并且把響應(yīng)的狀態(tài)保存在相應(yīng)的寄存器中,我們只要在中斷程序判斷各個(gè)寄存器就能完成主機(jī)的任務(wù)。
程序中藍(lán)色字是中斷類型的判斷,其對(duì)應(yīng)的宏定義就不列出來了。如果是這個(gè)中斷就會(huì)執(zhí)行相應(yīng)的中斷操作。并且一次中斷只有一種中斷類型,我們?cè)诿總€(gè)中斷響應(yīng)中加一段紅色字的程序,是為了保存每次中斷的狀態(tài),比如剛插上設(shè)備,來了一次BUSRESET總線復(fù)位中斷,就會(huì)進(jìn)入相應(yīng)的中斷操作,完了后記錄狀態(tài)test[conters]=!; conters++;意思是進(jìn)入了這個(gè)中斷就在這一組數(shù)的當(dāng)前位置設(shè)成!,并且位置記錄的變量加一,以便下一次記錄到下一個(gè)位置。這樣USB的過程我們就記錄了下來,
下面看一下記錄結(jié)果(其中的數(shù)字和字母是響應(yīng)標(biāo)準(zhǔn)請(qǐng)求時(shí)的程序產(chǎn)生的這里不羅列程序了)。


評(píng)論


技術(shù)專區(qū)

關(guān)閉