其他分享
首页 > 其他分享> > USB协议学习笔记 - CUSTOM HID控制LED

USB协议学习笔记 - CUSTOM HID控制LED

作者:互联网

简介

USB HID接收

修改方法

#include "led.h"

/* all LEDS gpio init */
void leds_gpio_init(void)
{
    GPIO_InitTypeDef GPIO_InitStruct = {0};

    LEDR_GPIO_RCC_ENABLE();
    LEDG_GPIO_RCC_ENABLE();
    LEDB_GPIO_RCC_ENABLE();

    HAL_GPIO_WritePin(LEDR_GPIO_PORT, LEDR_GPIO_PINS, GPIO_PIN_SET);
    HAL_GPIO_WritePin(LEDG_GPIO_PORT, LEDG_GPIO_PINS, GPIO_PIN_SET);
    HAL_GPIO_WritePin(LEDB_GPIO_PORT, LEDB_GPIO_PINS, GPIO_PIN_SET);

    GPIO_InitStruct.Pin = LEDR_GPIO_PINS;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
    HAL_GPIO_Init(LEDR_GPIO_PORT, &GPIO_InitStruct);

    GPIO_InitStruct.Pin = LEDG_GPIO_PINS;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
    HAL_GPIO_Init(LEDG_GPIO_PORT, &GPIO_InitStruct);

    GPIO_InitStruct.Pin = LEDB_GPIO_PINS;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
    HAL_GPIO_Init(LEDB_GPIO_PORT, &GPIO_InitStruct);
}

/* LEDR power control */
void LEDR_power_ctrl(unsigned int bon)
{
    if (bon == 0x01) /* LEDR ON */
    {
        HAL_GPIO_WritePin(LEDR_GPIO_PORT, LEDR_GPIO_PINS, GPIO_PIN_RESET);
    }
    else
    {
        HAL_GPIO_WritePin(LEDR_GPIO_PORT, LEDR_GPIO_PINS, GPIO_PIN_SET);
    }
}

/* LEDG power control */
void LEDG_power_ctrl(unsigned int bon)
{
    if (bon == 0x01) /* LEDG ON */
    {
        HAL_GPIO_WritePin(LEDG_GPIO_PORT, LEDG_GPIO_PINS, GPIO_PIN_RESET);
    }
    else
    {
        HAL_GPIO_WritePin(LEDG_GPIO_PORT, LEDG_GPIO_PINS, GPIO_PIN_SET);
    }
}

/* LEDB power control */
void LEDB_power_ctrl(unsigned int bon)
{
    if (bon == 0x01)
    {
        HAL_GPIO_WritePin(LEDB_GPIO_PORT, LEDB_GPIO_PINS, GPIO_PIN_RESET);
    }
    else
    {
        HAL_GPIO_WritePin(LEDB_GPIO_PORT, LEDB_GPIO_PINS, GPIO_PIN_SET);
    }
}
#ifndef __LED_H__
#define __LED_H__

#include "stm32l4xx_hal.h"

#define LEDR_GPIO_RCC_ENABLE()  __HAL_RCC_GPIOE_CLK_ENABLE()
#define LEDR_GPIO_PORT          GPIOE
#define LEDR_GPIO_PINS          GPIO_PIN_7

#define LEDG_GPIO_RCC_ENABLE()  __HAL_RCC_GPIOE_CLK_ENABLE()
#define LEDG_GPIO_PORT          GPIOE
#define LEDG_GPIO_PINS          GPIO_PIN_8

#define LEDB_GPIO_RCC_ENABLE()  __HAL_RCC_GPIOE_CLK_ENABLE()
#define LEDB_GPIO_PORT          GPIOE
#define LEDB_GPIO_PINS          GPIO_PIN_9

void leds_gpio_init(void);
void LEDG_power_ctrl(unsigned int bon);
void LEDR_power_ctrl(unsigned int bon);
void LEDB_power_ctrl(unsigned int bon);

#endif
/**
  * @brief  Manage the CUSTOM HID class events
  * @param  event_idx: Event index
  * @param  state: Event state
  * @retval USBD_OK if all operations are OK else USBD_FAIL
  */
static int8_t CUSTOM_HID_OutEvent_FS(uint8_t event_idx, uint8_t state)
{
    UNUSED(event_idx);
    UNUSED(state);

    USBD_CUSTOM_HID_HandleTypeDef *hhid = (USBD_CUSTOM_HID_HandleTypeDef *)hUsbDeviceFS.pClassData;

    uint32_t usb_recv_len = USBD_GetRxCount(&hUsbDeviceFS,CUSTOM_HID_EPIN_ADDR);

    if (hhid->Report_buf[0] == 0x61)
    {
        LEDR_power_ctrl(1);
    }
    else
    {
        LEDR_power_ctrl(0);
    }

    if (hhid->Report_buf[1] == 0x61)
    {
        LEDG_power_ctrl(1);
    }
    else
    {
        LEDG_power_ctrl(0);
    }

    if (hhid->Report_buf[2] == 0x61)
    {
        LEDB_power_ctrl(1);
    }
    else
    {
        LEDB_power_ctrl(0);
    }

    for (uint32_t i = 0; i < usb_recv_len; i++)
    {
        usb_recv_buf[i] = hhid->Report_buf[i];
    }

    /* Start next USB packet transfer once data processing is completed */
    USBD_CUSTOM_HID_ReceivePacket(&hUsbDeviceFS);

    return (USBD_OK);
}
typedef struct
{
  uint8_t  Report_buf[USBD_CUSTOMHID_OUTREPORT_BUF_SIZE];
  uint32_t Protocol;
  uint32_t IdleState;
  uint32_t AltSetting;
  uint32_t IsReportAvailable;
  CUSTOM_HID_StateTypeDef state;
} USBD_CUSTOM_HID_HandleTypeDef;

测试验证

在这里插入图片描述
在这里插入图片描述

小结

标签:LEDR,LED,USB,CUSTOM,LEDB,HID,LEDG,GPIO
来源: https://blog.csdn.net/tcjy1000/article/details/122797585