串口調試在項目中被使用越來越多,串口資源的緊缺也變的尤爲突出。很多本本人羣,更是深有體會,不準備一個USB轉串口工具就沒辦法進行開發。本章節來簡單概述STM32低端芯片上的USB虛擬串口的移植。在官方DEMO中已經提供了現成的程序,這裏對修改方法做簡單說明。
官方demo及驅動程序,我存放在百度盤:
首先打開官方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
-
<span style="background-color: white;">/* 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);
-
}</span>
複製代碼
2,修改代碼hw_config.c刪除無用代碼,新建立2組,讀FIFO和寫FIFO的函數。後面會用到。
代碼如下:
代碼2
-
<span style="background-color: white;">/* 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****/</span>
複製代碼
這裏要講一下爲什麼要屏蔽SystemInit(),因爲demo只運行虛擬串口功能,在USB未插入的情況下,是進入低功耗狀態,插入時從低功耗狀態退出後會調用此函數。當然我們在項目中一般不會這樣,系統是否運行和插USB接口沒有聯繫。所以我在下文中把進入低功耗代碼屏蔽了,自然也就不用喚醒代碼了。
圖7
關於USB口使能控制引腳,需要根據開發板的引腳定義來修改宏定義platform_config.h文件中,筆者使用的是神舟3號開發板,控制信號剛好和demo相反,所以修改hw_config.c代碼如下:
代碼3
-
<span style="background-color: white;">/*******************************************************************************
-
* 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);
-
}
-
}</span>
複製代碼
3,現在修改USB 回調函數中的代碼usb_endp.c文件。使用下文代碼替換:
代碼4
-
<span style="background-color: white;">/* 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****/</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個字節。
|