前段時間公司開發了一個安卓外設,主要是用某寶淘來的demo 在stm32F103的usb功能來和安卓設備的usb來通訊
敘述之前先來一個整體的框圖吧:
需要準備的設備有:
①.安卓手機或者安卓pad,(手機必須支持otg功能,否則就得用host功能了)
②.otg轉接線或者轉接頭
③.安卓數據線(一定要是能通訊數據的線纜,有些山寨的這個線只有2根線,沒有數據線)
④.帶有usb功能的單片機(這個usb需要能支持用戶自行定義)
接下來就開始搗鼓stm32單片機的程序了,下面是usb功能的代碼:
usb功能看了一下主要是借助hid的標準協議上衍生而來的一個usb標準設備
/******************** (C) COPYRIGHT 2010 STMicroelectronics ********************
* File Name : usb_desc.c
* Author : MCD Application Team
* Version : V3.2.1
* Date : 07/05/2010
* Description : Descriptors for Mass Storage Device
********************************************************************************
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*******************************************************************************/
/* Includes ------------------------------------------------------------------*/
#include "usb_desc.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Extern variables ----------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/* USB Standard Device Descriptor */
const u8 CustomHID_DeviceDescriptor[CUSTOMHID_SIZ_DEVICE_DESC] =
{
0x12, /*bLength */
USB_DEVICE_DESCRIPTOR_TYPE, /*bDescriptorType*/
0x00,0x02, /*bcdUSB */
0x00, /*bDeviceClass*/
0x00, /*bDeviceSubClass*/
0x00, /*bDeviceProtocol*/
0x08, /*bMaxPacketSize40*/
0x71,0x04, /*idVendor (0x0471)*/
0x08,0x24, /*idProduct = 0x2408*/
0x00,0x02, /*bcdDevice rel. 2.00*/
1, /*Index of string descriptor describing manufacturer */
2, /*Index of string descriptor describing product*/
3, /*Index of string descriptor describing the device serial number */
0x01 /*bNumConfigurations*/
}
; /* CustomHID_DeviceDescriptor */
/* USB Configuration Descriptor */
/* All Descriptors (Configuration, Interface, Endpoint, Class, Vendor */
const u8 CustomHID_ConfigDescriptor[ENEPOINT_NUM*7+18] =
{
0x09, /* bLength: Configuation Descriptor size */
USB_CONFIGURATION_DESCRIPTOR_TYPE, /* bDescriptorType: Configuration */
ENEPOINT_NUM*7+18, /*CUSTOMHID_SIZ_CONFIG_DESC, wTotalLength: Bytes returned */
0x00,
0x01, /* bNumInterfaces: 1 interface */
0x01, /* bConfigurationValue: Configuration value */
0x00, /* iConfiguration: Index of string descriptor describing
the configuration*/
0xC0, /* bmAttributes: Bus powered */
0x32, /* MaxPower 100 mA: this current is used for detecting Vbus */
/************** Descriptor of Custom HID interface ****************/
/* 09 */
0x09, /* bLength: Interface Descriptor size */
USB_INTERFACE_DESCRIPTOR_TYPE,/* bDescriptorType: Interface descriptor type */
0x00, /* bInterfaceNumber: Number of Interface */
0x00, /* bAlternateSetting: Alternate setting */
ENEPOINT_NUM, /* bNumEndpoints */
0x00, /* bInterfaceClass: HID=0X03,其他選0 */
0x00, /* bInterfaceSubClass : 1=BOOT, 0=no boot */
0x00, /* nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse */
0, /* iInterface: Index of string descriptor */
/********************以下只對HID的描述符 Descriptor of Custom HID HID ********************/
/* 18 */
// 0x09, /* bLength: HID Descriptor size */
// HID_DESCRIPTOR_TYPE, /* bDescriptorType: HID */
// 0x10, /* bcdHID: HID Class Spec release number */
// 0x01,
// 0x00, /* bCountryCode: Hardware target country */
// 0x01, /* bNumDescriptors: Number of HID class descriptors to follow */
// 0x22, /* bDescriptorType */
// CUSTOMHID_SIZ_REPORT_DESC,/* wItemLength: Total length of Report descriptor */
// 0x00,
/******************** Descriptor of Custom endpoints ******************/
0x07, /* bLength: Endpoint Descriptor size */
USB_ENDPOINT_DESCRIPTOR_TYPE, /* bDescriptorType: */
/* Endpoint descriptor type */
0x01, /* bEndpointAddress: */
/* Endpoint Address (OUT) */
USB_ENDPOINT_TYPE_BULK,/* bmAttributes: Interrupt endpoint */
0x10,0x00, /* wMaxPacketSize: 32 Bytes max */
0x20, /* bInterval: Polling Interval (20 ms) */
/* 27 */
0x07, /* bLength: Endpoint Descriptor size */
USB_ENDPOINT_DESCRIPTOR_TYPE, /* bDescriptorType: */
0x81, /* bEndpointAddress: Endpoint Address (IN) */
USB_ENDPOINT_TYPE_BULK, /* bmAttributes: Interrupt endpoint */
0x10, 0x00, /* wMaxPacketSize: 32 Bytes max */
0x20, /* bInterval: Polling Interval (32 ms) */
/* 34 */
0x07, /* bLength: Endpoint Descriptor size */
USB_ENDPOINT_DESCRIPTOR_TYPE, /* bDescriptorType: */
/* Endpoint descriptor type */
0x82, /* bEndpointAddress: */
/* Endpoint Address (OUT) */
USB_ENDPOINT_TYPE_BULK,/* bmAttributes: Interrupt endpoint */
0x40,0x00, /* wMaxPacketSize: 512 Bytes max */
0x20 /* bInterval: Polling Interval (20 ms) */
}
;
/* USB String Descriptors (optional) */
const u8 CustomHID_StringLangID[CUSTOMHID_SIZ_STRING_LANGID] =
{
CUSTOMHID_SIZ_STRING_LANGID,
USB_STRING_DESCRIPTOR_TYPE,
0x09,
0x04
}
; /* LangID = 0x0409: U.S. English */
const u8 CustomHID_StringVendor[CUSTOMHID_SIZ_STRING_VENDOR] =
{
CUSTOMHID_SIZ_STRING_VENDOR, /* Size of Vendor string */
USB_STRING_DESCRIPTOR_TYPE, /* bDescriptorType*/
/* Manufacturer: "STMicroelectronics" */
'O', 0,
'T', 0,
'G', 0,
};
const u8 CustomHID_StringProduct[CUSTOMHID_SIZ_STRING_PRODUCT] =
{
CUSTOMHID_SIZ_STRING_PRODUCT, /* bLength */
USB_STRING_DESCRIPTOR_TYPE, /* bDescriptorType */
'S', 0,
'T', 0,
'M', 0,
'3', 0,
'2', 0,
'A',0,
'n',0,
'd',0,
'r',0,
'o',0,
'i',0,
'd',0,
' ',0,
'U',0,
'S',0,
'B',0,
' ',0,
'O', 0,
'T', 0,
'G', 0,
};
u8 CustomHID_StringSerial[CUSTOMHID_SIZ_STRING_SERIAL] =
{
CUSTOMHID_SIZ_STRING_SERIAL, /* bLength */
USB_STRING_DESCRIPTOR_TYPE, /* bDescriptorType */
'T', 0,
'a', 0,
'o', 0,
'b', 0,
'a', 0,
'o', 0,
'-', 0,
'B', 0,
'e', 0,
'i', 0,
'j', 0,
'i',0,
'n',0,
'g',0,
'Y',0,
上一篇:利用樹莓派打造STM32無線在線調試器!
下一篇:stm32在linux編譯,遠程調試
推薦閱讀最新更新時間:2025-06-23 02:48




設計資源 培訓 開發板 精華推薦
- EVAL-ADF4350EB2Z,用于無線 LAN 的 ADF4350 PLL 時鐘發生器的評估板
- BM-78-PICTAIL、BM78 PICtail Plus 評估板為經典 SPP 或藍牙低功耗藍牙數據應用提供快速原型設計和開發
- Wifi四位熒光管時鐘
- LTC3403 的典型應用 - 具有旁路晶體管的 1.5MHz、600mA 同步降壓型穩壓器
- 【浙江理工電賽】LED閃光燈電源(H題)
- LT1612、2V 至 0.9V 降壓轉換器
- SOCORAD32:一個可破解的開源 ESP32 業余無線電板,具有步話機功能和數據通信
- 使用 Aimtec 的 AM3G-4812SH30Z 的參考設計
- AM1DR-0503SH30Z 3.3V 1 瓦 DC-DC 轉換器的典型應用
- LTC7813IUH 寬輸入范圍至 24V/5A 低 IQ 級聯升壓 + 降壓穩壓器 (VMID = 28V) 的典型應用電路