first commit
commit
1832bdca43
@ -0,0 +1,7 @@
|
||||
{
|
||||
// See http://go.microsoft.com/fwlink/?LinkId=827846
|
||||
// for the documentation about the extensions.json format
|
||||
"recommendations": [
|
||||
"platformio.platformio-ide"
|
||||
]
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
{
|
||||
"files.associations": {
|
||||
"usbd_core.h": "c",
|
||||
"usbd_conf.h": "c",
|
||||
"usbd_ctlreq.h": "c",
|
||||
"usbd_def.h": "c",
|
||||
"usbd_desc.h": "c",
|
||||
"usbd_customhid.h": "c",
|
||||
"usbd_custom_hid_if.h": "c",
|
||||
"usbd_ioreq.h": "c",
|
||||
"hid_def.h": "c",
|
||||
"stdbool.h": "c"
|
||||
}
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
Open Trackpad Controller
|
||||
|
||||
Game controller with trackpads
|
||||
Inspired by Steam Controller
|
||||
|
||||
Hardware:
|
||||
* We Act studios Black Pill stm32f411ce
|
||||
* elan 33200v-3600
|
||||
* Bunch of 3d prints
|
||||
|
||||
Credits:
|
||||
* hid_def: https://github.com/katyo/hid_def
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,70 @@
|
||||
#ifndef __MACRO_H__
|
||||
#define __MACRO_H__
|
||||
|
||||
#define _NONE(...)
|
||||
|
||||
#define _UNWR_(...) __VA_ARGS__
|
||||
#define _UNWR(a) _UNWR_ a
|
||||
|
||||
#define _CAT2_(a, b) a##b
|
||||
#define _CAT2(a, b) _CAT2_(a, b)
|
||||
|
||||
#define _CAT3_(a, b, c) a##b##c
|
||||
#define _CAT3(a, b, c) _CAT3_(a, b, c)
|
||||
|
||||
#define _CAT4_(a, b, c, d) a##b##c##d
|
||||
#define _CAT4(a, b, c, d) _CAT4_(a, b, c, d)
|
||||
|
||||
#define _CAT5_(a, b, c, d, e) a##b##c##d##e
|
||||
#define _CAT5(a, b, c, d, e) _CAT5_(a, b, c, d, e)
|
||||
|
||||
#define _NTH0_(a, ...) a
|
||||
#define _NTH0(...) _NTH0_(__VA_ARGS__)
|
||||
|
||||
#define _NTH1_(a, b, ...) b
|
||||
#define _NTH1(...) _NTH1_(__VA_ARGS__)
|
||||
|
||||
#define _NTH2_(a, b, c, ...) c
|
||||
#define _NTH2(...) _NTH2_(__VA_ARGS__)
|
||||
|
||||
#define _NTH3_(a, b, c, d, ...) d
|
||||
#define _NTH3(...) _NTH3_(__VA_ARGS__)
|
||||
|
||||
#define _NTH4_(a, b, c, d, e, ...) e
|
||||
#define _NTH4(...) _NTH4_(__VA_ARGS__)
|
||||
|
||||
#define _MAX2(a, b) ((a) > (b) ? (a) : (b))
|
||||
#define _MIN2(a, b) ((a) < (b) ? (a) : (b))
|
||||
|
||||
#define _CALL_(f, ...) f(__VA_ARGS__)
|
||||
#define _CALL(f, ...) _CALL_(f, ##__VA_ARGS__)
|
||||
|
||||
#define _EVAL0(...) __VA_ARGS__
|
||||
#define _EVAL1(...) _EVAL0(_EVAL0(_EVAL0(__VA_ARGS__)))
|
||||
#define _EVAL2(...) _EVAL1(_EVAL1(_EVAL1(__VA_ARGS__)))
|
||||
#define _EVAL3(...) _EVAL2(_EVAL2(_EVAL2(__VA_ARGS__)))
|
||||
#define _EVAL4(...) _EVAL3(_EVAL3(_EVAL3(__VA_ARGS__)))
|
||||
#define _EVAL(...) _EVAL4(_EVAL4(_EVAL4(__VA_ARGS__)))
|
||||
|
||||
#define _MAP_END(...)
|
||||
#define _MAP_OUT
|
||||
#define _MAP_COMMA ,
|
||||
|
||||
#define _MAP_GET_END2() 0, _MAP_END
|
||||
#define _MAP_GET_END1(...) _MAP_GET_END2
|
||||
#define _MAP_GET_END(...) _MAP_GET_END1
|
||||
#define _MAP_NEXT0(test, next, ...) next _MAP_OUT
|
||||
#define _MAP_NEXT1(test, next) _MAP_NEXT0(test, next, 0)
|
||||
#define _MAP_NEXT(test, next) _MAP_NEXT1(_MAP_GET_END test, next)
|
||||
|
||||
#define _MAP0(f, x, peek, ...) f(x) _MAP_NEXT(peek, _MAP1)(f, peek, __VA_ARGS__)
|
||||
#define _MAP1(f, x, peek, ...) f(x) _MAP_NEXT(peek, _MAP0)(f, peek, __VA_ARGS__)
|
||||
|
||||
/**
|
||||
* Applies the function macro `f` to each of the remaining parameters.
|
||||
*/
|
||||
#define _MAP(f, ...) _EVAL(_MAP1(f, __VA_ARGS__, ()()(), ()()(), ()()(), 0))
|
||||
|
||||
#define _ASSERT(x) ((1/(!!(x))) ? (x) : (x))
|
||||
|
||||
#endif /* __MACRO_H__ */
|
@ -0,0 +1,52 @@
|
||||
#ifndef USBD_DESCRIPTORS_H
|
||||
#define USBD_DESCRIPTORS_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#define USB_HID_CUSTOM_CONFIG_DESC_SIZ 34U
|
||||
#define USB_HID_CUSTOM_DESC_SIZ 9U
|
||||
#define USB_LEN_DEV_QUALIFIER_DESC 0x0AU
|
||||
#define HID_CUSTOM_REPORT_DESC_SIZE 29U
|
||||
|
||||
//#define HID_KEYBOARD_REPORT_DESC_SIZE 45U
|
||||
|
||||
#define HID_DESCRIPTOR_TYPE 0x21
|
||||
|
||||
#ifndef HID_HS_BINTERVAL
|
||||
#define HID_HS_BINTERVAL 0x07U
|
||||
#endif
|
||||
|
||||
#ifndef HID_FS_BINTERVAL
|
||||
#define HID_FS_BINTERVAL 0x0AU
|
||||
#endif
|
||||
|
||||
#define HID_CUSTOM_INTERFACE 0x00U
|
||||
#define HID_CUSTOM_EPIN_ADDR 0x81U
|
||||
#define HID_CUSTOM_EPIN_SIZE 0x08U
|
||||
|
||||
//#define HID_KEYBOARD_INTERFACE 0x01U
|
||||
//#define HID_KEYBOARD_EPIN_ADDR 0x82U
|
||||
//#define HID_KEYBOARD_EPIN_SIZE 0x08U
|
||||
|
||||
// USB HID device FS Configuration Descriptor
|
||||
extern uint8_t USBD_HID_CUSTOM_CfgFSDesc[USB_HID_CUSTOM_CONFIG_DESC_SIZ];
|
||||
|
||||
// USB HID device HS Configuration Descriptor
|
||||
extern uint8_t USBD_HID_CUSTOM_CfgHSDesc[USB_HID_CUSTOM_CONFIG_DESC_SIZ];
|
||||
|
||||
// USB HID device Other Speed Configuration Descriptor
|
||||
extern uint8_t USBD_HID_CUSTOM_OtherSpeedCfgDesc[USB_HID_CUSTOM_CONFIG_DESC_SIZ];
|
||||
|
||||
// USB HID device Configuration Descriptor
|
||||
extern uint8_t USBD_HID_CUSTOM_Desc[USB_HID_CUSTOM_DESC_SIZ];
|
||||
|
||||
// USB HID device Configuration Descriptor
|
||||
//extern uint8_t USBD_KEYBOARD_HID_Desc[USB_HID_DESC_SIZ];
|
||||
|
||||
// USB Standard Device Descriptor
|
||||
extern uint8_t USBD_HID_DeviceQualifierDesc[USB_LEN_DEV_QUALIFIER_DESC];
|
||||
|
||||
// USB Custom Report Descriptor
|
||||
extern uint8_t HID_CUSTOM_ReportDesc[HID_CUSTOM_REPORT_DESC_SIZE];
|
||||
|
||||
#endif
|
@ -0,0 +1,33 @@
|
||||
#ifndef USBD_DEVICE_H
|
||||
#define USBD_DEVICE_H
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
#define MOUSE_LEFT 1
|
||||
#define MOUSE_RIGHT 2
|
||||
#define MOUSE_MIDDLE 4
|
||||
#define MOUSE_ALL (MOUSE_LEFT | MOUSE_RIGHT | MOUSE_MIDDLE)
|
||||
|
||||
class USBD_Device
|
||||
{
|
||||
private:
|
||||
uint8_t _buttons;
|
||||
|
||||
void buttons(uint8_t b);
|
||||
|
||||
public:
|
||||
USBD_Device(void);
|
||||
|
||||
void begin(void);
|
||||
void end(void);
|
||||
|
||||
void click(uint8_t b = MOUSE_LEFT);
|
||||
|
||||
void move(signed char x, signed char y, signed char wheel = 0);
|
||||
|
||||
void press(uint8_t b = MOUSE_LEFT); // press LEFT by default
|
||||
void release(uint8_t b = MOUSE_LEFT); // release LEFT by default
|
||||
bool isPressed(uint8_t b = MOUSE_LEFT); // check LEFT by default
|
||||
};
|
||||
|
||||
#endif
|
@ -0,0 +1,41 @@
|
||||
#ifndef __USB_HID_CUSTOM_H
|
||||
#define __USB_HID_CUSTOM_H
|
||||
|
||||
#include "usbd_ioreq.h"
|
||||
#include "usbd_ep_conf.h"
|
||||
|
||||
#define HID_REPORT_DESC 0x22
|
||||
|
||||
#define HID_REQ_SET_PROTOCOL 0x0BU
|
||||
#define HID_REQ_GET_PROTOCOL 0x03U
|
||||
|
||||
#define HID_REQ_SET_IDLE 0x0AU
|
||||
#define HID_REQ_GET_IDLE 0x02U
|
||||
|
||||
#define HID_REQ_SET_REPORT 0x09U
|
||||
#define HID_REQ_GET_REPORT 0x01U
|
||||
|
||||
typedef enum
|
||||
{
|
||||
HID_IDLE = 0,
|
||||
HID_BUSY,
|
||||
}
|
||||
HID_StateTypeDef;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint32_t Protocol;
|
||||
uint32_t IdleState;
|
||||
uint32_t AltSetting;
|
||||
HID_StateTypeDef Mousestate;
|
||||
}
|
||||
USBD_HID_HandleTypeDef;
|
||||
|
||||
extern USBD_ClassTypeDef USBD_CUSTOM_HID;
|
||||
#define USBD_CUSTOM_HID_CLASS &USBD_CUSTOM_HID
|
||||
|
||||
uint8_t USBD_HID_CUSTOM_SendReport(USBD_HandleTypeDef *pdev, uint8_t *report, uint16_t len);
|
||||
|
||||
uint32_t USBD_HID_GetPollingInterval(USBD_HandleTypeDef *pdev);
|
||||
|
||||
#endif
|
@ -0,0 +1,9 @@
|
||||
#ifndef __USBD_HID_CUSTOM_IF_H
|
||||
#define __USBD_HID_CUSTOM_IF_H
|
||||
|
||||
void HID_Custom_Init();
|
||||
void HID_Custom_DeInit();
|
||||
|
||||
void HID_Custom_sendReport(uint8_t *report, uint16_t len);
|
||||
|
||||
#endif
|
@ -0,0 +1,25 @@
|
||||
; PlatformIO Project Configuration File
|
||||
;
|
||||
; Build options: build flags, source filter
|
||||
; Upload options: custom upload port, speed and extra flags
|
||||
; Library options: dependencies, extra library storages
|
||||
; Advanced options: extra scripting
|
||||
;
|
||||
; Please visit documentation for the other options and examples
|
||||
; https://docs.platformio.org/page/projectconf.html
|
||||
|
||||
[env:blackpill_f411ce]
|
||||
platform = ststm32
|
||||
board = blackpill_f411ce
|
||||
upload_protocol = stlink
|
||||
monitor_port = COM[4]
|
||||
monitor_speed = 256000
|
||||
framework = arduino
|
||||
build_flags =
|
||||
-D PIO_FRAMEWORK_ARDUINO_ENABLE_HID
|
||||
-D USBD_USE_HID_COMPOSITE
|
||||
-D USBCON
|
||||
-D USBD_VID=0x0483
|
||||
-D USBD_PID=0x0483
|
||||
-D USB_MANUFACTURER="Goshi"
|
||||
-D USB_PRODUCT="\"BLACKPILL_F411CE\""
|
@ -0,0 +1,56 @@
|
||||
#include <Arduino.h>
|
||||
|
||||
#include "usbd_hid_custom_if.h"
|
||||
|
||||
//USBD_Device device;
|
||||
|
||||
const uint8_t TRIGGER_RIGHT_PIN = PA3;
|
||||
const uint8_t TRACKPAD_CLICK_RIGHT_PIN = PB4;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint16_t x:16;
|
||||
uint16_t y:16;
|
||||
uint16_t r:16;
|
||||
} __packed PedalsReport_t;
|
||||
|
||||
PedalsReport_t PedalsReport;
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(256000);
|
||||
|
||||
pinMode(TRIGGER_RIGHT_PIN, INPUT_ANALOG);
|
||||
pinMode(TRACKPAD_CLICK_RIGHT_PIN, INPUT_PULLDOWN);
|
||||
|
||||
//device.begin();
|
||||
|
||||
printf("HID Init\n");
|
||||
|
||||
HID_Custom_Init();
|
||||
|
||||
printf("HID Init success\n");
|
||||
|
||||
PedalsReport = { 0, 1024/3, 2048/3 };
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
|
||||
uint32_t right_trigger = analogRead(TRIGGER_RIGHT_PIN);
|
||||
uint8_t right_tp_click = digitalRead(TRACKPAD_CLICK_RIGHT_PIN);
|
||||
|
||||
//Serial.printf("RT: %u RTPC: %u\n", right_trigger, right_tp_click);
|
||||
|
||||
PedalsReport.x = (PedalsReport.x + 10) % 1024;
|
||||
PedalsReport.y = (PedalsReport.y + 20) % 1024;
|
||||
PedalsReport.r = (PedalsReport.r + 30) % 1024;
|
||||
|
||||
//Serial.printf("%u %u %u\n", PedalsReport.x, PedalsReport.y, PedalsReport.r);
|
||||
|
||||
//device.move(PedalsReport.x, PedalsReport.y);
|
||||
|
||||
HID_Custom_sendReport((uint8_t*)(&PedalsReport), 6);
|
||||
|
||||
delay(100);
|
||||
}
|
@ -0,0 +1,189 @@
|
||||
#include "usbd_descriptors.h"
|
||||
|
||||
#include "usbd_def.h"
|
||||
|
||||
#include "hid_def.h"
|
||||
|
||||
// USB HID device FS Configuration Descriptor
|
||||
__ALIGN_BEGIN uint8_t USBD_HID_CUSTOM_CfgFSDesc[USB_HID_CUSTOM_CONFIG_DESC_SIZ] __ALIGN_END = {
|
||||
0x09, // bLength: Configuration Descriptor size
|
||||
USB_DESC_TYPE_CONFIGURATION, // bDescriptorType: Configuration
|
||||
LOBYTE(USB_HID_CUSTOM_CONFIG_DESC_SIZ), // wTotalLength: Bytes returned
|
||||
HIBYTE(USB_HID_CUSTOM_CONFIG_DESC_SIZ),
|
||||
0x01, //bNumInterfaces: 2 interface
|
||||
0x01, //bConfigurationValue: Configuration value
|
||||
0x00, //iConfiguration: Index of string descriptor describing the configuration
|
||||
0xC0, //bmAttributes: bus powered and no Support Remote Wake-up
|
||||
0x32, //MaxPower 100 mA: this current is used for detecting Vbus
|
||||
|
||||
//************* Descriptor of interface ***************
|
||||
// 09
|
||||
0x09, //bLength: Interface Descriptor size
|
||||
USB_DESC_TYPE_INTERFACE,//bDescriptorType: Interface descriptor type
|
||||
HID_CUSTOM_INTERFACE, //bInterfaceNumber: Number of Interface
|
||||
0x00, //bAlternateSetting: Alternate setting
|
||||
0x01, //bNumEndpoints
|
||||
0x03, //bInterfaceClass: HID
|
||||
0x00, //bInterfaceSubClass : 1=BOOT, 0=no boot
|
||||
0x00, //nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse
|
||||
0x00, //iInterface: Index of string descriptor
|
||||
//******************* Descriptor of Joystick Mouse HID *******************
|
||||
// 18
|
||||
0x09, //bLength: HID Descriptor size
|
||||
HID_DESCRIPTOR_TYPE, //bDescriptorType: HID
|
||||
0x11, //bcdHID: HID Class Spec release number
|
||||
0x01,
|
||||
0x00, //bCountryCode: Hardware target country
|
||||
0x01, //bNumDescriptors: Number of HID class descriptors to follow
|
||||
0x22, //bDescriptorType
|
||||
HID_CUSTOM_REPORT_DESC_SIZE,//wItemLength: Total length of Report descriptor
|
||||
0x00,
|
||||
//******************* Descriptor of Mouse endpoint *******************
|
||||
// 27
|
||||
0x07, //bLength: Endpoint Descriptor size
|
||||
USB_DESC_TYPE_ENDPOINT, //bDescriptorType:
|
||||
|
||||
HID_CUSTOM_EPIN_ADDR, //bEndpointAddress: Endpoint Address (IN)
|
||||
0x03, //bmAttributes: Interrupt endpoint
|
||||
HID_CUSTOM_EPIN_SIZE, //wMaxPacketSize: 4 Byte max
|
||||
0x00,
|
||||
HID_FS_BINTERVAL, //bInterval: Polling Interval
|
||||
};
|
||||
|
||||
// USB HID device HS Configuration Descriptor
|
||||
__ALIGN_BEGIN uint8_t USBD_HID_CUSTOM_CfgHSDesc[USB_HID_CUSTOM_CONFIG_DESC_SIZ] __ALIGN_END = {
|
||||
0x09, // bLength: Configuration Descriptor size
|
||||
USB_DESC_TYPE_CONFIGURATION, // bDescriptorType: Configuration
|
||||
LOBYTE(USB_HID_CUSTOM_CONFIG_DESC_SIZ), // wTotalLength: Bytes returned
|
||||
HIBYTE(USB_HID_CUSTOM_CONFIG_DESC_SIZ),
|
||||
0x01, //bNumInterfaces: 2 interface
|
||||
0x01, //bConfigurationValue: Configuration value
|
||||
0x00, //iConfiguration: Index of string descriptor describing the configuration
|
||||
0xC0, //bmAttributes: bus powered and no Support Remote Wake-up
|
||||
0x32, //MaxPower 100 mA: this current is used for detecting Vbus
|
||||
|
||||
//************* Descriptor of interface ***************
|
||||
// 09
|
||||
0x09, //bLength: Interface Descriptor size
|
||||
USB_DESC_TYPE_INTERFACE,//bDescriptorType: Interface descriptor type
|
||||
HID_CUSTOM_INTERFACE, //bInterfaceNumber: Number of Interface
|
||||
0x00, //bAlternateSetting: Alternate setting
|
||||
0x01, //bNumEndpoints
|
||||
0x03, //bInterfaceClass: HID
|
||||
0x00, //bInterfaceSubClass : 1=BOOT, 0=no boot
|
||||
0x00, //nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse
|
||||
0x00, //iInterface: Index of string descriptor
|
||||
//******************* Descriptor of Joystick Mouse HID *******************
|
||||
// 18
|
||||
0x09, //bLength: HID Descriptor size
|
||||
HID_DESCRIPTOR_TYPE, //bDescriptorType: HID
|
||||
0x11, //bcdHID: HID Class Spec release number
|
||||
0x01,
|
||||
0x00, //bCountryCode: Hardware target country
|
||||
0x01, //bNumDescriptors: Number of HID class descriptors to follow
|
||||
0x22, //bDescriptorType
|
||||
HID_CUSTOM_REPORT_DESC_SIZE,//wItemLength: Total length of Report descriptor
|
||||
0x00,
|
||||
//******************* Descriptor of Mouse endpoint *******************
|
||||
// 27
|
||||
0x07, //bLength: Endpoint Descriptor size
|
||||
USB_DESC_TYPE_ENDPOINT, //bDescriptorType:
|
||||
|
||||
HID_CUSTOM_EPIN_ADDR, //bEndpointAddress: Endpoint Address (IN)
|
||||
0x03, //bmAttributes: Interrupt endpoint
|
||||
HID_CUSTOM_EPIN_SIZE, //wMaxPacketSize: 4 Byte max
|
||||
0x00,
|
||||
HID_HS_BINTERVAL, //bInterval: Polling Interval
|
||||
};
|
||||
|
||||
// USB HID device Other Speed Configuration Descriptor
|
||||
__ALIGN_BEGIN uint8_t USBD_HID_CUSTOM_OtherSpeedCfgDesc[USB_HID_CUSTOM_CONFIG_DESC_SIZ] __ALIGN_END = {
|
||||
0x09, // bLength: Configuration Descriptor size
|
||||
USB_DESC_TYPE_CONFIGURATION, // bDescriptorType: Configuration
|
||||
LOBYTE(USB_HID_CUSTOM_CONFIG_DESC_SIZ), // wTotalLength: Bytes returned
|
||||
HIBYTE(USB_HID_CUSTOM_CONFIG_DESC_SIZ),
|
||||
0x01, //bNumInterfaces: 2 interface
|
||||
0x01, //bConfigurationValue: Configuration value
|
||||
0x00, //iConfiguration: Index of string descriptor describing the configuration
|
||||
0xC0, //bmAttributes: bus powered and no Support Remote Wake-up
|
||||
0x32, //MaxPower 100 mA: this current is used for detecting Vbus
|
||||
|
||||
//************* Descriptor of interface ***************
|
||||
// 09
|
||||
0x09, //bLength: Interface Descriptor size
|
||||
USB_DESC_TYPE_INTERFACE,//bDescriptorType: Interface descriptor type
|
||||
HID_CUSTOM_INTERFACE, //bInterfaceNumber: Number of Interface
|
||||
0x00, //bAlternateSetting: Alternate setting
|
||||
0x01, //bNumEndpoints
|
||||
0x03, //bInterfaceClass: HID
|
||||
0x00, //bInterfaceSubClass : 1=BOOT, 0=no boot
|
||||
0x00, //nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse
|
||||
0x00, //iInterface: Index of string descriptor
|
||||
//******************* Descriptor of Joystick Mouse HID *******************
|
||||
// 18
|
||||
0x09, //bLength: HID Descriptor size
|
||||
HID_DESCRIPTOR_TYPE, //bDescriptorType: HID
|
||||
0x11, //bcdHID: HID Class Spec release number
|
||||
0x01,
|
||||
0x00, //bCountryCode: Hardware target country
|
||||
0x01, //bNumDescriptors: Number of HID class descriptors to follow
|
||||
0x22, //bDescriptorType
|
||||
HID_CUSTOM_REPORT_DESC_SIZE,//wItemLength: Total length of Report descriptor
|
||||
0x00,
|
||||
//******************* Descriptor of Mouse endpoint *******************
|
||||
// 27
|
||||
0x07, //bLength: Endpoint Descriptor size
|
||||
USB_DESC_TYPE_ENDPOINT, //bDescriptorType:
|
||||
|
||||
HID_CUSTOM_EPIN_ADDR, //bEndpointAddress: Endpoint Address (IN)
|
||||
0x03, //bmAttributes: Interrupt endpoint
|
||||
HID_CUSTOM_EPIN_SIZE, //wMaxPacketSize: 4 Byte max
|
||||
0x00,
|
||||
HID_FS_BINTERVAL, //bInterval: Polling Interval
|
||||
};
|
||||
|
||||
// USB HID device Configuration Descriptor
|
||||
__ALIGN_BEGIN uint8_t USBD_HID_CUSTOM_Desc[USB_HID_CUSTOM_DESC_SIZ] __ALIGN_END = {
|
||||
USB_HID_CUSTOM_DESC_SIZ, //bLength: HID Descriptor size
|
||||
HID_DESCRIPTOR_TYPE, //bDescriptorType: HID
|
||||
0x11, //bcdHID: HID Class Spec release number
|
||||
0x01,
|
||||
0x00, //bCountryCode: Hardware target country
|
||||
0x01, //bNumDescriptors: Number of HID class descriptors to follow
|
||||
0x22, //bDescriptorType
|
||||
HID_CUSTOM_REPORT_DESC_SIZE,//wItemLength: Total length of Report descriptor
|
||||
0x00
|
||||
};
|
||||
|
||||
// USB Standard Device Descriptor
|
||||
__ALIGN_BEGIN uint8_t USBD_HID_DeviceQualifierDesc[USB_LEN_DEV_QUALIFIER_DESC] __ALIGN_END = {
|
||||
USB_LEN_DEV_QUALIFIER_DESC,
|
||||
USB_DESC_TYPE_DEVICE_QUALIFIER,
|
||||
0x00,
|
||||
0x02,
|
||||
0x00,
|
||||
0x00,
|
||||
0x00,
|
||||
0x40,
|
||||
0x01,
|
||||
0x00,
|
||||
};
|
||||
|
||||
__ALIGN_BEGIN uint8_t HID_CUSTOM_ReportDesc[HID_CUSTOM_REPORT_DESC_SIZE] __ALIGN_END = {
|
||||
HID_USAGE_PAGE(GENERIC_DESKTOP),
|
||||
HID_USAGE(JOYSTICK),
|
||||
HID_COLLECTION(APPLICATION),
|
||||
HID_COLLECTION(PHYSICAL),
|
||||
HID_USAGE_PAGE(GENERIC_DESKTOP),
|
||||
HID_USAGE(SLIDER),
|
||||
HID_USAGE(RY),
|
||||
HID_USAGE(RX),
|
||||
HID_LOGICAL_MINIMUM(1, 0),
|
||||
HID_LOGICAL_MAXIMUM(2, 1023),
|
||||
HID_REPORT_SIZE(16),
|
||||
HID_REPORT_COUNT(3),
|
||||
HID_INPUT(DATA, VARIABLE, ABSOLUTE),
|
||||
HID_END_COLLECTION(PHYSICAL),
|
||||
//HID_END_COLLECTION(APPLICATION)
|
||||
0xC0
|
||||
};
|
@ -0,0 +1,61 @@
|
||||
#include "usbd_device.h"
|
||||
|
||||
#include "usbd_hid_custom_if.h"
|
||||
|
||||
USBD_Device::USBD_Device(void)
|
||||
{
|
||||
_buttons = 0;
|
||||
}
|
||||
|
||||
void USBD_Device::begin(void)
|
||||
{
|
||||
HID_Custom_Init();
|
||||
}
|
||||
|
||||
void USBD_Device::end(void)
|
||||
{
|
||||
HID_Custom_DeInit();
|
||||
}
|
||||
|
||||
void USBD_Device::click(uint8_t b)
|
||||
{
|
||||
_buttons = b;
|
||||
move(0, 0, 0);
|
||||
_buttons = 0;
|
||||
move(0, 0, 0);
|
||||
}
|
||||
|
||||
void USBD_Device::move(signed char x, signed char y, signed char wheel)
|
||||
{
|
||||
uint8_t m[4];
|
||||
m[0] = _buttons;
|
||||
m[1] = x;
|
||||
m[2] = y;
|
||||
m[3] = wheel;
|
||||
|
||||
HID_Custom_sendReport(m, 4);
|
||||
}
|
||||
|
||||
void USBD_Device::buttons(uint8_t b)
|
||||
{
|
||||
if (b != _buttons)
|
||||
{
|
||||
_buttons = b;
|
||||
move(0, 0, 0);
|
||||
}
|
||||
}
|
||||
|
||||
void USBD_Device::press(uint8_t b)
|
||||
{
|
||||
buttons(_buttons | b);
|
||||
}
|
||||
|
||||
void USBD_Device::release(uint8_t b)
|
||||
{
|
||||
buttons(_buttons & ~b);
|
||||
}
|
||||
|
||||
bool USBD_Device::isPressed(uint8_t b)
|
||||
{
|
||||
return (b & _buttons) > 0;
|
||||
}
|
@ -0,0 +1,286 @@
|
||||
#include "usbd_hid_custom.h"
|
||||
#include "usbd_ctlreq.h"
|
||||
|
||||
#include "usbd_descriptors.h"
|
||||
|
||||
static uint8_t USBD_HID_Init(USBD_HandleTypeDef *pdev, uint8_t cfgidx);
|
||||
static uint8_t USBD_HID_DeInit(USBD_HandleTypeDef *pdev, uint8_t cfgidx);
|
||||
|
||||
static uint8_t USBD_CUSTOM_HID_Setup(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req);
|
||||
static uint8_t USBD_HID_MOUSE_Setup(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req);
|
||||
|
||||
static uint8_t *USBD_HID_GetFSCfgDesc(uint16_t *length);
|
||||
static uint8_t *USBD_HID_GetHSCfgDesc(uint16_t *length);
|
||||
static uint8_t *USBD_HID_GetOtherSpeedCfgDesc(uint16_t *length);
|
||||
static uint8_t *USBD_HID_GetDeviceQualifierDesc(uint16_t *length);
|
||||
|
||||
static uint8_t USBD_HID_DataIn(USBD_HandleTypeDef *pdev, uint8_t epnum);
|
||||
|
||||
USBD_ClassTypeDef USBD_CUSTOM_HID = {
|
||||
USBD_HID_Init,
|
||||
USBD_HID_DeInit,
|
||||
USBD_CUSTOM_HID_Setup,
|
||||
NULL, //EP0_TxSent
|
||||
NULL, //EP0_RxReady
|
||||
USBD_HID_DataIn, //DataIn
|
||||
NULL, //DataOut
|
||||
NULL, //SOF
|
||||
NULL,
|
||||
NULL,
|
||||
USBD_HID_GetHSCfgDesc,
|
||||
USBD_HID_GetFSCfgDesc,
|
||||
USBD_HID_GetOtherSpeedCfgDesc,
|
||||
USBD_HID_GetDeviceQualifierDesc,
|
||||
};
|
||||
|
||||
static uint8_t USBD_HID_Init(USBD_HandleTypeDef *pdev, uint8_t cfgidx)
|
||||
{
|
||||
UNUSED(cfgidx);
|
||||
uint8_t ret = USBD_OK;
|
||||
|
||||
// Open EP IN
|
||||
USBD_LL_OpenEP(pdev, HID_CUSTOM_EPIN_ADDR, USBD_EP_TYPE_INTR, HID_CUSTOM_EPIN_SIZE);
|
||||
pdev->ep_in[HID_CUSTOM_EPIN_ADDR & 0xFU].is_used = 1U;
|
||||
/*
|
||||
// Open EP IN
|
||||
USBD_LL_OpenEP(pdev,
|
||||
HID_KEYBOARD_EPIN_ADDR,
|
||||
USBD_EP_TYPE_INTR,
|
||||
HID_KEYBOARD_EPIN_SIZE);
|
||||
pdev->ep_in[HID_KEYBOARD_EPIN_ADDR & 0xFU].is_used = 1U;
|
||||
*/
|
||||
pdev->pClassData = USBD_malloc(sizeof(USBD_HID_HandleTypeDef));
|
||||
|
||||
if (pdev->pClassData == NULL)
|
||||
{
|
||||
ret = USBD_FAIL;
|
||||
}
|
||||
else
|
||||
{
|
||||
((USBD_HID_HandleTypeDef *)pdev->pClassData)->Mousestate = HID_IDLE;
|
||||
//((USBD_HID_HandleTypeDef *)pdev->pClassData)->Keyboardstate = HID_IDLE;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static uint8_t USBD_HID_DeInit(USBD_HandleTypeDef *pdev, uint8_t cfgidx)
|
||||
{
|
||||
UNUSED(cfgidx);
|
||||
/* Close HID EPs */
|
||||
USBD_LL_CloseEP(pdev, HID_CUSTOM_EPIN_ADDR);
|
||||
pdev->ep_in[HID_CUSTOM_EPIN_ADDR & 0xFU].is_used = 0U;
|
||||
/*
|
||||
USBD_LL_CloseEP(pdev, HID_KEYBOARD_EPIN_ADDR);
|
||||
pdev->ep_in[HID_KEYBOARD_EPIN_ADDR & 0xFU].is_used = 0U;
|
||||
*/
|
||||
/* Free allocated memory */
|
||||
if (pdev->pClassData != NULL)
|
||||
{
|
||||
USBD_free(pdev->pClassData);
|
||||
pdev->pClassData = NULL;
|
||||
}
|
||||
|
||||
return USBD_OK;
|
||||
}
|
||||
|
||||
static uint8_t USBD_CUSTOM_HID_Setup(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req)
|
||||
{
|
||||
/* Check which interface is targetted by this request */
|
||||
//if ((req->wIndex & 0x00FF) == HID_KEYBOARD_INTERFACE)
|
||||
//{
|
||||
// return USBD_HID_KEYBOARD_Setup(pdev, req);
|
||||
//}
|
||||
//else
|
||||
//{
|
||||
return USBD_HID_MOUSE_Setup(pdev, req);
|
||||
//}
|
||||
}
|
||||
|
||||
static uint8_t USBD_HID_MOUSE_Setup(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req)
|
||||
{
|
||||
USBD_HID_HandleTypeDef *hhid = (USBD_HID_HandleTypeDef*)pdev->pClassData;
|
||||
uint16_t len = 0U;
|
||||
uint8_t *pbuf = NULL;
|
||||
uint16_t status_info = 0U;
|
||||
USBD_StatusTypeDef ret = USBD_OK;
|
||||
|
||||
switch (req->bmRequest & USB_REQ_TYPE_MASK)
|
||||
{
|
||||
case USB_REQ_TYPE_CLASS:
|
||||
switch (req->bRequest)
|
||||
{
|
||||
case HID_REQ_SET_PROTOCOL:
|
||||
hhid->Protocol = (uint8_t)(req->wValue);
|
||||
break;
|
||||
|
||||
case HID_REQ_GET_PROTOCOL:
|
||||
USBD_CtlSendData(pdev, (uint8_t *)&hhid->Protocol, 1U);
|
||||
break;
|
||||
|
||||
case HID_REQ_SET_IDLE:
|
||||
hhid->IdleState = (uint8_t)(req->wValue >> 8);
|
||||
break;
|
||||
|
||||
case HID_REQ_GET_IDLE:
|
||||
USBD_CtlSendData(pdev, (uint8_t *)&hhid->IdleState, 1U);
|
||||
break;
|
||||
|
||||
default:
|
||||
USBD_CtlError(pdev, req);
|
||||
ret = USBD_FAIL;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
case USB_REQ_TYPE_STANDARD:
|
||||
switch (req->bRequest)
|
||||
{
|
||||
case USB_REQ_GET_STATUS:
|
||||
if (pdev->dev_state == USBD_STATE_CONFIGURED)
|
||||
{
|
||||
USBD_CtlSendData(pdev, (uint8_t *)(void *)&status_info, 2U);
|
||||
}
|
||||
else
|
||||
{
|
||||
USBD_CtlError(pdev, req);
|
||||
ret = USBD_FAIL;
|
||||
}
|
||||
break;
|
||||
|
||||
case USB_REQ_GET_DESCRIPTOR:
|
||||
if (req->wValue >> 8 == HID_REPORT_DESC)
|
||||
{
|
||||
len = MIN(HID_CUSTOM_REPORT_DESC_SIZE, req->wLength);
|
||||
pbuf = HID_CUSTOM_ReportDesc;
|
||||
}
|
||||
else if (req->wValue >> 8 == HID_DESCRIPTOR_TYPE)
|
||||
{
|
||||
pbuf = USBD_HID_CUSTOM_Desc;
|
||||
len = MIN(USB_HID_CUSTOM_DESC_SIZ, req->wLength);
|
||||
}
|
||||
else
|
||||
{
|
||||
USBD_CtlError(pdev, req);
|
||||
ret = USBD_FAIL;
|
||||
break;
|
||||
}
|
||||
USBD_CtlSendData(pdev, pbuf, len);
|
||||
break;
|
||||
|
||||
case USB_REQ_GET_INTERFACE :
|
||||
if (pdev->dev_state == USBD_STATE_CONFIGURED)
|
||||
{
|
||||
USBD_CtlSendData(pdev, (uint8_t *)&hhid->AltSetting, 1U);
|
||||
}
|
||||
else
|
||||
{
|
||||
USBD_CtlError(pdev, req);
|
||||
ret = USBD_FAIL;
|
||||
}
|
||||
break;
|
||||
|
||||
case USB_REQ_SET_INTERFACE :
|
||||
if (pdev->dev_state == USBD_STATE_CONFIGURED)
|
||||
{
|
||||
hhid->AltSetting = (uint8_t)(req->wValue);
|
||||
}
|
||||
else
|
||||
{
|
||||
USBD_CtlError(pdev, req);
|
||||
ret = USBD_FAIL;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
USBD_CtlError(pdev, req);
|
||||
ret = USBD_FAIL;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
USBD_CtlError(pdev, req);
|
||||
ret = USBD_FAIL;
|
||||
break;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
uint8_t USBD_HID_CUSTOM_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->Mousestate == HID_IDLE)
|
||||
{
|
||||
hhid->Mousestate = HID_BUSY;
|
||||
USBD_LL_Transmit(pdev, HID_CUSTOM_EPIN_ADDR, report, len);
|
||||
}
|
||||
}
|
||||
return USBD_OK;
|
||||
}
|
||||
|
||||
uint32_t USBD_HID_GetPollingInterval(USBD_HandleTypeDef *pdev)
|
||||
{
|
||||
uint32_t polling_interval = 0U;
|
||||
|
||||
/* HIGH-speed endpoints */
|
||||
if (pdev->dev_speed == USBD_SPEED_HIGH)
|
||||
{
|
||||
/* Sets the data transfer polling interval for high speed transfers.
|
||||
Values between 1..16 are allowed. Values correspond to interval
|
||||
of 2 ^ (bInterval-1). This option (8 ms, corresponds to HID_HS_BINTERVAL */
|
||||
polling_interval = (((1U << (HID_HS_BINTERVAL - 1U))) / 8U);
|
||||
}
|
||||
else
|
||||
{ /* LOW and FULL-speed endpoints */
|
||||
/* Sets the data transfer polling interval for low and full
|
||||
speed transfers */
|
||||
polling_interval = HID_FS_BINTERVAL;
|
||||
}
|
||||
|
||||
return ((uint32_t)(polling_interval));
|
||||
}
|
||||
|
||||
static uint8_t *USBD_HID_GetFSCfgDesc(uint16_t *length)
|
||||
{
|
||||
*length = sizeof(USBD_HID_CUSTOM_CfgFSDesc);
|
||||
return USBD_HID_CUSTOM_CfgFSDesc;
|
||||
}
|
||||
|
||||
static uint8_t *USBD_HID_GetHSCfgDesc(uint16_t *length)
|
||||
{
|
||||
*length = sizeof(USBD_HID_CUSTOM_CfgHSDesc);
|
||||
return USBD_HID_CUSTOM_CfgHSDesc;
|
||||
}
|
||||
|
||||
static uint8_t *USBD_HID_GetOtherSpeedCfgDesc(uint16_t *length)
|
||||
{
|
||||
*length = sizeof(USBD_HID_CUSTOM_OtherSpeedCfgDesc);
|
||||
return USBD_HID_CUSTOM_OtherSpeedCfgDesc;
|
||||
}
|
||||
|
||||
static uint8_t USBD_HID_DataIn(USBD_HandleTypeDef *pdev, uint8_t epnum)
|
||||
{
|
||||
/* Ensure that the FIFO is empty before a new transfer, this condition could
|
||||
be caused by a new transfer before the end of the previous transfer */
|
||||
//if (epnum == (HID_KEYBOARD_EPIN_ADDR & 0x7F))
|
||||
//{
|
||||
//((USBD_HID_HandleTypeDef *)pdev->pClassData)->Keyboardstate = HID_IDLE;
|
||||
//}
|
||||
//else if (epnum == (HID_CUSTOM_EPIN_ADDR & 0x7F))
|
||||
//{
|
||||
((USBD_HID_HandleTypeDef*)pdev->pClassData)->Mousestate = HID_IDLE;
|
||||
//}
|
||||
return USBD_OK;
|
||||
}
|
||||
|
||||
|
||||
static uint8_t *USBD_HID_GetDeviceQualifierDesc(uint16_t *length)
|
||||
{
|
||||
*length = sizeof(USBD_HID_DeviceQualifierDesc);
|
||||
return USBD_HID_DeviceQualifierDesc;
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
#include "usbd_desc.h"
|
||||
#include "usbd_hid_custom_if.h"
|
||||
#include "usbd_hid_custom.h"
|
||||
|
||||
USBD_HandleTypeDef hUSBD_Device_HID;
|
||||
|
||||
static bool HID_custom_initialized = false;
|
||||
|
||||
void HID_Custom_Init()
|
||||
{
|
||||
if (USBD_Init(&hUSBD_Device_HID, &USBD_Desc, 0) == USBD_OK)
|
||||
{
|
||||
if (USBD_RegisterClass(&hUSBD_Device_HID, USBD_CUSTOM_HID_CLASS) == USBD_OK)
|
||||
{
|
||||
USBD_Start(&hUSBD_Device_HID);
|
||||
HID_custom_initialized = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void HID_Custom_DeInit()
|
||||
{
|
||||
if (HID_custom_initialized)
|
||||
{
|
||||
USBD_Stop(&hUSBD_Device_HID);
|
||||
USBD_DeInit(&hUSBD_Device_HID);
|
||||
|
||||
HID_custom_initialized = false;
|
||||
}
|
||||
}
|
||||
|
||||
void HID_Custom_sendReport(uint8_t *report, uint16_t len)
|
||||
{
|
||||
USBD_HID_CUSTOM_SendReport(&hUSBD_Device_HID, report, len);
|
||||
}
|
Loading…
Reference in New Issue