-
Notifications
You must be signed in to change notification settings - Fork 6
/
setup_keyboard.cpp
112 lines (94 loc) · 2.8 KB
/
setup_keyboard.cpp
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
#include "ps2.h"
#include "smc_pins.h"
#include "setup_ps2.h"
/*
Watchdog
*/
#define WATCHDOG_ARM 255
#define WATCHDOG_DISABLE 0
/*
PS/2 Keyboard Response Codes
*/
#define PS2_BAT_OK 0xaa
#define PS2_BAT_FAIL 0xfc
#define PS2_ACK 0xfa
/*
PS/2 Host to Keyboard Commmands
*/
#define PS2_CMD_SET_LEDS 0xed
#define PS2_CMD_RESET 0xff
/*
Variables
*/
extern bool SYSTEM_POWERED;
extern PS2KeyboardPort<PS2_KBD_CLK, PS2_KBD_DAT, 16> Keyboard;
static volatile uint8_t watchdogExpiryState = KBD_STATE_RESET;
static volatile uint8_t kbd_init_state = 0;
void keyboardTick() {
static uint8_t watchdog = WATCHDOG_DISABLE;
// Return to OFF state if system powered down
if (!SYSTEM_POWERED && kbd_init_state != KBD_STATE_OFF) {
Keyboard.flush();
Keyboard.clearBAT();
kbd_init_state = KBD_STATE_OFF;
watchdog = WATCHDOG_DISABLE;
return;
}
// State machine
switch (kbd_init_state) {
case KBD_STATE_OFF:
if (SYSTEM_POWERED) {
kbd_init_state = KBD_STATE_BAT;
watchdog = WATCHDOG_ARM;
}
break;
case KBD_STATE_BAT:
if (Keyboard.BAT() == PS2_BAT_OK) {
kbd_init_state = KBD_STATE_SET_LEDS;
watchdog = WATCHDOG_ARM;
}
else if (Keyboard.BAT() == PS2_BAT_FAIL) {
kbd_init_state = KBD_STATE_RESET;
watchdog = WATCHDOG_ARM;
}
break;
case KBD_STATE_SET_LEDS:
Keyboard.sendPS2Command(PS2_CMD_SET_LEDS, 0x02);
kbd_init_state = KBD_STATE_SET_LEDS_ACK;
watchdog = WATCHDOG_ARM;
break;
case KBD_STATE_SET_LEDS_ACK:
if (Keyboard.getCommandStatus() == PS2_CMD_STATUS::CMD_ACK) {
kbd_init_state = KBD_STATE_READY;
watchdog = WATCHDOG_DISABLE;
}
break;
case KBD_STATE_READY:
break;
case KBD_STATE_RESET:
Keyboard.flush();
Keyboard.clearBAT();
Keyboard.sendPS2Command(PS2_CMD_RESET);
kbd_init_state = KBD_STATE_RESET_ACK;
watchdog = WATCHDOG_ARM;
break;
case KBD_STATE_RESET_ACK:
if (Keyboard.getCommandStatus() == PS2_CMD_STATUS::CMD_ACK) {
kbd_init_state = KBD_STATE_BAT;
watchdog = WATCHDOG_ARM;
}
break;
}
if (watchdog > 0) {
watchdog--;
if (watchdog == 0) {
kbd_init_state = watchdogExpiryState;
}
}
}
void keyboardReset() {
kbd_init_state = KBD_STATE_RESET;
}
uint8_t getKeyboardState() {
return kbd_init_state;
}