📄 usb_drv.c
字号:
* PARAMS:
* ep_num: number of the addressed endpoint
* *rbuf: address of the first data to write with the USB data
* data_length: number of bytes to read
* return: address of the next Ucharto send
*----------------------------------------------------------------------------
* PURPOSE:
* This function moves the data stored in the selected endpoint fifo to
* the address specified by *rbuf.
*----------------------------------------------------------------------------
* EXAMPLE:
* while(!(Usb_rx_complete)); // wait new packet received
* usb_read_packet(4,&first_data,usb_get_nb_byte); // read packet from ep 4
* Usb_clear_rx(); // acknowledge the transmit
*----------------------------------------------------------------------------
* NOTE:
* rbuf is incremented of 'data_length'.
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
Uchar* usb_read_packet (Uchar ep_num, Uchar* rbuf, Uchar data_length)
{
Uchar i;
Usb_select_ep(ep_num);
for (i = data_length; i != 0 ; i--, rbuf++) { *rbuf = Usb_read_byte(); }
return rbuf;
}
/*F**************************************************************************
* NAME: usb_halt_endpoint
*----------------------------------------------------------------------------
* PARAMS:
* ep_num: number of the addressed endpoint
* return: none
*----------------------------------------------------------------------------
* PURPOSE:
* This function sends a STALL handshake for the next Host request. A STALL
* handshake will be send for each next request untill a SETUP or a Clear Halt
* Feature occurs for this endpoint.
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
void usb_halt_endpoint (Uchar ep_num)
{
Usb_select_ep(ep_num);
UEPSTAX = 0x00;
Usb_set_stall_request();
}
/*F**************************************************************************
* NAME: usb_reset_endpoint
*----------------------------------------------------------------------------
* PARAMS:
* return: none
*----------------------------------------------------------------------------
* PURPOSE:
* This function resets the endpoint fifo. This should be performed before
* the first use of an endpoint and after a Clear Halt Feature for the
* endpoint
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
void usb_reset_endpoint (Uchar ep_num)
{
UEPRST = 0x01 << ep_num ;
UEPRST = 0x00;
}
/*F**************************************************************************
* NAME: configure_usb_clock
*----------------------------------------------------------------------------
* PARAMS:
* return: none
* The possible value for FOSC are :
* 3000 ( 3MHz)
* 6000 ( 6MHz)
* 8000 ( 8MHz)
* 12000 (12MHz)
* 16000 (16MHz)
* 18000 (18MHz)
* 20000 (20MHz)
* 24000 (24MHz)
* 32000 (32MHz)
* 40000 (40MHz)
* 48000 (48MHz)
* 0000 (frequency auto-dectection)
*----------------------------------------------------------------------------
* PURPOSE:
* This function configure the PLL to generate the 48MHz clock required by
* the USB controller, considering the FOSC defined in the "config.h" file.
*----------------------------------------------------------------------------
* EXAMPLE:
* #define FOSC 8000 // 8MHz
* configure_usb_clock();
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
void configure_usb_clock(void)
{
#if FOSC == 3000
Pll_set_div(PLL_3MHz);
Pll_enable();
#endif
#if FOSC == 4000
Pll_set_div(PLL_4MHz);
Pll_enable();
#endif
#if FOSC == 6000
Pll_set_div(PLL_6MHz);
Pll_enable();
#endif
#if FOSC == 8000
Pll_set_div(PLL_8MHz);
Pll_enable();
#endif
#if FOSC == 12000
Pll_set_div(PLL_12MHz);
Pll_enable();
#endif
#if FOSC == 16000
Pll_set_div(PLL_16MHz);
Pll_enable();
#endif
#if FOSC == 18000
Pll_set_div(PLL_18MHz);
Pll_enable();
#endif
#if FOSC == 20000
Pll_set_div(PLL_20MHz);
Pll_enable();
#endif
#if FOSC == 24000
Pll_set_div(PLL_24MHz);
Pll_enable();
#endif
#if FOSC == 32000
Pll_set_div(PLL_32MHz);
Pll_enable();
#endif
#if FOSC == 40000
Pll_set_div(PLL_40MHz);
Pll_enable();
#endif
#if FOSC == 48000
Usb_set_EXT48()
#endif
#if FOSC == 0000 /* frequency Auto-detection */
Uchar i;
Uchar reload;
PLLCON = 0x00;
if (CKCON0 & MSK_X2)
{
CKCON0 |= MSK_T0X2;
reload = 6;
}
else
{
CKCON0 &= ~MSK_T0X2;
reload = 9;
}
TMOD = 0x01; /* put Timer 0 in mode 1 */
USBINT = 0x00;
i=reload;
while (!(USBINT & MSK_SOFINT)) /* Do until Start Of Frame detection */
{
if (i==9)
{
Pll_stop(); /* external 48 MHz supposed */
Usb_set_EXT48();
}
else
{
Usb_clear_EXT48(); /* PLL output supposed */
Pll_set_div(pll_value[i]); /* configure PLL */
Pll_enable();
}
TH0 = TH0_value[i]; /* Run Timer 0 */
TL0 = TL0_value[i];
TCON |= 0x10;
while(((TCON & 0x20) != 0x20)); /* Wait Timer 0 Overflow */
TCON &= ~(0x30);
if (i==0)
{
i=reload;
}
else
{
i--;
}
}
TH0 = 0; /* Reset Timer 0 Registers */
TL0 = 0;
TCON = 0;
TMOD = 0;
#endif
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -