1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
| #include "main.h" #include "usbd_core.h" #include "usbd_hid.h"
#define HIDRAW_IN_EP 0x81 #define HIDRAW_IN_EP_SIZE 64 #define HIDRAW_IN_INTERVAL 10
#define HIDRAW_OUT_EP 0x02 #define HIDRAW_OUT_EP_SIZE 64 #define HIDRAW_OUT_EP_INTERVAL 10
#define USBD_VID 0x0480 #define USBD_PID 0x5850 #define USBD_MAX_POWER 100 #define USBD_LANGID_STRING 1033
#define USB_HID_CONFIG_DESC_SIZ (9 + 9 + 9 + 7 + 7)
#define HID_CUSTOM_REPORT_DESC_SIZE 33
static const uint8_t hid_descriptor[] = {USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0x00, 0x00, 0x00, USBD_VID, USBD_PID, 0x0002, 0x01), USB_CONFIG_DESCRIPTOR_INIT(USB_HID_CONFIG_DESC_SIZ, 0x01, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER), 0x09, USB_DESCRIPTOR_TYPE_INTERFACE, 0x00, 0x00, 0x02, 0x03, 0x00, 0x00, 0, 0x09, HID_DESCRIPTOR_TYPE_HID, 0x11, 0x01, 0x00, 0x01, 0x22, HID_CUSTOM_REPORT_DESC_SIZE, 0x00, 0x07, USB_DESCRIPTOR_TYPE_ENDPOINT, HIDRAW_IN_EP, 0x03, WBVAL(HIDRAW_IN_EP_SIZE), HIDRAW_IN_INTERVAL, 0x07, USB_DESCRIPTOR_TYPE_ENDPOINT, HIDRAW_OUT_EP, 0x03, WBVAL(HIDRAW_OUT_EP_SIZE), HIDRAW_OUT_EP_INTERVAL,
USB_LANGID_INIT(USBD_LANGID_STRING),
0x1a, USB_DESCRIPTOR_TYPE_STRING, 'S', 0x00, 'O', 0x00, 'N', 0x00, 'A', 0x00, 'V', 0x00, 'O', 0x00, 'X', 0x00, ' ', 0x00, 'M', 0x00, 'S', 0x00, 'C', 0x00, 'S', 0x00,
0x24, USB_DESCRIPTOR_TYPE_STRING, 'A', 0x00, 'T', 0x00, 'L', 0x00, 'A', 0x00, 'S', 0x00, ' ', 0x00, 'V', 0x00, 'O', 0x00, 'L', 0x00, 'T', 0x00, 'A', 0x00, 'G', 0x00, 'E', 0x00, ' ', 0x00, 'R', 0x00, 'E', 0x00, 'C', 0x00,
0x16, USB_DESCRIPTOR_TYPE_STRING, '2', 0x00, '0', 0x00, '2', 0x00, '3', 0x00, '/', 0x00, '1', 0x00, '2', 0x00, '/', 0x00, '1', 0x00, '5', 0x00, #ifdef CONFIG_USB_HS
0x0a, USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40, 0x01, 0x00, #endif 0x00};
static const uint8_t hid_custom_report_desc[HID_CUSTOM_REPORT_DESC_SIZE] = { 0x05, 0x8c, 0x09, 0x01, 0xa1, 0x01, 0x09, 0x03, 0x15, 0x00, 0x26, 0xff, 0x00, 0x75, 0x08, 0x95, 0x40, 0x81, 0x02, 0x09, 0x04, 0x15, 0x00, 0x26, 0xff, 0x00, 0x75, 0x08, 0x95, 0x40, 0x91, 0x02, 0xC0 };
USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t read_buffer[HIDRAW_OUT_EP_SIZE]; USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t send_buffer[HIDRAW_IN_EP_SIZE];
#define HID_STATE_IDLE 0 #define HID_STATE_BUSY 1
static volatile uint8_t custom_state;
void usbd_event_handler(uint8_t event) { switch (event) { case USBD_EVENT_RESET: break; case USBD_EVENT_CONNECTED: USB_LOG_RAW("Host Connected\n"); break; case USBD_EVENT_DISCONNECTED: USB_LOG_RAW("Host DisConnected\n"); dev_rec.start = 0; break; case USBD_EVENT_RESUME: break; case USBD_EVENT_SUSPEND: break; case USBD_EVENT_CONFIGURED: usbd_ep_start_read(HIDRAW_OUT_EP, read_buffer, HIDRAW_OUT_EP_SIZE); break; case USBD_EVENT_SET_REMOTE_WAKEUP: break; case USBD_EVENT_CLR_REMOTE_WAKEUP: break;
default: break; } }
static void usbd_hid_custom_in_callback(uint8_t ep, uint32_t nbytes) { USB_LOG_RAW("actual in len:%d\r\n", nbytes); custom_state = HID_STATE_IDLE; }
static void usbd_hid_custom_out_callback(uint8_t ep, uint32_t nbytes) { USB_LOG_RAW("actual out len:%d\r\n", nbytes); usbd_ep_start_read(HIDRAW_OUT_EP, read_buffer, 64); for (uint8_t i = 0; i < 64; i++) { rt_kprintf("%02x ", read_buffer[i]); } rt_kprintf("\r\n"); dev_rec.cmd = read_buffer[1]; switch (dev_rec.cmd) { case SET_RELAY_VALUE: set_relay(read_buffer[2]); break; case GET_INFO_INDS: case SET_AC_VOLTAGE: case SET_DC_VOLTAGE: case GET_MES_VALUE: uart_send_data(&read_buffer[2], read_buffer[0]); break; case SET_PARAM_VALUE: dev_rec.period = read_buffer[2] * 256 + read_buffer[3]; dev_rec.ch = read_buffer[4]; dev_rec.cmd = 0; break; case SET_START_REC: case SET_STOP_REC: dev_rec.start = read_buffer[2]; dev_rec.start ? timer_set_timeout(dev_rec.period) : timer_stop(); break; default: break; }
}
static struct usbd_endpoint custom_in_ep = {.ep_cb = usbd_hid_custom_in_callback, .ep_addr = HIDRAW_IN_EP};
static struct usbd_endpoint custom_out_ep = {.ep_cb = usbd_hid_custom_out_callback, .ep_addr = HIDRAW_OUT_EP};
struct usbd_interface intf0;
void hid_custom_init(void) { usbd_desc_register(hid_descriptor); usbd_add_interface(usbd_hid_init_intf(&intf0, hid_custom_report_desc, HID_CUSTOM_REPORT_DESC_SIZE)); usbd_add_endpoint(&custom_in_ep); usbd_add_endpoint(&custom_out_ep);
usbd_initialize(); }
void hid_send_data(uint8_t *data, uint8_t len) { uint8_t buf[64]; memset(buf, 0, len); memcpy(buf, data, len); int ret = usbd_ep_start_write(HIDRAW_IN_EP, buf, 64); if (ret < 0) { return; } }
|