-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathps2handl.c
1162 lines (1064 loc) · 42.7 KB
/
ps2handl.c
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/** @addtogroup 04 ps2handl PS/2 Keyboard Interface Handler
*
* @file ps2handl.c Power Control of a PS/2 key and general interface to and from PS/2 keyboard, including interrupt service routines.
*
* @brief <b>Power Control of a PS/2 key and general interface to and from PS/2 keyboard, including interrupt service routines.</b>
*
* @version 1.0.0
*
* @author @htmlonly © @endhtmlonly 2022
* Evandro Souza <[email protected]>
*
* @date 25 September 2022
*
* This library executes functions to interface and control a PS/2 keyboard, like:
* power control of a PS/2 key, general interface to read events and write commands to PS/2
* keyboard, including interrupt service routines on the STM32F4 and STM32F1 series of ARM
* Cortex Microcontrollers by ST Microelectronics.
*
* LGPL License Terms ref lgpl_license
*/
/*
* This file is part of the PS/2 to MSX keyboard Converter and
* MSX Keyboard Subsystem Emulator projects, using libopencm3 project.
*
* Copyright (C) 2022 Evandro Souza <[email protected]>
*
* This original SW is compiled to a Sharp/Epcom MSX HB-8000 and a brazilian ABNT2 PS/2 keyboard (ID=275)
* But it is possible to update the table sending a Intel Hex File through serial or USB
*
* This library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#include "ps2handl.h"
//PS/2 keyboard iteration constants
#define COMM_TYPE3_NO_REPEAT 0xF8 //248 Type 3 command
#define COMM_READ_ID 0xF2 //242
#define COMM_SET_TYPEMATIC_RATEDELAY 0xF3 //243 Type 2 command
#define COMM_ENABLE 0xf4 //244
#define COMM_SET_RESET_LEDS 0xED //237
#define COMM_ECHO 0xEE //238
#define KBCOMM_RESEND 0xFE //254
#define ARG_NO_ARG 0xFF //255
#define ARG_LOWRATE_LOWDELAY 0x7F //Type 2 (Delay 1 second to repeat. 2cps repeat rate)
#define KB_ACKNOWLEDGE 0xFA
#define KB_FIRST_ID 0xAB
#define KB_SECOND_ID 0x83
#define KB_SUCCESSFULL_BAT 0xAA
#define KB_ERROR_BAT 0xFC
//Global Vars
volatile uint16_t ps2int_state;
volatile uint8_t ps2int_TX_bit_idx;
volatile uint8_t ps2int_RX_bit_idx;
extern uint16_t state_overflow_tim2; //Declared on hr_timer_delay.c
extern uint64_t TIM2_Update_Cnt; //Declared on hr_timer_delay.c Overflow of time_between_ps2clk
volatile uint8_t command, argument;
volatile uint32_t prev_systicks;
volatile uint32_t ps2int_prev_systicks;
extern uint32_t systicks; //Declared on msxhid.cpp
extern uint64_t acctimeps2data0; //Declared on hr_timer_delay.c
extern uint32_t formerscancode; //declared on msxmap.cpp
extern uint8_t scancode[4]; //declared on msxmap.cpp
extern uint64_t time_between_ps2clk; //Declared on hr_timer_delay.c
extern uint16_t fail_count; //declared on msxhid.cpp
volatile bool formerps2datapin, update_ps2_leds;
volatile bool ps2_keyb_detected, ps2numlockstate;
volatile bool command_running, echo_received;
bool caps_state, kana_state, caps_former, kana_former;
volatile bool mount_scancode_OK; //used on mount_scancode()
volatile bool ps2_keystr_e0 = false;
volatile bool ps2_keystr_e1 = false;
volatile bool ps2_keystr_f0 = false;
volatile uint8_t ps2_byte_received;
volatile uint8_t mount_scancode_count_status = 0;
//Need to stay as global to avoid creating different instancies
volatile uint8_t ps2_recv_buffer[PS2_RECV_BUFFER_SIZE];
volatile uint8_t ps2_recv_put_ptr;
volatile uint8_t ps2_recv_get_ptr;
extern char _ebss[];
//Local prototypes (not declared in ps2handl.h)
void init_ps2_recv_buffer(void);
bool available_ps2_byte(void);
uint8_t get_ps2_byte(volatile uint8_t*);
void send_start_bit_next(uint16_t);
void ps2_clock_send(bool);
void ps2_clock_receive(bool);
void ps2_send_command(uint8_t, uint8_t);
void reset_mount_scancode_machine(void);
void send_start_bit_now(void);
void send_start_bit2(void);
void send_start_bit3(void);
//Power on PS/2 Keyboard and related pins setup
void power_on_ps2_keyboard()
{
// PS/2 keyboard Clock and Data pins
#if MCU == STM32F103
gpio_set(PS2_CLK_O_PORT, PS2_CLK_O_PIN); //Hi-Z
gpio_set_mode(PS2_CLK_O_PORT, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, PS2_CLK_O_PIN);
gpio_port_config_lock(PS2_CLK_O_PORT, PS2_CLK_O_PIN);
#endif
#if MCU == STM32F401
gpio_set_af(PS2_CLK_I_PORT, GPIO_AF1, PS2_CLK_I_PIN); //Set Alternate function
gpio_mode_setup(PS2_CLK_I_PORT, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, PS2_CLK_I_PIN);
gpio_set(PS2_CLK_O_PORT, PS2_CLK_O_PIN); //Hi-Z
gpio_set_output_options(PS2_CLK_O_PORT, GPIO_OTYPE_OD, GPIO_OSPEED_100MHZ, PS2_CLK_O_PIN);
gpio_mode_setup(PS2_CLK_O_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_PULLUP, PS2_CLK_O_PIN);
#if PS2_CLK_INTERRUPT == GPIO_INT
exti_select_source(PS2_CLK_I_EXTI, PS2_CLK_I_PORT);
exti_set_trigger(PS2_CLK_I_EXTI, EXTI_TRIGGER_FALLING);
exti_reset_request(PS2_CLK_I_EXTI);
exti_enable_request(PS2_CLK_I_EXTI);
#endif //#if PS2_CLK_INTERRUPT == GPIO_INT
gpio_port_config_lock(PS2_CLK_I_PORT, PS2_CLK_I_PIN);
gpio_port_config_lock(PS2_CLK_O_PORT, PS2_CLK_O_PIN);
#endif //#if MCU == STM32F401
// PS/2 keyboard Data pin
#if MCU == STM32F103
gpio_set(PS2_DATA_PORT, PS2_DATA_PIN); //Hi-Z
gpio_set_mode(PS2_DATA_PORT, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, PS2_DATA_PIN);
gpio_port_config_lock(PS2_DATA_PORT, PS2_DATA_PIN);
#endif
#if MCU == STM32F401
gpio_set_af(PS2_DATA_PORT, GPIO_AF1, PS2_DATA_PIN); //Set Alternate function
gpio_set(PS2_DATA_PORT, PS2_DATA_PIN); //Hi-Z
gpio_mode_setup(PS2_DATA_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_PULLUP, PS2_DATA_PIN);
gpio_set_output_options(PS2_DATA_PORT, GPIO_OTYPE_OD, GPIO_OSPEED_25MHZ, PS2_DATA_PIN);
gpio_port_config_lock(PS2_DATA_PORT, PS2_DATA_PIN);
#endif
//PS/2 power control pin
#if MCU == STM32F103
gpio_set_mode(PS2_POWER_CTR_PORT, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, PS2_POWER_CTR_PIN);
#endif
#if MCU == STM32F401
gpio_set_af(PS2_POWER_CTR_PORT, GPIO_AF1, PS2_POWER_CTR_PIN); //Set Alternate function
gpio_mode_setup(PS2_POWER_CTR_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, PS2_POWER_CTR_PIN);
gpio_set_output_options(PS2_POWER_CTR_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_100MHZ, PS2_POWER_CTR_PIN);
#endif
gpio_set(PS2_POWER_CTR_PORT, PS2_POWER_CTR_PIN); //Turn on PS/2 Keyboard
#if PS2_CLK_INTERRUPT == GPIO_INT
// Enable EXTI15_10 interrupt.
nvic_enable_irq(NVIC_EXTI15_10_IRQ);
//High priority to avoid PS/2 interrupt loss
nvic_set_priority(NVIC_EXTI15_10_IRQ, IRQ_PRI_EXT15);
#endif //#if PS2_CLK_INTERRUPT == GPIO_INT
//Starts with RX state: PS2INT_RECEIVE
ps2int_state = PS2INT_RECEIVE;
ps2int_RX_bit_idx = 0;
command_running = false;
init_ps2_recv_buffer();
}
void power_off_ps2_keyboard()
{
gpio_clear(PS2_POWER_CTR_PORT, PS2_POWER_CTR_PIN);
gpio_clear(PS2_DATA_PORT, PS2_DATA_PIN);
gpio_clear(PS2_CLK_O_PORT, PS2_CLK_O_PIN);
con_send_string((uint8_t*)"\r\nPS/2 interface powered down.\r\n\n");
}
bool keyboard_check_alive(void)
{
uint32_t systicks_start_command; //Initial time mark
while(ps2int_RX_bit_idx != 0) __asm("nop");
systicks_start_command = systicks;
ps2_send_command(COMM_ENABLE, ARG_NO_ARG); //Enable command.
while (command_running && (systicks - systicks_start_command) < 2) __asm("nop"); //Must be excecuted in less than 67ms
if(command_running)
{
return false;
}
systicks_start_command = systicks;
echo_received = false;
ps2_send_command(COMM_ECHO, ARG_NO_ARG); //Echo command.
while (command_running && (systicks - systicks_start_command) < 3) __asm("nop"); //Must be excecuted in less than 67ms
if(!command_running)
{
return echo_received; //Inside interrupt this will be updated
}
return false;
}
// Initialize receive ringbuffer
void init_ps2_recv_buffer()
{
uint8_t i;
formerscancode = 0;
ps2_recv_put_ptr=0;
ps2_recv_get_ptr=0;
for(i = 0; i < PS2_RECV_BUFFER_SIZE; ++i)
ps2_recv_buffer[i]=0;
}
// Verify if there is an available ps2_byte_received on the receive ring buffer, but does not fetch this one
bool available_ps2_byte()
{
uint8_t i;
i = ps2_recv_get_ptr;
if(i == ps2_recv_put_ptr)
//No char in buffer
return false;
else
return true;
}
// Fetches the next ps2_byte_received from the receive ring buffer
uint8_t get_ps2_byte(volatile uint8_t *buff)
{
uint8_t i, result;
i = ps2_recv_get_ptr;
if(i == ps2_recv_put_ptr)
//No char in buffer
return 0;
result = buff[i];
i++;
ps2_recv_get_ptr = i & (uint16_t)(PS2_RECV_BUFFER_SIZE - 1); //if(i >= (uint16_t)SERIAL_RING_BUFFER_SIZE) i = 0;
return result;
}
bool ps2_keyb_detect(void)
{
uint32_t systicks_start_command; //Initial time mark
uint8_t mountstring[16]; //Used in con_send_string()
uint8_t ch;
//uint16_t qty_in_buffer;
/*serial_wait_tx_ends();*/
//Clean RX serial buffer to not false glitch database_setup
while (con_available_get_char())
ch = con_get_char();
ch &= 0xFF; //only to avoid unused variable warning.
//Wait for 2.5s to keyboard execute its own power on and BAT (Basic Assurance Test) procedure
systicks_start_command = systicks;
prev_systicks = systicks;
ps2_keyb_detected = false;
uint16_t localcount = 0;
while ( ((systicks-systicks_start_command) < (25 * FREQ_INT_SYSTICK / 10)) && (!available_ps2_byte()) ) //Wait 2500ms for keyboard power on
{
if(prev_systicks != systicks)
{//systicks updated. Each 15 ticks is a "#" |.....|
if((systicks - systicks_start_command - localcount) > 14)
{
localcount = systicks - systicks_start_command;
con_send_string((uint8_t*)"\r to proceed BAT: |");
for(uint16_t i = 0; i < (uint8_t)(localcount / 15); i++)
con_send_string((uint8_t*)"#");
}
}
prev_systicks = systicks; //To avoid errors on keyboard power up BEFORE the first access
}
//Fill bar graph
localcount = systicks - systicks_start_command;
con_send_string((uint8_t*)"\r to proceed BAT: |");
for(uint16_t i = 0; i < (uint8_t)(localcount / 15); i++)
con_send_string((uint8_t*)"#");
if ((systicks-systicks_start_command) >= (25*3))
{
//User messages
con_send_string((uint8_t*)"\r\n.. Timeout on BAT: No keyboard!\r\n");
return ps2_keyb_detected;
}
//PS/2 keyboard might already sent its BAT result. Check it:
ps2_byte_received = get_ps2_byte(ps2_recv_buffer);
if(ps2_byte_received != 0)
{
if(ps2_byte_received == KB_SUCCESSFULL_BAT)
{
//User messages
con_send_string((uint8_t*)"\r\n.. BAT (Basic Assurance Test) OK in ");
conv_uint32_to_dec((prev_systicks - systicks_start_command), mountstring);
con_send_string((uint8_t*)mountstring);
con_send_string((uint8_t*)" ticks;\r\n");
}
else
{
//User messages
con_send_string((uint8_t*)".. BAT not OK: Received 0x");
conv_uint8_to_2a_hex(ps2_byte_received, mountstring);
con_send_string((uint8_t*)mountstring);
con_send_string((uint8_t*)" instead of 0xAA\r\n");
}
}
//Send command Read ID. It musts responds with 0xFA (implicit), 0xAB, 0x83
// Wait clock line to be unactive for 100 ms (3 systicks)
//con_send_string((uint8_t*)"Sending Read ID comm\r\n");
systicks_start_command = systicks;
ps2_send_command(COMM_READ_ID, ARG_NO_ARG); //Read ID command.
while (command_running && (systicks - systicks_start_command) < (3 * FREQ_INT_SYSTICK / 10)) //Must be excecuted in less than 100ms
{
prev_systicks = systicks; //To avoid errors on keyboard power up BEFORE the first access
if(systicks != systicks_start_command)
prev_systicks = systicks; //To avoid errors on keyboard power up BEFORE the first access
}
if (!command_running)
{
//con_send_string((uint8_t*)"Waiting 0xAB\r\n");
systicks_start_command = systicks;
while(!available_ps2_byte() && (systicks - systicks_start_command) < (FREQ_INT_SYSTICK / 10))
__asm("nop");
ps2_byte_received = get_ps2_byte(ps2_recv_buffer);
if(ps2_byte_received == KB_FIRST_ID)
{
//con_send_string((uint8_t*)"Waiting 0x83\r\n");
systicks_start_command = systicks;
while(!available_ps2_byte() && (systicks - systicks_start_command) < (FREQ_INT_SYSTICK / 10))
__asm("nop");
ps2_byte_received = get_ps2_byte(ps2_recv_buffer);
if(ps2_byte_received == KB_SECOND_ID)
{
ps2_keyb_detected = true;
//User messages
con_send_string((uint8_t*)".. PS/2 Keyboard detected;\r\n");
}
else
{
//con_send_string((uint8_t*)"Did not receive 0x83");
con_send_string((uint8_t*)".. PS/2 Keyboard not detected!\r\n");
return ps2_keyb_detected;
}
}
else
{
//con_send_string((uint8_t*)"Did not receive 0xAB");
return ps2_keyb_detected;
}
}
else
{
//User messages
/*con_send_string((uint8_t*)"PS/2 ReadID command not OK. Elapsed time was ");
conv_uint32_to_8a_hex((systicks - systicks_start_command), mountstring);
con_send_string((uint8_t*)mountstring);
con_send_string((uint8_t*)"\r\n"); */
return ps2_keyb_detected;
}
//The objective of this block is to minimize the keyboard interruptions, to keep time to high priority MSX interrupts.
//Send type 3 command 0xFA (Set Key Type Make/Break - This one only disables typematic repeat):
// If it does not receive "ack" (0xFA), then send type 2 command 0xF3 + 0x7F (2cps repeat rate + 1 second delay)
// It musts respond with an "ack" after the first byte, than with a second "ack" after the second byte.
//User messages
//con_send_string((uint8_t*)"Type 2 sets typematic repeat 0xF3 0x7F requested\r\n");
//Type 2 command: Set typematic rate to 2 cps and delay to 1 second.
systicks_start_command = systicks;
ps2_send_command(COMM_SET_TYPEMATIC_RATEDELAY, ARG_LOWRATE_LOWDELAY);
while (command_running && (systicks - systicks_start_command) < (2 * FREQ_INT_SYSTICK / 10)) //Must be excecuted in less than 200ms
__asm("nop");
if (!command_running)
//User messages
con_send_string((uint8_t*)".. Delay 1 second to repeat, 2cps repeat rate (Type 2 command) OK;\r\n");
else
{
//User messages
con_send_string((uint8_t*)".. Type 3 Disables typematic repeat 0xFA requested\r\n");
//.1 second delay (to display serial contents)
systicks_start_command = systicks;
while ((systicks - systicks_start_command) < (FREQ_INT_SYSTICK / 10)) __asm("nop");
systicks_start_command = systicks;
//Type 3 command: Set All Keys Make/Break: This one only disables typematic repeat and applies to all keys
ps2_send_command(COMM_TYPE3_NO_REPEAT, ARG_NO_ARG);
while (command_running && (systicks - systicks_start_command) < (FREQ_INT_SYSTICK / 10)) __asm("nop"); //Must be excecuted in less than 100ms
if (!command_running)
//User messages
con_send_string((uint8_t*)".. Type 3 Disables typematic 0xFA repeat OK\r\n");
}
return ps2_keyb_detected;
}
void reset_requested(void)
{
power_off_ps2_keyboard();
//Wait here 1/2 second to consolidate this power off
uint32_t readsysticks = systicks;
while (systicks <= (readsysticks + (FREQ_INT_SYSTICK / 2))) __asm("nop");
systick_interrupt_disable();
#if MCU == STM32F401
uint32_t *magic = (uint32_t *)&_ebss;
magic[0] = BOOTMAGIC0;
magic[1] = BOOTMAGIC1;
#endif //#if MCU == STM32F401
scb_reset_system();
for(;;) {};//Wait here until reset
}
/*************************************************************************************************/
/****************************** Support to other ISR's ******************************************/
/*************************************************************************************************/
void ps2_send_command(uint8_t cmd, uint8_t argm)
{
uint8_t mountstring[16]; //Used in con_send_string()
/*uint32_t systicks_start_command; //Initial time mark
systicks_start_command = systicks;
while(command_running && ((systicks_start_command - systicks) < 2) )
__asm("nop"); */
if(ps2int_state != PS2INT_RECEIVE)
{
//User messages
con_send_string((uint8_t*)"0x");
conv_uint16_to_4a_hex(ps2int_state, mountstring);
con_send_string((uint8_t*)mountstring);
con_send_string((uint8_t*)", instead of 0x0400\r\n");
}
command = cmd;
argument = argm;
ps2int_state = PS2INT_SEND_COMMAND;
send_start_bit_now();
}
void ps2_update_leds(bool num, bool caps, bool scroll)
{
uint8_t mountstring[16]; //Used in con_send_string()
if(ps2int_state != PS2INT_RECEIVE)
{
//User messages
con_send_string((uint8_t*)"0x");
conv_uint16_to_4a_hex(ps2int_state, mountstring);
con_send_string((uint8_t*)mountstring);
con_send_string((uint8_t*)", instead of 0x0400\r\n");
}
command = COMM_SET_RESET_LEDS;
argument = (scroll<<0)|(num<<1)|(caps<<2);
ps2int_state = PS2INT_SEND_COMMAND;
send_start_bit_now();
}
//Insert a delay before run send_start_bit_now()
void send_start_bit_next(uint16_t x_usec)
{
delay_usec(TIM_HR, x_usec, send_start_bit_now); //wait x_usec and go to send_start_bit on TIM_HR_TIMER overflow interrupt
}
//This three functions are the split of Transmit Initiator, to avoid stuck inside an interrupt due to 120u and 20usec
void send_start_bit_now(void)
{
timer_disable_irq(TIM_HR, TIM_DIER_CC1IE); // Disable interrupt on Capture/Compare1, but keeps on overflow
#if PS2_CLK_INTERRUPT == GPIO_INT
exti_disable_request(PS2_CLK_I_EXTI);
#endif
command_running = true; //Here command_OK is initialized
gpio_set(PS2_DATA_PORT, PS2_DATA_PIN);
gpio_clear(PS2_CLK_O_PORT, PS2_CLK_O_PIN);
//if keyboard interrupt was not disabled, it would be interrupted here, pointing to something not coded
//Something was wrong with original delay, so I decided to use TIM_HR_TIMER Capture/Compare interrupt
// See hr_timer_delay.c file
//now insert a 120us delay and run step 2 of send_start_bit function
delay_usec(TIM_HR, 120, send_start_bit2); //wait 120usec and go to send_start_bit2 on TIM_HR_TIMER overflow interrupt
}
void send_start_bit2(void) //Second part of send_start_bit
{
gpio_clear(PS2_DATA_PORT, PS2_DATA_PIN); //this is the start bit
//now insert a 10us delay and run step 3 of send_start_bit function
delay_usec(TIM_HR, 10, send_start_bit3); /*wait 10usec and go to send_start_bit3 on TIM_HR_TIMER overflow interrupt*/
}
void send_start_bit3(void) //Third part of send_start_bit
{
ps2int_prev_systicks = systicks;
//Rise clock edge starts the PS/2 device to receive command/argument
gpio_set(PS2_CLK_O_PORT, PS2_CLK_O_PIN);
ps2int_TX_bit_idx = 0; // In TX Int, as we don't manage start bit inside int, idx can start with 0.
prepares_capture(TIM_HR);
#if PS2_CLK_INTERRUPT == GPIO_INT
exti_reset_request(PS2_CLK_I_EXTI);
exti_enable_request(PS2_CLK_I_EXTI);
#endif
}
/***************** Excecution of bitbang Support to other ISR's **********************/
/* Enter point of PS/2 clock line, called from interrupt handled by msxhid */
/* Here is decided if the int is going to be treated as send or receive*/
void ps2_clock_update(bool ps2datapin_logicstate)
{
if (!ps2datapin_logicstate && (ps2datapin_logicstate == formerps2datapin))
{
//State low is repeated
acctimeps2data0 += time_between_ps2clk;
if (acctimeps2data0 >= 200000) //.2s
{
gpio_set(PS2_DATA_PORT, PS2_DATA_PIN);
ps2int_state = PS2INT_RECEIVE;
ps2int_RX_bit_idx = 0;
}
}
else
acctimeps2data0 = 0; //Reset acc time counter
formerps2datapin = ps2datapin_logicstate; //To compare at next bit
uint8_t mountstring[16]; //Used in con_send_string()
/*Any keyboard interrupt that comes after 900 micro seconds means an error condition,
but I`m considering it as an error for about 100 ms, to acommodate this to power on, to answer
to Read ID command. I observed this behavior on my own PS/2 keyboards. It is huge!*/
if( ((ps2int_state == PS2INT_SEND_COMMAND) || (ps2int_state == PS2INT_SEND_ARGUMENT))
&& ((ps2int_TX_bit_idx != 0) && (systicks - ps2int_prev_systicks) > 1) )
{ //reset to PS/2 receive condition
gpio_set(PS2_DATA_PORT, PS2_DATA_PIN);
//User messages (debug)
con_send_string((uint8_t*)"ps2_clock_sent reseted - Timeout = ");
conv_uint32_to_dec((systicks - ps2int_prev_systicks), mountstring);
con_send_string((uint8_t*)mountstring);
con_send_string((uint8_t*)", ps2int_TX_bit_idx = ");
conv_uint32_to_dec((uint32_t)ps2int_TX_bit_idx, mountstring);
con_send_string((uint8_t*)mountstring);
con_send_string((uint8_t*)", ");
conv_uint16_to_4a_hex(ps2int_state, mountstring);
con_send_string((uint8_t*)mountstring);
con_send_string((uint8_t*)";\r\n");
ps2int_state = PS2INT_RECEIVE;
ps2int_RX_bit_idx = 0;
}
if( (ps2int_state == PS2INT_SEND_COMMAND) || (ps2int_state == PS2INT_SEND_ARGUMENT) )
ps2_clock_send(ps2datapin_logicstate);
else
ps2_clock_receive(ps2datapin_logicstate);
}
void ps2_clock_send(bool ps2datapin_logicstate)
{
uint8_t mountstring[16]; //Used in con_send_string()
ps2int_prev_systicks = systicks;
//Time check - The same for all bits
if (time_between_ps2clk > 10000) // time >10ms
con_send_string((uint8_t*)"Time > 10ms on TX");
//|variável| = `if`(condição) ? <valor1 se true> : <valor2 se false>;:
//Only two TX states of send: ps2_send_command & send_argument
uint8_t data_byte = (ps2int_state == PS2INT_SEND_COMMAND) ? command : argument;
//if( (ps2int_TX_bit_idx >= 0) && (ps2int_TX_bit_idx < 8) ) //The first test will be always true
if(ps2int_TX_bit_idx < 8)
{
bool bit = data_byte & (1 << (ps2int_TX_bit_idx));
ps2int_TX_bit_idx++;
//User messages (debug)
/*con_send_string((uint8_t*)"sent bit #");
conv_uint32_to_dec((uint32_t)ps2int_TX_bit_idx-1, mountstring);
con_send_string((uint8_t*)mountstring);
con_send_string((uint8_t*)": ");*/
if(bit)
{
gpio_set(PS2_DATA_PORT, PS2_DATA_PIN);
//User messages (debug)
/*con_send_string((uint8_t*)"1\r\n");*/
}
else
{
gpio_clear(PS2_DATA_PORT, PS2_DATA_PIN);
//User messages (debug)
/*con_send_string((uint8_t*)"0\r\n");*/
}
}
else if(ps2int_TX_bit_idx == 8)
{//parity
bool parity =! __builtin_parity(data_byte);
//User messages (debug)
//con_send_string((uint8_t*)"sent p: "); //This print continues below
if(parity)
{
gpio_set(PS2_DATA_PORT, PS2_DATA_PIN);
//User messages (debug)
//con_send_string((uint8_t*)"1\r\n");
}
else
{
gpio_clear(PS2_DATA_PORT, PS2_DATA_PIN);
//User messages (debug)
//con_send_string((uint8_t*)"0\r\n");
}
ps2int_TX_bit_idx = 9;
}
else if(ps2int_TX_bit_idx == 9)
{//stop bit
gpio_set(PS2_DATA_PORT, PS2_DATA_PIN);
ps2int_TX_bit_idx = 10;
//User messages (debug)
//con_send_string((uint8_t*)"sent stop\r\n");
}
else if(ps2int_TX_bit_idx >= 10)
{
if(ps2datapin_logicstate == false)
{
// ACK bit ok
//User messages (debug)
/*con_send_string((uint8_t*)"TX Data sent OK: 0x");
conv_uint8_to_2a_hex(data_byte, mountstring);
con_send_string((uint8_t*)mountstring);
con_send_string((uint8_t*)", 0x");
conv_uint16_to_4a_hex(ps2int_state, mountstring);
con_send_string((uint8_t*)mountstring);
con_send_string((uint8_t*)"\r\n");*/
}
else
{
// Ack bit NOT ok
gpio_set(PS2_DATA_PORT, PS2_DATA_PIN); //To warranty that is not caused by let this pin LOW
//User messages (debug)
con_send_string((uint8_t*)"Trying to send 0x");
conv_uint8_to_2a_hex(data_byte, mountstring);
con_send_string((uint8_t*)mountstring);
con_send_string((uint8_t*)". ACK bit not received from keyboard\r\n");
ps2int_state = PS2INT_RECEIVE; //force to receive_status in absence of something better
ps2int_RX_bit_idx = 0;
}
if(ps2int_state == PS2INT_SEND_COMMAND)
{
//Although the original logic pointed state=PS2INT_RECEIVE,
//after send the commmand, you have to wait the Acknowlodge of the PS/2 command.
//It is fixed here.
if(command != COMM_ECHO)
{
ps2int_state = PS2INT_WAIT_FOR_COMMAND_ACK;
ps2int_RX_bit_idx = 0;
//User messages (debug)
/*con_send_string((uint8_t*)"TX: new 0x");
conv_uint16_to_4a_hex(ps2int_state, mountstring);
con_send_string((uint8_t*)mountstring);
con_send_string((uint8_t*)"\r\n");*/
}
else
{ //New state created to acomodate waiting for echo
ps2int_state = PS2INT_WAIT_FOR_ECHO;
ps2int_RX_bit_idx = 0;
}
}
else if(ps2int_state == PS2INT_SEND_ARGUMENT)
{
ps2int_state = PS2INT_WAIT_FOR_ARGUMENT_ACK;
ps2int_RX_bit_idx = 0;
//User messages (debug)
/*con_send_string((uint8_t*)"TX: new 0x");
conv_uint16_to_4a_hex(ps2int_state, mountstring);
con_send_string((uint8_t*)mountstring);
con_send_string((uint8_t*)"\r\n");*/
}
else
{
gpio_set(PS2_DATA_PORT, PS2_DATA_PIN);
ps2int_state = PS2INT_RECEIVE;
ps2int_RX_bit_idx = 0;
//User messages (debug)
/*con_send_string((uint8_t*)"TX: new 0x");
conv_uint16_to_4a_hex(ps2int_state, mountstring);
con_send_string((uint8_t*)mountstring);
con_send_string((uint8_t*)"\r\n");*/
}
}
}
void ps2_clock_receive(bool ps2datapin_logicstate)
{
static uint8_t data_word, stop_bit, parity_bit;
uint8_t mountstring[16]; //Used in con_send_string()
//Verify RX timeout, that is quite restricted, if compared to Send Command/Argument
if ( (ps2int_RX_bit_idx != 0) && (time_between_ps2clk > 120) ) //because if RX_bit_idx == 0 will be the reset
{
con_send_string((uint8_t*)"ps2_clock_receive - Timeout\r\n");
ps2int_RX_bit_idx = 0;
}
ps2int_prev_systicks = systicks;
if(ps2int_RX_bit_idx == 0)
{
//Force this interface to put data line in Hi-Z to avoid unspected behavior in case of errors
gpio_set(PS2_DATA_PORT, PS2_DATA_PIN);
//User messages (debug)
//con_send_string((uint8_t*)"RX: ps2int_state = 0x");
//conv_uint16_to_4a_hex(ps2int_state, mountstring);
//con_send_string((uint8_t*)mountstring);
//con_send_string((uint8_t*)"\r\n");
data_word = 0;
stop_bit = 0xff;
parity_bit = 0xff;
ps2int_RX_bit_idx = 1; //Points to the next bit
bool start_bit = ps2datapin_logicstate;
if(start_bit)
{
fail_count++;
ps2int_RX_bit_idx = 0; //reset
}
}
else if( /*(ps2int_RX_bit_idx>0) && */(ps2int_RX_bit_idx<9) ) // collect bits 1 to 8 (D0 to D7)
{
data_word |= (ps2datapin_logicstate << (ps2int_RX_bit_idx - 1));
ps2int_RX_bit_idx++;
}
else if(ps2int_RX_bit_idx == 9)
{
parity_bit = ps2datapin_logicstate;
ps2int_RX_bit_idx++;
}
else if(ps2int_RX_bit_idx == 10)
{ // start + 8 + stop + parity (but started with 0)
ps2int_RX_bit_idx = 0; //next (reset) PS/2 receive condition
stop_bit = ps2datapin_logicstate;
bool parity_ok = __builtin_parity((data_word<<1)|parity_bit);
//User messages (debug)
/*con_send_string((uint8_t*)"RX Data: 0x");
conv_uint8_to_2a_hex(data_word, mountstring);
con_send_string((uint8_t*)mountstring);
if(parity_ok)
con_send_string((uint8_t*)", pbit OK,");
else
con_send_string((uint8_t*)", pbit issue,");
if(stop_bit == 1)
con_send_string((uint8_t*)" sbit OK,");
else
con_send_string((uint8_t*)" sbit issue,");
con_send_string((uint8_t*)" 0x");
conv_uint16_to_4a_hex(ps2int_state, mountstring);
con_send_string((uint8_t*)mountstring);
con_send_string((uint8_t*)"\r\n");*/
if(parity_ok && (stop_bit == 1) ) //start bit condition was already tested above
{ // ps2int_status receive procesing block (begin)
if (ps2int_state == PS2INT_RECEIVE)
{
//this put routine is new
uint8_t i = ps2_recv_put_ptr;
uint8_t i_next = (i + 1) & (uint8_t)(PS2_RECV_BUFFER_SIZE - 1);
if (i_next != ps2_recv_get_ptr)
{
ps2_recv_buffer[i] = data_word;
ps2_recv_put_ptr = i_next;
}
}
else if (ps2int_state == PS2INT_WAIT_FOR_COMMAND_ACK)
{
if(data_word == KB_ACKNOWLEDGE) //0xFA is Acknowledge from PS/2 keyboard
{
if(argument == ARG_NO_ARG) //0xFF is an empty argument
{
if(command == COMM_ECHO)
{
//Wait for ECHO
ps2int_state = PS2INT_WAIT_FOR_ECHO;
ps2int_RX_bit_idx = 0;//reset PS/2 receive condition
command_running = false;
}
else
{
//no argument: set to receive
ps2int_state = PS2INT_RECEIVE;
ps2int_RX_bit_idx = 0;//reset PS/2 receive condition
command_running = false;
}
}
else
{
//argument is not empty (!=ARG_NO_ARG). Send it
ps2int_state = PS2INT_SEND_ARGUMENT;
send_start_bit_next(150);
}
}
else if(data_word == KBCOMM_RESEND) //0xFE is Resend
{
ps2int_state = PS2INT_SEND_COMMAND;
send_start_bit_next(150); //Send BOTH command and argument
} //if(data_word==KBCOMM_RESEND) //0xFE is Resend
else
{
//reset PS/2 receive condition
ps2int_state = PS2INT_RECEIVE;
ps2int_RX_bit_idx = 0;
command_running = false;
fail_count++;
//User messages (debug)
con_send_string((uint8_t*)"Got unexpected command response: 0x");
conv_uint8_to_2a_hex(data_word, mountstring);
con_send_string((uint8_t*)mountstring);
con_send_string((uint8_t*)"\r\n");
} //else if(data_word==KBCOMM_RESEND) //0xFE is Resend
}
else if (ps2int_state == PS2INT_WAIT_FOR_ARGUMENT_ACK)
{
if(data_word == KB_ACKNOWLEDGE) //Acknowledge from PS/2 keyboard
{
//Acknowledge received => set to receive
ps2int_state = PS2INT_RECEIVE;
ps2int_RX_bit_idx = 0;//Prepares for the next PS/2 receive condition
command_running = false;
}
else if(data_word == KBCOMM_RESEND) //0xFE is Resend
{
ps2int_state = PS2INT_SEND_COMMAND; //Resend BOTH command AND argument
send_start_bit_next(150);
}
else
{
//reset PS/2 receive condition
ps2int_state = PS2INT_RECEIVE;
ps2int_RX_bit_idx = 0;
command_running = false;
//User messages (debug)
con_send_string((uint8_t*)"Got unexpected command response: 0x");
conv_uint8_to_2a_hex(data_word, mountstring);
con_send_string((uint8_t*)mountstring);
con_send_string((uint8_t*)"\r\n");
}
} // ps2int_status receive procesing block (end)
else if (ps2int_state == PS2INT_WAIT_FOR_ECHO)
{
if(data_word == COMM_ECHO) //Echo received from PS/2 keyboard
{
//Echo received => set to receive
ps2int_state = PS2INT_RECEIVE;
ps2int_RX_bit_idx = 0;//Prepares for the next PS/2 receive condition
command_running = false;
echo_received = true;
}
else //if(data_word == COMM_ECHO)
{
//reset PS/2 receive condition
ps2int_state = PS2INT_RECEIVE;
ps2int_RX_bit_idx = 0;
echo_received = false;
command_running = false;
//User messages (debug)
con_send_string((uint8_t*)"In 0x");
conv_uint16_to_4a_hex(ps2int_state, mountstring);
con_send_string((uint8_t*)mountstring);
con_send_string((uint8_t*)", received 0x");
conv_uint8_to_2a_hex(data_word, mountstring);
con_send_string((uint8_t*)mountstring);
con_send_string((uint8_t*)" instead of COMM_ECHO (0xEE)\r\n");
} //if(data_word == COMM_ECHO)
} //else if (ps2int_state == PS2INT_WAIT_FOR_ECHO)
} //if(start_bit==0 && stop_bit==1 && parity_ok)
else
{
//reset PS/2 receive condition
ps2int_state = PS2INT_RECEIVE;
ps2int_RX_bit_idx = 0;
command_running = false;
//User messages (debug)
con_send_string((uint8_t*)"Framming Error. RX Data: 0x");
conv_uint8_to_2a_hex(data_word, mountstring);
con_send_string((uint8_t*)mountstring);
con_send_string((uint8_t*)", parity ");
mountstring[0] = parity_bit ? '1' : '0';
mountstring[1] = 0;
con_send_string((uint8_t*)mountstring);
con_send_string((uint8_t*)", Stop ");
mountstring[0] = stop_bit ? '1' : '0';
mountstring[1] = 0;
con_send_string((uint8_t*)mountstring);
con_send_string((uint8_t*)"\r\n");
fail_count++;
}
} //else if(ps2int_RX_bit_idx==10)
}
void reset_mount_scancode_machine()
{
mount_scancode_count_status = 0;
ps2_keystr_e0 = false;
ps2_keystr_e1 = false;
ps2_keystr_f0 = false;
}
bool mount_scancode()
{
//Check MSX CAPS and Kana status update
if( (caps_state = gpio_get(CAPSLOCK_PORT, CAPSLOCK_PIN)) != caps_former )
update_ps2_leds = true;
if( (kana_state = gpio_get(KANA_PORT, KANA_PIN)) != kana_former )
update_ps2_leds = true;
if (!mount_scancode_OK)
{
while(available_ps2_byte() && !mount_scancode_OK)
{
ps2_byte_received = get_ps2_byte(ps2_recv_buffer);
//User messages (debug)
/*con_send_string((uint8_t*)"Mount_scancode RX Ch=");
conv_uint8_to_2a_hex(ps2_byte_received, mountstring);
con_send_string((uint8_t*)mountstring);
con_send_string((uint8_t*)"\r\n"); */
switch (mount_scancode_count_status)
{
case 0: //It's reading the first byte of ps2_byte_received
{
if(ps2_byte_received < 0xE0) //If until 0xDF, included, runs here
{
//Concluded. 1 byte only scan code.
scancode[1] = ps2_byte_received;
//Conclui scan
scancode[0] = 1;
mount_scancode_OK = true;
reset_mount_scancode_machine();
return true;
}
if(ps2_byte_received == 0xE0)
{
//0xE0 + (Any != 0xF0) <= 2 bytes
//0xE0 + 0xF0 + (Any) <= 3 bytes
ps2_keystr_e0 = true;
scancode[1] = ps2_byte_received;
scancode[0] = 1;
mount_scancode_count_status = 1; //points to next case
break;
}
if(ps2_byte_received == 0xE1)
{
// Pause/Break key: 8 bytes (0xE1 + 7 bytes). Store only the 3 first bytes and discard the others
ps2_keystr_e1 = true;
scancode[1] = ps2_byte_received;
scancode[0] = 1;
mount_scancode_count_status = 1; //points to next case
break;
}
if(ps2_byte_received == 0xF0)
{
//Always 2 bytes 0xF0 + (Any)
ps2_keystr_f0 = true;
scancode[1] = ps2_byte_received;
scancode[0] = 1;
mount_scancode_count_status = 1; //points to next case
break;
}
break; //case syntax suggested
} //case 0:
case 1:
{
if (ps2_keystr_e0)
{
if(ps2_byte_received != 0xF0)
{
if(ps2_byte_received != 0x12)
{
//2 bytes and this ps2_byte_received is != 0xF0 e != 0x12, so, concluded
scancode[2] = ps2_byte_received;
scancode[0] = 2;
//task concluded