📄 usb.h
字号:
/*F**************************************************************************
* NAME: usb_clock_init
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
*----------------------------------------------------------------------------
* PURPOSE:
* This function initializes the USB clock and PLL .
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
void clock_usb_init(void)
{SetPLL(26,2);
EnPLL();
Usb_set_clock(2);
}
/*F**************************************************************************
* NAME: usb_init
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
*----------------------------------------------------------------------------
* PURPOSE:
* This function initializes the USB controller and resets the endpoints FIFOs.
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
void usb_init (void)
{
Usb_enable(); /* enable USB */
UEPRST = 0x07; /* Reset EP 0, 1 and 2 */
UEPRST = 0x00;
endpoint_status[EP_CONTROL] = 0x00;
endpoint_status[EP_IN] = 0x00;
endpoint_status[EP_OUT] = 0x00;
Usb_select_ep(EP_CONTROL); /* control endpoint config */
UEPCONX = CONTROL;
}
/*F**************************************************************************
* NAME: usb_ep_init
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
*----------------------------------------------------------------------------
* PURPOSE:
* This function configures the endpoints.
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
void usb_ep_init (void)
{
Usb_select_ep(EP_CONTROL);
UEPCONX = CONTROL;
Usb_select_ep(EP_IN); /* endpoints configuration */
UEPCONX = BULK_IN ;
Usb_select_ep(EP_OUT);
UEPCONX = BULK_OUT;
UEPRST = 0x07;
UEPRST = 0x00;
}
/*F**************************************************************************
* NAME: usb_send_ep0_packet
*----------------------------------------------------------------------------
* PARAMS:
* *tbuf: address of the first data to send
* data_length: number of bytes to send
*
* return: address of the next byte to send
*----------------------------------------------------------------------------
* PURPOSE:
* This function sends the data over the default control endpoint.
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
Byte* send_ep0_packet (Byte *tbuf, Byte data_length)
{
Byte i;
Usb_select_ep(EP_CONTROL);
for (i = data_length; i != 0 ; i--, tbuf++)
{
Usb_write_byte(*tbuf);
}
Usb_set_TXRDY(); /* Send packet */
return tbuf;
}
/*F**************************************************************************
* NAME: usb_enumeration_process
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
*----------------------------------------------------------------------------
* PURPOSE:
* This function manages the enumeration process
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
void usb_enumeration_process (void)
{
Usb_select_ep(EP_CONTROL);
usb_read_request();
}
/*F**************************************************************************
* NAME: usb_read_request
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
*----------------------------------------------------------------------------
* PURPOSE:
* This function reads the SETUP request sent to the default control endpoint
* and the appropriate function. When exiting of the usb_read_request
* function, the device is ready to manage the next request.
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE: list of supported requests:
* GET_DESCRIPTOR
* GET_CONFIGURATION
* SET_ADDRESS
* SET_CONFIGURATION
* CLEAR_FEATURE
* SET_FEATURE
* GET_STATUS
* GET_MAX_LUN
* MASS_STORAGE_RESET
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
void usb_read_request (void)
{
bmRequestType = Usb_read_byte(); /* read bmRequestType */
switch (Usb_read_byte()) /* test the bRequest value */
{
case GET_DESCRIPTOR:
usb_get_descriptor();
break;
case GET_CONFIGURATION:
usb_get_configuration();
break;
case SET_ADDRESS:
usb_set_address();
break;
case SET_CONFIGURATION:
usb_set_configuration();
break;
case CLEAR_FEATURE:
usb_clear_feature();
break;
case SET_FEATURE:
usb_set_feature();
break;
case GET_STATUS:
usb_get_status();
break;
case GET_MAX_LUN:
usb_mass_storage_get_lun();
break;
case MASS_STORAGE_RESET:
usb_mass_storage_reset();
break;
default:
Usb_clear_RXSETUP();
Usb_set_STALLRQ();
while (!Usb_STALL_sent());
Usb_clear_STALLRQ();
Usb_clear_STALLED();
break;
}
}
/*F**************************************************************************
* NAME: usb_set_address
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
*----------------------------------------------------------------------------
* PURPOSE:
* This function manages the SET_ADDRESS request. The new address is stored
* in the USBADDR register
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
void usb_set_address (void)
{
Byte add;
Usb_clear_DIR();
add = Usb_read_byte(); /* store the LSB of wValue = address */
UEPRST = 0x01 ;
UEPRST = 0x00 ;
Usb_clear_RXSETUP();
Usb_set_TXRDY(); /* send a ZLP for STATUS phase */
Usb_set_FADDEN();
while (!(Usb_tx_complete()));
Usb_clear_TXCMPL();
Usb_configure_address(add);
}
/*F**************************************************************************
* NAME: usb_set_configuration
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
*----------------------------------------------------------------------------
* PURPOSE:
* This function manages the SET_CONFIGURATION request.
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
void usb_set_configuration (void)
{
Uchar configuration_number;
configuration_number = Usb_read_byte();
Usb_clear_DIR();
Usb_clear_RXSETUP();
Usb_set_TXRDY(); /* send a ZLP for STATUS phase */
if (configuration_number == 0)
{
Usb_clear_CONFG();
}
else
{
Usb_set_CONFG();
}
while (!Usb_tx_complete());
Usb_clear_TXCMPL();
Usb_select_ep(EP_IN); /* endpoints configuration */
UEPCONX = BULK_IN ;
Usb_select_ep(EP_OUT);
UEPCONX = BULK_OUT;
}
/*F**************************************************************************
* NAME: usb_get_descriptor
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
*----------------------------------------------------------------------------
* PURPOSE:
* This function manages the GET_DESCRIPTOR request.
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
void usb_get_descriptor (void)
{
Byte data_to_transfer;
Uint16 wLength;
Byte descriptor_type;
Byte string_type;
Usb_set_DIR(); /* set out on EP0 */
zlp = FALSE; /* no zero length packet */
string_type = Usb_read_byte(); /* read LSB of wValue */
descriptor_type = Usb_read_byte(); /* read MSB of wValue */
switch (descriptor_type)
{
case DEVICE:
{
data_to_transfer = sizeof (usb_device_descriptor);
pbuffer = &(usb_device_descriptor.bLength);
break;
}
case CONFIGURATION:
{
data_to_transfer = sizeof (usb_configuration);
pbuffer = &(usb_configuration.cfg.bLength);
break;
}
case STRING:
{
switch (string_type)
{
case LANG_ID:
{
data_to_transfer = sizeof (usb_language);
pbuffer = &(usb_language.bLength);
break;
}
case MAN_INDEX:
{
data_to_transfer = sizeof (usb_manufacturer);
pbuffer = &(usb_manufacturer.bLength);
break;
}
case PROD_INDEX:
{
data_to_transfer = sizeof (usb_product);
pbuffer = &(usb_product.bLength);
break;
}
case SN_INDEX:
{
data_to_transfer = sizeof (usb_serial_number);
pbuffer = &(usb_serial_number.bLength);
break;
}
default:
{
Usb_clear_RXSETUP();
while (!Usb_setup_received())
{
Usb_set_STALLRQ(); /* send stall */
while ((!(Usb_STALL_sent())) && (Usb_setup_received()));
Usb_clear_STALLED();
Usb_clear_STALLRQ();
}
Usb_clear_DIR(); /* set in on EP0 */
return;
}
}
break;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -