關於STM32F103RC usb虛擬串列埠
轉載自:https://www.cnblogs.com/hiker-blogs/p/stm32_usb.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 /* 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也一起存放在上文章開始的連結中。