CH582M_CDC设备请求描述符失败

打了5块样板,同一个程序不断下载测试,有一个板子一直正常,其它板子时好时坏,什么原因呢?怎样查找错误?故障时候扫描不到usb port,设备管理器提示未知设备(设备请求描述符失败)

#include "CH58x_common.h"
#include "main.h"
#include "ads1110.h"
#include "atk_lora_01.h"
extern void InitUSBDevPara(void);
extern void InitUSBDevice(void);
extern void USB_IRQProcessHandler( void );
extern UINT8 SendUSBData(UINT8 *p_send_dat,UINT16 send_len);

extern UINT8 Ep1DataOUTLen ;
extern UINT8 Ep1DataOUTFlag ;
extern UINT8 Ep1OUTDataBuf[MAX_PACKET_SIZE];

uint8_t lora_send_data_process(uint8_t* txbuf,int32_t dat);

//#define LORA_MASTER_MODE
//#define LORA_SLAVE_MODE

volatile uint32_t lora_AUX_flage = FALSE;
uint16_t aux_timeout=1000;
uint8_t ads1110_delay = 10;
int32_t adcValue=0;
uint8_t adc_txBuf[20];
uint8_t lora_txCnt=0;

uint16_t master_add = 0x1000;
uint16_t slave_addr = 0x0001;
uint8_t master_channel = 20;


void DebugInit( void )
{
    GPIOB_SetBits(led1_pin);
    GPIOB_ModeCfg( led1_pin, GPIO_ModeOut_PP_5mA );
    GPIOB_ModeCfg( vout_pin, GPIO_ModeOut_PP_20mA );
}

int main()
{
    SetSysClock( CLK_SOURCE_PLL_60MHz );

    DebugInit();  //PA9
    printf("start\n");

    InitUSBDevPara();
    InitUSBDevice();
    PFIC_EnableIRQ( USB_IRQn );

    I2CInit();
    ads1110_mode(SINGLE_MODE|PGA1|SPS15|DRDY);

    lora_GPIO_cfg();

    TMR0_TimerInit( FREQ_SYS / 10 );                  // 设置定时时间 100ms
    TMR0_ITCfg( ENABLE, TMR0_3_IT_CYC_END );          // 开启中断
    PFIC_EnableIRQ( TMR0_IRQn );
    vout_on();
    lora_Sta = receiveSta;
    while(1)
    {

        //        SYS_ResetExecute();
//        DelayMs(10);
//        led1_toggle;
        switch (lora_Sta) {
        case receiveSta:
            loraRxCnt = 0;
            while(LORA_AUX_IN() == GPIO_High_Level)
            {
                lora_AUX_flage = 1;
            }
#ifdef LORA_MASTER_MODE
            if(Ep1DataOUTFlag == 1){
                Ep1DataOUTFlag = 0;
                lora_clear_rxbuf();
                for (uint8_t i = 0; i < Ep1DataOUTLen; ++i) {
                    loraRxBuf[i] = Ep1OUTDataBuf[i];
                }
                lora_Sta = sendSta;
            }else if(lora_AUX_flage ==1){
                lora_AUX_flage =0;
                SendUSBData(loraRxBuf,loraRxCnt);// strlen(loraRxBuf));
            }
#else
            if(Ep1DataOUTFlag == 1){
                Ep1DataOUTFlag = 0;
                lora_clear_rxbuf();
                for (uint8_t i = 0; i < Ep1DataOUTLen; ++i) {
                    loraRxBuf[i] = Ep1OUTDataBuf[i];
                }
                if((loraRxBuf[0] == 'A') && (loraRxBuf[1] == 'T')){
                    lora_Sta = configrueSta;
                }
                else {
                   // getADS1110(&adcValue);
                    lora_Sta = sendSta;
                }
            }else if(lora_AUX_flage ==1){
                lora_AUX_flage =0;
               // getADS1110(&adcValue);

                if((loraRxBuf[0] == 'A') && (loraRxBuf[1] == 'T')){
                    lora_Sta = configrueSta;
                }
                else {
                    lora_Sta = sendSta;
                }
            }
#endif


            break;
        case sendSta:
#ifdef LORA_MASTER_MODE
            SendUSBData(loraRxBuf, strlen(loraRxBuf));
            strcpy(loraRxBuf,"AT+CWMODE=0\r\n");
            if(lora_set_model(DISABLE,loraRxBuf)==FALSE){
                strcpy(adc_txBuf, "ERROR");
            }
            DelayMs(50);
            lora_sendData(adc_txBuf, strlen(adc_txBuf));
            SendUSBData(adc_txBuf, strlen(adc_txBuf));
            lora_clear_rxbuf();
            lora_Sta = receiveSta;
#else
            //SendUSBData(loraRxBuf, loraRxCnt);
            strcpy(loraRxBuf,"AT+CWMODE=0\r\n");
            if(lora_set_model(DISABLE,loraRxBuf)==FALSE){
                strcpy(adc_txBuf, "ERROR");
            }
            adc_txBuf[0] = master_add>>8;
            adc_txBuf[1] = master_add & 0x00ff;
            adc_txBuf[2] = master_channel;
            adc_txBuf[3] = adcValue>>24;
            adc_txBuf[4] = adcValue>>16;
            adc_txBuf[5] = adcValue>>8;
            adc_txBuf[6] = adcValue;
            DelayMs(50);
            lora_sendData(adc_txBuf, 7);
            SendUSBData(adc_txBuf, 7);
            lora_clear_rxbuf();
            lora_Sta = receiveSta;
#endif
            break;
        case configrueSta:
            lora_set_model(ENABLE,loraRxBuf);
            DelayMs(50);
            lora_sendCMD(loraRxBuf);
            aux_timeout = 1000;
            while(LORA_AUX_IN() == GPIO_Low_Level){
                DelayMs(1);
                if(aux_timeout-- == 0){
                    break;
                }
            }
            aux_timeout = 1000;
            while(LORA_AUX_IN() == GPIO_High_Level){
                DelayMs(1);
                if(aux_timeout-- == 0){
                    break;
                }
            }
            SendUSBData(loraRxBuf, strlen(loraRxBuf));
            lora_clear_rxbuf();
            lora_Sta = receiveSta;
            break;
        default:lora_Sta = receiveSta;
        break;
        }
        USB_IRQProcessHandler();
        //        if(ads1110_delay == 0){
        //            ads1110_delay=5;
        //            getADS1110(&adcValue);
        //            sprintf(txBuf,"%d\r\n",adcValue);
        //            SendUSBData(txBuf,strlen(txBuf));
        //            GPIOB_InverseBits(led1_pin);
        //        }
        // 注意当主频为80M时,Sleep睡眠唤醒中断不可调用flash内代码。
        //        LowPower_Sleep( RB_PWR_RAM30K | RB_PWR_RAM2K );       //只保留30+2K SRAM 供电
        //        HSECFG_Current( HSE_RCur_100 );     // 降为额定电流(低功耗函数中提升了HSE偏置电流)
    }
}

unsigned int crc16(unsigned char *puchMsg, unsigned int usDataLen)
{
    unsigned char i;
    unsigned int crc = 0xffff;
    unsigned char TT;
    while (usDataLen--) // ???????
    {
        crc = crc ^ (*puchMsg);
        puchMsg++;
        for (i = 0; i < 8; i++)
        {
            TT = crc & 1;
            crc = crc >> 1;
            crc = crc & 0x7fff;
            if (TT == 1)
                crc = crc ^ 0xa001;
            crc = crc & 0xffff;
        }
    }
    return crc;
}

uint8_t lora_send_data_process(uint8_t* txbuf,int32_t dat){
    uint8_t result = FALSE;
    uint8_t num = 0;
    uint16_t crc =0;
    txbuf[num++] = master_add >> 8;
    txbuf[num++] = master_add;
    txbuf[num++] = master_channel;
    txbuf[num++] = slave_addr >> 8;
    txbuf[num++] = slave_addr & 0x00ff;
    txbuf[num++] = 4;
    txbuf[num++] = dat >> 24;
    txbuf[num++] = dat >> 16;
    txbuf[num++] = dat >> 8;
    txbuf[num++] = dat ;
    crc = crc16(txbuf, num);
    txbuf[num++] = crc >> 8;
    txbuf[num++] = crc & 0x00ff;
    lora_sendData(loraTxBuf, num);
    return result;
}
uint8_t receive_data_decode(uint8_t *buf,uint8_t len){
    uint8_t result = FALSE;
    uint16_t crc =0;
    crc = crc16(buf, len - 2);
    if(((crc & 0x00ff) == buf[len -2]) && ((crc >> 8) == buf[len -1])){
        result = TRUE;
    }
    else {
        result = FALSE;
    }
    return result;
}

__INTERRUPT
__HIGH_CODE
void TMR0_IRQHandler( void )        // TMR0 定时中断
{
    if ( TMR0_GetITFlag( TMR0_3_IT_CYC_END ) )
    {
        TMR0_ClearITFlag( TMR0_3_IT_CYC_END );      // 清除中断标志
        led1_toggle;

    }
}


无法识别的设备有可能跟硬件有关系,比如芯片焊接不好,外围电容有问题,供电不好。
如果都可以通过芯片内置的BOOT,借助USB接口进行下载,那基本可用排除硬件问题(因为USB下载时,至少说明焊接,供电和USB走线问题不大),如果USB下载都不行,那就不用查软件,先按照焊接,供电,芯片外围,USB走线查一下硬件问题。


上面的代码在进行USB设备枚举时,建议使用USB中断,这样才能及时响应主机的命令,因为前面的通讯还有比较大的延时,可能造成枚举失败,这部分可以参考EVT里面的USB从机例子进行移植。


谢谢,查出来了,确实是外围芯片工作时序不对造成卡顿,电脑识别不了;可以把flash读写详细资料发一份么?谢谢!个人信息保护,已隐藏


只有登录才能回复,可以选择微信账号登录