+ case WAIT_SETTLE:
+ // wait for keyboard to settle after plugin
+ if (timer_elapsed(init_time) > 1000) {
+ state = AT_RESET;
+ }
+ break;
+ case AT_RESET:
+ ibmpc_host_isr_clear();
+ ibmpc_host_enable();
+ wait_ms(1); // keyboard can't respond to command without this
+
+ // SKIDATA-2-DE(and some other keyboards?) stores 'Code Set' setting in nonvlatile memory
+ // and keeps it until receiving reset. Sending reset here may be useful to clear it, perhaps.
+ // https://github.com/tmk/tmk_keyboard/wiki/IBM-PC-AT-Keyboard-Protocol#select-alternate-scan-codesf0
+
+ // reset command
+ if (0xFA == ibmpc_host_send(0xFF)) {
+ state = WAIT_AA;
+ } else {
+ state = XT_RESET;
+ }
+ xprintf("A%u ", timer_read());
+ break;
+ case XT_RESET:
+ // Reset XT-initialize keyboard
+ // XT: hard reset 500ms for IBM XT Type-1 keyboard and clones
+ // XT: soft reset 20ms min(clock Lo)
+ ibmpc_host_disable(); // soft reset: inihibit(clock Lo/Data Hi)
+ IBMPC_RST_LO(); // hard reset: reset pin Lo
+
+ init_time = timer_read();
+ state = XT_RESET_WAIT;
+ break;
+ case XT_RESET_WAIT:
+ if (timer_elapsed(init_time) > 500) {
+ state = XT_RESET_DONE;
+ }
+ break;
+ case XT_RESET_DONE:
+ IBMPC_RST_HIZ(); // hard reset: reset pin HiZ
+ ibmpc_host_isr_clear();
+ ibmpc_host_enable(); // soft reset: idle(clock Hi/Data Hi)
+
+ xprintf("X%u ", timer_read());
+ init_time = timer_read();
+ state = WAIT_AA;
+ break;
+ case WAIT_AA:
+ // 1) Read BAT code and ID on keybaord power-up
+ // For example, XT/AT sends 'AA' and Terminal sends 'AA BF BF' after BAT
+ // AT 84-key: POR and BAT can take 900-9900ms according to AT TechRef [8] 4-7
+ // AT 101/102-key: POR and BAT can take 450-2500ms according to AT TechRef [8] 4-39
+ // 2) Read key typed by user or anything after error on protocol or scan code
+ // This can happen in case of keyboard hotswap, unstable hardware, signal integrity problem or bug
+
+ /* wait until keyboard sends any code without 10000ms timeout
+ if (timer_elapsed(init_time) > 10000) {
+ state = READ_ID;
+ }
+ */
+ if (ibmpc_host_recv() != -1) { // wait for AA
+ xprintf("W%u ", timer_read());
+ init_time = timer_read();
+ state = WAIT_AABF;
+ }
+ break;
+ case WAIT_AABF:
+ // NOTE: we can omit to wait BF BF
+ // ID takes 500ms max? TechRef [8] 4-41, though 1ms is enough for 122-key Terminal 6110345
+ if (timer_elapsed(init_time) > 500) {
+ state = READ_ID;
+ }
+ if (ibmpc_host_recv() != -1) { // wait for BF
+ xprintf("W%u ", timer_read());
+ init_time = timer_read();
+ state = WAIT_AABFBF;
+ }
+ break;
+ case WAIT_AABFBF:
+ if (timer_elapsed(init_time) > 500) {
+ state = READ_ID;
+ }
+ if (ibmpc_host_recv() != -1) { // wait for BF
+ xprintf("W%u ", timer_read());