| 我试着修改了一下,有点问题 1,不停电下载字符串可以正确响应,但是无法重启到usb_Writer那里
 2,接收重发那里,单个包在120以内比较稳定,120-127有时会接收不到,超过端点5最大IN的长度,分包发送时,接收不到任何信息。而且使用wireshark抓包会一直刷IN的信息
 附上代码,main那里在1ms的定时器里加了个msTick++,注释了uart中动态调节主频的,其他修改均在下面代码中明显标注了
 
 复制代码#include "ai8051u.h"
#include "ai_usb_ex.h"
#include "string.h"
#include "uart.h"
#include "CDC_ex_desc.h"
LINECODING LineCoding1;
LINECODING LineCoding2;
BYTE Interface;
BOOL Ep4InBusy;
BOOL Ep5InBusy;
BOOL Ep4OutBusy;
BOOL Ep5OutBusy;
BYTE xdata UsbBuffer[256];
void uart_settings(BYTE index)
{
                Interface = index;
    if (Interface == CDC1_FIRST_INTERFACE)
    {
        LineCoding1.bCharFormat = 0;
        LineCoding1.bDataBits = 8;
    }
                else if (Interface == CDC2_FIRST_INTERFACE)
    {
        LineCoding2.bCharFormat = 0;
        LineCoding2.bDataBits = 8;
    }
    uart_set_parity();
    uart_set_baud();
}
void usb_in_ep4()
{
    BYTE csr;
    usb_write_reg(INDEX, 4);
    csr = usb_read_reg(INCSR1);
    if (csr & INSTSTL)
    {
        usb_write_reg(INCSR1, INCLRDT);
    }
    if (csr & INUNDRUN)
    {
        usb_write_reg(INCSR1, 0);
    }
}
void usb_in_ep5()
{
    BYTE csr;
    usb_write_reg(INDEX, 5);
    csr = usb_read_reg(INCSR1);
    if (csr & INSTSTL)
    {
        usb_write_reg(INCSR1, INCLRDT);
    }
    if (csr & INUNDRUN)
    {
        usb_write_reg(INCSR1, 0);
    }
    Ep5InBusy = 0;
}
////////////////////////////////////////////////////////////////////////
//补两个函数
void usb_write_fifo(BYTE fifo, BYTE *pdat, BYTE cnt)
{
        while (cnt--)
        {
        usb_write_reg(fifo, *pdat++);
    }
}
void _usb_bulk_intr_in(BYTE *pData, BYTE bSize, BYTE ep)
{
    usb_write_fifo((BYTE)(FIFO0 + ep), pData, bSize);
    usb_write_reg(INCSR1, INIPRDY);
}
////////////////////////////////////////////////////////////////////////
void usb_out_ep5()
{
    BYTE csr;
    BYTE cnt,addr,i=0;
    usb_write_reg(INDEX, 5);
    csr = usb_read_reg(OUTCSR1);
    if (csr & OUTSTSTL)
    {
        usb_write_reg(OUTCSR1, OUTCLRDT);
    }
    if (csr & OUTOPRDY)
    {
        cnt = usb_read_reg(OUTCOUNT1);
        while (cnt--)
        {
                        UsbBuffer[i] = usb_read_reg(FIFO5);
            Uart2RxBuffer[Uart2RxWptr++] = UsbBuffer[i];
                        i++;
        }
                /////////////////////////////////////////////////////////
                //判断命令  @STCISP#
                if(i==8) 
                {
                        UsbBuffer[i]=0;
                        if(strcmp(UsbBuffer,"@STCISP#")==0)
                        {
                                EUSB = 0;
                                USBCON = 0x00;
                USBCLK = 0x00;
                IRC48MCR = 0x00;
                msTick=0;//main中1ms定时器计数
                                while(msTick<20);
                    
                                IAP_CONTR = 0x60;
                                while(1);
                        }
                }
                //回发数据
                EUSB=0;
                usb_write_reg(INDEX, 5);
                addr=0;
                do
                {
                        cnt=(i > EP5IN_SIZE) ? EP5IN_SIZE : i;
                        while (usb_read_reg(INCSR1) & INIPRDY);
                        _usb_bulk_intr_in(&UsbBuffer[addr], cnt, 5);
                        addr += cnt;
                        i -= cnt;
                }while(cnt>=EP5IN_SIZE);
                EUSB=1;
                /////////////////////////////////////////////////////////                
                
                
                
                
        if ((BYTE)(Uart2RxWptr - Uart2RxRptr) >= (BYTE)(256 - EP5OUT_SIZE))
        {
            Ep5OutBusy = 1;
        }
        else
        {
            usb_write_reg(OUTCSR1, 0);
        }
    }
}
void initCDCEx(){
                //修改PID
                DEVICEDESC[10] = 0x0a;
                DEVICEDESC[11] = 0xFF;
                set_usb_uart_settings_callback(uart_settings);
                
                //添加CDC,使用EP4 EP5
                usb_add_interface(CDC2_FIRST_INTERFACE,CONFIGDESC_CDC2_HEAD,sizeof(CONFIGDESC_CDC2_HEAD),&LineCoding2);
                usb_add_interface(CDC2_FIRST_INTERFACE+1,CONFIGDESC_CDC2_BODY,sizeof(CONFIGDESC_CDC2_BODY),NULL);
                set_usb_ep_callback(4,64,usb_in_ep4,0,0);
                set_usb_ep_callback(5,128,usb_in_ep5,248,usb_out_ep5);
}
 
 
 |