ATY_LIB V2_102_230218
ATY_LIB for general devices or ALGO
 
Loading...
Searching...
No Matches
usb_hid.c
Go to the documentation of this file.
1// Copyright 2021 IOsetting <iosetting(at)outlook.com>
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#include "fw_hal.h"
16#include <string.h>
17
24__XDATA uint8_t HidFreature[64];
25__XDATA uint8_t HidInput[64];
26__XDATA uint8_t HidOutput[64];
29
30void USB_Init();
32
33void main()
34{
36 USB_Init();
38
39 memset(HidOutput, 0x00, sizeof(HidOutput));
40 HidOutput[0] = 0xaa;
41 HidOutput[1] = 0x55;
42 HidOutput[2] = 0x02;
43 HidOutput[3] = 0x01;
44 HidOutput[4] = 0x02;
45 HidOutput[5] = 0x01;
46 HidOutput[6] = 0x02;
48
49 while (1);
50}
51
53{
59
60 USB_WriteReg(FADDR, 0x00);
61 USB_WriteReg(POWER, 0x08);
65 USB_WriteReg(POWER, 0x01);
67
69}
70
72{
73 uint8_t intrusb;
74 uint8_t intrin;
75 uint8_t introut;
76 uint8_t csr;
77 uint8_t cnt;
78 uint16_t len = 0;
79 intrusb = USB_ReadReg(INTRUSB);
80 intrin = USB_ReadReg(INTRIN1);
81 introut = USB_ReadReg(INTROUT1);
82
86 if (intrusb & RSTIF)
87 {
93 }
94
98 if (intrin & EP0IF)
99 {
101 csr = USB_ReadReg(CSR0);
102 if (csr & STSTL) // Sent Stall
103 {
104 USB_WriteReg(CSR0, csr & ~STSTL);
106 }
107
108 if (csr & SUEND) // Setup End
109 {
110 USB_WriteReg(CSR0, csr | SSUEND);
111 }
112
113 switch (usb_ep0_stag.bStage)
114 {
116 if (csr & OPRDY) // Out Packet Ready
117 {
123 {
125 switch (usb_request.bRequest)
126 {
129 break;
130
139 break;
140
143 switch (usb_request.wValue.bb.bh)
144 {
147 len = sizeof(DEVICEDESC);
148 break;
149
152 len = sizeof(CONFIGDESC);
153 break;
154
156 switch (usb_request.wValue.bb.bl)
157 {
158 case 0:
160 len = sizeof(LANGIDDESC);
161 break;
162
163 case 1:
165 len = sizeof(MANUFACTDESC);
166 break;
167
168 case 2:
170 len = sizeof(PRODUCTDESC);
171 break;
172
173 default:
175 break;
176 }
177 break;
178
181 len = sizeof(HIDREPORTDESC);
182 break;
183
184 default:
186 break;
187 }
188 if (len < usb_ep0_stag.wResidue)
189 {
191 }
192 break;
193
194 default:
196 break;
197 }
198 break;
199
201 switch (usb_request.bRequest)
202 {
206 break;
207
211 break;
212
214 break;
215
216 // case USB_HidReq_GetIdle:
217 // case USB_HidReq_GetProtocol:
218 // case USB_HidReq_SetProtocol:
219 default:
221 break;
222 }
223 break;
224
225 default:
227 break;
228 }
229
230 switch (usb_ep0_stag.bStage)
231 {
234 goto L_Ep0SendData;
235 break;
236
239 break;
240
244 break;
245
249 break;
250 }
251 }
252 break;
253
255 if (!(csr & IPRDY))
256 {
257 L_Ep0SendData:
258 cnt = usb_ep0_stag.wResidue > 64 ? 64 : usb_ep0_stag.wResidue;
260 usb_ep0_stag.wResidue -= cnt;
261 usb_ep0_stag.pData += cnt;
262 if (usb_ep0_stag.wResidue == 0)
263 {
266 }
267 else
268 {
270 }
271 }
272 break;
273
275 if (csr & OPRDY)
276 {
278 usb_ep0_stag.wResidue -= cnt;
279 usb_ep0_stag.pData += cnt;
280 if (usb_ep0_stag.wResidue == 0)
281 {
284 }
285 else
286 {
288 }
289 }
290 break;
291 }
292 }
293
297 if (intrin & EP1INIF)
298 {
300 csr = USB_ReadReg(INCSR1);
301 if (csr & INSTSTL)
302 {
304 }
305 if (csr & INUNDRUN)
306 {
308 }
309 }
310
314 if (introut & EP1OUTIF)
315 {
317 csr = USB_ReadReg(OUTCSR1);
318 if (csr & OUTSTSTL)
319 {
321 }
322 if (csr & OUTOPRDY)
323 {
327 if((HidInput[0] == 0xaa) && (HidInput[1] == 0x55) && (HidInput[2] == 0x01))
328 {
332 }
333 }
334 }
335}
336
337__CODE uint8_t DEVICEDESC[18] =
338{
339 0x12, //bLength(18);
340 0x01, //bDescriptorType(Device);
341 0x00,0x02, //bcdUSB(2.00);
342 0x00, //bDeviceClass(0);
343 0x00, //bDeviceSubClass0);
344 0x00, //bDeviceProtocol(0);
345 0x40, //bMaxPacketSize0(64);
346 0x54,0x53, //idVendor(5354);
347 0x80,0x43, //idProduct(4380);
348 0x00,0x01, //bcdDevice(1.00);
349 0x01, //iManufacturer(1);
350 0x02, //iProduct(2);
351 0x00, //iSerialNumber(0);
352 0x01, //bNumConfigurations(1);
353};
354
355__CODE uint8_t CONFIGDESC[41] =
356{
357 0x09, //bLength(9);
358 0x02, //bDescriptorType(Configuration);
359 0x29,0x00, //wTotalLength(41);
360 0x01, //bNumInterfaces(1);
361 0x01, //bConfigurationValue(1);
362 0x00, //iConfiguration(0);
363 0x80, //bmAttributes(BUSPower);
364 0x32, //MaxPower(100mA);
365 0x09, //bLength(9);
366 0x04, //bDescriptorType(Interface);
367 0x00, //bInterfaceNumber(0);
368 0x00, //bAlternateSetting(0);
369 0x02, //bNumEndpoints(2);
370 0x03, //bInterfaceClass(HID);
371 0x00, //bInterfaceSubClass(0);
372 0x00, //bInterfaceProtocol(0);
373 0x00, //iInterface(0);
374 0x09, //bLength(9);
375 0x21, //bDescriptorType(HID);
376 0x01,0x01, //bcdHID(1.01);
377 0x00, //bCountryCode(0);
378 0x01, //bNumDescriptors(1);
379 0x22, //bDescriptorType(HID Report);
380 0x1b,0x00, //wDescriptorLength(27);
381 0x07, //bLength(7);
382 0x05, //bDescriptorType(Endpoint);
383 0x81, //bEndpointAddress(EndPoint1 as IN);
384 0x03, //bmAttributes(Interrupt);
385 0x40,0x00, //wMaxPacketSize(64);
386 0x01, //bInterval(10ms);
387 0x07, //bLength(7);
388 0x05, //bDescriptorType(Endpoint);
389 0x01, //bEndpointAddress(EndPoint1 as OUT);
390 0x03, //bmAttributes(Interrupt);
391 0x40,0x00, //wMaxPacketSize(64);
392 0x01, //bInterval(10ms);
393};
394
395__CODE uint8_t HIDREPORTDESC[27] =
396{
397 0x05,0x0c, //USAGE_PAGE(Consumer);
398 0x09,0x01, //USAGE(Consumer Control);
399 0xa1,0x01, //COLLECTION(Application);
400 0x15,0x00, // LOGICAL_MINIMUM(0);
401 0x25,0xff, // LOGICAL_MAXIMUM(255);
402 0x75,0x08, // REPORT_SIZE(8);
403 0x95,0x40, // REPORT_COUNT(64);
404 0x09,0x01, // USAGE(Consumer Control);
405 0xb1,0x02, // FEATURE(Data,Variable);
406 0x09,0x01, // USAGE(Consumer Control);
407 0x81,0x02, // INPUT(Data,Variable);
408 0x09,0x01, // USAGE(Consumer Control);
409 0x91,0x02, // OUTPUT(Data,Variable);
410 0xc0, //END_COLLECTION;
411};
412
413__CODE uint8_t LANGIDDESC[4] =
414{
415 0x04,0x03,
416 0x09,0x04,
417};
418
419__CODE uint8_t MANUFACTDESC[8] =
420{
421 0x08,0x03,
422 'S',0,
423 'T',0,
424 'C',0,
425};
426
427__CODE uint8_t PRODUCTDESC[30] =
428{
429 0x1e,0x03,
430 'S',0,
431 'T',0,
432 'C',0,
433 '8',0,
434 ' ',0,
435 'F',0,
436 'w',0,
437 'L',0,
438 'i',0,
439 'b',0,
440 ' ',0,
441 'H',0,
442 'I',0,
443 'D',0,
444};
445
447{
448 uint8_t i;
449 uint8_t cs=0;
450 for (i = 0; i < len; i++)
451 {
452 cs += buf[i];
453 }
454 return cs;
455}
#define EXTI_USB_SetIntPriority(__PRIORITY__)
Definition: fw_exti.h:228
@ EXTI_IntPriority_Highest
Definition: fw_exti.h:26
#define EXTI_USB_SetIntState(__STATE__)
Definition: fw_exti.h:129
#define EXTI_Global_SetIntState(__STATE__)
Definition: fw_exti.h:115
#define EXTI_VectUSB
Definition: fw_exti.h:66
#define GPIO_P3_SetMode(__PINS__, __MODE__)
Definition: fw_gpio.h:89
@ GPIO_Pin_0
Definition: fw_gpio.h:45
@ GPIO_Pin_1
Definition: fw_gpio.h:46
@ GPIO_Mode_Input_HIP
Definition: fw_gpio.h:25
#define SYS_EnableOscillator48M()
Definition: fw_sys.h:153
unsigned short uint16_t
Definition: fw_types.h:19
@ HAL_State_ON
Definition: fw_types.h:71
unsigned char uint8_t
Definition: fw_types.h:18
#define SDSTL
Definition: fw_usb.h:70
@ USB_HidReq_SetIdle
Definition: fw_usb.h:148
@ USB_HidReq_SetReport
Definition: fw_usb.h:147
@ USB_HidReq_GetReport
Definition: fw_usb.h:144
#define INTROUT1E
Definition: fw_usb.h:52
#define OUTMAXP
Definition: fw_usb.h:91
#define CSR0
Definition: fw_usb.h:67
#define INCLRDT
Definition: fw_usb.h:77
#define EP1OUTIF
Definition: fw_usb.h:39
#define POWER
Definition: fw_usb.h:26
#define INTRUSB
Definition: fw_usb.h:40
#define OUTCSR1
Definition: fw_usb.h:92
#define EP0IF
Definition: fw_usb.h:33
#define INUNDRUN
Definition: fw_usb.h:81
#define INCSR1
Definition: fw_usb.h:76
#define INMODEOUT
Definition: fw_usb.h:88
#define USB_SelectEndPoint(__INDEX__)
Definition: fw_usb.h:230
void USB_WriteReg(uint8_t addr, uint8_t dat)
#define INSTSTL
Definition: fw_usb.h:78
#define INTROUT1
Definition: fw_usb.h:34
#define EP1INIF
Definition: fw_usb.h:32
uint8_t USB_ReadReg(uint8_t addr)
#define OUTOPRDY
Definition: fw_usb.h:100
#define SSUEND
Definition: fw_usb.h:68
#define INTRIN1E
Definition: fw_usb.h:45
void USB_WriteFIFO(uint8_t fifo, uint8_t *pdat, uint8_t cnt)
@ USB_RequestType_Standard
Definition: fw_usb.h:168
@ USB_RequestType_Class
Definition: fw_usb.h:169
#define INCSR2
Definition: fw_usb.h:84
uint8_t USB_ReadFIFO(uint8_t fifo, uint8_t *pdat)
#define FADDR
Definition: fw_usb.h:25
#define INTRIN1
Definition: fw_usb.h:27
@ USB_CtrlState_DataIn
Definition: fw_usb.h:122
@ USB_CtrlState_Stalled
Definition: fw_usb.h:124
@ USB_CtrlState_Idle
Definition: fw_usb.h:120
@ USB_CtrlState_DataOut
Definition: fw_usb.h:123
@ USB_CtrlState_SettingUp
Definition: fw_usb.h:121
#define INTRUSBE
Definition: fw_usb.h:58
#define FIFO0
Definition: fw_usb.h:109
#define OUTCLRDT
Definition: fw_usb.h:93
@ USB_ClockSource_6M
Definition: fw_usb.h:175
#define USB_SetClockSource(__SOURCE__)
Definition: fw_usb.h:190
#define IPRDY
Definition: fw_usb.h:74
#define USB_SetEnabled(__STATE__)
Definition: fw_usb.h:196
#define USB_SetDpDmPullUp(__STATE__)
Definition: fw_usb.h:203
#define INIPRDY
Definition: fw_usb.h:83
@ USB_DescriptorType_String
Definition: fw_usb.h:156
@ USB_DescriptorType_Report
Definition: fw_usb.h:160
@ USB_DescriptorType_Device
Definition: fw_usb.h:154
@ USB_DescriptorType_Configuration
Definition: fw_usb.h:155
#define RSTIF
Definition: fw_usb.h:42
#define FIFO1
Definition: fw_usb.h:110
@ USB_StdReq_GetDescriptor
Definition: fw_usb.h:133
@ USB_StdReq_SetConfiguration
Definition: fw_usb.h:136
@ USB_StdReq_SetAddress
Definition: fw_usb.h:132
#define INMAXP
Definition: fw_usb.h:66
#define DATEND
Definition: fw_usb.h:72
#define STSTL
Definition: fw_usb.h:73
#define REQUEST_TYPE_MASK
Definition: fw_usb.h:164
#define SOPRDY
Definition: fw_usb.h:69
#define SUEND
Definition: fw_usb.h:71
#define OUTSTSTL
Definition: fw_usb.h:94
#define INMODEIN
Definition: fw_usb.h:87
#define OPRDY
Definition: fw_usb.h:75
uint8_t __XDATA i
uint16_t wResidue
Definition: fw_usb.h:255
uint8_t bStage
Definition: fw_usb.h:254
uint8_t * pData
Definition: fw_usb.h:256
uint16_2uint8_t wLength
Definition: fw_usb.h:249
uint8_t bmRequestType
Definition: fw_usb.h:245
uint8_t bRequest
Definition: fw_usb.h:246
uint16_2uint8_t wValue
Definition: fw_usb.h:247
struct uint16_2uint8_t::_bb bb
__XDATA uint8_t HidFreature[64]
Definition: usb_hid.c:24
INTERRUPT(USB_Routine, EXTI_VectUSB)
Definition: usb_hid.c:71
__XDATA uint8_t HidInput[64]
Definition: usb_hid.c:25
__CODE uint8_t DEVICEDESC[18]
Definition: usb_hid.c:18
__CODE uint8_t MANUFACTDESC[8]
Definition: usb_hid.c:22
__XDATA uint8_t HidOutput[64]
Definition: usb_hid.c:26
USB_EP0_Stage_t usb_ep0_stag
Definition: usb_hid.c:28
USB_Request_t usb_request
Definition: usb_hid.c:27
__CODE uint8_t HIDREPORTDESC[27]
Definition: usb_hid.c:20
__CODE uint8_t PRODUCTDESC[30]
Definition: usb_hid.c:23
uint8_t CalCheckSum(uint8_t *buf, uint8_t len)
Definition: usb_hid.c:446
void main()
Definition: usb_hid.c:33
__CODE uint8_t CONFIGDESC[41]
Definition: usb_hid.c:19
void USB_Init()
Definition: usb_hid.c:52
__CODE uint8_t LANGIDDESC[4]
Definition: usb_hid.c:21