一乐电子

一乐电子百科

 找回密码
 请使用微信账号登录和注册会员

QQ登录

只需一步,快速开始

快捷登录

手机号码,快捷登录

搜索
查看: 2329|回复: 3
收起左侧

[STM8/STM32] STM32 官方VCP虚拟串口,不接USB主程序不运行原因

[复制链接]
发表于 2016-3-21 16:51 | 显示全部楼层 |阅读模式
最近使用STM32 VCP,但是不连接USB主程序不运行。查看库文件发现USB挂起时使STM32进入了睡眠状态。把挂起函数电源控制部分代码注释掉,问题解决(代码没有更深入研究,仅供参考)。


/*******************************************************************************
* Function Name  : Suspend
* Description    : sets suspend mode operating conditions
* Input          : None.
* Output         : None.
* Return         : USB_SUCCESS.
*******************************************************************************/
void Suspend(void)
{
    uint32_t i =0;
    uint16_t wCNTR;
    uint32_t tmpreg = 0;
  __IO uint32_t savePWR_CR=0;
    /* suspend preparation */
    /* ... */
    /*Store CNTR value */
    wCNTR = _GetCNTR();  

    /* This a sequence to apply a force RESET to handle a robustness case */
   
    /*Store endpoints registers status */
    for (i=0;i<8;i++) EP[i] = _GetENDPOINT(i);
   
    /* unmask RESET flag */
    wCNTR|=CNTR_RESETM;
    _SetCNTR(wCNTR);
   
    /*apply FRES */
    wCNTR|=CNTR_FRES;
    _SetCNTR(wCNTR);
   
    /*clear FRES*/
    wCNTR&=~CNTR_FRES;
    _SetCNTR(wCNTR);
   
    /*poll for RESET flag in ISTR*/
    while((_GetISTR()&ISTR_RESET) == 0);
   
    /* clear RESET flag in ISTR */
    _SetISTR((uint16_t)CLR_RESET);
   
    //restore Enpoints
    for (i=0;i<8;i++)
    _SetENDPOINT(i, EP[i]);
   
    // Now it is safe to enter macrocell in suspend mode
    wCNTR |= CNTR_FSUSP;
    _SetCNTR(wCNTR);
    /*
// force low-power mode in the macrocell  
    wCNTR = _GetCNTR();
    wCNTR |= CNTR_LPMODE;
     _SetCNTR(wCNTR);
   
    //prepare entry in low power mode (STOP mode)
    // Select the regulator state in STOP mode
    savePWR_CR = PWR->CR;
    tmpreg = PWR->CR;
    // Clear PDDS and LPDS bits  
    tmpreg &= ((uint32_t)0xFFFFFFFC);
    //Set LPDS bit according to PWR_Regulator value  
    tmpreg |= PWR_Regulator_LowPower;
    //Store the new value  
     PWR->CR = tmpreg;
    // Set SLEEPDEEP bit of Cortex System Control Register  
#if defined (STM32F30X) || defined (STM32F37X)
        SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
#else
       SCB->SCR |= SCB_SCR_SLEEPDEEP;      
#endif
    */
    //enter system in STOP mode, only when wakeup flag in not set
    if((_GetISTR()&ISTR_WKUP)==0)
    {
        
        __WFI();
        /*
        // Reset SLEEPDEEP bit of Cortex System Control Register
#if defined (STM32F30X) || defined (STM32F37X)
                SCB->SCR &= (uint32_t)~((uint32_t)SCB_SCR_SLEEPDEEP_Msk);
#else
                SCB->SCR &= (uint32_t)~((uint32_t)SCB_SCR_SLEEPDEEP);
#endif
        */
    }
    else
    {
        // Clear Wakeup flag  
        _SetISTR(CLR_WKUP);
    // clear FSUSP to abort entry in suspend mode   
        wCNTR = _GetCNTR();
        wCNTR&=~CNTR_FSUSP;
        _SetCNTR(wCNTR);
/*        
        //restore sleep mode configuration  
        // restore Power regulator config in sleep mode
        PWR->CR = savePWR_CR;
        
    // Reset SLEEPDEEP bit of Cortex System Control Register  
#if defined (STM32F30X) || defined (STM32F37X)        
                SCB->SCR &= (uint32_t)~((uint32_t)SCB_SCR_SLEEPDEEP_Msk);
#else
                SCB->SCR &= (uint32_t)~((uint32_t)SCB_SCR_SLEEPDEEP);
#endif
        */
    }
}
发表于 2016-3-21 18:03 | 显示全部楼层
一看到代码  瞬间睡着了
发表于 2016-3-26 14:04 | 显示全部楼层
不知道为什么,一看到库函数,头立马晕了。
发表于 2016-5-27 22:06 | 显示全部楼层
正常,那个例程是基于中断的

本版积分规则

QQ|一淘宝店|手机版|商店|电子DIY套件|一乐电子 ( 粤ICP备09076165号 ) 公安备案粤公网安备 44522102000183号

GMT+8, 2024-6-17 08:17 , Processed in 0.061162 second(s), 36 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表