CubeMX Stm32f4 Usb Custom Hid

HAL ve Open Stm32 ile çalışmalarımıza USB ile devam ediyoruz.


HID YAPILACAKLAR

1.CUBEMX ile CostumHid projesi ayarlanacak
2.usbd_custom_hid_if.c içerisine Hid Desc Eklenecek

Hid Desc

0x06, 0x00, 0xff,              // USAGE_PAGE (Generic Desktop)
0x09, 0x01,                    // USAGE (Vendor Usage 1)
0xa1, 0x01,                    // COLLECTION (Application)
0x85, 0x01,                    //   REPORT_ID (1)
0x09, 0x01,                    //   USAGE (Vendor Usage 1)
0x15, 0x00,                    //   LOGICAL_MINIMUM (0)
0x25, 0x01,                    //   LOGICAL_MAXIMUM (1)
0x75, 0x08,                    //   REPORT_SIZE (8)
0x95, 0x01,                    //   REPORT_COUNT (1)
0xB1, 0x82,                    //   FEATURE (Data,Var,Abs,Vol)
0x85, 0x01,                    //   REPORT_ID (1)
0x09, 0x01,                    //   USAGE (Vendor Usage 1)
0x91, 0x82,                    //   OUTPUT (Data,Var,Abs,Vol)
0x85, 0x02,                    //   REPORT_ID (2)
0x09, 0x02,                    //   USAGE (Vendor Usage 2)
0x15, 0x00,                    //   LOGICAL_MINIMUM (0)
0x25, 0x01,                    //   LOGICAL_MAXIMUM (1)
0x75, 0x08,                    //   REPORT_SIZE (8)
0x95, 0x01,                    //   REPORT_COUNT (1)
0xB1, 0x82,                    //   FEATURE (Data,Var,Abs,Vol)
0x85, 0x02,                    //   REPORT_ID (2)
0x09, 0x02,                    //   USAGE (Vendor Usage 2)
0x91, 0x82,                    //   OUTPUT (Data,Var,Abs,Vol)
0x85, 0x03,                    //   REPORT_ID (3)
0x09, 0x03,                    //   USAGE (Vendor Usage 3)
0x15, 0x00,                    //   LOGICAL_MINIMUM (0)
0x25, 0x01,                                      //   LOGICAL_MAXIMUM (255)
0x75, 0x08,                    //   REPORT_SIZE (8)
0x95, 0x01,                    //   REPORT_COUNT (1)
0xB1, 0x82,                    //   FEATURE (Data,Var,Abs,Vol)
0x85, 0x03,                    //   REPORT_ID (3)
0x09, 0x03,                    //   USAGE (Vendor Usage 3)
0x91, 0x82,                    //   OUTPUT (Data,Var,Abs,Vol)
0x85, 0x04,                    //   REPORT_ID (4)
0x09, 0x04,                    //   USAGE (Vendor Usage 4)
0x15, 0x00,                    //   LOGICAL_MINIMUM (0)
0x25, 0x01,                                      //   LOGICAL_MAXIMUM (255)
0x75, 0x08,                    //   REPORT_SIZE (8)
0x95, 0x01,                    //   REPORT_COUNT (1)
0xB1, 0x82,                    //   FEATURE (Data,Var,Abs,Vol)
0x85, 0x04,                    //   REPORT_ID (4)
0x09, 0x04,                    //   USAGE (Vendor Usage 4)
0x91, 0x82,                    //   OUTPUT (Data,Var,Abs,Vol)
0x85, 0x05,                    //   REPORT_ID (5)
0x09, 0x05,                    //   USAGE (Vendor Usage 5)
0x75, 0x08,                    //   REPORT_SIZE (8)
0x95, 0x04,                    //   REPORT_COUNT (4)
0x81, 0x82,                    //   INPUT (Data,Var,Abs,Vol)
0x85, 0x06,                    //   REPORT_ID (6)
0x09, 0x06,                    //   USAGE (Vendor Usage 6)
0x75, 0x08,                    //   REPORT_SIZE (8)
0x95, 0x04,                    //   REPORT_COUNT (4)
0x81, 0x82,                    //   INPUT (Data,Var,Abs,Vol)

3. eklenen dec in boyutu usbd_conf.h içerisindeki

#define USBD_CUSTOM_HID_REPORT_DESC_SIZE 108
olarak bildirilir.

2.Main.c de yapılacaklar

a.
#include "usbd_customhid.h"
b.
/* USER CODE BEGIN PV */
/* Private variables ———————————————————*/
extern USBD_HandleTypeDef hUsbDeviceFS;
uint8_t dataToSend[5];
uint8_t btn_stat=0;
c.main Fonksiyonunun içerisinde butonu kontrol edecek kodlar yazılır
if (HAL_GPIO_ReadPin(B1_GPIO_Port,B1_Pin)){
dataToSend[0] = 5;
dataToSend[1] = 0;
}
else{
dataToSend[0] = 5;
dataToSend[1] = 1;
}
USBD_CUSTOM_HID_SendReport(&hUsbDeviceFS, dataToSend, 5);
HAL_Delay(100);

3.usbd_custom_hid_if.c İÇERİSİNDE YAPILACAKLAR

a.
/* USER CODE BEGIN PRIVATE_VARIABLES */
uint8_t dataToReceive[2];
/* USER CODE END PRIVATE_VARIABLES */
b.
static int8_t CUSTOM_HID_OutEvent_FS (uint8_t event_idx, uint8_t state){
/* USER CODE BEGIN 6 */
USBD_CUSTOM_HID_HandleTypeDef *hhid = (USBD_CUSTOM_HID_HandleTypeDef*)hUsbDeviceFS.pClassData;
for (uint8_t i = 0; i < 4; i++)
{
dataToReceive[i] = hhid->Report_buf[i];
}
if ((dataToReceive[0]==1)&&(dataToReceive[1]==1))
{
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_SET);
}
if ((dataToReceive[0]==1)&&(dataToReceive[1]==0))
{
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_RESET);
}
if ((dataToReceive[0]==2)&&(dataToReceive[1]==1))
{
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_13, GPIO_PIN_SET);
}
if ((dataToReceive[0]==2)&&(dataToReceive[1]==0))
{
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_13, GPIO_PIN_RESET);
}
if ((dataToReceive[0]==3)&&(dataToReceive[1]==1))
{
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_SET);
}
if ((dataToReceive[0]==3)&&(dataToReceive[1]==0))
{
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_RESET);
}
if ((dataToReceive[0]==4)&&(dataToReceive[1]==1))
{
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_15, GPIO_PIN_SET);
}
if ((dataToReceive[0]==4)&&(dataToReceive[1]==0))
{
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_15, GPIO_PIN_RESET);
}
return (0);
/* USER CODE END 6 */
}

Projenin tüm dosyalarına ulaşmak için
https://yadi.sk/d/QGnFdMTj3EeFwC

%d blogcu bunu beğendi: