[经验] STM32 USB虚拟串口(有源码)

来源:互联网 发布:淘宝批量上传 编辑:程序博客网 时间:2024/06/04 18:47

原文出处:http://bbs.elecfans.com/jishu_467116_1_1.html

串口调试在项目中被使用越来越多,串口资源的紧缺也变的尤为突出。很多本本人群,更是深有体会,不准备一个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
  1. <span style="background-color: white;">/* Includes ------------------------------------------------------------------*/
  2. #include "hw_config.h"
  3. #include "usb_lib.h"
  4. #include "usb_istr.h"


  5. /*******************************************************************************
  6. * Function Name  : USB_IRQHandler
  7. * Description    : This function handles USB Low Priority interrupts
  8. *                  requests.
  9. * Input          : None
  10. * Output         : None
  11. * Return         : None
  12. *******************************************************************************/
  13. #if defined(STM32L1XX_MD) || defined(STM32L1XX_HD)|| defined(STM32L1XX_MD_PLUS)|| defined (STM32F37X)
  14. void USB_LP_IRQHandler(void)
  15. #else
  16. void USB_LP_CAN1_RX0_IRQHandler(void)
  17. #endif
  18. {
  19.   USB_Istr();
  20. }

  21. /*******************************************************************************
  22. * Function Name  : USB_FS_WKUP_IRQHandler
  23. * Description    : This function handles USB WakeUp interrupt request.
  24. * Input          : None
  25. * Output         : None
  26. * Return         : None
  27. *******************************************************************************/

  28. #if defined(STM32L1XX_MD) || defined(STM32L1XX_HD)|| defined(STM32L1XX_MD_PLUS)
  29. void USB_FS_WKUP_IRQHandler(void)
  30. #else
  31. void USBWakeUp_IRQHandler(void)
  32. #endif
  33. {
  34.   EXTI_ClearITPendingBit(EXTI_Line18);
  35. }</span>
复制代码

2,修改代码hw_config.c删除无用代码,新建立2组,读FIFO和写FIFO的函数。后面会用到。
代码如下:
代码2
  1. <span style="background-color: white;">/* Includes ------------------------------------------------------------------*/

  2. #include "usb_lib.h"
  3. #include "usb_prop.h"
  4. #include "usb_desc.h"
  5. #include "hw_config.h"
  6. #include "usb_pwr.h"
  7. #include "Queue.h"


  8. /* Private typedef -----------------------------------------------------------*/
  9. /* Private define ------------------------------------------------------------*/
  10. /* Private macro -------------------------------------------------------------*/
  11. /* Private variables ---------------------------------------------------------*/
  12. ErrorStatus HSEStartUpStatus;
  13. USART_InitTypeDef USART_InitStructure;
  14. EXTI_InitTypeDef EXTI_InitStructure;


  15. #define USB_COM_RX_BUF_SIZE         (1024 + 256)
  16. #define USB_COM_TX_BUF_SIZE         (1024 + 256)

  17. static QUEUE8_t m_QueueUsbComRx         = {0};
  18. static QUEUE8_t m_QueueUsbComTx         = {0};
  19. static uint8_t  m_UsbComRxBuf[USB_COM_RX_BUF_SIZE]      = {0};     
  20. static uint8_t  m_UsbComTxBuf[USB_COM_TX_BUF_SIZE]      = {0};   

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

  23. extern LINE_CODING linecoding;

  24. /* Private function prototypes -----------------------------------------------*/
  25. /* Private functions ---------------------------------------------------------*/
  26. /*******************************************************************************
  27. * Function Name  : Set_System
  28. * Description    : Configures Main system clocks & power
  29. * Input          : None.
  30. * Return         : None.
  31. *******************************************************************************/
  32. void Set_System(void)
  33. {
  34.   GPIO_InitTypeDef GPIO_InitStructure;

  35.   QUEUE_PacketCreate(&m_QueueUsbComRx, m_UsbComRxBuf, sizeof(m_UsbComRxBuf));
  36.   QUEUE_PacketCreate(&m_QueueUsbComTx, m_UsbComTxBuf, sizeof(m_UsbComTxBuf));
  37.   
  38.   /* Enable USB_DISCONNECT GPIO clock */
  39.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIO_DISCONNECT, ENABLE);

  40.   /* Configure USB pull-up pin */
  41.   GPIO_InitStructure.GPIO_Pin = USB_DISCONNECT_PIN;
  42.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  43.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  44.   GPIO_Init(USB_DISCONNECT, &GPIO_InitStructure);
  45.   
  46.   /* Configure the EXTI line 18 connected internally to the USB IP */
  47.   EXTI_ClearITPendingBit(EXTI_Line18);
  48.   EXTI_InitStructure.EXTI_Line = EXTI_Line18; 
  49.   EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
  50.   EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  51.   EXTI_Init(&EXTI_InitStructure);

  52.   
  53. }

  54. /*******************************************************************************
  55. * Function Name  : Set_USBClock
  56. * Description    : Configures USB Clock input (48MHz)
  57. * Input          : None.
  58. * Return         : None.
  59. *******************************************************************************/
  60. void Set_USBClock(void)
  61. {
  62.   /* Select USBCLK source */
  63.   RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5);
  64.   
  65.   /* Enable the USB clock */
  66.   RCC_APB1PeriphClockCmd(RCC_APB1Periph_USB, ENABLE);
  67. }

  68. /*******************************************************************************
  69. * Function Name  : Enter_LowPowerMode
  70. * Description    : Power-off system clocks and power while entering suspend mode
  71. * Input          : None.
  72. * Return         : None.
  73. *******************************************************************************/
  74. void Enter_LowPowerMode(void)
  75. {
  76.   /* Set the device state to suspend */
  77.   bDeviceState = SUSPENDED;
  78. }

  79. /*******************************************************************************
  80. * Function Name  : Leave_LowPowerMode
  81. * Description    : Restores system clocks and power while exiting suspend mode
  82. * Input          : None.
  83. * Return         : None.
  84. *******************************************************************************/
  85. void Leave_LowPowerMode(void)
  86. {
  87.   DEVICE_INFO *pInfo = &Device_Info;

  88.   /* Set the device state to the correct state */
  89.   if (pInfo->Current_Configuration != 0)
  90.   {
  91.     /* Device configured */
  92.     bDeviceState = CONFIGURED;
  93.   }
  94.   else
  95.   {
  96.     bDeviceState = ATTACHED;
  97.   }
  98.   /*Enable SystemCoreClock*/
  99. //  SystemInit();
  100. }

  101. /*******************************************************************************
  102. * Function Name  : USB_Interrupts_Config
  103. * Description    : Configures the USB interrupts
  104. * Input          : None.
  105. * Return         : None.
  106. *******************************************************************************/
  107. void USB_Interrupts_Config(void)
  108. {
  109.   NVIC_InitTypeDef NVIC_InitStructure; 

  110.   NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn;
  111.   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
  112.   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  113.   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  114.   NVIC_Init(&NVIC_InitStructure);
  115.   
  116.     /* Enable the USB Wake-up interrupt */
  117.   NVIC_InitStructure.NVIC_IRQChannel = USBWakeUp_IRQn;
  118.   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  119.   NVIC_Init(&NVIC_InitStructure);
  120. }

  121. /*******************************************************************************
  122. * Function Name  : USB_Cable_Config
  123. * Description    : Software Connection/Disconnection of USB Cable
  124. * Input          : None.
  125. * Return         : Status
  126. *******************************************************************************/
  127. void USB_Cable_Config (FunctionalState NewState)
  128. {
  129.   if (NewState == DISABLE)
  130.   {
  131.     GPIO_ResetBits(USB_DISCONNECT, USB_DISCONNECT_PIN);
  132.   }
  133.   else
  134.   {
  135.     GPIO_SetBits(USB_DISCONNECT, USB_DISCONNECT_PIN);
  136.   }
  137. }

  138. /*******************************************************************************
  139. * Function Name : void USB_Config(void)
  140. * Description   : USB系统初始化
  141. * Input         : 
  142. * Output        : 
  143. * Other         : 
  144. * Date          : 2014.11.28
  145. *******************************************************************************/
  146. void USB_Config(void)
  147. {
  148.     Set_System();

  149.     Set_USBClock();

  150.     USB_Interrupts_Config();

  151.     USB_Init();
  152. }

  153. /*******************************************************************************
  154. * Function Name : uint32_t USB_RxRead(uint8_t *buffter, uint32_t buffterSize)
  155. * Description   : 从USB接收缓存中读数据
  156. * Input         : 
  157. * Output        : 
  158. * Other         : 
  159. * Date          : 2014.11.28
  160. *******************************************************************************/
  161. uint32_t USB_RxRead(uint8_t *buffter, uint32_t buffterSize)
  162. {
  163.     return QUEUE_PacketOut(&m_QueueUsbComRx, buffter, buffterSize);
  164. }
  165. /*******************************************************************************
  166. * Function Name : uint32_t USB_RxWrite(uint8_t *buffter, uint32_t writeLen)
  167. * Description   : 写数据到USB接收缓存中
  168. * Input         : 
  169. * Output        : 
  170. * Other         : 
  171. * Date          : 2014.11.28
  172. *******************************************************************************/
  173. uint32_t USB_RxWrite(uint8_t *buffter, uint32_t writeLen)
  174. {
  175.     return QUEUE_PacketIn(&m_QueueUsbComRx, buffter, writeLen);
  176. }
  177. /*******************************************************************************
  178. * Function Name : uint32_t USB_TxRead(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_TxRead(uint8_t *buffter, uint32_t buffterSize)
  186. {
  187.     return QUEUE_PacketOut(&m_QueueUsbComTx, buffter, buffterSize);;
  188. }
  189. /*******************************************************************************
  190. * Function Name : uint32_t USB_TxWrite(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_TxWrite(uint8_t *buffter, uint32_t writeLen)
  198. {
  199.     return QUEUE_PacketIn(&m_QueueUsbComTx, buffter, writeLen);
  200. }



  201. /*******************************************************************************
  202. * Function Name  : Get_SerialNum.
  203. * Description    : Create the serial number string descriptor.
  204. * Input          : None.
  205. * Output         : None.
  206. * Return         : None.
  207. *******************************************************************************/
  208. void Get_SerialNum(void)
  209. {
  210.   uint32_t Device_Serial0, Device_Serial1, Device_Serial2;

  211.   Device_Serial0 = *(uint32_t*)ID1;
  212.   Device_Serial1 = *(uint32_t*)ID2;
  213.   Device_Serial2 = *(uint32_t*)ID3;  

  214.   Device_Serial0 += Device_Serial2;

  215.   if (Device_Serial0 != 0)
  216.   {
  217.     IntToUnicode (Device_Serial0, &Virtual_Com_Port_StringSerial[2] , 8);
  218.     IntToUnicode (Device_Serial1, &Virtual_Com_Port_StringSerial[18], 4);
  219.   }
  220. }

  221. /*******************************************************************************
  222. * Function Name  : HexToChar.
  223. * Description    : Convert Hex 32Bits value into char.
  224. * Input          : None.
  225. * Output         : None.
  226. * Return         : None.
  227. *******************************************************************************/
  228. static void IntToUnicode (uint32_t value , uint8_t *pbuf , uint8_t len)
  229. {
  230.   uint8_t idx = 0;
  231.   
  232.   for( idx = 0 ; idx < len ; idx ++)
  233.   {
  234.     if( ((value >> 28)) < 0xA )
  235.     {
  236.       pbuf[ 2* idx] = (value >> 28) + '0';
  237.     }
  238.     else
  239.     {
  240.       pbuf[2* idx] = (value >> 28) + 'A' - 10; 
  241.     }
  242.     
  243.     value = value << 4;
  244.     
  245.     pbuf[ 2* idx + 1] = 0;
  246.   }
  247. }

  248. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/</span>
复制代码
这里要讲一下为什么要屏蔽SystemInit(),因为demo只运行虚拟串口功能,在USB未插入的情况下,是进入低功耗状态,插入时从低功耗状态退出后会调用此函数。当然我们在项目中一般不会这样,系统是否运行和插USB接口没有联系。所以我在下文中把进入低功耗代码屏蔽了,自然也就不用唤醒代码了。
图7
关于USB口使能控制引脚,需要根据开发板的引脚定义来修改宏定义platform_config.h文件中,笔者使用的是神舟3号开发板,控制信号刚好和demo相反,所以修改hw_config.c代码如下:

代码3
  1. <span style="background-color: white;">/*******************************************************************************
  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. }</span>
复制代码

3,现在修改USB 回调函数中的代码usb_endp.c文件。使用下文代码替换:
代码4
  1. <span style="background-color: white;">/* 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. /* Private typedef -----------------------------------------------------------*/
  9. /* Private define ------------------------------------------------------------*/

  10. /* Interval between sending IN packets in frame number (1 frame = 1ms) */
  11. #define VCOMPORT_IN_FRAME_INTERVAL             5

  12. /* Private macro -------------------------------------------------------------*/
  13. /* Private variables ---------------------------------------------------------*/
  14. static uint8_t txBuffter[VIRTUAL_COM_PORT_DATA_SIZE] = {0};
  15. static volatile uint8_t txFlg = 0;
  16. static volatile uint32_t FrameCount = 0;


  17. /* Private function prototypes -----------------------------------------------*/
  18. /* Private functions ---------------------------------------------------------*/

  19. /*******************************************************************************
  20. * Function Name  : EP1_IN_Callback
  21. * Description    :
  22. * Input          : None.
  23. * Output         : None.
  24. * Return         : None.
  25. *******************************************************************************/
  26. void EP1_IN_Callback (void)
  27. {
  28.     uint16_t len = 0;
  29.     
  30.     if (1 == txFlg)
  31.     {
  32.         len = USB_TxRead(txBuffter, sizeof(txBuffter));

  33.         if (len > 0)
  34.         {
  35.             UserToPMABufferCopy(txBuffter, ENDP1_TXADDR, len);
  36.             SetEPTxCount(ENDP1, len);
  37.             SetEPTxValid(ENDP1); 
  38.             FrameCount = 0;
  39.         }
  40.         else
  41.         {
  42.             txFlg = 0;
  43.         }
  44.     }
  45. }

  46. /*******************************************************************************
  47. * Function Name  : EP3_OUT_Callback
  48. * Description    :
  49. * Input          : None.
  50. * Output         : None.
  51. * Return         : None.
  52. *******************************************************************************/
  53. void EP3_OUT_Callback(void)
  54. {
  55.   static uint8_t buffter[VIRTUAL_COM_PORT_DATA_SIZE] = {0};

  56.   uint16_t USB_Rx_Cnt;
  57.   
  58.   /* Get the received data buffer and update the counter */
  59.   USB_Rx_Cnt = USB_SIL_Read(EP3_OUT, buffter);
  60.   
  61.   /* USB data will be immediately processed, this allow next USB traffic being 
  62.   NAKed till the end of the USART Xfer */
  63.   USB_RxWrite(buffter, USB_Rx_Cnt);

  64.   /* Enable the receive of data on EP3 */
  65.   SetEPRxValid(ENDP3);

  66. }


  67. /*******************************************************************************
  68. * Function Name  : SOF_Callback / INTR_SOFINTR_Callback
  69. * Description    :
  70. * Input          : None.
  71. * Output         : None.
  72. * Return         : None.
  73. *******************************************************************************/
  74. void SOF_Callback(void)
  75. {
  76.     uint16_t len = 0;

  77.     if(bDeviceState == CONFIGURED)
  78.     {
  79.         if (0 == txFlg)
  80.         {
  81.             if (FrameCount++ == VCOMPORT_IN_FRAME_INTERVAL)
  82.             {
  83.                 /* Reset the frame counter */
  84.                 FrameCount = 0;

  85.                 /* Check the data to be sent through IN pipe */
  86.                 len = USB_TxRead(txBuffter, sizeof(txBuffter));

  87.                 if (len > 0)
  88.                 {
  89.                     UserToPMABufferCopy(txBuffter, ENDP1_TXADDR, len);
  90.                     SetEPTxCount(ENDP1, len);
  91.                     SetEPTxValid(ENDP1);

  92.                     txFlg = 1;
  93.                 }
  94.             }
  95.         }
  96.     }  
  97. }
  98. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/</span>
复制代码
这里讲下大概意思,函数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个字节。

2 0
原创粉丝点击