zoukankan      html  css  js  c++  java
  • STM32 USB虚拟串口(转)

    源:STM32 USB虚拟串口

    串口调试在项目中被使用越来越多,串口资源的紧缺也变的尤为突出。很多本本人群,更是深有体会,不准备一个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

    /* Includes ------------------------------------------------------------------*/
    #include "hw_config.h"
    #include "usb_lib.h"
    #include "usb_istr.h"
    
    
    /*******************************************************************************
    * Function Name  : USB_IRQHandler
    * Description    : This function handles USB Low Priority interrupts
    *                  requests.
    * Input          : None
    * Output         : None
    * Return         : None
    *******************************************************************************/
    #if defined(STM32L1XX_MD) || defined(STM32L1XX_HD)|| defined(STM32L1XX_MD_PLUS)|| defined (STM32F37X)
    void USB_LP_IRQHandler(void)
    #else
    void USB_LP_CAN1_RX0_IRQHandler(void)
    #endif
    {
      USB_Istr();
    }
    
    /*******************************************************************************
    * Function Name  : USB_FS_WKUP_IRQHandler
    * Description    : This function handles USB WakeUp interrupt request.
    * Input          : None
    * Output         : None
    * Return         : None
    *******************************************************************************/
    
    #if defined(STM32L1XX_MD) || defined(STM32L1XX_HD)|| defined(STM32L1XX_MD_PLUS)
    void USB_FS_WKUP_IRQHandler(void)
    #else
    void USBWakeUp_IRQHandler(void)
    #endif
    {
      EXTI_ClearITPendingBit(EXTI_Line18);
    }

    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****/

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

    图7

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

    代码3

    /*******************************************************************************
    * 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);
      }
    }

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

    代码4

    /* Includes ------------------------------------------------------------------*/
    #include "usb_lib.h"
    #include "usb_desc.h"
    #include "usb_mem.h"
    #include "hw_config.h"
    #include "usb_istr.h"
    #include "usb_pwr.h"
    
    /* Private typedef -----------------------------------------------------------*/
    /* Private define ------------------------------------------------------------*/
    
    /* Interval between sending IN packets in frame number (1 frame = 1ms) */
    #define VCOMPORT_IN_FRAME_INTERVAL             5
    
    /* Private macro -------------------------------------------------------------*/
    /* Private variables ---------------------------------------------------------*/
    static uint8_t txBuffter[VIRTUAL_COM_PORT_DATA_SIZE] = {0};
    static volatile uint8_t txFlg = 0;
    static volatile uint32_t FrameCount = 0;
    
    
    /* Private function prototypes -----------------------------------------------*/
    /* Private functions ---------------------------------------------------------*/
    
    /*******************************************************************************
    * Function Name  : EP1_IN_Callback
    * Description    :
    * Input          : None.
    * Output         : None.
    * Return         : None.
    *******************************************************************************/
    void EP1_IN_Callback (void)
    {
        uint16_t len = 0;
        
        if (1 == txFlg)
        {
            len = USB_TxRead(txBuffter, sizeof(txBuffter));
    
            if (len > 0)
            {
                UserToPMABufferCopy(txBuffter, ENDP1_TXADDR, len);
                SetEPTxCount(ENDP1, len);
                SetEPTxValid(ENDP1); 
                FrameCount = 0;
            }
            else
            {
                txFlg = 0;
            }
        }
    }
    
    /*******************************************************************************
    * Function Name  : EP3_OUT_Callback
    * Description    :
    * Input          : None.
    * Output         : None.
    * Return         : None.
    *******************************************************************************/
    void EP3_OUT_Callback(void)
    {
      static uint8_t buffter[VIRTUAL_COM_PORT_DATA_SIZE] = {0};
    
      uint16_t USB_Rx_Cnt;
      
      /* Get the received data buffer and update the counter */
      USB_Rx_Cnt = USB_SIL_Read(EP3_OUT, buffter);
      
      /* USB data will be immediately processed, this allow next USB traffic being 
      NAKed till the end of the USART Xfer */
      USB_RxWrite(buffter, USB_Rx_Cnt);
    
      /* Enable the receive of data on EP3 */
      SetEPRxValid(ENDP3);
    
    }
    
    
    /*******************************************************************************
    * Function Name  : SOF_Callback / INTR_SOFINTR_Callback
    * Description    :
    * Input          : None.
    * Output         : None.
    * Return         : None.
    *******************************************************************************/
    void SOF_Callback(void)
    {
        uint16_t len = 0;
    
        if(bDeviceState == CONFIGURED)
        {
            if (0 == txFlg)
            {
                if (FrameCount++ == VCOMPORT_IN_FRAME_INTERVAL)
                {
                    /* Reset the frame counter */
                    FrameCount = 0;
    
                    /* Check the data to be sent through IN pipe */
                    len = USB_TxRead(txBuffter, sizeof(txBuffter));
    
                    if (len > 0)
                    {
                        UserToPMABufferCopy(txBuffter, ENDP1_TXADDR, len);
                        SetEPTxCount(ENDP1, len);
                        SetEPTxValid(ENDP1);
    
                        txFlg = 1;
                    }
                }
            }
        }  
    }
    /************************ (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也一起存放在上文章开始的链接中。

  • 相关阅读:
    git提交代码到远程仓库github
    git报错记录
    关于VSCode的一些设置
    css之列表数据前加上小方框
    EChats使用报错之 《"TypeError: Cannot read property 'getAttribute' of undefined"》
    EChats使用之给图表加箭头以及渐变
    vue报错之(Do not use v-for index as key on <transition-group> children)
    在vue项目中使用mock模拟数据
    Vue项目中关于EChats的使用
    使用mock数据实现登录时的一次bug记录
  • 原文地址:https://www.cnblogs.com/LittleTiger/p/4429649.html
Copyright © 2011-2022 走看看