2021-08-28 16:57:00 +00:00
|
|
|
|
/* SmartyKit 1 - PS/2 keyboard driver v.1.1
|
|
|
|
|
* (with automatic mode if no PS/2 keyboard is connected)
|
2021-04-29 05:34:03 +00:00
|
|
|
|
* http://www.smartykit.io/
|
|
|
|
|
* Copyright (C) 2020, Sergey Panarin <sergey@smartykit.io>
|
|
|
|
|
*
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
(at your option) any later version.
|
|
|
|
|
This program 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 General Public License for more details.
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2022-01-22 17:29:34 +00:00
|
|
|
|
#include <PS2KeyAdvanced.h>
|
2021-04-29 05:34:03 +00:00
|
|
|
|
|
|
|
|
|
// Keyboard Driver pins connections:
|
|
|
|
|
// D0 & D1 – reserved for Serial connection
|
|
|
|
|
// D2 –> PS/2 Socket Data pin
|
|
|
|
|
// D3 -> 4-16 ports decoder Kbd (2) out pin (signal is active when CPU reads Keyboatrd port)
|
|
|
|
|
// D4 -> PS/2 Socket Clk pin
|
|
|
|
|
// D5 -> Keyboard BIT7 pin output
|
|
|
|
|
// D6-D13 -> Keyboard data bus output (8 pins) to connect to Keyboard port (char code is transfered here)
|
|
|
|
|
// A4 -> Low-rate clock output for CPU CLK input
|
|
|
|
|
// A5 -> Low-rate clock output for indicating LED connection
|
|
|
|
|
|
2022-01-22 17:29:34 +00:00
|
|
|
|
#define PS2KEYBOARDCLOCK_PIN 2
|
2022-01-22 17:35:20 +00:00
|
|
|
|
#define KEYBOARD_RD_PIN 3
|
2022-01-22 17:29:34 +00:00
|
|
|
|
#define PS2KEYBOARD_DATA_PIN 4
|
|
|
|
|
#define KEYBOARD_BIT7_PIN 5
|
2021-04-29 05:34:03 +00:00
|
|
|
|
|
2022-01-22 17:29:34 +00:00
|
|
|
|
const int KeyboardPort[8] = {6, 7, 8, 9, 10, 11, 12, 13};
|
2021-04-29 05:34:03 +00:00
|
|
|
|
static boolean BIT7flag = false;
|
2022-01-22 17:29:34 +00:00
|
|
|
|
static boolean autoMode = false; //run pre-set command or wait for user input from PS/2 keyboard
|
2021-08-28 16:57:00 +00:00
|
|
|
|
static boolean autoCommandSent = false; // run pre-set command only once after reboot of Keyboard controller
|
2021-04-29 05:34:03 +00:00
|
|
|
|
|
|
|
|
|
//low-rate clock for tests on A4-A5
|
2022-01-22 17:29:34 +00:00
|
|
|
|
#define LORATE_CLOCK_PIN A4
|
|
|
|
|
#define LORATE_CLOCK_LED_PIN A5
|
|
|
|
|
|
|
|
|
|
//10 milliseconds = 100 times per second (100 Hz)
|
2022-01-23 12:07:27 +00:00
|
|
|
|
#define LORATE_CLOCK_PERIOD_MILLIS 100
|
2021-04-29 05:34:03 +00:00
|
|
|
|
|
2022-01-22 17:29:34 +00:00
|
|
|
|
PS2KeyAdvanced keyboard;
|
2021-04-29 05:34:03 +00:00
|
|
|
|
|
|
|
|
|
void setup()
|
|
|
|
|
{
|
2022-01-22 17:29:34 +00:00
|
|
|
|
pinMode(LORATE_CLOCK_PIN, OUTPUT);
|
|
|
|
|
pinMode(LORATE_CLOCK_LED_PIN, OUTPUT);
|
2021-04-29 05:34:03 +00:00
|
|
|
|
|
2022-01-22 17:35:20 +00:00
|
|
|
|
pinMode(KEYBOARD_RD_PIN, INPUT_PULLUP);
|
2021-04-29 05:34:03 +00:00
|
|
|
|
|
2022-01-22 17:29:34 +00:00
|
|
|
|
for (int bit = 0; bit < 8; bit++) {
|
|
|
|
|
pinMode(KeyboardPort[bit], OUTPUT);
|
2022-01-22 17:35:20 +00:00
|
|
|
|
}
|
2022-01-22 17:29:34 +00:00
|
|
|
|
pinMode(KEYBOARD_BIT7_PIN, OUTPUT);
|
2022-01-23 12:07:27 +00:00
|
|
|
|
ClearKeyboardBIT7();
|
|
|
|
|
|
|
|
|
|
Serial.begin(9600);
|
|
|
|
|
Serial.println("SmartyKit 1 PS2 Keyboard is ready...");
|
|
|
|
|
|
2021-08-28 16:57:00 +00:00
|
|
|
|
if (autoMode)
|
|
|
|
|
delay(7500); //for start without Keyboard connected
|
|
|
|
|
else
|
|
|
|
|
delay(500);
|
2021-04-29 05:34:03 +00:00
|
|
|
|
|
2022-01-22 17:29:34 +00:00
|
|
|
|
keyboard.begin(PS2KEYBOARD_DATA_PIN, PS2KEYBOARDCLOCK_PIN);
|
|
|
|
|
keyboard.echo();
|
2022-01-23 12:07:27 +00:00
|
|
|
|
delay( 6 );
|
|
|
|
|
uint16_t scan_code = keyboard.read( );
|
2021-04-29 05:34:03 +00:00
|
|
|
|
|
2022-01-23 12:07:27 +00:00
|
|
|
|
if( (scan_code & 0xff) == PS2_KEY_ECHO || (scan_code & 0xff) == PS2_KEY_BAT || (scan_code & 0xff) == 0xe8 )
|
|
|
|
|
Serial.println( "Keyboard OK.." ); // Response was Echo or power up
|
|
|
|
|
else
|
|
|
|
|
if ((scan_code & 0xff) == 0)
|
|
|
|
|
{
|
|
|
|
|
Serial.println( "Keyboard Not Found" );
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Serial.print( "Invalid Code received of " );
|
|
|
|
|
Serial.println(scan_code, HEX);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
attachInterrupt(digitalPinToInterrupt(KEYBOARD_RD_PIN), cpuReadsKeyboard, FALLING);
|
2021-04-29 05:34:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void cpuReadsKeyboard(void)
|
|
|
|
|
{
|
2022-01-23 12:07:27 +00:00
|
|
|
|
ClearKeyboardBIT7();
|
2021-04-29 05:34:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-23 12:07:27 +00:00
|
|
|
|
void SetKeyboardBIT7()
|
2021-04-29 05:34:03 +00:00
|
|
|
|
{
|
2022-01-22 17:29:34 +00:00
|
|
|
|
digitalWrite(KEYBOARD_BIT7_PIN, HIGH);
|
2021-04-29 05:34:03 +00:00
|
|
|
|
BIT7flag = true;
|
|
|
|
|
}
|
2022-01-22 17:29:34 +00:00
|
|
|
|
|
2022-01-23 12:07:27 +00:00
|
|
|
|
void ClearKeyboardBIT7()
|
2021-04-29 05:34:03 +00:00
|
|
|
|
{
|
2022-01-22 17:29:34 +00:00
|
|
|
|
digitalWrite(KEYBOARD_BIT7_PIN, LOW);
|
2021-04-29 05:34:03 +00:00
|
|
|
|
BIT7flag = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void sendCharToKeyboardPort(char c)
|
2022-01-23 12:07:27 +00:00
|
|
|
|
{
|
|
|
|
|
Serial.print(c);
|
|
|
|
|
if (c == '\r')
|
|
|
|
|
Serial.println();
|
|
|
|
|
|
2022-01-22 17:29:34 +00:00
|
|
|
|
for (int bit = 0; bit < 8 ; bit++)
|
2021-04-29 05:34:03 +00:00
|
|
|
|
{
|
2022-01-22 17:29:34 +00:00
|
|
|
|
if (c & (1 << (bit)))
|
|
|
|
|
digitalWrite(KeyboardPort[bit], HIGH);
|
2021-04-29 05:34:03 +00:00
|
|
|
|
else
|
2022-01-22 17:29:34 +00:00
|
|
|
|
digitalWrite(KeyboardPort[bit], LOW);
|
2021-04-29 05:34:03 +00:00
|
|
|
|
}
|
2022-01-22 17:29:34 +00:00
|
|
|
|
digitalWrite(KeyboardPort[7], HIGH);
|
2022-01-23 12:07:27 +00:00
|
|
|
|
SetKeyboardBIT7();
|
2021-04-29 05:34:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-23 12:07:27 +00:00
|
|
|
|
void loop()
|
|
|
|
|
{
|
2021-08-28 16:57:00 +00:00
|
|
|
|
//run pre-set command in auto-mode when PS/2 keyboard is not connected
|
|
|
|
|
//only once after reboot of Keyboard controller
|
|
|
|
|
if (autoMode && !autoCommandSent)
|
|
|
|
|
{
|
|
|
|
|
autoCommandSent = true;
|
|
|
|
|
runAutoModeCommand();
|
|
|
|
|
}
|
2021-04-29 05:34:03 +00:00
|
|
|
|
|
|
|
|
|
//check PS2 input
|
2022-01-23 12:07:27 +00:00
|
|
|
|
if (!BIT7flag && keyboard.available())
|
|
|
|
|
{
|
2022-01-22 17:29:34 +00:00
|
|
|
|
uint16_t scan_code = keyboard.read();
|
2022-01-23 12:07:27 +00:00
|
|
|
|
|
|
|
|
|
if (!( scan_code & PS2_BREAK ))
|
|
|
|
|
{
|
|
|
|
|
char c = scan_code & 0xff;
|
|
|
|
|
|
|
|
|
|
if (c == PS2_KEY_TAB)
|
|
|
|
|
runCommand();
|
|
|
|
|
|
|
|
|
|
//process Backspace, Left Arrow, Delete as Apple I backspace '_'
|
|
|
|
|
if (c == PS2_KEY_BACK) {
|
|
|
|
|
c = '_';
|
|
|
|
|
} else if (c == PS2_KEY_L_ARROW) {
|
|
|
|
|
c = '_';
|
|
|
|
|
} else if (c == PS2_KEY_DELETE) {
|
|
|
|
|
c = '_';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//make all symbols uppercase (from 'a' (ASCII code 0x61) to 'z' (ASCII code 0x7A))
|
|
|
|
|
//as in original Apple-1
|
|
|
|
|
c = toupper(c);
|
|
|
|
|
//print c to Keyboard Port to be read by CPU
|
|
|
|
|
sendCharToKeyboardPort(c);
|
2021-04-29 05:34:03 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//low-rate clock
|
2022-01-22 17:29:34 +00:00
|
|
|
|
digitalWrite(LORATE_CLOCK_PIN, HIGH);
|
|
|
|
|
digitalWrite(LORATE_CLOCK_LED_PIN, HIGH);
|
|
|
|
|
delay(LORATE_CLOCK_PERIOD_MILLIS); // wait for a LORATE_CLOCK_PERIOD_MILLIS
|
|
|
|
|
digitalWrite(LORATE_CLOCK_PIN, LOW);
|
|
|
|
|
digitalWrite(LORATE_CLOCK_LED_PIN, LOW);
|
|
|
|
|
delay(LORATE_CLOCK_PERIOD_MILLIS); // wait for a LORATE_CLOCK_PERIOD_MILLIS
|
2021-04-29 05:34:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-28 16:57:00 +00:00
|
|
|
|
//running pre-set Woz OS command for auto mode
|
|
|
|
|
void runAutoModeCommand()
|
|
|
|
|
{
|
2022-01-23 12:07:27 +00:00
|
|
|
|
const int cmd_count = 1;
|
2021-08-28 16:57:00 +00:00
|
|
|
|
String cmd1 = String("F000R\r"); // Woz face demo program at $F000
|
|
|
|
|
String commands[] = {cmd1};
|
2022-01-23 12:07:27 +00:00
|
|
|
|
for (int i = 0; i < cmd_count; i++)
|
2021-08-28 16:57:00 +00:00
|
|
|
|
for (int j = 0; j < commands[i].length(); j++)
|
|
|
|
|
{
|
|
|
|
|
char c = commands[i].charAt(j);
|
|
|
|
|
sendCharToKeyboardPort(c);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//running pre-set Woz OS command (drawing 8x8 pixel art)
|
|
|
|
|
void runCommand()
|
2021-04-29 05:34:03 +00:00
|
|
|
|
{
|
2022-01-23 12:07:27 +00:00
|
|
|
|
const int cmd_count = 4;
|
2021-04-29 05:34:03 +00:00
|
|
|
|
String cmd1 = String("1111: 88 A8 50\r");
|
|
|
|
|
String cmd2 = String(":07 61 92\r");
|
|
|
|
|
String cmd3 = String(":94 67\r");
|
|
|
|
|
String cmd4 = String("FC00R\r");
|
|
|
|
|
String commands[] = {cmd1, cmd2, cmd3, cmd4};
|
2022-01-23 12:07:27 +00:00
|
|
|
|
for (int i = 0; i < cmd_count; i++)
|
2021-04-29 05:34:03 +00:00
|
|
|
|
for (int j = 0; j < commands[i].length(); j++)
|
|
|
|
|
{
|
|
|
|
|
char c = commands[i].charAt(j);
|
|
|
|
|
sendCharToKeyboardPort(c);
|
|
|
|
|
}
|
|
|
|
|
}
|