串口調試在項目中被使用越來越多,串口資源的緊缺也變的尤為突出。很多本本人群,更是深有體會,不准備一個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
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也一起存放在上文章開始的鏈接中。
