银杏科技有限公司旗下技术文档发布平台 | |||
技术支持电话 | 0379-69926675-801 | ||
技术支持邮件 | Gingko@vip.163.com | ||
版本 | 日期 | 作者 | 修改内容 |
---|---|---|---|
V1.0 | 2020-07-30 | gingko | 初次建立 |
1.在主界面选择File–>New Project 或者直接点击ACCEE TO MCU SELECTOR 2.出现芯片型号选择,搜索自己芯片的型号,双击型号,或者点击Start Project进入配置 在搜索栏的下面,提供的各 种查找方式,可以选择芯片内核,型号,等等,可以帮助你查找芯片。本实验选取的芯片型号为:STM32H750IBKx。 3.配置RCC,使用外部时钟源 4.时基源选择SysTick 5.将PA10,PB7,PB8设置为GPIO_Output 6.将ARM_KEY对应的引脚PH7设置为GPIO_Intput 7.引脚模式配置 8.配置USB_OTG_FS 9.配置USB_DEVICE 10.时钟源设置,选择外部高速时钟源,配置为最大主频 11.工程文件的设置, 这里就是工程的各种配置 我们只用到有限几个,其他的默认即可 IDE我们使用的是 MDK V5.27 12.点击Code Generator,进行进一步配置
int main(void) { int i; unsigned char buffer[64]; unsigned char send_buffer[64]; /* MCU 配置 */ /* 重置所有外围设备,初始化Flash接口和Systick. */ HAL_Init(); /* 配置系统时钟 */ SystemClock_Config(); i2c.initialize(); axp152.initialize(); axp152.set_dcdc1(3500);//[ARM & FPGA] axp152.set_dcdc2(1200);//[FPGA INT] axp152.set_dcdc3(3300);//[DCOUT3] axp152.set_dcdc4(3300);//[DCOUT4] axp152.set_aldo1(3300);//[BK3] axp152.set_aldo2(3300);//[ALDOOUT2] axp152.set_dldo1(3300);//[BK0] axp152.set_dldo2(3300);//[BK1] HAL_Delay(200); /* 初始化所有已配置的外围设备 */ MX_GPIO_Init(); MX_USB_DEVICE_Init(); /* 无限循环 */ while (1) { if(_250ms_flag == 1){ _250ms_flag = 0; if(hUsbDeviceFS.dev_state == USBD_STATE_CONFIGURED){ memset(send_buffer,0,64); if(ARM_KEY_STATE == KEY_DOWN) sprintf((char *)send_buffer,"key:KEY PRESS"); else sprintf((char *)send_buffer,"key:"); USBD_HID_SendReport(&hUsbDeviceFS,send_buffer,64); } } //接收命令处理 if(usb_receive_flag == 1){ usb_receive_flag = 0; memcpy(buffer,usb_receive_buffer,usb_receive_counter); memset(usb_receive_buffer,0,usb_receive_counter); for(i = 0;i < 64;i++){ buffer[i] = tolower(buffer[i]); } command_process(buffer); } } }
/*发送HID报告*/ uint8_t USBD_HID_SendReport(USBD_HandleTypeDef *pdev, uint8_t *report, uint16_t len) { USBD_HID_HandleTypeDef *hhid = (USBD_HID_HandleTypeDef *)pdev->pClassData; if (pdev->dev_state == USBD_STATE_CONFIGURED) { if (hhid->state == HID_IDLE) { hhid->state = HID_BUSY; USBD_LL_Transmit(pdev, HID_EPIN_ADDR, report, len); //通过端点传输数据 } } return USBD_OK; }
static int command_process(unsigned char * buffer) { char *p; p = (char *)buffer; //led灯命令处理 if(memcmp(p,"led_red_on",strlen("led_red_on")) == 0){ LED_ON; }else if(memcmp(p,"led_red_off",strlen("led_red_off")) == 0){ LED_OFF; } return 0; }
void MX_USB_DEVICE_Init(void) { /* 初始化USB设备库,添加支持的类并启动该库 */ if (USBD_Init(&hUsbDeviceFS, &FS_Desc, DEVICE_FS) != USBD_OK) { Error_Handler(); }//初始化设备堆栈并加载类驱动程序 if (USBD_RegisterClass(&hUsbDeviceFS, &USBD_HID) != USBD_OK) { Error_Handler(); }//将类驱动程序链接到Device Core if (USBD_Start(&hUsbDeviceFS) != USBD_OK) { Error_Handler(); }//启动USB Device Core. HAL_PWREx_EnableUSBVoltageDetector(); //启用USB电压电平检测器。 }
static uint8_t USBD_HID_Init(USBD_HandleTypeDef *pdev, uint8_t cfgidx) { /* Open EP IN */ USBD_LL_OpenEP(pdev, HID_EPIN_ADDR, USBD_EP_TYPE_INTR, HID_EPIN_SIZE); //打开低级驱动程序的端点 pdev->ep_in[HID_EPIN_ADDR & 0xFU].is_used = 1U; pdev->pClassData = USBD_malloc(sizeof(USBD_HID_HandleTypeDef)); if (pdev->pClassData == NULL) { return USBD_FAIL; } ((USBD_HID_HandleTypeDef *)pdev->pClassData)->state = HID_IDLE; return USBD_OK; }
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace usb_hid { public partial class Form1 : Form { usb_hid_class usbhid = null; bool red_flag = true; public Form1() { InitializeComponent(); usbhid = new usb_hid_class(); button_red_on.Enabled = false; usbhid.DataReceived += usb_hid_data_receive; usbhid.DeviceRemoved += usb_hid_device_remove; } void usb_hid_device_remove(object sender, EventArgs e) { if (InvokeRequired) { Invoke(new EventHandler(usb_hid_device_remove), new object[] { sender, e }); } else { MessageBox.Show("设备移除", "提示"); } } void usb_hid_data_receive(object sender, EventArgs e) { int temp; string str; report myRP = (report)e; if (InvokeRequired) { try { Invoke(new EventHandler(usb_hid_data_receive), new object[] { sender, e }); } catch (Exception) { return; } } else { str = usb_hid_class.ByteToHexString(myRP.reportBuff); temp = str.IndexOf("key:"); if (temp >= 0) { key_display.Text = str.Substring(4, str.Length - 4); key_display.Select(key_display.TextLength, 0); key_display.ScrollToCaret(); } } } private void button_connect_Click(object sender, EventArgs e) { int temp; foreach (string device in usbhid.GetDeviceList()) { temp = device.IndexOf("#vid_0483&pid_5720#"); if (temp >= 0) { usbhid.OpenUSBHid(device); button_red_on.Enabled = true; button_green_on.Enabled = true; button_blue_on.Enabled = true; return; } } MessageBox.Show("未发现设备", "提示"); } private void button_red_on_Click(object sender, EventArgs e) { string data; if (red_flag) { red_flag = false; button_red_on.Text = "红灯灭"; data = " led_red_on"; while (data.Length < 65) { data += ' '; } usbhid.WriteUSBHID(data); } else { red_flag = true; button_red_on.Text = "红灯亮"; data = " led_red_off"; while (data.Length < 65) { data += ' '; } usbhid.WriteUSBHID(data); } } private void Form1_Load(object sender, EventArgs e) { } } }