ATY_LIB V2_102_230218
ATY_LIB for general devices or ALGO
 
Loading...
Searching...
No Matches
usb_keyboard.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
23#include "fw_hal.h"
24#include <string.h>
25
26#define KeyIO P0
27
28__BIT B_1ms; // 1ms flag
32uint16_t KeyCode; // code of key pressed
33uint16_t OldKeyCode; // for key status changing check
34uint16_t NewKeyCode; // new key status
36
43__CODE uint8_t KeyMap[16];
44__XDATA uint8_t HidFreature[64];
45__XDATA uint8_t HidInput[64];
46__XDATA uint8_t HidOutput[64];
49
50void USB_Init(void);
51void KeyScan(void);
52void SendKeyStatus(void);
53
54void main()
55{
56 uint8_t i;
57
61
62 USB_Init();
63
69
70 for (i = 0; i < 8; i++)
71 {
72 HidInput[i] = 0;
73 }
74
75 while (1)
76 {
77 if (B_1ms) // every 1 ms
78 {
79 B_1ms = 0;
80 if (++cnt50ms >= 50) // scan every 50 ms
81 {
82 cnt50ms = 0;
83 KeyScan();
84 }
85 if (KeyChangeFlag) // if key status changed
86 {
87 KeyChangeFlag = 0;
89 }
90 }
91 }
92}
93
95{
101
102 USB_WriteReg(FADDR, 0x00);
103 USB_WriteReg(POWER, 0x08);
104 USB_WriteReg(INTRIN1E, 0x3f);
105 USB_WriteReg(INTROUT1E, 0x3f);
106 USB_WriteReg(INTRUSBE, 0x00);
107 USB_WriteReg(POWER, 0x01);
109
111}
112
114{
115 uint8_t intrusb;
116 uint8_t intrin;
117 uint8_t introut;
118 uint8_t csr;
119 uint8_t cnt;
120 uint16_t len = 0;
121 intrusb = USB_ReadReg(INTRUSB);
122 intrin = USB_ReadReg(INTRIN1);
123 introut = USB_ReadReg(INTROUT1);
124 if (intrusb & RSTIF)
125 {
131 }
132 if (intrin & EP0IF)
133 {
135 csr = USB_ReadReg(CSR0);
136 if (csr & STSTL)
137 {
138 USB_WriteReg(CSR0, csr & ~STSTL);
140 }
141 if (csr & SUEND)
142 {
143 USB_WriteReg(CSR0, csr | SSUEND);
144 }
145 switch (usb_ep0_stage.bStage)
146 {
148 if (csr & OPRDY)
149 {
155 {
157 switch (usb_request.bRequest)
158 {
161 break;
162
171 break;
172
175 switch (usb_request.wValue.bb.bh)
176 {
179 len = sizeof(DEVICEDESC);
180 break;
181
184 len = sizeof(CONFIGDESC);
185 break;
186
188 switch (usb_request.wValue.bb.bl)
189 {
190 case 0:
192 len = sizeof(LANGIDDESC);
193 break;
194
195 case 1:
197 len = sizeof(MANUFACTDESC);
198 break;
199
200 case 2:
202 len = sizeof(PRODUCTDESC);
203 break;
204
205 default:
207 break;
208 }
209 break;
210
213 len = sizeof(HIDREPORTDESC);
214 break;
215
216 default:
218 break;
219 }
220 if (len < usb_ep0_stage.wResidue)
221 {
223 }
224 break;
225
226 default:
228 break;
229 }
230 break;
231
233 switch (usb_request.bRequest)
234 {
238 break;
239
243 break;
244
246 break;
247
248 // case USB_HidReq_GetIdle:
249 // case USB_HidReq_GetProtocol:
250 // case USB_HidReq_SetProtocol:
251 default:
253 break;
254 }
255 break;
256
257 default:
259 break;
260 }
261
262 switch (usb_ep0_stage.bStage)
263 {
266 goto L_Ep0SendData;
267 break;
268
271 break;
272
276 break;
277
281 break;
282 }
283 }
284 break;
285
287 if (!(csr & IPRDY))
288 {
289 L_Ep0SendData:
292 usb_ep0_stage.wResidue -= cnt;
293 usb_ep0_stage.pData += cnt;
294 if (usb_ep0_stage.wResidue == 0)
295 {
298 }
299 else
300 {
302 }
303 }
304 break;
305
307 if (csr & OPRDY)
308 {
310 usb_ep0_stage.wResidue -= cnt;
311 usb_ep0_stage.pData += cnt;
312 if (usb_ep0_stage.wResidue == 0)
313 {
316 }
317 else
318 {
320 }
321 }
322 break;
323 }
324 }
325
326 if (intrin & EP1INIF)
327 {
329 csr = USB_ReadReg(INCSR1);
330 if (csr & INSTSTL)
331 {
333 }
334 if (csr & INUNDRUN)
335 {
337 }
338 }
339
340 if (introut & EP1OUTIF)
341 {
343 csr = USB_ReadReg(OUTCSR1);
344 if (csr & OUTSTSTL)
345 {
347 }
348 if (csr & OUTOPRDY)
349 {
352
353 P6 = ~HidOutput[0]; // update LED status
354 }
355 }
356}
357
359{
360 uint8_t i;
361
362 i = 0;
363 while (dat)
364 {
365 if (dat & 0x8000) i++;
366 dat <<= 1;
367 }
368 return i;
369}
370
371//HidInput first byte for special keys,second byte is reserved,the reset 6 bytes for normal keys
373{
374 uint8_t i,n;
375
376 if(KeyCode) // if key pressed
377 {
378 // allow 3 keys pressed simultaneously
379 if(KeyCount(KeyCode) > 3)
380 {
381 return; // too many keys
382 }
383 else
384 {
385 n = 2;
386 for (i = 0; i < 16; i++)
387 {
388 if (i == 1)
389 {
390 if (KeyCode & (1 << i)) // left Ctrl
391 {
392 HidInput[0] |= 1;
393 }
394 else
395 {
396 HidInput[0] &= ~1;
397 }
398 }
399 else if (i == 2)
400 {
401 if (KeyCode & (1 << i)) // left alt
402 {
403 HidInput[0] |= 1 << 2;
404 }
405 else
406 {
407 HidInput[0] &= ~(1 << 2);
408 }
409 }
410 else
411 {
412 if (KeyCode & (1 << i))
413 {
414 HidInput[n++] = KeyMap[i];
415 }
416 }
417 }
418
419 for(; n<8; n++)
420 {
421 HidInput[n]=0; // fill 0 to the rest
422 }
423 }
424 }
425 else // if no key pressed, return 0
426 {
427 for (i = 0; i < 8; i++)
428 {
429 HidInput[i] = 0;
430 }
431 }
432
433 // return 8 bytes data
435 for (i = 0; i < 8; i++)
436 {
438 }
440}
441
443{
444 B_1ms = 1;
445}
446
447/*****************************************************
448 Key Matrix Scan
449
450 Y P04 P05 P06 P07
451 | | | |
452X | | | |
453P00 ---- K00 ---- K01 ---- K02 ---- K03 ----
454 | | | |
455P01 ---- K04 ---- K05 ---- K06 ---- K07 ----
456 | | | |
457P02 ---- K08 ---- K09 ---- K10 ---- K11 ----
458 | | | |
459P03 ---- K12 ---- K13 ---- K14 ---- K15 ----
460 | | | |
461******************************************************/
462
463void KeyScan(void)
464{
466
467 KeyIO = 0x0F;
468 SYS_DelayUs(1);
469 if ((KeyIO & 0x0F) == 0x0F) // no key pressed
470 {
471 NewKeyCode = 0;
472 }
473 else // start scan
474 {
475 // scan first line
476 KeyIO = (uint8_t)~0x10;
477 SYS_DelayUs(1);
478 // save 4 keys status
479 temp = KeyIO & 0x0F;
480
481 // second line
482 KeyIO = (uint8_t)~0x20;
483 SYS_DelayUs(1);
484 temp |= KeyIO << 4;
485
486 // save current key status
487 NewKeyCode = (~temp) & 0xFF;
488
489 // third line
490 KeyIO = (uint8_t)~0x40;
491 SYS_DelayUs(1);
492 temp = KeyIO & 0x0F;
493
494 // 4th line
495 KeyIO = (uint8_t)~0x80;
496 SYS_DelayUs(1);
497 temp |= KeyIO << 4;
498
499 // save all 16 keys' status in 2 bytes, 1 indicates key pressed
500 NewKeyCode |= (((uint16_t)~temp) << 8);
501 }
502
503 if (NewKeyCode != OldKeyCode)
504 {
505 KeyHoldTime = 0;
507 KeyChangeTemp = 1;
508 }
509 else
510 {
511 KeyHoldTime++;
512 if (KeyHoldTime >= 1)
513 {
514 KeyHoldTime = 1;
516 if (KeyChangeTemp)
517 {
518 KeyChangeTemp = 0;
519 KeyChangeFlag = 1; // Set send flag
520 }
521 }
522 }
523}
524
525__CODE uint8_t DEVICEDESC[18] =
526{
527 0x12, //bLength(18);
528 0x01, //bDescriptorType(Device);
529 0x00,0x02, //bcdUSB(2.00);
530 0x00, //bDeviceClass(0);
531 0x00, //bDeviceSubClass0);
532 0x00, //bDeviceProtocol(0);
533 0x40, //bMaxPacketSize0(64);
534 0x54,0x53, //idVendor(5354);
535 0x80,0x44, //idProduct(4480);
536 0x00,0x01, //bcdDevice(1.00);
537 0x01, //iManufacturer(1);
538 0x02, //iProduct(2);
539 0x03, //iSerialNumber(3);
540 0x01, //bNumConfigurations(1);
541};
542
543__CODE uint8_t CONFIGDESC[41] =
544{
545 0x09, //bLength(9);
546 0x02, //bDescriptorType(Configuration);
547 0x29,0x00, //wTotalLength(41);
548 0x01, //bNumInterfaces(1);
549 0x01, //bConfigurationValue(1);
550 0x00, //iConfiguration(0);
551 0xa0, //bmAttributes(BUSPower);
552 0xc8, //MaxPower(400mA);
553
554 0x09, //bLength(9);
555 0x04, //bDescriptorType(Interface);
556 0x00, //bInterfaceNumber(0);
557 0x00, //bAlternateSetting(0);
558 0x02, //bNumEndpoints(2);
559 0x03, //bInterfaceClass(HID);
560 0x01, //bInterfaceSubClass(1);
561 0x01, //bInterfaceProtocol(1);
562 0x00, //iInterface(0);
563
564 0x09, //bLength(9);
565 0x21, //bDescriptorType(HID);
566 0x01,0x10, //bcdHID(1.01);
567 0x00, //bCountryCode(0);
568 0x01, //bNumDescriptors(1);
569 0x22, //bDescriptorType(HID Report);
570 0x3f,0x00, //wDescriptorLength(63);
571 0x07, //bLength(7);
572 0x05, //bDescriptorType(Endpoint);
573 0x81, //bEndpointAddress(EndPoint1 as IN);
574 0x03, //bmAttributes(Interrupt);
575 0x40,0x00, //wMaxPacketSize(64);
576 0x0a, //bInterval(10ms);
577 0x07, //bLength(7);
578 0x05, //bDescriptorType(Endpoint);
579 0x01, //bEndpointAddress(EndPoint1 as OUT);
580 0x03, //bmAttributes(Interrupt);
581 0x40,0x00, //wMaxPacketSize(64);
582 0x0a, //bInterval(10ms);
583};
584
585__CODE uint8_t HIDREPORTDESC[63] =
586{
587 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
588 0x09, 0x06, // USAGE (Keyboard)
589 0xa1, 0x01, // COLLECTION (Application)
590 0x05, 0x07, // USAGE_PAGE (Keyboard)
591 0x19, 0xe0, // USAGE_MINIMUM (Keyboard LeftControl)
592 0x29, 0xe7, // USAGE_MAXIMUM (Keyboard Right GUI)
593 0x15, 0x00, // LOGICAL_MINIMUM (0)
594 0x25, 0x01, // LOGICAL_MAXIMUM (1)
595 0x75, 0x01, // REPORT_SIZE (1)
596 0x95, 0x08, // REPORT_COUNT (8)
597 0x81, 0x02, // INPUT (Data,Var,Abs)
598 0x95, 0x01, // REPORT_COUNT (1)
599 0x75, 0x08, // REPORT_SIZE (8)
600 0x81, 0x03, // INPUT (Cnst,Var,Abs)
601 0x95, 0x05, // REPORT_COUNT (5)
602 0x75, 0x01, // REPORT_SIZE (1)
603 0x05, 0x08, // USAGE_PAGE (LEDs)
604 0x19, 0x01, // USAGE_MINIMUM (Num Lock)
605 0x29, 0x05, // USAGE_MAXIMUM (Kana)
606 0x91, 0x02, // OUTPUT (Data,Var,Abs)
607 0x95, 0x01, // REPORT_COUNT (1)
608 0x75, 0x03, // REPORT_SIZE (3)
609 0x91, 0x03, // OUTPUT (Cnst,Var,Abs)
610 0x95, 0x06, // REPORT_COUNT (6)
611 0x75, 0x08, // REPORT_SIZE (8)
612 0x15, 0x00, // LOGICAL_MINIMUM (0)
613 0x25, 0xFF, // LOGICAL_MAXIMUM (255)
614 0x05, 0x07, // USAGE_PAGE (Keyboard)
615 0x19, 0x00, // USAGE_MINIMUM (Reserved (no event indicated))
616 0x29, 0x65, // USAGE_MAXIMUM (Keyboard Application)
617 0x81, 0x00, // INPUT (Data,Ary,Abs)
618 0xc0 // END_COLLECTION
619};
620
621__CODE uint8_t LANGIDDESC[4] =
622{
623 0x04,0x03,
624 0x09,0x04,
625};
626
627__CODE uint8_t MANUFACTDESC[8] =
628{
629 0x08,0x03,
630 'S',0,
631 'T',0,
632 'C',0,
633};
634
635__CODE uint8_t PRODUCTDESC[30] =
636{
637 0x1e,0x03,
638 'S',0,
639 'T',0,
640 'C',0,
641 ' ',0,
642 'U',0,
643 'S',0,
644 'B',0,
645 ' ',0,
646 'D',0,
647 'e',0,
648 'v',0,
649 'i',0,
650 'c',0,
651 'e',0,
652};
653
654__CODE uint8_t KeyMap[16] = {
655 0x53, //0: Num Lock
656 0xFF, //1: NULL - Left Ctrl
657 0xFF, //2: NULL - Left ALT
658 0x2A, //3: BackSpace
659
660 0x5F, //4: 7
661 0x60, //5: 8
662 0x61, //6: 9
663 0x62, //7: 0
664
665 0x5C, //8: 4
666 0x5D, //9: 5
667 0x5E, //A: 6
668 0x63, //B: DEL
669
670 0x59, //C: 1
671 0x5A, //D: 2
672 0x5B, //E: 3
673 0x58, //F: Return
674};
temp
Definition: DS18B20_ATY.c:215
__CODE int8_t dat[20]
#define EXTI_USB_SetIntPriority(__PRIORITY__)
Definition: fw_exti.h:228
#define EXTI_Timer0_SetIntPriority(__PRIORITY__)
Definition: fw_exti.h:214
@ EXTI_IntPriority_High
Definition: fw_exti.h:25
@ EXTI_IntPriority_Highest
Definition: fw_exti.h:26
#define EXTI_USB_SetIntState(__STATE__)
Definition: fw_exti.h:129
#define EXTI_Timer0_SetIntState(__STATE__)
Definition: fw_exti.h:116
#define EXTI_Global_SetIntState(__STATE__)
Definition: fw_exti.h:115
#define EXTI_VectTimer0
Definition: fw_exti.h:38
#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_All
Definition: fw_gpio.h:53
@ GPIO_Pin_1
Definition: fw_gpio.h:46
#define GPIO_P1_SetMode(__PINS__, __MODE__)
Definition: fw_gpio.h:79
#define GPIO_P6_SetMode(__PINS__, __MODE__)
Definition: fw_gpio.h:105
@ GPIO_Mode_Input_HIP
Definition: fw_gpio.h:25
@ GPIO_Mode_Output_PP
Definition: fw_gpio.h:24
@ GPIO_Mode_InOut_QBD
Definition: fw_gpio.h:23
#define SYS_EnableOscillator48M()
Definition: fw_sys.h:153
void SYS_DelayUs(uint16_t t)
Definition: fw_sys.c:75
@ TIM_TimerMode_16BitAuto
Definition: fw_tim.h:24
void TIM_Timer0_Config(HAL_State_t freq1t, TIM_TimerMode_t mode, uint16_t frequency)
Definition: fw_tim.c:38
#define TIM_Timer0_SetRunState(__STATE__)
Definition: fw_tim.h:35
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_keyboard.c:44
INTERRUPT(USB_Routine, EXTI_VectUSB)
Definition: usb_keyboard.c:113
uint16_t NewKeyCode
Definition: usb_keyboard.c:34
__XDATA uint8_t HidInput[64]
Definition: usb_keyboard.c:45
__BIT KeyChangeFlag
Definition: usb_keyboard.c:30
void USB_Init(void)
Definition: usb_keyboard.c:94
__XDATA uint8_t HidOutput[64]
Definition: usb_keyboard.c:46
__CODE uint8_t MANUFACTDESC[8]
Definition: usb_keyboard.c:41
__CODE uint8_t HIDREPORTDESC[63]
Definition: usb_keyboard.c:39
USB_Request_t usb_request
Definition: usb_keyboard.c:47
#define KeyIO
Definition: usb_keyboard.c:26
uint16_t KeyHoldTime
Definition: usb_keyboard.c:35
__CODE uint8_t DEVICEDESC[18]
Definition: usb_keyboard.c:37
USB_EP0_Stage_t usb_ep0_stage
Definition: usb_keyboard.c:48
uint16_t OldKeyCode
Definition: usb_keyboard.c:33
__CODE uint8_t CONFIGDESC[41]
Definition: usb_keyboard.c:38
__BIT KeyChangeTemp
Definition: usb_keyboard.c:29
uint16_t cnt50ms
Definition: usb_keyboard.c:31
uint16_t KeyCode
Definition: usb_keyboard.c:32
uint8_t KeyCount(uint16_t dat)
Definition: usb_keyboard.c:358
void main()
Definition: usb_keyboard.c:54
void SendKeyStatus(void)
Definition: usb_keyboard.c:372
void KeyScan(void)
Definition: usb_keyboard.c:463
__CODE uint8_t PRODUCTDESC[30]
Definition: usb_keyboard.c:42
__CODE uint8_t LANGIDDESC[4]
Definition: usb_keyboard.c:40
__BIT B_1ms
Definition: usb_keyboard.c:28
__CODE uint8_t KeyMap[16]
Definition: usb_keyboard.c:43