目錄: 小孫想要總結這一年來學到的關於stm32的USB相關知識,但又不知道怎麼總結,於是決定 從頭開始調試固件庫代碼,直到實現USB功能為止! 首先準備參照正點原子《第88講 USB虛擬串口實驗-M3》,把HID相關庫包含進工程中,工 程選用正點原子的USART試驗。因為目前手裡的開發板是“微雪電 ...
目錄: 小孫想要總結這一年來學到的關於stm32的USB相關知識,但又不知道怎麼總結,於是決定 從頭開始調試固件庫代碼,直到實現USB功能為止! 首先準備參照正點原子《第88講 USB虛擬串口實驗-M3》,把HID相關庫包含進工程中,工 程選用正點原子的USART試驗。因為目前手裡的開發板是“微雪電子”的stm32f103cbt6開發板, 其硬體串口為:USART1(PA9和PA10)和SART2(PA2和PA3)。準備先把正點原子《實驗4-串口 實驗》實例代碼移植到這款開發板上,好方便以後調試使用。 因為正點原子stm32f1開發板使用的晶振和手裡的開發板一樣都是8M的,所以在Keil里直接修 改MCU型號和Flash大小後準備編譯下載。沒有報錯。





然後再重新燒錄,結果一次就成功了。













































第三步程式中搜索結果:




--------------- 第四步---------------------------------------------------------------------------
移植Custom_HID代碼到正點原子的串口代碼中,希望一切順利。 參照PPT《第88講 USB虛擬串口實驗-M3》,開始進行下列移植: 1.打開ST官方的Custom_HID常式,如下圖所示






xxxxxxxxxx
1
1
1
#include "sys.h"
另外, 去掉#define USE_STM32303C_EVAL等巨集定義, 無需使用
②,修改hw_config.c。
2.1,去掉stm32_it.h,並添加一些其他頭文件,如下:
<wiz_code_mirror>
xxxxxxxxxx
10
10
1
2
3
4
5
6
7
8
9
10
因為HID常式中此處沒有stm32_it.h,所以直接替換成上面的代碼。
2.2,去掉HSEStartUpStatus和EXTI_InitStructure等結構體和變數的定義,
, 採用如下代碼替代 :


xxxxxxxxxx
10
10
1
//USB喚醒中斷服務函數
2
void USBWakeUp_IRQHandler(void)
3
{
4
EXTI_ClearITPendingBit(EXTI_Line18);//清除USB喚醒中斷掛起位
5
}//
6
USB中斷處理函數
7
void USB_LP_CAN1_RX0_IRQHandler(void)
8
{
9
USB_Istr();
10
}
2.4,修改Set_USBClock函數為:
2.5,修改Enter_LowPowerMode函數為:
2.6,修改Leave_LowPowerMode函數為:
2.7,修改USB_Interrupts_Config函數為:
2.8,修改USB_Cable_Config函數為:
2.9,刪除USART_Config_Default函數,新增USB_Port_Set函數,代碼如下:
因為當前常式中沒有虛擬串口,所以不用刪除,直接添加USB_Port_Set函數即可。
2.10,修改USART_Config函數為:(和串口相關,不用管):
2.11,修改USB_To_USART_Send_Data函數為:(和串口相關,不用管):
2.12,刪除Handle_USBAsynchXfer和USART_To_USB_Send_Data這兩個
函數, 然後, 新增USB_USART_SendData函數, 用於虛擬串口發送一個位元組
到USB(這裡實際上只寫到了發送FIFO, 最終還是由EP1_IN_Callback函數實
現輸出給USB):(和串口相關,不用管):
2.13, 刪除IntToUnicode函數前面的static關鍵字,
新增usb_printf函數, 用於實現USB虛擬串口的printf, 代碼如下: (和串口相關,不用管):
2.14, 修改hw_config.h, 刪除MASS_MEMORY_START等巨集定義, 然後, 新
增USB_USART_TXFIFO_SIZE等巨集定義和結構體, 代碼如下: (和串口相關,不用管):
2.15, 修改hw_config.h, 新增IntToUnicode、USB_Port_Set
USB_USART_SendData、和usb_printf等函數的聲明, 代碼如下: (和串口相關,不用管):
④, 修改usb_endp.c。
4.1, 修改EP1_IN_Callback函數為: (和串口相關,不用管):
4.2, 修改SOF_Callback函數為:(工程中沒有這個函數的定義,直接從虛擬串口的工程中複製過來):
⑤, 修改usb_prop.c。 本常式沒有用到USART_Config_Default函數, 所以:
註釋掉Virtual_Com_Port_init函數裡面對該函數的調用:(工程中沒有這個函數,不用管):
⑥, 修改usb_pwr.c。 修改Suspend函數為:
6, 修改main.c。
因為虛擬串口常式中的main.c裡面執行的是串口收發,所以這裡只用其初始化部分,while(1)
裡面什麼也不做。結果編譯後報錯如下:

xxxxxxxxxx
1
1
1
void GPIO_Configuration(void)
但是程式中並未調用它,所以可以直接屏蔽掉。
重新編譯後,第一個報錯:KEY_BUTTON_EXTI_LINE未定義。定位到函數如下:
<wiz_code_mirror>
xxxxxxxxxx
1
1
1
void EXTI_Configuration(void)
但是程式中並未調用它,所以可以直接屏蔽掉。
重新編譯後,第一個報錯:ADC1_DR_Address未定義。定位到函數如下:
<wiz_code_mirror>
xxxxxxxxxx
1
1
1
void ADC_Configuration(void)
但是程式中並未調用它,所以可以直接屏蔽掉。
重新編譯後,第一個報錯:LED1未定義。定位到函數如下:
<wiz_code_mirror>
xxxxxxxxxx
1
1
1
void EP1_OUT_Callback(void)
該函數的功能是接收從PC端發過來的數據。不能屏蔽掉。因為函數內容可以
自定義,於是把裡面原來開發板上的功能(主要是開關LED)屏蔽掉。只接收數據,
不做處理。
重新編譯後,第一個報錯:LED1未定義。定位到函數如下:第一個報錯:還是LED1未定義。定位到函數如下: <wiz_code_mirror>
xxxxxxxxxx
1
1
1
void CustomHID_Status_In(void)
該函數的功能是:狀態輸入。雖然不知道具體是做什麼的,但是根據網上看過
的HID程式資料,這個函數不能刪除。於是只屏蔽掉裡面關於LED等信息的語句。
重新編譯後,只有3個錯誤了:

第一個報錯:bDeviceState未定義。定位到函數如下: <wiz_code_mirror>
xxxxxxxxxx
1
1
1
void SOF_Callback(void)
關於這個函數,網上找到下列解釋:
函數SOF_Callback定時查詢用戶是否有要發送的數據,如果有則進行發送,
在發送完成後會觸發發送中斷EP1_IN_Callback函數,如果發送完畢就不調用
SetEPTxValid(ENDP1)函數,發送完成後就不會再觸發EP1_IN_Callback函數。
所以就不刪除了。bDeviceState在代碼中肯定是定義過了的,應該是被屏蔽了。
搜索變數bDeviceState,看它的定義在什麼地方,為什麼被屏蔽了。結果如下:


xxxxxxxxxx
4
4
1
#include "hw_config.h"
2
#include "usb_lib.h"
3
#include "usb_istr.h"
4
#include "usb_prop.h"
乾脆直接把USB虛擬串口相同文件中包含的頭文件都包含進來編譯看看。結果如下:















因為沒有刨根問底的精神,小孫這幾年的電子生涯一直是閉著眼睛瞎混的,沒有 好好的鑽研讓自己拿得出手的技術,所以每天都生活在自卑和失落中。 綜上所述,在屏蔽掉uart_init()函數後,程式並不能被PC識別為HID設備。 俗話說,人不能讓不撞南牆不回頭,這裡是裝了南牆必須回頭,否則沒有哪個 老闆願意給你薪水。所以,小孫準備從ST的HID工程本身提供的初始化HID方式 嘗試實現HID功能。畢竟,網上大多數HID工程初始化部分都和ST的HID部分類似, 而跟他上面自己移植的不一樣。 ****************** 根據ST的HID修改工程代碼 *********************** 預想中的修改步驟如下: 第一步:先用HID工程中的初始化代碼替換上面工程中的初始化代碼; 第二步:根據報錯,修改上面步驟中被修改過的相關函數,使HID初始化成功; 第三步:添加HID發送和接收函數,並實現功能; ---------------