• 关于STM32F103RC usb虚拟串口


    转载自: https://www.cnblogs.com/hiker-blogs/p/stm32_usb.html

    百度网盘内容存在,本人测试例程可用,并转载保存,以便日后学习移植方法与思路。

    以下为转载博主思路与方法:

    串口调试在项目中被使用越来越多,串口资源的紧缺也变的尤为突出。很多本本人群,更是深有体会,不准备一个USB转串口工具就没办法进行开发。本章节来简单概述STM32低端芯片上的USB虚拟串口的移植。在官方DEMO中已经提供了现成的程序,这里对修改方法做简单说明。

             官方demo及驱动程序,我存放在百度盘:

             http://pan.baidu.com/s/1hq3moE4

    首先打开官方demo我们开始进行移植,第一步复制我们可用的文件,操作如下:

    ProjectsVirtual_COM_Port文件夹下,复制红线部分

    图1

    图2

    我为了方便演示统放在usb/src文件夹下:

    图3

    现在复制USB的库文件,这些文件不需要我们修改:

    图4

    上图中的文件统一放在usb/lib文件夹下:

    图5

             好了现在所需要的文件我们以复制完了。这里先讲一下DEMO程序的主要工作流程:

    图6

             由上图可知,PC通过虚拟串口发送数据到STM32 usb口,STM32再通过usart1发送数据到PC串口。我们做项目时,只用USB虚拟串口即可。所以我们现在需要把串口发送部分删除。把USB做为一个COM口来使用。我们要如何使用这个USB口呢?demo中是把USB发送数据做了一个缓存,先把要发送的数据存入缓存中,然后由USB自动发送出去。而接收部分是直接通过串口透传。我们在应用时就需要用到两个FIFO,1是发送,这个和demo方式是样;2是接收,接收也做一个缓存,我们通过查询来判断是否收到新数据。这下大家应该明白为什么使用两个FIFO了。 我这里有写好的FIFO库函数可直接使用Queue.c文件。

             现在开始修改:

    1,stm32_it.c 更名为usb_it.c删除无用代码,只保留usb中断函数,和唤醒函数。代码如下:

    代码1

    复制代码
     1 /* Includes ------------------------------------------------------------------*/
     2 #include "hw_config.h"
     3 #include "usb_lib.h"
     4 #include "usb_istr.h"
     5 
     6 
     7 /*******************************************************************************
     8 * Function Name  : USB_IRQHandler
     9 * Description    : This function handles USB Low Priority interrupts
    10 *                  requests.
    11 * Input          : None
    12 * Output         : None
    13 * Return         : None
    14 *******************************************************************************/
    15 #if defined(STM32L1XX_MD) || defined(STM32L1XX_HD)|| defined(STM32L1XX_MD_PLUS)|| defined (STM32F37X)
    16 void USB_LP_IRQHandler(void)
    17 #else
    18 void USB_LP_CAN1_RX0_IRQHandler(void)
    19 #endif
    20 {
    21   USB_Istr();
    22 }
    23 
    24 /*******************************************************************************
    25 * Function Name  : USB_FS_WKUP_IRQHandler
    26 * Description    : This function handles USB WakeUp interrupt request.
    27 * Input          : None
    28 * Output         : None
    29 * Return         : None
    30 *******************************************************************************/
    31 
    32 #if defined(STM32L1XX_MD) || defined(STM32L1XX_HD)|| defined(STM32L1XX_MD_PLUS)
    33 void USB_FS_WKUP_IRQHandler(void)
    34 #else
    35 void USBWakeUp_IRQHandler(void)
    36 #endif
    37 {
    38   EXTI_ClearITPendingBit(EXTI_Line18);
    39 }
    复制代码

    2,修改代码hw_config.c删除无用代码,新建立2组,读FIFO和写FIFO的函数。后面会用到。

    代码如下:

    代码2

    /* Includes ------------------------------------------------------------------*/

    #include "usb_lib.h"
    #include "usb_prop.h"
    #include "usb_desc.h"
    #include "hw_config.h"
    #include "usb_pwr.h"
    #include "Queue.h"


    /* Private typedef -----------------------------------------------------------*/
    /* Private define ------------------------------------------------------------*/
    /* Private macro -------------------------------------------------------------*/
    /* Private variables ---------------------------------------------------------*/
    ErrorStatus HSEStartUpStatus;
    USART_InitTypeDef USART_InitStructure;
    EXTI_InitTypeDef EXTI_InitStructure;


    #define USB_COM_RX_BUF_SIZE (1024 + 256)
    #define USB_COM_TX_BUF_SIZE (1024 + 256)

    static QUEUE8_t m_QueueUsbComRx = {0};
    static QUEUE8_t m_QueueUsbComTx = {0};
    static uint8_t m_UsbComRxBuf[USB_COM_RX_BUF_SIZE] = {0};
    static uint8_t m_UsbComTxBuf[USB_COM_TX_BUF_SIZE] = {0};

    static void IntToUnicode (uint32_t value , uint8_t *pbuf , uint8_t len);
    /* Extern variables ----------------------------------------------------------*/

    extern LINE_CODING linecoding;

    /* Private function prototypes -----------------------------------------------*/
    /* Private functions ---------------------------------------------------------*/
    /*******************************************************************************
    * Function Name : Set_System
    * Description : Configures Main system clocks & power
    * Input : None.
    * Return : None.
    *******************************************************************************/
    void Set_System(void)
    {
    GPIO_InitTypeDef GPIO_InitStructure;

    QUEUE_PacketCreate(&m_QueueUsbComRx, m_UsbComRxBuf, sizeof(m_UsbComRxBuf));
    QUEUE_PacketCreate(&m_QueueUsbComTx, m_UsbComTxBuf, sizeof(m_UsbComTxBuf));

    /* Enable USB_DISCONNECT GPIO clock */
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIO_DISCONNECT, ENABLE);

    /* Configure USB pull-up pin */
    GPIO_InitStructure.GPIO_Pin = USB_DISCONNECT_PIN;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_Init(USB_DISCONNECT, &GPIO_InitStructure);

    /* Configure the EXTI line 18 connected internally to the USB IP */
    EXTI_ClearITPendingBit(EXTI_Line18);
    EXTI_InitStructure.EXTI_Line = EXTI_Line18;
    EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
    EXTI_InitStructure.EXTI_LineCmd = ENABLE;
    EXTI_Init(&EXTI_InitStructure);


    }

    /*******************************************************************************
    * Function Name : Set_USBClock
    * Description : Configures USB Clock input (48MHz)
    * Input : None.
    * Return : None.
    *******************************************************************************/
    void Set_USBClock(void)
    {
    /* Select USBCLK source */
    RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5);

    /* Enable the USB clock */
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USB, ENABLE);
    }

    /*******************************************************************************
    * Function Name : Enter_LowPowerMode
    * Description : Power-off system clocks and power while entering suspend mode
    * Input : None.
    * Return : None.
    *******************************************************************************/
    void Enter_LowPowerMode(void)
    {
    /* Set the device state to suspend */
    bDeviceState = SUSPENDED;
    }

    /*******************************************************************************
    * Function Name : Leave_LowPowerMode
    * Description : Restores system clocks and power while exiting suspend mode
    * Input : None.
    * Return : None.
    *******************************************************************************/
    void Leave_LowPowerMode(void)
    {
    DEVICE_INFO *pInfo = &Device_Info;

    /* Set the device state to the correct state */
    if (pInfo->Current_Configuration != 0)
    {
    /* Device configured */
    bDeviceState = CONFIGURED;
    }
    else
    {
    bDeviceState = ATTACHED;
    }
    /*Enable SystemCoreClock*/
    // SystemInit();
    }

    /*******************************************************************************
    * Function Name : USB_Interrupts_Config
    * Description : Configures the USB interrupts
    * Input : None.
    * Return : None.
    *******************************************************************************/
    void USB_Interrupts_Config(void)
    {
    NVIC_InitTypeDef NVIC_InitStructure;

    NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);

    /* Enable the USB Wake-up interrupt */
    NVIC_InitStructure.NVIC_IRQChannel = USBWakeUp_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_Init(&NVIC_InitStructure);
    }

    /*******************************************************************************
    * Function Name : USB_Cable_Config
    * Description : Software Connection/Disconnection of USB Cable
    * Input : None.
    * Return : Status
    *******************************************************************************/
    void USB_Cable_Config (FunctionalState NewState)
    {
    if (NewState == DISABLE)
    {
    GPIO_ResetBits(USB_DISCONNECT, USB_DISCONNECT_PIN);
    }
    else
    {
    GPIO_SetBits(USB_DISCONNECT, USB_DISCONNECT_PIN);
    }
    }

    /*******************************************************************************
    * Function Name : void USB_Config(void)
    * Description : USB系统初始化
    * Input :
    * Output :
    * Other :
    * Date : 2014.11.28
    *******************************************************************************/
    void USB_Config(void)
    {
    Set_System();

    Set_USBClock();

    USB_Interrupts_Config();

    USB_Init();
    }

    /*******************************************************************************
    * Function Name : uint32_t USB_RxRead(uint8_t *buffter, uint32_t buffterSize)
    * Description : 从USB接收缓存中读数据
    * Input :
    * Output :
    * Other :
    * Date : 2014.11.28
    *******************************************************************************/
    uint32_t USB_RxRead(uint8_t *buffter, uint32_t buffterSize)
    {
    return QUEUE_PacketOut(&m_QueueUsbComRx, buffter, buffterSize);
    }
    /*******************************************************************************
    * Function Name : uint32_t USB_RxWrite(uint8_t *buffter, uint32_t writeLen)
    * Description : 写数据到USB接收缓存中
    * Input :
    * Output :
    * Other :
    * Date : 2014.11.28
    *******************************************************************************/
    uint32_t USB_RxWrite(uint8_t *buffter, uint32_t writeLen)
    {
    return QUEUE_PacketIn(&m_QueueUsbComRx, buffter, writeLen);
    }
    /*******************************************************************************
    * Function Name : uint32_t USB_TxRead(uint8_t *buffter, uint32_t buffterSize)
    * Description : 从USB发送缓存中读数据
    * Input :
    * Output :
    * Other :
    * Date : 2014.11.28
    *******************************************************************************/
    uint32_t USB_TxRead(uint8_t *buffter, uint32_t buffterSize)
    {
    return QUEUE_PacketOut(&m_QueueUsbComTx, buffter, buffterSize);;
    }
    /*******************************************************************************
    * Function Name : uint32_t USB_TxWrite(uint8_t *buffter, uint32_t writeLen)
    * Description : 写数据到USB发送缓存中
    * Input :
    * Output :
    * Other :
    * Date : 2014.11.28
    *******************************************************************************/
    uint32_t USB_TxWrite(uint8_t *buffter, uint32_t writeLen)
    {
    return QUEUE_PacketIn(&m_QueueUsbComTx, buffter, writeLen);
    }

    /*******************************************************************************
    * Function Name : Get_SerialNum.
    * Description : Create the serial number string descriptor.
    * Input : None.
    * Output : None.
    * Return : None.
    *******************************************************************************/
    void Get_SerialNum(void)
    {
    uint32_t Device_Serial0, Device_Serial1, Device_Serial2;

    Device_Serial0 = *(uint32_t*)ID1;
    Device_Serial1 = *(uint32_t*)ID2;
    Device_Serial2 = *(uint32_t*)ID3;

    Device_Serial0 += Device_Serial2;

    if (Device_Serial0 != 0)
    {
    IntToUnicode (Device_Serial0, &Virtual_Com_Port_StringSerial[2] , 8);
    IntToUnicode (Device_Serial1, &Virtual_Com_Port_StringSerial[18], 4);
    }
    }

    /*******************************************************************************
    * Function Name : HexToChar.
    * Description : Convert Hex 32Bits value into char.
    * Input : None.
    * Output : None.
    * Return : None.
    *******************************************************************************/
    static void IntToUnicode (uint32_t value , uint8_t *pbuf , uint8_t len)
    {
    uint8_t idx = 0;

    for( idx = 0 ; idx < len ; idx ++)
    {
    if( ((value >> 28)) < 0xA )
    {
    pbuf[ 2* idx] = (value >> 28) + '0';
    }
    else
    {
    pbuf[2* idx] = (value >> 28) + 'A' - 10;
    }

    value = value << 4;

    pbuf[ 2* idx + 1] = 0;
    }
    }

    /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

    复制代码
      1 /* Includes ------------------------------------------------------------------*/
      2 
      3 #include "usb_lib.h"
      4 #include "usb_prop.h"
      5 #include "usb_desc.h"
      6 #include "hw_config.h"
      7 #include "usb_pwr.h"
      8 #include "Queue.h"
      9 
     10 
     11 /* Private typedef -----------------------------------------------------------*/
     12 /* Private define ------------------------------------------------------------*/
     13 /* Private macro -------------------------------------------------------------*/
     14 /* Private variables ---------------------------------------------------------*/
     15 ErrorStatus HSEStartUpStatus;
     16 USART_InitTypeDef USART_InitStructure;
     17 EXTI_InitTypeDef EXTI_InitStructure;
     18 
     19 
     20 #define USB_COM_RX_BUF_SIZE         (1024 + 256)
     21 #define USB_COM_TX_BUF_SIZE         (1024 + 256)
     22 
     23 static QUEUE8_t m_QueueUsbComRx         = {0};
     24 static QUEUE8_t m_QueueUsbComTx         = {0};
     25 static uint8_t  m_UsbComRxBuf[USB_COM_RX_BUF_SIZE]      = {0};     
     26 static uint8_t  m_UsbComTxBuf[USB_COM_TX_BUF_SIZE]      = {0};   
     27 
     28 static void IntToUnicode (uint32_t value , uint8_t *pbuf , uint8_t len);
     29 /* Extern variables ----------------------------------------------------------*/
     30 
     31 extern LINE_CODING linecoding;
     32 
     33 /* Private function prototypes -----------------------------------------------*/
     34 /* Private functions ---------------------------------------------------------*/
     35 /*******************************************************************************
     36 * Function Name  : Set_System
     37 * Description    : Configures Main system clocks & power
     38 * Input          : None.
     39 * Return         : None.
     40 *******************************************************************************/
     41 void Set_System(void)
     42 {
     43   GPIO_InitTypeDef GPIO_InitStructure;
     44 
     45   QUEUE_PacketCreate(&m_QueueUsbComRx, m_UsbComRxBuf, sizeof(m_UsbComRxBuf));
     46   QUEUE_PacketCreate(&m_QueueUsbComTx, m_UsbComTxBuf, sizeof(m_UsbComTxBuf));
     47   
     48   /* Enable USB_DISCONNECT GPIO clock */
     49   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIO_DISCONNECT, ENABLE);
     50 
     51   /* Configure USB pull-up pin */
     52   GPIO_InitStructure.GPIO_Pin = USB_DISCONNECT_PIN;
     53   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
     54   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
     55   GPIO_Init(USB_DISCONNECT, &GPIO_InitStructure);
     56   
     57   /* Configure the EXTI line 18 connected internally to the USB IP */
     58   EXTI_ClearITPendingBit(EXTI_Line18);
     59   EXTI_InitStructure.EXTI_Line = EXTI_Line18; 
     60   EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
     61   EXTI_InitStructure.EXTI_LineCmd = ENABLE;
     62   EXTI_Init(&EXTI_InitStructure);
     63 
     64   
     65 }
     66 
     67 /*******************************************************************************
     68 * Function Name  : Set_USBClock
     69 * Description    : Configures USB Clock input (48MHz)
     70 * Input          : None.
     71 * Return         : None.
     72 *******************************************************************************/
     73 void Set_USBClock(void)
     74 {
     75   /* Select USBCLK source */
     76   RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5);
     77   
     78   /* Enable the USB clock */
     79   RCC_APB1PeriphClockCmd(RCC_APB1Periph_USB, ENABLE);
     80 }
     81 
     82 /*******************************************************************************
     83 * Function Name  : Enter_LowPowerMode
     84 * Description    : Power-off system clocks and power while entering suspend mode
     85 * Input          : None.
     86 * Return         : None.
     87 *******************************************************************************/
     88 void Enter_LowPowerMode(void)
     89 {
     90   /* Set the device state to suspend */
     91   bDeviceState = SUSPENDED;
     92 }
     93 
     94 /*******************************************************************************
     95 * Function Name  : Leave_LowPowerMode
     96 * Description    : Restores system clocks and power while exiting suspend mode
     97 * Input          : None.
     98 * Return         : None.
     99 *******************************************************************************/
    100 void Leave_LowPowerMode(void)
    101 {
    102   DEVICE_INFO *pInfo = &Device_Info;
    103 
    104   /* Set the device state to the correct state */
    105   if (pInfo->Current_Configuration != 0)
    106   {
    107     /* Device configured */
    108     bDeviceState = CONFIGURED;
    109   }
    110   else
    111   {
    112     bDeviceState = ATTACHED;
    113   }
    114   /*Enable SystemCoreClock*/
    115 //  SystemInit();
    116 }
    117 
    118 /*******************************************************************************
    119 * Function Name  : USB_Interrupts_Config
    120 * Description    : Configures the USB interrupts
    121 * Input          : None.
    122 * Return         : None.
    123 *******************************************************************************/
    124 void USB_Interrupts_Config(void)
    125 {
    126   NVIC_InitTypeDef NVIC_InitStructure; 
    127 
    128   NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn;
    129   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
    130   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    131   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    132   NVIC_Init(&NVIC_InitStructure);
    133   
    134     /* Enable the USB Wake-up interrupt */
    135   NVIC_InitStructure.NVIC_IRQChannel = USBWakeUp_IRQn;
    136   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    137   NVIC_Init(&NVIC_InitStructure);
    138 }
    139 
    140 /*******************************************************************************
    141 * Function Name  : USB_Cable_Config
    142 * Description    : Software Connection/Disconnection of USB Cable
    143 * Input          : None.
    144 * Return         : Status
    145 *******************************************************************************/
    146 void USB_Cable_Config (FunctionalState NewState)
    147 {
    148   if (NewState == DISABLE)
    149   {
    150     GPIO_ResetBits(USB_DISCONNECT, USB_DISCONNECT_PIN);
    151   }
    152   else
    153   {
    154     GPIO_SetBits(USB_DISCONNECT, USB_DISCONNECT_PIN);
    155   }
    156 }
    157 
    158 /*******************************************************************************
    159 * Function Name : void USB_Config(void)
    160 * Description   : USB系统初始化
    161 * Input         : 
    162 * Output        : 
    163 * Other         : 
    164 * Date          : 2014.11.28
    165 *******************************************************************************/
    166 void USB_Config(void)
    167 {
    168     Set_System();
    169 
    170     Set_USBClock();
    171 
    172     USB_Interrupts_Config();
    173 
    174     USB_Init();
    175 }
    176 
    177 /*******************************************************************************
    178 * Function Name : uint32_t USB_RxRead(uint8_t *buffter, uint32_t buffterSize)
    179 * Description   : 从USB接收缓存中读数据
    180 * Input         : 
    181 * Output        : 
    182 * Other         : 
    183 * Date          : 2014.11.28
    184 *******************************************************************************/
    185 uint32_t USB_RxRead(uint8_t *buffter, uint32_t buffterSize)
    186 {
    187     return QUEUE_PacketOut(&m_QueueUsbComRx, buffter, buffterSize);
    188 }
    189 /*******************************************************************************
    190 * Function Name : uint32_t USB_RxWrite(uint8_t *buffter, uint32_t writeLen)
    191 * Description   : 写数据到USB接收缓存中
    192 * Input         : 
    193 * Output        : 
    194 * Other         : 
    195 * Date          : 2014.11.28
    196 *******************************************************************************/
    197 uint32_t USB_RxWrite(uint8_t *buffter, uint32_t writeLen)
    198 {
    199     return QUEUE_PacketIn(&m_QueueUsbComRx, buffter, writeLen);
    200 }
    201 /*******************************************************************************
    202 * Function Name : uint32_t USB_TxRead(uint8_t *buffter, uint32_t buffterSize)
    203 * Description   : 从USB发送缓存中读数据
    204 * Input         : 
    205 * Output        : 
    206 * Other         : 
    207 * Date          : 2014.11.28
    208 *******************************************************************************/
    209 uint32_t USB_TxRead(uint8_t *buffter, uint32_t buffterSize)
    210 {
    211     return QUEUE_PacketOut(&m_QueueUsbComTx, buffter, buffterSize);;
    212 }
    213 /*******************************************************************************
    214 * Function Name : uint32_t USB_TxWrite(uint8_t *buffter, uint32_t writeLen)
    215 * Description   : 写数据到USB发送缓存中
    216 * Input         : 
    217 * Output        : 
    218 * Other         : 
    219 * Date          : 2014.11.28
    220 *******************************************************************************/
    221 uint32_t USB_TxWrite(uint8_t *buffter, uint32_t writeLen)
    222 {
    223     return QUEUE_PacketIn(&m_QueueUsbComTx, buffter, writeLen);
    224 }
    225 
    226 
    227 
    228 /*******************************************************************************
    229 * Function Name  : Get_SerialNum.
    230 * Description    : Create the serial number string descriptor.
    231 * Input          : None.
    232 * Output         : None.
    233 * Return         : None.
    234 *******************************************************************************/
    235 void Get_SerialNum(void)
    236 {
    237   uint32_t Device_Serial0, Device_Serial1, Device_Serial2;
    238 
    239   Device_Serial0 = *(uint32_t*)ID1;
    240   Device_Serial1 = *(uint32_t*)ID2;
    241   Device_Serial2 = *(uint32_t*)ID3;  
    242 
    243   Device_Serial0 += Device_Serial2;
    244 
    245   if (Device_Serial0 != 0)
    246   {
    247     IntToUnicode (Device_Serial0, &Virtual_Com_Port_StringSerial[2] , 8);
    248     IntToUnicode (Device_Serial1, &Virtual_Com_Port_StringSerial[18], 4);
    249   }
    250 }
    251 
    252 /*******************************************************************************
    253 * Function Name  : HexToChar.
    254 * Description    : Convert Hex 32Bits value into char.
    255 * Input          : None.
    256 * Output         : None.
    257 * Return         : None.
    258 *******************************************************************************/
    259 static void IntToUnicode (uint32_t value , uint8_t *pbuf , uint8_t len)
    260 {
    261   uint8_t idx = 0;
    262   
    263   for( idx = 0 ; idx < len ; idx ++)
    264   {
    265     if( ((value >> 28)) < 0xA )
    266     {
    267       pbuf[ 2* idx] = (value >> 28) + '0';
    268     }
    269     else
    270     {
    271       pbuf[2* idx] = (value >> 28) + 'A' - 10; 
    272     }
    273     
    274     value = value << 4;
    275     
    276     pbuf[ 2* idx + 1] = 0;
    277   }
    278 }
    279 
    280 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
    复制代码

    这里要讲一下为什么要屏蔽SystemInit(),因为demo只运行虚拟串口功能,在USB未插入的情况下,是进入低功耗状态,插入时从低功耗状态退出后会调用此函数。当然我们在项目中一般不会这样,系统是否运行和插USB接口没有联系。所以我在下文中把进入低功耗代码屏蔽了,自然也就不用唤醒代码了。

    图7

    关于USB口使能控制引脚,需要根据开发板的引脚定义来修改宏定义platform_config.h文件中,笔者使用的是神舟3号开发板,控制信号刚好和demo相反,所以修改hw_config.c代码如下:

    代码3

    复制代码
     1 /*******************************************************************************
     2 * Function Name  : USB_Cable_Config
     3 * Description    : Software Connection/Disconnection of USB Cable
     4 * Input          : None.
     5 * Return         : Status
     6 *******************************************************************************/
     7 void USB_Cable_Config (FunctionalState NewState)
     8 {
     9   if (NewState == DISABLE)
    10   {
    11     GPIO_ResetBits(USB_DISCONNECT, USB_DISCONNECT_PIN);
    12   }
    13   else
    14   {
    15     GPIO_SetBits(USB_DISCONNECT, USB_DISCONNECT_PIN);
    16   }
    17 }
    复制代码

    3,现在修改USB 回调函数中的代码usb_endp.c文件。使用下文代码替换:

    代码4

    复制代码
      1 /* Includes ------------------------------------------------------------------*/
      2 #include "usb_lib.h"
      3 #include "usb_desc.h"
      4 #include "usb_mem.h"
      5 #include "hw_config.h"
      6 #include "usb_istr.h"
      7 #include "usb_pwr.h"
      8 
      9 /* Private typedef -----------------------------------------------------------*/
     10 /* Private define ------------------------------------------------------------*/
     11 
     12 /* Interval between sending IN packets in frame number (1 frame = 1ms) */
     13 #define VCOMPORT_IN_FRAME_INTERVAL             5
     14 
     15 /* Private macro -------------------------------------------------------------*/
     16 /* Private variables ---------------------------------------------------------*/
     17 static uint8_t txBuffter[VIRTUAL_COM_PORT_DATA_SIZE] = {0};
     18 static volatile uint8_t txFlg = 0;
     19 static volatile uint32_t FrameCount = 0;
     20 
     21 
     22 /* Private function prototypes -----------------------------------------------*/
     23 /* Private functions ---------------------------------------------------------*/
     24 
     25 /*******************************************************************************
     26 * Function Name  : EP1_IN_Callback
     27 * Description    :
     28 * Input          : None.
     29 * Output         : None.
     30 * Return         : None.
     31 *******************************************************************************/
     32 void EP1_IN_Callback (void)
     33 {
     34     uint16_t len = 0;
     35     
     36     if (1 == txFlg)
     37     {
     38         len = USB_TxRead(txBuffter, sizeof(txBuffter));
     39 
     40         if (len > 0)
     41         {
     42             UserToPMABufferCopy(txBuffter, ENDP1_TXADDR, len);
     43             SetEPTxCount(ENDP1, len);
     44             SetEPTxValid(ENDP1); 
     45             FrameCount = 0;
     46         }
     47         else
     48         {
     49             txFlg = 0;
     50         }
     51     }
     52 }
     53 
     54 /*******************************************************************************
     55 * Function Name  : EP3_OUT_Callback
     56 * Description    :
     57 * Input          : None.
     58 * Output         : None.
     59 * Return         : None.
     60 *******************************************************************************/
     61 void EP3_OUT_Callback(void)
     62 {
     63   static uint8_t buffter[VIRTUAL_COM_PORT_DATA_SIZE] = {0};
     64 
     65   uint16_t USB_Rx_Cnt;
     66   
     67   /* Get the received data buffer and update the counter */
     68   USB_Rx_Cnt = USB_SIL_Read(EP3_OUT, buffter);
     69   
     70   /* USB data will be immediately processed, this allow next USB traffic being 
     71   NAKed till the end of the USART Xfer */
     72   USB_RxWrite(buffter, USB_Rx_Cnt);
     73 
     74   /* Enable the receive of data on EP3 */
     75   SetEPRxValid(ENDP3);
     76 
     77 }
     78 
     79 
     80 /*******************************************************************************
     81 * Function Name  : SOF_Callback / INTR_SOFINTR_Callback
     82 * Description    :
     83 * Input          : None.
     84 * Output         : None.
     85 * Return         : None.
     86 *******************************************************************************/
     87 void SOF_Callback(void)
     88 {
     89     uint16_t len = 0;
     90 
     91     if(bDeviceState == CONFIGURED)
     92     {
     93         if (0 == txFlg)
     94         {
     95             if (FrameCount++ == VCOMPORT_IN_FRAME_INTERVAL)
     96             {
     97                 /* Reset the frame counter */
     98                 FrameCount = 0;
     99 
    100                 /* Check the data to be sent through IN pipe */
    101                 len = USB_TxRead(txBuffter, sizeof(txBuffter));
    102 
    103                 if (len > 0)
    104                 {
    105                     UserToPMABufferCopy(txBuffter, ENDP1_TXADDR, len);
    106                     SetEPTxCount(ENDP1, len);
    107                     SetEPTxValid(ENDP1);
    108 
    109                     txFlg = 1;
    110                 }
    111             }
    112         }
    113     }  
    114 }
    115 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
    复制代码

    这里讲下大概意思,函数EP3_OUT_Callback是在USB口收到数据后,将数据存入FIFO中。

    函数SOF_Callback定时查询用户是否有要发送的数据,如果有则进行发送,在发送完成后会触发发送中断EP1_IN_Callback函数,如果发送完毕就不调用SetEPTxValid(ENDP1)函数,发送完成后就不会再触发EP1_IN_Callback函数。

    4,修改usb_pwr.c在前文中说到:不让系统进入休眠状态,这里屏蔽185行 __WFI();

    5,修改usb_prop.c屏蔽COM初始化代码。137行USART_Config_Default(); 237行USART_Config();

    6,修改usb_desc.c 这里修改需要参考一些USB专业的书籍,推荐全圈圈的书,讲的通俗易懂。关于本程序的驱动,笔者在win7下测试可以自动安装,如果无法自动安装可使用文章开始的链接中的驱动程序。本文件如果修改需谨慎,其中pid,vid是制造商ID和产品编号,如果修改了那驱动也要对应修改,官方驱动就无法自动进行安装了。

    到这里移植就差不多完成了,下面进行测试。由于USB虚拟串口不受波特率限制,所以笔者进行过50k/s的压力测试,运行半小时未丢1个字节。

    移植好的工程STM32_UsbVirtualCom.rar也一起存放在上文章开始的链接中。

  • 相关阅读:
    XML之四种解析dom,sax,jdom,dom4j原理及性能比较
    uni-app 下小程序bindgetuserinfo不回调原因
    微信小程序 -- 真机不打开调试无法正常使用小程序的坑
    如何申请腾讯位置服务的密钥
    使用Promise封装小程序wx.request的实现方法
    关于vue-router当中addRoutes的使用
    前端Promise总结笔记
    css怎么设置超出几行显示省略号?
    大数据Spark和Hadoop以及区别(干货)
    Spark和Hadoop的区别和比较
  • 原文地址:https://www.cnblogs.com/Gustavo-G/p/14010988.html
Copyright © 2020-2023  润新知