银杏科技有限公司旗下技术文档发布平台 | |||
技术支持电话 | 0379-69926675-801 | ||
技术支持邮件 | Gingko@vip.163.com | ||
版本 | 日期 | 作者 | 修改内容 |
---|---|---|---|
V1.0 | 2020-03-11 | 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_HS 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]; HAL_Init(); SystemClock_Config(); i2c.initialize(); axp152.initialize(); axp152.set_dcdc1(3500);//[ARM & FPGA BK1/2/6 &OTHER] axp152.set_dcdc2(1200);//[FPGA INT & PLL D] axp152.set_aldo1(2500);//[FPGA PLL A] axp152.set_dcdc4(3300);//[POWER_OUTPUT] axp152.set_dcdc3(3300);//[FPGA BK4][Adjustable] axp152.set_aldo2(3300);//[FPGA BK3][Adjustable] axp152.set_dldo1(3300);//[FPGA BK7][Adjustable] axp152.set_dldo2(3300);//[FPGA BK5][Adjustable] HAL_Delay(200); MX_GPIO_Init(); MX_USB_DEVICE_Init(); LED_OFF; while (1) { if(_250ms_flag == 1){ _250ms_flag = 0; if(hUsbDeviceHS.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(&hUsbDeviceHS,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); } } }
void MX_USB_DEVICE_Init(void) { /* 初始化USB设备库,添加支持的类并启动该库 */ if (USBD_Init(&hUsbDeviceHS, &HS_Desc, DEVICE_HS) != USBD_OK) { Error_Handler(); } if (USBD_RegisterClass(&hUsbDeviceHS, &USBD_HID) != USBD_OK) { Error_Handler(); } if (USBD_Start(&hUsbDeviceHS) != USBD_OK) { Error_Handler(); } HAL_PWREx_EnableUSBVoltageDetector(); }
static uint8_t USBD_HID_Init (USBD_HandleTypeDef *pdev, uint8_t cfgidx) { uint8_t ret = 0; /* Open EP IN */ USBD_LL_OpenEP(pdev, HID_EPIN_ADDR, USBD_EP_TYPE_INTR, HID_EPIN_SIZE); /* Open Ep Out */ USBD_LL_OpenEP(pdev, HID_EPOUT_ADDR, USBD_EP_TYPE_INTR, HID_EPOUT_SIZE); /* 准备输出端点以接收下一个数据包 */ USBD_LL_PrepareReceive(pdev, HID_EPOUT_ADDR, usb_receive_buffer, HID_EPOUT_SIZE); pdev->pClassData = USBD_malloc(sizeof (USBD_HID_HandleTypeDef)); if(pdev->pClassData == NULL) { ret = 1; } else { ((USBD_HID_HandleTypeDef *)pdev->pClassData)->state = HID_IDLE; } return ret; }
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; }
* 此上位机测试软件使用Microsoft Visual Studio开发平台编写,用于实现上位机和下位机的双向数据传输,部分源码如下: <code c> 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) { } } }