历史上的今天
今天是:2025年03月25日(星期二)
2020年03月25日 | stm32 usb和安卓手机的通讯过程
2020-03-25 来源:eefocus
前段时间公司开发了一个安卓外设,主要是用某宝淘来的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,
史海拾趣
|
关于S3C6410从SD\"启动设置\"和\"操作\"问题,好像三星也没有说从SD卡启动可以不要NOR FLASH和NAND FLASH, 也没有提到如何把那些*.bin和*.nb0放到SD卡中,是不是直接支持NK也放到SD卡中从SD启动。不知道大家有没有 这方便的操作文档。 编译完了 ...… 查看全部问答> |
|
1.用STM32的芯片做主机,PIC16F677作为从机作数据采集。主机用模拟方式实现IIC通信,速率是400K,从机是通过配置相关的IIC寄存器实现。 2.上电后用示波器观察,主机有发送配置地址0XF0和10连续的共10个位的数据。 3.发送的地址和677的SSPADD配置地 ...… 查看全部问答> |
|
工作地点:北京 (面试地:北京) 招聘岗位:软件工程师、高级软件工程师 职责描述 1)负责通信产品软件模块设计、开发工作,完成相关的设计文档、代码编写。 2)参与软件模块的部分测试工作,完成测试用例的设计、执行与测试报告的输出。 3) ...… 查看全部问答> |




