pikapython/bsp/ch582/StdPeriphDriver/CH58x_usbhostBase.c

691 lines
21 KiB
C
Raw Normal View History

2022-03-26 20:51:40 +08:00
/********************************** (C) COPYRIGHT *******************************
* File Name : CH58x_usbhost.c
* Author : WCH
* Version : V1.2
* Date : 2021/11/17
* Description
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
* SPDX-License-Identifier: Apache-2.0
*******************************************************************************/
#include "CH58x_common.h"
#if DISK_LIB_ENABLE
#include "CHRV3UFI.H"
#endif
uint8_t UsbDevEndp0Size; // USB<53><EFBFBD>Ķ˵<C4B6>0<EFBFBD><30><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ߴ<EFBFBD>
uint8_t FoundNewDev;
_RootHubDev ThisUsbDev; //ROOT<4F><54>
_DevOnHubPort DevOnHubPort[HUB_MAX_PORTS]; // <20>ٶ<EFBFBD>:<3A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>1<EFBFBD><31><EFBFBD>ⲿHUB,ÿ<><C3BF><EFBFBD>ⲿHUB<55><42><EFBFBD><EFBFBD><EFBFBD><EFBFBD>HUB_MAX_PORTS<54><53><EFBFBD>˿<EFBFBD>(<28><><EFBFBD>˲<EFBFBD><CBB2><EFBFBD>)
uint8_t *pHOST_RX_RAM_Addr;
uint8_t *pHOST_TX_RAM_Addr;
/*<2A><>ȡ<EFBFBD><EFBFBD><E8B1B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
__attribute__((aligned(4))) const uint8_t SetupGetDevDescr[] = {USB_REQ_TYP_IN, USB_GET_DESCRIPTOR, 0x00,
USB_DESCR_TYP_DEVICE, 0x00, 0x00, sizeof(USB_DEV_DESCR), 0x00};
/*<2A><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
__attribute__((aligned(4))) const uint8_t SetupGetCfgDescr[] = {USB_REQ_TYP_IN, USB_GET_DESCRIPTOR, 0x00,
USB_DESCR_TYP_CONFIG, 0x00, 0x00, 0x04, 0x00};
/*<2A><><EFBFBD><EFBFBD>USB<53><42>ַ*/
__attribute__((aligned(4))) const uint8_t SetupSetUsbAddr[] = {USB_REQ_TYP_OUT, USB_SET_ADDRESS, USB_DEVICE_ADDR, 0x00,
0x00, 0x00, 0x00, 0x00};
/*<2A><><EFBFBD><EFBFBD>USB<53><42><EFBFBD><EFBFBD>*/
__attribute__((aligned(4))) const uint8_t SetupSetUsbConfig[] = {USB_REQ_TYP_OUT, USB_SET_CONFIGURATION, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00};
/*<2A><><EFBFBD><EFBFBD>USB<53>ӿ<EFBFBD><D3BF><EFBFBD><EFBFBD><EFBFBD>*/
__attribute__((aligned(4))) const uint8_t SetupSetUsbInterface[] = {USB_REQ_RECIP_INTERF, USB_SET_INTERFACE, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00};
/*<2A><><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD>STALL*/
__attribute__((aligned(4))) const uint8_t SetupClrEndpStall[] = {USB_REQ_TYP_OUT | USB_REQ_RECIP_ENDP, USB_CLEAR_FEATURE,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
/*********************************************************************
* @fn DisableRootHubPort
*
* @brief <EFBFBD>ر<EFBFBD>ROOT-HUB<EFBFBD>˿<EFBFBD>,ʵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӳ<EFBFBD><EFBFBD><EFBFBD>Ѿ<EFBFBD><EFBFBD>Զ<EFBFBD><EFBFBD>ر<EFBFBD>,<EFBFBD>˴<EFBFBD>ֻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һЩ<EFBFBD>״̬
*
* @param none
*
* @return none
*/
void DisableRootHubPort(void)
{
#ifdef FOR_ROOT_UDISK_ONLY
CHRV3DiskStatus = DISK_DISCONNECT;
#endif
#ifndef DISK_BASE_BUF_LEN
ThisUsbDev.DeviceStatus = ROOT_DEV_DISCONNECT;
ThisUsbDev.DeviceAddress = 0x00;
#endif
}
/*********************************************************************
* @fn AnalyzeRootHub
*
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ROOT-HUB״̬,<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ROOT-HUB<EFBFBD>˿ڵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD>
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>γ<EFBFBD>,<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><EFBFBD><EFBFBD>DisableRootHubPort()<EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<EFBFBD><EFBFBD><EFBFBD>˿ڹر<EFBFBD>,<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD>,<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD>˿ڵ<EFBFBD>״̬λ
*
* @param none
*
* @return <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ERR_SUCCESSΪû<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ERR_USB_CONNECTΪ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ERR_USB_DISCONΪ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ͽ<EFBFBD>
*/
uint8_t AnalyzeRootHub(void)
{
uint8_t s;
s = ERR_SUCCESS;
if(R8_USB_MIS_ST & RB_UMS_DEV_ATTACH)
{ // <20><EFBFBD><E8B1B8><EFBFBD><EFBFBD>
#ifdef DISK_BASE_BUF_LEN
if(CHRV3DiskStatus == DISK_DISCONNECT
#else
if(ThisUsbDev.DeviceStatus == ROOT_DEV_DISCONNECT // <20><><EFBFBD><EFBFBD><E2B5BD><EFBFBD><EFBFBD><E8B1B8><EFBFBD><EFBFBD>
#endif
|| (R8_UHOST_CTRL & RB_UH_PORT_EN) == 0x00)
{ // <20><><EFBFBD><EFBFBD><E2B5BD><EFBFBD><EFBFBD><E8B1B8><EFBFBD><EFBFBD>,<2C><><EFBFBD><EFBFBD>δ<EFBFBD><CEB4><EFBFBD><EFBFBD><><CBB5><EFBFBD>Ǹղ<C7B8><D5B2><EFBFBD>
DisableRootHubPort(); // <20>رն˿<D5B6>
#ifdef DISK_BASE_BUF_LEN
CHRV3DiskStatus = DISK_CONNECT;
#else
ThisUsbDev.DeviceSpeed = R8_USB_MIS_ST & RB_UMS_DM_LEVEL ? 0 : 1;
ThisUsbDev.DeviceStatus = ROOT_DEV_CONNECTED; //<2F><><EFBFBD><EFBFBD><EFBFBD>ӱ<EFBFBD>־
#endif
PRINT("USB dev in\n");
s = ERR_USB_CONNECT;
}
}
#ifdef DISK_BASE_BUF_LEN
else if(CHRV3DiskStatus >= DISK_CONNECT)
{
#else
else if(ThisUsbDev.DeviceStatus >= ROOT_DEV_CONNECTED)
{ //<2F><><EFBFBD><EFBFBD><EFBFBD>γ<EFBFBD>
#endif
DisableRootHubPort(); // <20>رն˿<D5B6>
PRINT("USB dev out\n");
if(s == ERR_SUCCESS)
{
s = ERR_USB_DISCON;
}
}
// R8_USB_INT_FG = RB_UIF_DETECT; // <20><><EFBFBD>жϱ<D0B6>־
return (s);
}
/*********************************************************************
* @fn SetHostUsbAddr
*
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD>USB<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>USB<EFBFBD><EFBFBD><EFBFBD>ַ
*
* @param addr - USB<EFBFBD><EFBFBD><EFBFBD>ַ
*
* @return none
*/
void SetHostUsbAddr(uint8_t addr)
{
R8_USB_DEV_AD = (R8_USB_DEV_AD & RB_UDA_GP_BIT) | (addr & MASK_USB_ADDR);
}
/*********************************************************************
* @fn SetUsbSpeed
*
* @brief <EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD>ǰUSB<EFBFBD>ٶ<EFBFBD>
*
* @param FullSpeed - USB<EFBFBD>ٶ<EFBFBD>
*
* @return none
*/
void SetUsbSpeed(uint8_t FullSpeed)
{
#ifndef DISK_BASE_BUF_LEN
if(FullSpeed) // ȫ<><C8AB>
{
R8_USB_CTRL &= ~RB_UC_LOW_SPEED; // ȫ<><C8AB>
R8_UH_SETUP &= ~RB_UH_PRE_PID_EN; // <20><>ֹPRE PID
}
else
{
R8_USB_CTRL |= RB_UC_LOW_SPEED; // <20><><EFBFBD><EFBFBD>
}
#endif
(void)FullSpeed;
}
/*********************************************************************
* @fn ResetRootHubPort
*
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<EFBFBD><EFBFBD>λ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>,Ϊö<EFBFBD><EFBFBD><EFBFBD>׼<EFBFBD><EFBFBD>,<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ΪĬ<EFBFBD><EFBFBD>Ϊȫ<EFBFBD><EFBFBD>
*
* @param none
*
* @return none
*/
void ResetRootHubPort(void)
{
UsbDevEndp0Size = DEFAULT_ENDP0_SIZE; //USB<53><EFBFBD>Ķ˵<C4B6>0<EFBFBD><30><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ߴ<EFBFBD>
SetHostUsbAddr(0x00);
R8_UHOST_CTRL &= ~RB_UH_PORT_EN; // <20>ص<EFBFBD><D8B5>˿<EFBFBD>
SetUsbSpeed(1); // Ĭ<><C4AC>Ϊȫ<CEAA><C8AB>
R8_UHOST_CTRL = (R8_UHOST_CTRL & ~RB_UH_LOW_SPEED) | RB_UH_BUS_RESET; // Ĭ<><C4AC>Ϊȫ<CEAA><C8AB>,<2C><>ʼ<EFBFBD><CABC>λ
mDelaymS(15); // <20><>λʱ<CEBB><CAB1>10mS<6D><53>20mS
R8_UHOST_CTRL = R8_UHOST_CTRL & ~RB_UH_BUS_RESET; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ
mDelayuS(250);
R8_USB_INT_FG = RB_UIF_DETECT; // <20><><EFBFBD>жϱ<D0B6>־
}
/*********************************************************************
* @fn EnableRootHubPort
*
* @brief ʹ<EFBFBD><EFBFBD>ROOT-HUB<EFBFBD>˿<EFBFBD>,<EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD>bUH_PORT_EN<EFBFBD><EFBFBD>1<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˿<EFBFBD>,<EFBFBD><EFBFBD>Ͽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܵ<EFBFBD><EFBFBD>·<EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD><EFBFBD>
*
* @param none
*
* @return <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ERR_SUCCESSΪ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ERR_USB_DISCONΪ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
uint8_t EnableRootHubPort(void)
{
#ifdef DISK_BASE_BUF_LEN
if(CHRV3DiskStatus < DISK_CONNECT)
CHRV3DiskStatus = DISK_CONNECT;
#else
if(ThisUsbDev.DeviceStatus < ROOT_DEV_CONNECTED)
ThisUsbDev.DeviceStatus = ROOT_DEV_CONNECTED;
#endif
if(R8_USB_MIS_ST & RB_UMS_DEV_ATTACH)
{ // <20><><EFBFBD>
#ifndef DISK_BASE_BUF_LEN
if((R8_UHOST_CTRL & RB_UH_PORT_EN) == 0x00)
{ // <20><>δʹ<CEB4><CAB9>
ThisUsbDev.DeviceSpeed = (R8_USB_MIS_ST & RB_UMS_DM_LEVEL) ? 0 : 1;
if(ThisUsbDev.DeviceSpeed == 0)
{
R8_UHOST_CTRL |= RB_UH_LOW_SPEED; // <20><><EFBFBD><EFBFBD>
}
}
#endif
R8_UHOST_CTRL |= RB_UH_PORT_EN; //ʹ<><CAB9>HUB<55>˿<EFBFBD>
return (ERR_SUCCESS);
}
return (ERR_USB_DISCON);
}
#ifndef DISK_BASE_BUF_LEN
/*********************************************************************
* @fn SelectHubPort
*
* @brief ѡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>HUB<EFBFBD><EFBFBD>
*
* @param HubPortIndex - ѡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ROOT-HUB<EFBFBD>˿ڵ<EFBFBD><EFBFBD>ⲿHUB<EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD>˿<EFBFBD>
*
* @return None
*/
void SelectHubPort(uint8_t HubPortIndex)
{
if(HubPortIndex) // ѡ<><D1A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD>ROOT-HUB<55>˿ڵ<CBBF><DAB5>ⲿHUB<55><42>ָ<EFBFBD><D6B8><EFBFBD>˿<EFBFBD>
{
SetHostUsbAddr(DevOnHubPort[HubPortIndex - 1].DeviceAddress); // <20><><EFBFBD><EFBFBD>USB<53><42><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD>USB<53><EFBFBD><E8B1B8>ַ
SetUsbSpeed(DevOnHubPort[HubPortIndex - 1].DeviceSpeed); // <20><><EFBFBD>õ<EFBFBD>ǰUSB<53>ٶ<EFBFBD>
if(DevOnHubPort[HubPortIndex - 1].DeviceSpeed == 0) // ͨ<><CDA8><EFBFBD>ⲿHUB<55><42><EFBFBD><EFBFBD><EFBFBD><EFBFBD>USB<53>豸ͨѶ<CDA8><D1B6>Ҫǰ<D2AA><C7B0>ID
{
R8_UEP1_CTRL |= RB_UH_PRE_PID_EN; // <20><><EFBFBD><EFBFBD>PRE PID
mDelayuS(100);
}
}
else
{
SetHostUsbAddr(ThisUsbDev.DeviceAddress); // <20><><EFBFBD><EFBFBD>USB<53><42><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD>USB<53><EFBFBD><E8B1B8>ַ
SetUsbSpeed(ThisUsbDev.DeviceSpeed); // <20><><EFBFBD><EFBFBD>USB<53><EFBFBD><E8B1B8><EFBFBD>ٶ<EFBFBD>
}
}
#endif
/*********************************************************************
* @fn WaitUSB_Interrupt
*
* @brief <EFBFBD>ȴ<EFBFBD>USB<EFBFBD>ж<EFBFBD>
*
* @param none
*
* @return <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ERR_SUCCESS <EFBFBD><EFBFBD><EFBFBD>ݽ<EFBFBD><EFBFBD>ջ<EFBFBD><EFBFBD>߷<EFBFBD><EFBFBD>ͳɹ<EFBFBD>,<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ERR_USB_UNKNOWN <EFBFBD><EFBFBD><EFBFBD>ݽ<EFBFBD><EFBFBD>ջ<EFBFBD><EFBFBD>߷<EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD><EFBFBD>
*/
uint8_t WaitUSB_Interrupt(void)
{
uint16_t i;
for(i = WAIT_USB_TOUT_200US; i != 0 && (R8_USB_INT_FG & RB_UIF_TRANSFER) == 0; i--)
{
;
}
return ((R8_USB_INT_FG & RB_UIF_TRANSFER) ? ERR_SUCCESS : ERR_USB_UNKNOWN);
}
/*********************************************************************
* @fn USBHostTransact
*
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŀ<EFBFBD>Ķ˵<EFBFBD><EFBFBD><EFBFBD>ַ/PID<EFBFBD><EFBFBD><EFBFBD><EFBFBD>,ͬ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>־,<EFBFBD><EFBFBD>20u<EFBFBD><EFBFBD>λ<EFBFBD><EFBFBD>NAK<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD>(0<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,0xFFFF<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>),<EFBFBD><EFBFBD><EFBFBD><EFBFBD>0<EFBFBD>ɹ<EFBFBD>,<EFBFBD><EFBFBD>ʱ/<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD><EFBFBD>ӳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>,Ϊ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ٶ<EFBFBD>,Ӧ<EFBFBD>öԱ<EFBFBD><EFBFBD>ӳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ż<EFBFBD>
*
* @param endp_pid - <EFBFBD><EFBFBD><EFBFBD>ƺ͵<EFBFBD>ַ, <EFBFBD><EFBFBD>4λ<EFBFBD><EFBFBD>token_pid<EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <EFBFBD><EFBFBD>4λ<EFBFBD>Ƕ˵<EFBFBD><EFBFBD><EFBFBD>ַ
* @param tog - ͬ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>־
* @param timeout - <EFBFBD><EFBFBD>ʱʱ<EFBFBD><EFBFBD>
*
* @return ERR_USB_UNKNOWN <EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӳ<EFBFBD><EFBFBD><EFBFBD>
* ERR_USB_DISCON <EFBFBD><EFBFBD>Ͽ<EFBFBD>
* ERR_USB_CONNECT <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* ERR_SUCCESS <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
uint8_t USBHostTransact(uint8_t endp_pid, uint8_t tog, uint32_t timeout)
{
uint8_t TransRetry;
uint8_t s, r;
uint16_t i;
R8_UH_RX_CTRL = R8_UH_TX_CTRL = tog;
TransRetry = 0;
do
{
R8_UH_EP_PID = endp_pid; // ָ<><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>PID<49><44>Ŀ<EFBFBD>Ķ˵<C4B6><CBB5><EFBFBD>
R8_USB_INT_FG = RB_UIF_TRANSFER;
for(i = WAIT_USB_TOUT_200US; i != 0 && (R8_USB_INT_FG & RB_UIF_TRANSFER) == 0; i--)
{
;
}
R8_UH_EP_PID = 0x00; // ֹͣUSB<53><42><EFBFBD><EFBFBD>
if((R8_USB_INT_FG & RB_UIF_TRANSFER) == 0)
{
return (ERR_USB_UNKNOWN);
}
if(R8_USB_INT_FG & RB_UIF_DETECT)
{ // USB<53><EFBFBD><E8B1B8><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD>
// mDelayuS( 200 ); // <20>ȴ<EFBFBD><C8B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
R8_USB_INT_FG = RB_UIF_DETECT;
s = AnalyzeRootHub(); // <20><><EFBFBD><EFBFBD>ROOT-HUB״̬
if(s == ERR_USB_CONNECT)
FoundNewDev = 1;
#ifdef DISK_BASE_BUF_LEN
if(CHRV3DiskStatus == DISK_DISCONNECT)
{
return (ERR_USB_DISCON);
} // USB<53><EFBFBD>Ͽ<EFBFBD><CFBF>¼<EFBFBD>
if(CHRV3DiskStatus == DISK_CONNECT)
{
return (ERR_USB_CONNECT);
} // USB<53><EFBFBD><E8B1B8><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD>
#else
if(ThisUsbDev.DeviceStatus == ROOT_DEV_DISCONNECT)
{
return (ERR_USB_DISCON);
} // USB<53><EFBFBD>Ͽ<EFBFBD><CFBF>¼<EFBFBD>
if(ThisUsbDev.DeviceStatus == ROOT_DEV_CONNECTED)
{
return (ERR_USB_CONNECT);
} // USB<53><EFBFBD><E8B1B8><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD>
#endif
mDelayuS(200); // <20>ȴ<EFBFBD><C8B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}
if(R8_USB_INT_FG & RB_UIF_TRANSFER) // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD>
{
if(R8_USB_INT_ST & RB_UIS_TOG_OK)
{
return (ERR_SUCCESS);
}
r = R8_USB_INT_ST & MASK_UIS_H_RES; // USB<53>豸Ӧ<E8B1B8><D3A6>״̬
if(r == USB_PID_STALL)
{
return (r | ERR_USB_TRANSFER);
}
if(r == USB_PID_NAK)
{
if(timeout == 0)
{
return (r | ERR_USB_TRANSFER);
}
if(timeout < 0xFFFFFFFF)
{
timeout--;
}
--TransRetry;
}
else
switch(endp_pid >> 4)
{
case USB_PID_SETUP:
case USB_PID_OUT:
if(r)
{
return (r | ERR_USB_TRANSFER);
} // <20><><EFBFBD>dz<EFBFBD>ʱ/<2F><><EFBFBD><EFBFBD>,<2C><><EFBFBD><EFBFBD>Ӧ<EFBFBD><D3A6>
break; // <20><>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>
case USB_PID_IN:
if(r == USB_PID_DATA0 || r == USB_PID_DATA1)
{ // <20><>ͬ<EFBFBD><CDAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E8B6AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
} // <20><>ͬ<EFBFBD><CDAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
else if(r)
{
return (r | ERR_USB_TRANSFER);
} // <20><><EFBFBD>dz<EFBFBD>ʱ/<2F><><EFBFBD><EFBFBD>,<2C><><EFBFBD><EFBFBD>Ӧ<EFBFBD><D3A6>
break; // <20><>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>
default:
return (ERR_USB_UNKNOWN); // <20><><EFBFBD><EFBFBD><EFBFBD>ܵ<EFBFBD><DCB5><EFBFBD><EFBFBD><EFBFBD>
break;
}
}
else
{ // <20><><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>,<2C><>Ӧ<EFBFBD>÷<EFBFBD><C3B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
R8_USB_INT_FG = 0xFF; /* <20><><EFBFBD>жϱ<D0B6>־ */
}
mDelayuS(15);
} while(++TransRetry < 3);
return (ERR_USB_TRANSFER); // Ӧ<><D3A6><EFBFBD><EFBFBD>ʱ
}
/*********************************************************************
* @fn HostCtrlTransfer
*
* @brief ִ<EFBFBD>п<EFBFBD><EFBFBD>ƴ<EFBFBD><EFBFBD><EFBFBD>,8<EFBFBD>ֽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>pSetupReq<EFBFBD><EFBFBD>,DataBufΪ<EFBFBD><EFBFBD>ѡ<EFBFBD><EFBFBD><EFBFBD>շ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*
* @param DataBuf - <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD>պͷ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<EFBFBD><EFBFBD>ôDataBuf<EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ч<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><EFBFBD>ź<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param RetLen - ʵ<EFBFBD>ʳɹ<EFBFBD><EFBFBD>շ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܳ<EFBFBD><EFBFBD>ȱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>RetLenָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֽڱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*
* @return ERR_USB_BUF_OVER IN״̬<EFBFBD>׶γ<EFBFBD><EFBFBD><EFBFBD>
* ERR_SUCCESS <EFBFBD><EFBFBD><EFBFBD>ݽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɹ<EFBFBD>
*/
uint8_t HostCtrlTransfer(uint8_t *DataBuf, uint8_t *RetLen)
{
uint16_t RemLen = 0;
uint8_t s, RxLen, RxCnt, TxCnt;
uint8_t *pBuf;
uint8_t *pLen;
pBuf = DataBuf;
pLen = RetLen;
mDelayuS(200);
if(pLen)
{
*pLen = 0; // ʵ<>ʳɹ<CAB3><C9B9>շ<EFBFBD><D5B7><EFBFBD><EFBFBD>ܳ<EFBFBD><DCB3><EFBFBD>
}
R8_UH_TX_LEN = sizeof(USB_SETUP_REQ);
s = USBHostTransact(USB_PID_SETUP << 4 | 0x00, 0x00, 200000 / 20); // SETUP<55>׶<EFBFBD>,200mS<6D><53>ʱ
if(s != ERR_SUCCESS)
{
return (s);
}
R8_UH_RX_CTRL = R8_UH_TX_CTRL = RB_UH_R_TOG | RB_UH_R_AUTO_TOG | RB_UH_T_TOG | RB_UH_T_AUTO_TOG; // Ĭ<><C4AC>DATA1
R8_UH_TX_LEN = 0x01; // Ĭ<><C4AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݹ<EFBFBD>״̬<D7B4>׶<EFBFBD>ΪIN
RemLen = pSetupReq->wLength;
if(RemLen && pBuf) // <20><>Ҫ<EFBFBD>շ<EFBFBD><D5B7><EFBFBD><EFBFBD><EFBFBD>
{
if(pSetupReq->bRequestType & USB_REQ_TYP_IN) // <20><>
{
while(RemLen)
{
mDelayuS(200);
s = USBHostTransact(USB_PID_IN << 4 | 0x00, R8_UH_RX_CTRL, 200000 / 20); // IN<49><4E><EFBFBD><EFBFBD>
if(s != ERR_SUCCESS)
{
return (s);
}
RxLen = R8_USB_RX_LEN < RemLen ? R8_USB_RX_LEN : RemLen;
RemLen -= RxLen;
if(pLen)
{
*pLen += RxLen; // ʵ<>ʳɹ<CAB3><C9B9>շ<EFBFBD><D5B7><EFBFBD><EFBFBD>ܳ<EFBFBD><DCB3><EFBFBD>
}
for(RxCnt = 0; RxCnt != RxLen; RxCnt++)
{
*pBuf = pHOST_RX_RAM_Addr[RxCnt];
pBuf++;
}
if(R8_USB_RX_LEN == 0 || (R8_USB_RX_LEN & (UsbDevEndp0Size - 1)))
{
break; // <20>̰<EFBFBD>
}
}
R8_UH_TX_LEN = 0x00; // ״̬<D7B4>׶<EFBFBD>ΪOUT
}
else // <20><>
{
while(RemLen)
{
mDelayuS(200);
R8_UH_TX_LEN = RemLen >= UsbDevEndp0Size ? UsbDevEndp0Size : RemLen;
for(TxCnt = 0; TxCnt != R8_UH_TX_LEN; TxCnt++)
{
pHOST_TX_RAM_Addr[TxCnt] = *pBuf;
pBuf++;
}
s = USBHostTransact(USB_PID_OUT << 4 | 0x00, R8_UH_TX_CTRL, 200000 / 20); // OUT<55><54><EFBFBD><EFBFBD>
if(s != ERR_SUCCESS)
{
return (s);
}
RemLen -= R8_UH_TX_LEN;
if(pLen)
{
*pLen += R8_UH_TX_LEN; // ʵ<>ʳɹ<CAB3><C9B9>շ<EFBFBD><D5B7><EFBFBD><EFBFBD>ܳ<EFBFBD><DCB3><EFBFBD>
}
}
// R8_UH_TX_LEN = 0x01; // ״̬<D7B4>׶<EFBFBD>ΪIN
}
}
mDelayuS(200);
s = USBHostTransact((R8_UH_TX_LEN ? USB_PID_IN << 4 | 0x00 : USB_PID_OUT << 4 | 0x00), RB_UH_R_TOG | RB_UH_T_TOG, 200000 / 20); // STATUS<55>׶<EFBFBD>
if(s != ERR_SUCCESS)
{
return (s);
}
if(R8_UH_TX_LEN == 0)
{
return (ERR_SUCCESS); // ״̬OUT
}
if(R8_USB_RX_LEN == 0)
{
return (ERR_SUCCESS); // ״̬IN,<2C><><EFBFBD><EFBFBD>IN״̬<D7B4><CCAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD><DDB3><EFBFBD>
}
return (ERR_USB_BUF_OVER); // IN״̬<D7B4>׶δ<D7B6><CEB4><EFBFBD>
}
/*********************************************************************
* @fn CopySetupReqPkg
*
* @brief <EFBFBD><EFBFBD><EFBFBD>ƿ<EFBFBD><EFBFBD>ƴ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*
* @param pReqPkt - <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ
*
* @return none
*/
void CopySetupReqPkg(const uint8_t *pReqPkt) // <20><><EFBFBD>ƿ<EFBFBD><C6BF>ƴ<EFBFBD><C6B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
{
uint8_t i;
for(i = 0; i != sizeof(USB_SETUP_REQ); i++)
{
((uint8_t *)pSetupReq)[i] = *pReqPkt;
pReqPkt++;
}
}
/*********************************************************************
* @fn CtrlGetDeviceDescr
*
* @brief <EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> pHOST_TX_RAM_Addr <EFBFBD><EFBFBD>
*
* @param none
*
* @return ERR_USB_BUF_OVER <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȴ<EFBFBD><EFBFBD><EFBFBD>
* ERR_SUCCESS <EFBFBD>ɹ<EFBFBD>
*/
uint8_t CtrlGetDeviceDescr(void)
{
uint8_t s;
uint8_t len;
UsbDevEndp0Size = DEFAULT_ENDP0_SIZE;
CopySetupReqPkg((int8_t *)SetupGetDevDescr);
s = HostCtrlTransfer(Com_Buffer, &len); // ִ<>п<EFBFBD><D0BF>ƴ<EFBFBD><C6B4><EFBFBD>
if(s != ERR_SUCCESS)
{
return (s);
}
UsbDevEndp0Size = ((PUSB_DEV_DESCR)Com_Buffer)->bMaxPacketSize0; // <20>˵<EFBFBD>0<EFBFBD><30><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<2C><><EFBFBD>Ǽ򻯴<C7BC><F2BBAFB4><EFBFBD>,<2C><><EFBFBD><EFBFBD>Ӧ<EFBFBD><D3A6><EFBFBD>Ȼ<EFBFBD>ȡǰ8<C7B0>ֽں<D6BD><DABA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>UsbDevEndp0Size<7A>ټ<EFBFBD><D9BC><EFBFBD>
if(len < ((PUSB_SETUP_REQ)SetupGetDevDescr)->wLength)
{
return (ERR_USB_BUF_OVER); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȴ<EFBFBD><C8B4><EFBFBD>
}
return (ERR_SUCCESS);
}
/*********************************************************************
* @fn CtrlGetConfigDescr
*
* @brief <EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> pHOST_TX_RAM_Addr <EFBFBD><EFBFBD>
*
* @param none
*
* @return ERR_USB_BUF_OVER <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȴ<EFBFBD><EFBFBD><EFBFBD>
* ERR_SUCCESS <EFBFBD>ɹ<EFBFBD>
*/
uint8_t CtrlGetConfigDescr(void)
{
uint8_t s;
uint8_t len;
CopySetupReqPkg((int8_t *)SetupGetCfgDescr);
s = HostCtrlTransfer(Com_Buffer, &len); // ִ<>п<EFBFBD><D0BF>ƴ<EFBFBD><C6B4><EFBFBD>
if(s != ERR_SUCCESS)
{
return (s);
}
if(len < ((PUSB_SETUP_REQ)SetupGetCfgDescr)->wLength)
{
return (ERR_USB_BUF_OVER); // <20><><EFBFBD>س<EFBFBD><D8B3>ȴ<EFBFBD><C8B4><EFBFBD>
}
len = ((PUSB_CFG_DESCR)Com_Buffer)->wTotalLength;
CopySetupReqPkg((uint8_t *)SetupGetCfgDescr);
pSetupReq->wLength = len; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܳ<EFBFBD><DCB3><EFBFBD>
s = HostCtrlTransfer(Com_Buffer, &len); // ִ<>п<EFBFBD><D0BF>ƴ<EFBFBD><C6B4><EFBFBD>
if(s != ERR_SUCCESS)
{
return (s);
}
#ifdef DISK_BASE_BUF_LEN
if(len > 64)
len = 64;
memcpy(TxBuffer, Com_Buffer, len); //U<>̲<EFBFBD><CCB2><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>TxBuffer
#endif
return (ERR_SUCCESS);
}
/*********************************************************************
* @fn CtrlSetUsbAddress
*
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD>USB<EFBFBD><EFBFBD><EFBFBD>ַ
*
* @param addr - <EFBFBD><EFBFBD><EFBFBD>ַ
*
* @return ERR_SUCCESS <EFBFBD>ɹ<EFBFBD>
*/
uint8_t CtrlSetUsbAddress(uint8_t addr)
{
uint8_t s;
CopySetupReqPkg((int8_t *)SetupSetUsbAddr);
pSetupReq->wValue = addr; // USB<53><EFBFBD><E8B1B8>ַ
s = HostCtrlTransfer(NULL, NULL); // ִ<>п<EFBFBD><D0BF>ƴ<EFBFBD><C6B4><EFBFBD>
if(s != ERR_SUCCESS)
{
return (s);
}
SetHostUsbAddr(addr); // <20><><EFBFBD><EFBFBD>USB<53><42><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD>USB<53><EFBFBD><E8B1B8>ַ
mDelaymS(10); // <20>ȴ<EFBFBD>USB<53><EFBFBD><E8B1B8><EFBFBD>ɲ<EFBFBD><C9B2><EFBFBD>
return (ERR_SUCCESS);
}
/*********************************************************************
* @fn CtrlSetUsbConfig
*
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD>USB<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*
* @param cfg - <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
*
* @return ERR_SUCCESS <EFBFBD>ɹ<EFBFBD>
*/
uint8_t CtrlSetUsbConfig(uint8_t cfg)
{
CopySetupReqPkg((int8_t *)SetupSetUsbConfig);
pSetupReq->wValue = cfg; // USB<53><EFBFBD><E8B1B8><EFBFBD><EFBFBD>
return (HostCtrlTransfer(NULL, NULL)); // ִ<>п<EFBFBD><D0BF>ƴ<EFBFBD><C6B4><EFBFBD>
}
/*********************************************************************
* @fn CtrlClearEndpStall
*
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD>STALL
*
* @param endp - <EFBFBD>˵<EFBFBD><EFBFBD><EFBFBD>ַ
*
* @return ERR_SUCCESS <EFBFBD>ɹ<EFBFBD>
*/
uint8_t CtrlClearEndpStall(uint8_t endp)
{
CopySetupReqPkg((int8_t *)SetupClrEndpStall); // <20><><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD><CBB5>Ĵ<EFBFBD><C4B4><EFBFBD>
pSetupReq->wIndex = endp; // <20>˵<EFBFBD><CBB5><EFBFBD>ַ
return (HostCtrlTransfer(NULL, NULL)); // ִ<>п<EFBFBD><D0BF>ƴ<EFBFBD><C6B4><EFBFBD>
}
/*********************************************************************
* @fn CtrlSetUsbIntercace
*
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD>USB<EFBFBD><EFBFBD>ӿ<EFBFBD>
*
* @param cfg - <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
*
* @return ERR_SUCCESS <EFBFBD>ɹ<EFBFBD>
*/
uint8_t CtrlSetUsbIntercace(uint8_t cfg)
{
CopySetupReqPkg((int8_t *)SetupSetUsbInterface);
pSetupReq->wValue = cfg; // USB<53><EFBFBD><E8B1B8><EFBFBD><EFBFBD>
return (HostCtrlTransfer(NULL, NULL)); // ִ<>п<EFBFBD><D0BF>ƴ<EFBFBD><C6B4><EFBFBD>
}
/*********************************************************************
* @fn USB_HostInit
*
* @brief USB<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܳ<EFBFBD>ʼ<EFBFBD><EFBFBD>
*
* @param none
*
* @return none
*/
void USB_HostInit(void)
{
R8_USB_CTRL = RB_UC_HOST_MODE;
R8_UHOST_CTRL = 0;
R8_USB_DEV_AD = 0x00;
R8_UH_EP_MOD = RB_UH_EP_TX_EN | RB_UH_EP_RX_EN;
R16_UH_RX_DMA = (uint16_t)(uint32_t)pHOST_RX_RAM_Addr;
R16_UH_TX_DMA = (uint16_t)(uint32_t)pHOST_TX_RAM_Addr;
R8_UH_RX_CTRL = 0x00;
R8_UH_TX_CTRL = 0x00;
R8_USB_CTRL = RB_UC_HOST_MODE | RB_UC_INT_BUSY | RB_UC_DMA_EN;
R8_UH_SETUP = RB_UH_SOF_EN;
R8_USB_INT_FG = 0xFF;
DisableRootHubPort();
R8_USB_INT_EN = RB_UIE_TRANSFER | RB_UIE_DETECT;
FoundNewDev = 0;
}