当前位置: 首页 > news >正文

STM32 USB虚拟串口(转)

源:STM32 USB虚拟串口

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

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

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

 

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

Projects\Virtual_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也一起存放在上文章开始的链接中。

 

相关文章:

  • leetcode------Gas Station
  • 实用的JS代码段(表单篇)
  • 【VMCloud云平台】SCAP(四)连接公有云(二)
  • easytouch使用方法
  • Django从安装到目录创建
  • Liam的C# 学习历程(五):正则表达式(Regular Expressions)
  • 收集谷歌替代网站
  • fsync与数据库日志刷新
  • 第十六次课:Servlet实现商品用户评价
  • Canvas绘画功能(待补充)
  • RabbitMQ(六)远程连接
  • FileInputStream与FileOutputStream类
  • Octopus系列之数据上传格式要求说明
  • IIS 之 HTTP 错误 500.19(无法访问请求页面,因为该页的相关配置数据无效)
  • 依据波形的转折点文件,转换成波形文件
  • CentOS6 编译安装 redis-3.2.3
  • Javascript编码规范
  • Javascript设计模式学习之Observer(观察者)模式
  • JavaWeb(学习笔记二)
  • sublime配置文件
  • Vue ES6 Jade Scss Webpack Gulp
  • 关于Java中分层中遇到的一些问题
  • 后端_ThinkPHP5
  • 将回调地狱按在地上摩擦的Promise
  • 免费小说阅读小程序
  • 前端设计模式
  • 如何借助 NoSQL 提高 JPA 应用性能
  • 协程
  • 移动互联网+智能运营体系搭建=你家有金矿啊!
  • 用jQuery怎么做到前后端分离
  • 在Mac OS X上安装 Ruby运行环境
  • MiKTeX could not find the script engine ‘perl.exe‘ which is required to execute ‘latexmk‘.
  • 如何通过报表单元格右键控制报表跳转到不同链接地址 ...
  • 微龛半导体获数千万Pre-A轮融资,投资方为国中创投 ...
  • 新海诚画集[秒速5センチメートル:樱花抄·春]
  • 资深实践篇 | 基于Kubernetes 1.61的Kubernetes Scheduler 调度详解 ...
  • #define
  • $.ajax中的eval及dataType
  • (03)光刻——半导体电路的绘制
  • (1/2)敏捷实践指南 Agile Practice Guide ([美] Project Management institute 著)
  • (二十五)admin-boot项目之集成消息队列Rabbitmq
  • (附源码)spring boot网络空间安全实验教学示范中心网站 毕业设计 111454
  • (附源码)springboot掌上博客系统 毕业设计063131
  • (附源码)ssm航空客运订票系统 毕业设计 141612
  • (教学思路 C#之类三)方法参数类型(ref、out、parmas)
  • (企业 / 公司项目)前端使用pingyin-pro将汉字转成拼音
  • (源码版)2024美国大学生数学建模E题财产保险的可持续模型详解思路+具体代码季节性时序预测SARIMA天气预测建模
  • ***php进行支付宝开发中return_url和notify_url的区别分析
  • .Net Core与存储过程(一)
  • .NET 服务 ServiceController
  • .NET 设计模式初探
  • /usr/lib/mysql/plugin权限_给数据库增加密码策略遇到的权限问题
  • @data注解_SpringBoot 使用WebSocket打造在线聊天室(基于注解)
  • @KafkaListener注解详解(一)| 常用参数详解
  • @ModelAttribute注解使用