├── README.md ├── settings.png └── blue_pill_keyboard.ino /README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeoffLedak/Blue-Pill-Sega-Keyboard-Adapter/HEAD/README.md -------------------------------------------------------------------------------- /settings.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeoffLedak/Blue-Pill-Sega-Keyboard-Adapter/HEAD/settings.png -------------------------------------------------------------------------------- /blue_pill_keyboard.ino: -------------------------------------------------------------------------------- 1 | /* 2 | PS/2 to Sega XBAND keyboard adapter by Geoff Ledak 3 | For use on "Bill Pill" STM32 board 4 | https://github.com/GeoffLedak/Blue-Pill-Sega-Keyboard-Adapter 5 | 6 | Sega pinout: 7 | 8 | 1 2 3 4 5 9 | 6 7 8 9 10 | 11 | Pin 5 is +5V 12 | Pin 8 is Ground 13 | 14 | Sega Pin - Blue Pill pin 15 | ------------------------- 16 | 17 | 1 = PB12 18 | 2 = PB13 19 | 3 = PB14 20 | 4 = PB15 21 | 6 (TL) = PA10 22 | 7 (TH) = PA8 23 | 9 (TR) = PA9 24 | 25 | 5 = +5V 26 | 8 = Ground 27 | 28 | 1.5k Ohm pullup resistors are attached to all 7 pins (pins 1, 2, 3, 4, 6, 7, and 9) 29 | 30 | 31 | PS/2 Clock = PB10 32 | PS/2 Data = PB11 33 | 34 | 10k Ohm pullup resistors are attached to clock and data 35 | 36 | --------------------------- 37 | 38 | bit0 PB12 39 | bit1 PB13 40 | bit2 PB14 41 | bit3 PB15 42 | 43 | TH PA8 controlled by sega 44 | TR PA9 controlled by sega 45 | TL PA10 controlled by adapter 46 | */ 47 | 48 | #define KEYBOARD_DATA_PIN PB11 49 | #define KEYBOARD_CLOCK_PIN PB10 50 | 51 | #define KEYBOARD_CLOCK_PIN_BIT 0b0000010000000000 52 | 53 | #define TH_BIT 0b0000000100000000 // PA8 54 | #define TR_BIT 0b0000001000000000 // PA9 55 | #define TL_BIT 0b0000010000000000 // PA10 56 | 57 | // Scancode buffer 58 | #define BUFFER_SIZE 128 59 | volatile uint8_t buffer[BUFFER_SIZE]; 60 | volatile uint8_t head, tail; 61 | 62 | // Control data (to send to keyboard, Reset, Caps LED, etc) buffer 63 | volatile uint8_t sendBuffer[BUFFER_SIZE]; 64 | volatile uint8_t sendHead, sendTail; 65 | 66 | volatile char waitingForAck = 0; 67 | 68 | volatile uint8_t ACKtimeout = 0; 69 | 70 | volatile char PS2busy = 0; 71 | volatile char WriteToPS2keyboard = 0; 72 | 73 | volatile uint8_t bitcount = 0; 74 | volatile uint8_t incoming = 0; 75 | volatile uint8_t outgoing = 0; 76 | volatile uint32_t prev_ms = 0; 77 | 78 | volatile uint8_t _parity = 0; 79 | 80 | volatile char hasParityError = 0; 81 | 82 | volatile char requestResendFromKeyboard = 0; 83 | 84 | unsigned long LEDstatusByte = 0; 85 | volatile char LEDstatusIncoming = 0; 86 | 87 | unsigned long TypematicStatusByte = 0x0000002B; // defaults: 00101011 - 10.9 char/sec - 500ms 88 | volatile char TypematicStatusIncoming = 0; 89 | 90 | volatile char waitingForResetAck = 0; 91 | 92 | volatile uint8_t resendTimeout = 0; 93 | 94 | void setup() 95 | { 96 | // Serial.begin(9600); 97 | 98 | // Setup AT keyboard communication 99 | 100 | // set KEYBOARD_DATA_PIN (PB11) to input floating 101 | GPIOB->regs->CRH = (GPIOB->regs->CRH & 0xFFFF0FFF) | 0x00004000; // CNF = 01 MODE = 00 102 | 103 | // set KEYBOARD_CLOCK_PIN (PB10) to input floating 104 | GPIOB->regs->CRH = (GPIOB->regs->CRH & 0xFFFFF0FF) | 0x00000400; // CNF = 01 MODE = 00 105 | 106 | head = 0; 107 | tail = 0; 108 | sendHead = 0; 109 | sendTail = 0; 110 | 111 | attachInterrupt(KEYBOARD_CLOCK_PIN, ps2interrupt, FALLING); 112 | 113 | 114 | // Setup XBAND communication 115 | 116 | /* 117 | CRH is used to set type/and or speed of pins 8-15 of the port 118 | CRL is used to set type/and or speed of pins 0-7 of the port 119 | 120 | Out of these 4 bits, the low 2 bits are MODE, and high 2 bits are CNF. 121 | */ 122 | 123 | // set TH (PA8) to input floating 124 | GPIOA->regs->CRH = (GPIOA->regs->CRH & 0xFFFFFFF0) | 0x00000004; // CNF = 01 MODE = 00 125 | 126 | // set TR (PA9) to input floating 127 | GPIOA->regs->CRH = (GPIOA->regs->CRH & 0xFFFFFF0F) | 0x00000040; // CNF = 01 MODE = 00 128 | 129 | // set TL (PA10) to output open drain 130 | GPIOA->regs->CRH = (GPIOA->regs->CRH & 0xFFFFF0FF) | 0x00000500; // CNF = 01 MODE = 01 131 | 132 | initPins(); 133 | } 134 | 135 | 136 | void loop() 137 | { 138 | // do nothing while we wait for TH to go high again (transaction complete) 139 | do{ } 140 | while( (GPIOA->regs->IDR & TH_BIT) != TH_BIT ); 141 | 142 | // wait for TH to go low 143 | do{ } 144 | while( (GPIOA->regs->IDR & TH_BIT) != 0 ); 145 | 146 | Talk_To_Sega(); 147 | 148 | 149 | if( requestResendFromKeyboard ) 150 | { 151 | requestResendFromKeyboard = 0; 152 | sendResendRequest(); 153 | } 154 | 155 | else if( ( sendHead != sendTail ) && !waitingForAck ) 156 | { 157 | ACKtimeout = 0; 158 | sendNow(); 159 | } 160 | 161 | else if( ( sendHead != sendTail ) && waitingForAck ) 162 | { 163 | ACKtimeout++; 164 | 165 | if( ACKtimeout >= 30 ) 166 | { 167 | requestResendFromKeyboard = 0; 168 | clearSendBuffer(); 169 | sendEnableScanCommand(); 170 | ACKtimeout = 0; 171 | waitingForResetAck = 1; 172 | 173 | sendNow(); 174 | } 175 | } 176 | 177 | } 178 | 179 | 180 | void initPins() 181 | { 182 | delayMicroseconds(5); 183 | 184 | // make the data port an output, open drain // CNF = 01 MODE = 01 185 | GPIOB->regs->CRH = (GPIOB->regs->CRH & 0x0000FFFF) | 0x55550000; 186 | 187 | // present 1st nybble of ID 0xC 0b 1100 3210 188 | GPIOB->regs->ODR = (GPIOB->regs->ODR & 0b0000111111111111) | 0b1100000000000000; 189 | 190 | delayMicroseconds(1); 191 | 192 | // Raise TL (key ACK) (PA10) 193 | GPIOA->regs->ODR = (GPIOA->regs->ODR & 0b1111101111111111) | 0b0000010000000000; 194 | } 195 | 196 | 197 | short waitForPin(short pin, short value) 198 | { 199 | short numLoops = 5000; 200 | 201 | while(numLoops != 0) 202 | { 203 | __asm__("nop\n\t"); 204 | 205 | if( (GPIOA->regs->IDR & TH_BIT) != 0 ) // TH went high, transaction aborted 206 | return 0; 207 | 208 | if( value == LOW && (GPIOA->regs->IDR & pin) == value ) // We found the value we want, return 1 209 | return 1; 210 | 211 | if( value == HIGH && (GPIOA->regs->IDR & pin) == pin ) // We found the value we want, return 1 212 | return 1; 213 | 214 | numLoops--; 215 | } 216 | 217 | return 0; // We timed out, return 0 218 | } 219 | 220 | 221 | void endWait() 222 | { 223 | short numLoops = 5000; // this number of loops will give us a timeout of 224 | // about 1.19 milliseconds which is what the 225 | while(numLoops != 0) // original keyboard has 226 | { 227 | __asm__("nop\n\t"); 228 | 229 | if( (GPIOA->regs->IDR & TH_BIT) != 0 ) // TH went high, transaction aborted 230 | return; 231 | 232 | numLoops--; 233 | } 234 | 235 | return; // We timed out 236 | } 237 | 238 | 239 | void Talk_To_Sega() 240 | { 241 | // gen TH = select 242 | // gen TR = request 243 | // key TL = acknowledge 244 | 245 | // -------------------------------------------------------------------------------- 246 | // Give Sega the 3 remaining nibbles of our 4 nibble Catapult Keyboard ID - $C369 247 | // -------------------------------------------------------------------------------- 248 | 249 | delayMicroseconds(9); 250 | 251 | // present 2nd nybble of ID 0x3 0b 0011 3210 252 | GPIOB->regs->ODR = (GPIOB->regs->ODR & 0b0000111111111111) | 0b0011000000000000; 253 | 254 | if( !waitForPin(TR_BIT, LOW) ) { // wait for TR (REQ) to go LOW 255 | initPins(); 256 | return; 257 | } 258 | 259 | 260 | delayMicroseconds(7); 261 | 262 | // present 3rd nybble of ID 0x6 0b 0110 3210 263 | GPIOB->regs->ODR = (GPIOB->regs->ODR & 0b0000111111111111) | 0b0110000000000000; 264 | 265 | delayMicroseconds(1); 266 | 267 | // Lower TL (key ACK) (PA10) 268 | GPIOA->regs->ODR = (GPIOA->regs->ODR & 0b1111101111111111) | 0b0000000000000000; 269 | 270 | if( !waitForPin(TR_BIT, HIGH) ) { // wait for TR (gen REQ) to go HIGH 271 | initPins(); 272 | return; 273 | } 274 | 275 | delayMicroseconds(5); 276 | 277 | // turn the data port around (make it an input), is this a write? 278 | // make the data port an input, floating // CNF = 01 MODE = 00 279 | GPIOB->regs->CRH = (GPIOB->regs->CRH & 0x0000FFFF) | 0x44440000; 280 | 281 | delayMicroseconds(7); 282 | 283 | unsigned short value = GPIOB->regs->IDR & 0b1111000000000000; 284 | 285 | // does the Sega want to send us data? 286 | if(value == 0) 287 | { 288 | Listen_To_Sega(); 289 | return; 290 | } 291 | 292 | // otherwise 293 | // make data port an output 294 | GPIOB->regs->CRH = (GPIOB->regs->CRH & 0x0000FFFF) | 0x55550000; 295 | 296 | delayMicroseconds(1); 297 | 298 | // present 4th nybble of ID 0x9 0b 1001 3210 299 | GPIOB->regs->ODR = (GPIOB->regs->ODR & 0b0000111111111111) | 0b1001000000000000; 300 | 301 | delayMicroseconds(2); 302 | 303 | // Raise TL (key ACK) (PA10) 304 | GPIOA->regs->ODR = (GPIOA->regs->ODR & 0b1111101111111111) | 0b0000010000000000; 305 | 306 | if( !waitForPin(TR_BIT, LOW) ) { // wait for TR (REQ) to go LOW 307 | delayMicroseconds(4); // if TH went high here, this is a find 308 | initPins(); 309 | return; 310 | } 311 | 312 | // -------------------------------------------------------------------------------- 313 | // TALK to Sega 314 | // -------------------------------------------------------------------------------- 315 | 316 | 317 | // is there stuff in the buffer? 318 | 319 | uint8_t s = get_scan_code(); 320 | 321 | if(s == 0) // buffer is empty 322 | { 323 | delayMicroseconds(7); 324 | 325 | // zero bytes to send 0000 326 | GPIOB->regs->ODR = (GPIOB->regs->ODR & 0b0000111111111111) | 0b0000000000000000; 327 | 328 | delayMicroseconds(1); 329 | 330 | // Lower TL (key ACK) (PA10) 331 | GPIOA->regs->ODR = (GPIOA->regs->ODR & 0b1111101111111111) | 0b0000000000000000; 332 | 333 | 334 | endWait(); // wait for start to go up 335 | initPins(); // We're all done 336 | return; 337 | } 338 | else // buffer has stuff in it 339 | { 340 | uint16_t length = 0; 341 | uint8_t scancodes[15]; 342 | uint8_t *index = scancodes; 343 | 344 | *index = 0; // put a type byte of 0 to send first 345 | index++; 346 | length++; 347 | 348 | while( s != 0) // now add the actual scancodes 349 | { 350 | *index = s; 351 | index++; 352 | s = get_scan_code(); 353 | length++; 354 | 355 | if( length > 14 ) 356 | break; 357 | } 358 | 359 | 360 | // now send stuff here 361 | 362 | delayMicroseconds(4); 363 | 364 | // how many bytes we want to send 365 | GPIOB->regs->ODR = (GPIOB->regs->ODR & 0b0000111111111111) | ( length << 12 ); 366 | 367 | delayMicroseconds(1); 368 | 369 | // Lower TL (key ACK) (PA10) 370 | GPIOA->regs->ODR = (GPIOA->regs->ODR & 0b1111101111111111) | 0b0000000000000000; 371 | 372 | 373 | 374 | index = scancodes; 375 | 376 | while( length > 0 ) // then send the actual scancodes 377 | { 378 | 379 | if( !waitForPin(TR_BIT, HIGH) ) { // wait for TR (gen REQ) to go HIGH 380 | initPins(); 381 | return; 382 | } 383 | 384 | delayMicroseconds(3); 385 | 386 | // write high nibble 387 | GPIOB->regs->ODR = (GPIOB->regs->ODR & 0b0000111111111111) | ( (*index >> 4) << 12 ); 388 | 389 | delayMicroseconds(6); 390 | 391 | // Raise TL (key ACK) (PA10) 392 | GPIOA->regs->ODR = (GPIOA->regs->ODR & 0b1111101111111111) | 0b0000010000000000; 393 | 394 | 395 | // -------------- 396 | 397 | 398 | if( !waitForPin(TR_BIT, LOW) ) { // wait for TR (gen REQ) to go LOW 399 | initPins(); 400 | return; 401 | } 402 | 403 | delayMicroseconds(4); 404 | 405 | // write low nibble 406 | GPIOB->regs->ODR = (GPIOB->regs->ODR & 0b0000111111111111) | ( (*index & 0xF) << 12 ); 407 | 408 | delayMicroseconds(5); 409 | 410 | // Lower TL (key ACK) (PA10) 411 | GPIOA->regs->ODR = (GPIOA->regs->ODR & 0b1111101111111111) | 0b0000000000000000; 412 | 413 | index++; 414 | length--; 415 | } 416 | 417 | 418 | endWait(); // wait for start to go up 419 | initPins(); // We're all done 420 | return; 421 | } 422 | 423 | } 424 | 425 | 426 | void Listen_To_Sega() 427 | { 428 | // Raise TL (key ACK) (PA10) 429 | GPIOA->regs->ODR = (GPIOA->regs->ODR & 0b1111101111111111) | 0b0000010000000000; 430 | 431 | if( !waitForPin(TR_BIT, LOW) ) { // wait for TR (REQ) to go LOW 432 | initPins(); 433 | return; 434 | } 435 | 436 | delayMicroseconds(2); 437 | 438 | // get the BYTE count (includes TYPE, which we don't care about) 439 | short byteCount = (GPIOB->regs->IDR & 0b1111000000000000) >> 12; 440 | 441 | delayMicroseconds(3); 442 | 443 | // Lower TL (key ACK) (PA10) 444 | GPIOA->regs->ODR = (GPIOA->regs->ODR & 0b1111101111111111) | 0b0000000000000000; 445 | 446 | 447 | 448 | 449 | 450 | 451 | if( !waitForPin(TR_BIT, HIGH) ) { // wait for TR (gen REQ) to go HIGH 452 | initPins(); 453 | return; 454 | } 455 | 456 | delayMicroseconds(2); 457 | 458 | short type1 = (GPIOB->regs->IDR & 0b1111000000000000) >> 12; 459 | 460 | delayMicroseconds(6); 461 | 462 | // Raise TL (key ACK) (PA10) 463 | GPIOA->regs->ODR = (GPIOA->regs->ODR & 0b1111101111111111) | 0b0000010000000000; 464 | 465 | 466 | 467 | 468 | 469 | if( !waitForPin(TR_BIT, LOW) ) { // wait for TR (REQ) to go LOW 470 | initPins(); 471 | return; 472 | } 473 | 474 | delayMicroseconds(2); 475 | 476 | short type2 = (GPIOB->regs->IDR & 0b1111000000000000) >> 12; 477 | 478 | delayMicroseconds(2); 479 | 480 | // Lower TL (key ACK) (PA10) 481 | GPIOA->regs->ODR = (GPIOA->regs->ODR & 0b1111101111111111) | 0b0000000000000000; 482 | 483 | byteCount--; 484 | 485 | 486 | 487 | 488 | static volatile uint8_t incomingValue = 0; 489 | 490 | 491 | while(byteCount > 0) 492 | { 493 | 494 | if( !waitForPin(TR_BIT, HIGH) ) { // wait for TR (gen REQ) to go HIGH 495 | initPins(); 496 | return; 497 | } 498 | 499 | delayMicroseconds(2); 500 | 501 | incomingValue = (GPIOB->regs->IDR & 0b1111000000000000) >> 12; 502 | incomingValue <<= 4; 503 | 504 | delayMicroseconds(7); 505 | 506 | // Raise TL (key ACK) (PA10) 507 | GPIOA->regs->ODR = (GPIOA->regs->ODR & 0b1111101111111111) | 0b0000010000000000; 508 | 509 | 510 | 511 | 512 | if( !waitForPin(TR_BIT, LOW) ) { // wait for TR (REQ) to go LOW 513 | initPins(); 514 | return; 515 | } 516 | 517 | delayMicroseconds(2); 518 | 519 | incomingValue |= ((GPIOB->regs->IDR & 0b1111000000000000) >> 12); 520 | 521 | delayMicroseconds(4); 522 | 523 | // Lower TL (key ACK) (PA10) 524 | GPIOA->regs->ODR = (GPIOA->regs->ODR & 0b1111101111111111) | 0b0000000000000000; 525 | 526 | 527 | 528 | processByteFromSega(incomingValue); 529 | 530 | byteCount--; 531 | } 532 | 533 | 534 | // Serial.print("FROM SEGA "); 535 | // Serial.println(incomingValue, HEX); 536 | 537 | 538 | endWait(); // wait for start to go up 539 | initPins(); // We're all done 540 | return; 541 | } 542 | 543 | 544 | void sendNow() 545 | { 546 | outgoing = get_byte_to_send_to_keyboard(); 547 | 548 | // Spin here until PS2busy == 0 549 | // and keyboard clock pin is high 550 | do { } 551 | while(PS2busy != 0 && (GPIOB->regs->IDR & KEYBOARD_CLOCK_PIN_BIT) != KEYBOARD_CLOCK_PIN_BIT ); 552 | 553 | PS2busy = 1; 554 | WriteToPS2keyboard = 1; 555 | 556 | waitingForAck = 1; 557 | 558 | _parity = 0; 559 | bitcount = 0; 560 | 561 | 562 | // set pins to outputs and high 563 | 564 | // set KEYBOARD_DATA_PIN (PB11) high 565 | GPIOB->regs->ODR = (GPIOB->regs->ODR & 0b1111011111111111) | 0b0000100000000000; 566 | 567 | // set KEYBOARD_DATA_PIN (PB11) output open drain 568 | GPIOB->regs->CRH = (GPIOB->regs->CRH & 0xFFFF0FFF) | 0x00005000; // CNF = 01 MODE = 01 569 | 570 | 571 | // set KEYBOARD_CLOCK_PIN (PB10) high 572 | GPIOB->regs->ODR = (GPIOB->regs->ODR & 0b1111101111111111) | 0b0000010000000000; 573 | 574 | // set KEYBOARD_CLOCK_PIN (PB10) output open drain 575 | GPIOB->regs->CRH = (GPIOB->regs->CRH & 0xFFFFF0FF) | 0x00000500; // CNF = 01 MODE = 01 576 | 577 | 578 | delayMicroseconds( 10 ); 579 | 580 | 581 | // set KEYBOARD_CLOCK_PIN (PB10) low 582 | GPIOB->regs->ODR = (GPIOB->regs->ODR & 0b1111101111111111) | 0b0000000000000000; 583 | 584 | // set clock low for 60us 585 | delayMicroseconds( 60 ); 586 | 587 | 588 | // set KEYBOARD_DATA_PIN (PB11) low 589 | GPIOB->regs->ODR = (GPIOB->regs->ODR & 0b1111011111111111) | 0b0000000000000000; 590 | 591 | 592 | // set KEYBOARD_CLOCK_PIN (PB10) to input floating 593 | GPIOB->regs->CRH = (GPIOB->regs->CRH & 0xFFFFF0FF) | 0x00000400; // CNF = 01 MODE = 00 594 | } 595 | 596 | 597 | void sendResendRequest() 598 | { 599 | outgoing = get_byte_to_send_to_keyboard(); 600 | 601 | PS2busy = 1; 602 | WriteToPS2keyboard = 1; 603 | 604 | waitingForAck = 1; 605 | 606 | _parity = 0; 607 | bitcount = 0; 608 | 609 | 610 | // set KEYBOARD_DATA_PIN (PB11) output open drain 611 | GPIOB->regs->CRH = (GPIOB->regs->CRH & 0xFFFF0FFF) | 0x00005000; // CNF = 01 MODE = 01 612 | 613 | 614 | // set KEYBOARD_DATA_PIN (PB11) low 615 | GPIOB->regs->ODR = (GPIOB->regs->ODR & 0b1111011111111111) | 0b0000000000000000; 616 | 617 | 618 | // set KEYBOARD_CLOCK_PIN (PB10) to input floating 619 | GPIOB->regs->CRH = (GPIOB->regs->CRH & 0xFFFFF0FF) | 0x00000400; // CNF = 01 MODE = 00 620 | } 621 | 622 | 623 | void sendSpecificByte(uint8_t byteValue) 624 | { 625 | outgoing = byteValue; 626 | 627 | // Spin here until PS2busy == 0 628 | // and keyboard clock pin is high 629 | do { } 630 | while(PS2busy != 0 && (GPIOB->regs->IDR & KEYBOARD_CLOCK_PIN_BIT) != KEYBOARD_CLOCK_PIN_BIT ); 631 | 632 | PS2busy = 1; 633 | WriteToPS2keyboard = 1; 634 | 635 | waitingForAck = 1; 636 | 637 | _parity = 0; 638 | bitcount = 0; 639 | 640 | 641 | // set pins to outputs and high 642 | 643 | // set KEYBOARD_DATA_PIN (PB11) high 644 | GPIOB->regs->ODR = (GPIOB->regs->ODR & 0b1111011111111111) | 0b0000100000000000; 645 | 646 | // set KEYBOARD_DATA_PIN (PB11) output open drain 647 | GPIOB->regs->CRH = (GPIOB->regs->CRH & 0xFFFF0FFF) | 0x00005000; // CNF = 01 MODE = 01 648 | 649 | 650 | // set KEYBOARD_CLOCK_PIN (PB10) high 651 | GPIOB->regs->ODR = (GPIOB->regs->ODR & 0b1111101111111111) | 0b0000010000000000; 652 | 653 | // set KEYBOARD_CLOCK_PIN (PB10) output open drain 654 | GPIOB->regs->CRH = (GPIOB->regs->CRH & 0xFFFFF0FF) | 0x00000500; // CNF = 01 MODE = 01 655 | 656 | 657 | delayMicroseconds( 10 ); 658 | 659 | 660 | // set KEYBOARD_CLOCK_PIN (PB10) low 661 | GPIOB->regs->ODR = (GPIOB->regs->ODR & 0b1111101111111111) | 0b0000000000000000; 662 | 663 | // set clock low for 60us 664 | delayMicroseconds( 60 ); 665 | 666 | 667 | // set KEYBOARD_DATA_PIN (PB11) low 668 | GPIOB->regs->ODR = (GPIOB->regs->ODR & 0b1111011111111111) | 0b0000000000000000; 669 | 670 | // set KEYBOARD_CLOCK_PIN (PB10) to input floating 671 | GPIOB->regs->CRH = (GPIOB->regs->CRH & 0xFFFFF0FF) | 0x00000400; // CNF = 01 MODE = 00 672 | } 673 | 674 | 675 | void ps2interrupt() 676 | { 677 | if( ( GPIOB->regs->IDR & 0b0000010000000000 ) != 0 ) 678 | return; 679 | 680 | if( WriteToPS2keyboard ) 681 | send_bit(); 682 | else 683 | { 684 | uint32_t now_ms; 685 | uint8_t val; 686 | 687 | // make sure we read in the middle of the low clock 688 | delayMicroseconds(15); 689 | 690 | // Read value of KEYBOARD_DATA_PIN 691 | val = ( GPIOB->regs->IDR & 0b0000100000000000 ) >> 11; 692 | 693 | now_ms = millis(); 694 | 695 | if( now_ms - prev_ms > 250 ) 696 | { 697 | bitcount = 0; 698 | incoming = 0; 699 | PS2busy = 0; 700 | hasParityError = 0; 701 | } 702 | 703 | prev_ms = now_ms; 704 | bitcount++; 705 | 706 | switch( bitcount ) 707 | { 708 | case 1: // Start bit 709 | _parity = 0; 710 | PS2busy = 1; 711 | break; 712 | case 2: 713 | case 3: 714 | case 4: 715 | case 5: 716 | case 6: 717 | case 7: 718 | case 8: 719 | case 9: // Data bits 720 | _parity += val; // another one received ? 721 | incoming |= (val << (bitcount - 2) ); 722 | break; 723 | 724 | case 10: // Parity check 725 | 726 | 727 | _parity &= 1; // Get LSB if 1 = odd number of 1's so parity bit should be 0 728 | 729 | if( _parity == val ) // Both same parity error 730 | { 731 | hasParityError = 1; 732 | // Serial.println("P.E ===="); 733 | } 734 | 735 | break; 736 | 737 | case 11: // Stop bit lots of spare time now 738 | 739 | processByteFromKeyboard(); 740 | 741 | bitcount = 0; 742 | incoming = 0; 743 | 744 | PS2busy = 0; 745 | hasParityError = 0; 746 | 747 | default: 748 | bitcount = 0; 749 | PS2busy = 0; 750 | hasParityError = 0; 751 | } 752 | } 753 | } 754 | 755 | 756 | void send_bit() 757 | { 758 | uint8_t val; 759 | 760 | bitcount++; // Now point to next bit 761 | 762 | switch( bitcount ) 763 | { 764 | case 1: 765 | // set KEYBOARD_DATA_PIN low (it should be low already) 766 | GPIOB->regs->ODR = (GPIOB->regs->ODR & 0b1111011111111111); 767 | break; 768 | case 2: 769 | case 3: 770 | case 4: 771 | case 5: 772 | case 6: 773 | case 7: 774 | case 8: 775 | case 9: 776 | // Data bits 777 | val = outgoing & 0x01; // get LSB 778 | 779 | // send bit. KEYBOARD_DATA_PIN = PB11 780 | GPIOB->regs->ODR = (GPIOB->regs->ODR & 0b1111011111111111) | (val << 11); 781 | 782 | _parity += val; // another one received ? 783 | outgoing >>= 1; // right _SHIFT one place for next bit 784 | break; 785 | case 10: 786 | // Parity - Send LSB if 1 = odd number of 1's so parity should be 0 787 | GPIOB->regs->ODR = (GPIOB->regs->ODR & 0b1111011111111111) | (( ~_parity & 1 ) << 11); 788 | 789 | break; 790 | case 11: // Stop bit write change to input for high stop bit 791 | // set KEYBOARD_DATA_PIN (PB11) to input floating 792 | GPIOB->regs->CRH = (GPIOB->regs->CRH & 0xFFFF0FFF) | 0x00004000; // CNF = 01 MODE = 00 793 | break; 794 | 795 | case 12: // Acknowledge bit low we cannot do anything if high instead of low 796 | 797 | bitcount = 0; 798 | PS2busy = 0; 799 | outgoing = 0; 800 | WriteToPS2keyboard = 0; 801 | 802 | default: // in case of weird error and end of byte reception re-sync 803 | bitcount = 0; 804 | } 805 | } 806 | 807 | 808 | static inline uint8_t get_scan_code(void) 809 | { 810 | uint8_t c, i; 811 | 812 | i = tail; 813 | if (i == head) return 0; 814 | i++; 815 | if (i >= BUFFER_SIZE) i = 0; 816 | c = buffer[i]; 817 | tail = i; 818 | return c; 819 | } 820 | 821 | 822 | static inline uint8_t get_byte_to_send_to_keyboard(void) 823 | { 824 | uint8_t c, i; 825 | 826 | i = sendTail; 827 | if (i == sendHead) return 0; 828 | i++; 829 | if (i >= BUFFER_SIZE) i = 0; 830 | c = sendBuffer[i]; 831 | sendTail = i; 832 | return c; 833 | } 834 | 835 | 836 | void clearSendBuffer() 837 | { 838 | sendHead = 0; 839 | sendTail = 0; 840 | } 841 | 842 | 843 | void sendEnableScanCommand() 844 | { 845 | addByteToSendBuffer(0xF4); 846 | } 847 | 848 | 849 | void processByteFromKeyboard() 850 | { 851 | // Serial.println(incoming, HEX); 852 | 853 | if( incoming == 0xFF || incoming == 0x00 ) 854 | { 855 | // Serial.println("SHIET"); 856 | // shit is fucked. Reset everything 857 | requestResendFromKeyboard = 0; 858 | clearSendBuffer(); 859 | sendEnableScanCommand(); 860 | waitingForResetAck = 1; 861 | waitingForAck = 0; 862 | resendTimeout = 0; 863 | } 864 | 865 | else if( hasParityError ) 866 | { 867 | handleParityErrorFromKeyboard(); 868 | } 869 | 870 | else if( incoming == 0xAA ) 871 | { 872 | // 0xAA is received if PS/2 keyboard is disconnected from adapter then re-connected 873 | // set LEDs and Typematic repeat 874 | 875 | requestResendFromKeyboard = 0; 876 | clearSendBuffer(); 877 | sendEnableScanCommand(); 878 | waitingForResetAck = 1; 879 | waitingForAck = 0; 880 | resendTimeout = 0; 881 | } 882 | 883 | else if( incoming == 0xFA || incoming == 0xFE ) 884 | { 885 | waitingForAck = 0; 886 | 887 | if( incoming == 0xFA ) 888 | { 889 | resendTimeout = 0; 890 | } 891 | 892 | else if( incoming == 0xFE ) 893 | { 894 | resendTimeout++; 895 | 896 | if( resendTimeout >= 10) 897 | { 898 | resendTimeout = 0; 899 | 900 | // reset the keyboard as if we received 0xFF 901 | requestResendFromKeyboard = 0; 902 | clearSendBuffer(); 903 | sendEnableScanCommand(); 904 | waitingForResetAck = 1; 905 | waitingForAck = 0; 906 | resendTimeout = 0; 907 | } 908 | else 909 | { 910 | // Back up buffer so we resend last byte 911 | if( sendTail == 0 ) sendTail = (BUFFER_SIZE - 1); 912 | else sendTail--; 913 | } 914 | } 915 | 916 | if( waitingForResetAck ) 917 | { 918 | waitingForResetAck = 0; 919 | sendTypematicAndLEDs(); 920 | } 921 | } 922 | 923 | 924 | else 925 | { 926 | // add byte from keyboard to buffer 927 | 928 | uint8_t i = head + 1; 929 | 930 | if (i >= BUFFER_SIZE) i = 0; 931 | 932 | if (i != tail) 933 | { 934 | buffer[i] = incoming; 935 | head = i; 936 | } 937 | } 938 | 939 | } 940 | 941 | 942 | void processByteFromSega(uint8_t incomingValue) 943 | { 944 | // Reset LED and Typematic status 945 | 946 | if( incomingValue == 0xFF ) 947 | { 948 | LEDstatusByte = 0; 949 | LEDstatusIncoming = 0; 950 | 951 | TypematicStatusByte = 0x0000002B; 952 | TypematicStatusIncoming = 0; 953 | 954 | clearSendBuffer(); 955 | 956 | addByteToSendBuffer(0xED); 957 | addByteToSendBuffer(LEDstatusByte); 958 | 959 | addByteToSendBuffer(0xF3); 960 | addByteToSendBuffer(TypematicStatusByte); 961 | } 962 | 963 | 964 | // Keyboard LEDs 965 | 966 | else if( incomingValue == 0xED ) 967 | { 968 | LEDstatusIncoming = 1; 969 | } 970 | else if( LEDstatusIncoming ) 971 | { 972 | LEDstatusByte = incomingValue; 973 | LEDstatusIncoming = 0; 974 | 975 | // Hit LED register 976 | addByteToSendBuffer(0xED); 977 | 978 | // Set LEDs 979 | addByteToSendBuffer(LEDstatusByte); 980 | } 981 | 982 | 983 | // Typematic repeat 984 | 985 | else if( incomingValue == 0xF3 ) 986 | { 987 | TypematicStatusIncoming = 1; 988 | } 989 | else if( TypematicStatusIncoming ) 990 | { 991 | TypematicStatusByte = incomingValue; 992 | TypematicStatusIncoming = 0; 993 | 994 | // Hit Typematic register 995 | addByteToSendBuffer(0xF3); 996 | 997 | // Set Typematic stuff 998 | addByteToSendBuffer(TypematicStatusByte); 999 | } 1000 | 1001 | 1002 | // otherwise it's not a valid byte 1003 | // so just throw it away 1004 | } 1005 | 1006 | 1007 | void sendTypematicAndLEDs() 1008 | { 1009 | // Hit LED register 1010 | addByteToSendBuffer(0xED); 1011 | 1012 | // Set LEDs 1013 | addByteToSendBuffer(LEDstatusByte); 1014 | 1015 | 1016 | // Hit Typematic register 1017 | addByteToSendBuffer(0xF3); 1018 | 1019 | // Set Typematic stuff 1020 | addByteToSendBuffer(TypematicStatusByte); 1021 | } 1022 | 1023 | 1024 | void handleParityErrorFromKeyboard() 1025 | { 1026 | // Serial.println("P error"); 1027 | 1028 | waitingForAck = 0; 1029 | requestResendFromKeyboard = 1; 1030 | 1031 | 1032 | // add 0xFE (request resend) to beginning of send buffer 1033 | 1034 | sendBuffer[sendTail] = 0xFE; 1035 | 1036 | if( sendTail == 0) 1037 | sendTail = BUFFER_SIZE - 1; 1038 | else 1039 | sendTail--; 1040 | 1041 | 1042 | // Send resend request immediately 1043 | 1044 | // set KEYBOARD_CLOCK_PIN (PB10) output open drain 1045 | GPIOB->regs->CRH = (GPIOB->regs->CRH & 0xFFFFF0FF) | 0x00000500; // CNF = 01 MODE = 01 1046 | 1047 | // set KEYBOARD_CLOCK_PIN (PB10) low 1048 | GPIOB->regs->ODR = (GPIOB->regs->ODR & 0b1111101111111111) | 0b0000000000000000; 1049 | } 1050 | 1051 | 1052 | void addByteToSendBuffer(volatile uint8_t value) 1053 | { 1054 | uint8_t index = sendHead + 1; 1055 | 1056 | if (index >= BUFFER_SIZE) index = 0; 1057 | 1058 | if (index != sendTail) 1059 | { 1060 | sendBuffer[index] = value; 1061 | sendHead = index; 1062 | } 1063 | } 1064 | --------------------------------------------------------------------------------