基於stm32的自定義HID裝置開發與上位機通訊實現(附原始碼)
阿新 • • 發佈:2019-02-14
現在主流的安卓手機資料連線線,Mini-usb、Micro-usb,Type-c,產品追隨主流,非聯網裝置,摒棄ST-LINK、JLINK,直接用usb資料傳輸升級。主要實現與HID裝置的通訊即人機互動。本文主要介紹了HID裝置的下位機通訊連線與上位機裝置識別。
下位機:
1.準備工作,所需檔案,如下圖所示:
2.環境搭建:建立keil開發環境檔案,並新增相應的原始檔,不作詳細解釋。
3、在platform_config.h檔案中修改相應的gpio口,只需要修改UDBDP,因為這個gpio口接了一個上拉電阻。
#define USB_DISCONNECT GPIOA #define USB_DISCONNECT_PIN GPIO_Pin_12 #define RCC_APB2Periph_GPIO_DISCONNECT RCC_APB2Periph_GPIOB
4、加入usb裝置初始化
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIO_DISCONNECT, ENABLE);
GPIO_InitStructure.GPIO_Pin = USB_DISCONNECT_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(USB_DISCONNECT, &GPIO_InitStructure);
5.在usb_desc.c中修改裝置描述符(PID和VID):
6、設定USB時鐘
void Set_USBClock(void) {
RCC_OTGFSCLKConfig(RCC_OTGFSCLKSource_PLLVCO_Div3);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_OTG_FS,ENABLE);
RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USB, ENABLE); }
上位機:
主要呼叫了windows系統庫user32.dll、hid.dll和setupapi.dll
以下是呼叫windows的API的函式 ,下面關於API介面部分呼叫舉例:
// 獲得GUID
[DllImport("hid.dll")]
private static extern void HidD_GetHidGuid(ref Guid HidGuid);
//過濾裝置,獲取需要的裝置
[DllImport("setupapi.dll", SetLastError = true)]
private static extern IntPtr SetupDiGetClassDevs(ref Guid ClassGuid, uint Enumerator, IntPtr HwndParent, DIGCF Flags);
[DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern Boolean SetupDiDestroyDeviceInfoList(IntPtr deviceInfoSet);
//獲取裝置,true獲取到
[DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern Boolean SetupDiEnumDeviceInterfaces(IntPtr deviceInfoSet, IntPtr deviceInfoData, ref Guid interfaceClassGuid, UInt32 memberIndex, ref SP_DEVICE_INTERFACE_DATA deviceInterfaceData);
上位機關鍵處理:非UI執行緒訪問介面執行緒以處理及資料傳送。
示例①接收資料處理:
myHid.DataReceived += new EventHandler<HID.Report>(myhid_DataReceived); /// <summary>
/// 事件:資料到達,處理此事件以接收輸入資料
/// </summary>
public event EventHandler<Report> DataReceived;
protected virtual void OnDataReceived(Report e)
{
DataReceived.Invoke(this, e);
}
/// <summary>
/// 建立一個非UI執行緒訪問介面執行緒
/// </summary>
string receiveData = "";
//資料到達事件
protected void myhid_DataReceived(object sender, report e)
{
RecDataBuffer = e.reportBuff;
receiveData = "";
receiveData = new ASCIIEncoding().GetString(RecDataBuffer);
//MessageBox.Show(receiveData);
SetShootRecoordText(receiveData);
}
private delegate void SetShootRecoordTextCallback(string text);
//在給textBox1.text賦值的地方呼叫以下方法即可
private void SetShootRecoordText(string text)
{
// InvokeRequired需要比較呼叫執行緒ID和建立執行緒ID
// 如果它們不相同則返回true
if (this.msg.InvokeRequired)
{
SetShootRecoordTextCallback d = new SetShootRecoordTextCallback(SetShootRecoordText);
this.Invoke(d, new object[] { text });
}
else
{
this.msg.Text += text;
msg.SelectionStart = msg.Text.Length;
msg.ScrollToCaret();
}
}
/// <summary>
/// 非同步讀取結束,發出有資料到達事件
/// </summary>
/// <param name="iResult">這裡是輸入報告的陣列</param>
private void ReadCompleted(IAsyncResult iResult)
{
byte[] readBuff = (byte[])(iResult.AsyncState);
try
{
if (deviceOpened == true)
{
hidDevice.EndRead(iResult);//讀取結束,如果讀取錯誤就會產生一個異常
byte[] reportData = new byte[readBuff.Length - 1];
for (int i = 1; i < readBuff.Length; i++)
reportData[i - 1] = readBuff[i];
Report e = new Report(readBuff[0], reportData);
OnDataReceived(e); //發出資料到達訊息
BeginAsyncRead();//啟動下一次讀操作
}
}
catch (IOException e)//讀寫錯誤,裝置可能已經被移除
{
EventArgs ex = new EventArgs();
OnDeviceRemoved(ex);//發出裝置移除訊息
}
}
示例②資料傳送處理:
public readonly byte reportID;
public readonly byte[] reportBuff;
public Report(byte id, byte[] arrayBuff)
{
reportID = id;
reportBuff = arrayBuff;
}
/// <summary>
///
/// </summary>
/// <param name="buffer"></param>
/// <returns></returns>
public HID_RETURN Write(Report r)
{
if (deviceOpened)
{
try
{
byte[] buffer = new byte[outputReportLength];
buffer[0] = r.reportID;
int maxBufferLength = 0;
if (r.reportBuff.Length < outputReportLength - 1)
maxBufferLength = r.reportBuff.Length;
else
maxBufferLength = outputReportLength - 1;
for (int i = 1; i <= maxBufferLength; i++)
buffer[i] = r.reportBuff[i - 1];
hidDevice.Write(buffer, 0, OutputReportLength);
}
catch
{
EventArgs ex = new EventArgs();
OnDeviceRemoved(ex);//發出裝置移除訊息
}
}
return HID_RETURN.WRITE_FAILD;
}
上位機避免應用程式執行卡死,主要方式是定義委託,用非UI執行緒控制去訪問控制元件。
測試結果:
/// <summary>
/// 向下位機發送資料,下位機收到應答返回應答。
/// </summary> 關鍵點:實現Encoding.Default.GetBytes(str)
/// <param name="sender"></param>
/// <param name="e"></param>
private void LED_CheckedChanged(object sender, EventArgs e)
{
Byte[] data = new Byte[64];
string str = "hello stm32!";
if (LedCheck.CheckState == CheckState.Checked)
{
//data[1] = Convert.ToByte(0x01););
LedCheck.BackColor = Color.Green;
}
else
{
LedCheck.BackColor = GroupBox.DefaultBackColor;
}
data = Encoding.Default.GetBytes(str);
Report r = new Report(0, data);
myHid.Write(r);
}
}
int main(void)
{
static int flag=0;
uint8_t ret=-1;
uint8_t Rcvdata[64];
uint8_t Sendata[64]="hello hid !";
Set_System();//系統時鐘初始化
USB_Interrupts_Config();
Set_USBClock();
USB_Init();
LED_Initializes();
LED_ON;
while(1)
{
ret=USB_GetData(Rcvdata,sizeof(Rcvdata));
if(ret==(sizeof(Rcvdata))&& strcmp(Rcvdata,"hello stm32!")==0)
{
flag=1;
}
if(flag==1)
{
USB_SendData(Rcvdata,sizeof(Rcvdata));
delay(10000000);
USB_SendData(Sendata,sizeof(Sendata));
delay(10000000);
FreeUserBuffer(ENDP2, EP_DBUF_OUT);
ClearDTOG_TX(ENDP2);
memset(Rcvdata,sizeof(uint8_t),sizeof(Rcvdata));
flag=0;
}
FreeUserBuffer(ENDP1,EP_DBUF_IN);
ClearDTOG_RX(ENDP1);
LED_OFF;
delay(1000000);
LED_ON;
delay(1000000);
}
}
上位機(c#,vs2017):點選下載
下位機(stm32f103,keil5):點選下載