1 /* Copyright (C) 2011 by Jacob Alexander
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
4 * of this software and associated documentation files (the "Software"), to deal
5 * in the Software without restriction, including without limitation the rights
6 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 * copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 // ----- Includes -----
25 #include <avr/interrupt.h>
27 #include <util/delay.h>
34 #include "scan_loop.h"
38 // ----- Defines -----
42 #define CLK_PORT PORTD
46 #define DATA_READ PIND
47 #define DATA_PORT PORTD
51 #define INTR_PORT PORTD
59 #define READ_CLK CLK_READ & (1 << CLK_PIN) ? 1 : 0
60 #define READ_DATA DATA_READ & (1 << DATA_PIN) ? 0 : 1
62 #define UNSET_INTR() INTR_DDR &= ~(1 << INTR_PIN)
63 #define SET_INTR() INTR_DDR |= (1 << INTR_PIN)
65 #define bufferAdd(byte) \
66 if ( KeyIndex_BufferUsed < KEYBOARD_BUFFER ) \
67 KeyIndex_Buffer[KeyIndex_BufferUsed++] = byte
70 // ----- Variables -----
72 // Buffer used to inform the macro processing module which keys have been detected as pressed
73 volatile uint8_t KeyIndex_Buffer[KEYBOARD_BUFFER];
74 volatile uint8_t KeyIndex_BufferUsed;
77 // Scan Code Retrieval Variables
78 uint8_t inputData = 0xFF;
79 uint8_t packet_index = 0;
83 // ----- Functions -----
86 inline void scan_setup()
88 // Initially reset the keyboard (just in case we are in a wierd state)
91 // Setup SPI for data input using the clock and data inputs
95 CLK_DDR &= ~(1 << CLK_PIN);
96 DATA_DDR &= ~(1 << DATA_PIN);
99 CLK_PORT &= ~(1 << CLK_PIN); // (CLK)
100 DATA_PORT &= ~(1 << DATA_PIN); // (/DATA)
103 // Setup Keyboard Interrupt
104 INTR_DDR &= ~(1 << INTR_PIN);
105 INTR_PORT &= ~(1 << INTR_PIN);
107 // Setup Keyboard Reset Line
112 // Main Detection Loop
113 inline uint8_t scan_loop()
117 if ( packet_index == 8 )
119 // Disable Error LED, proper key found
123 // Crazy Debug (Read the Scan Code)
125 hexToStr_op( inputData, tmpStr, 2 );
126 dPrintStrsNL( "Read Data: 0x", tmpStr );
128 // - Map the scan code to the index array -
129 // If the 8th bit is high, remove the keypress, else, add the keypress
130 // The lower 7 bits are the array index
131 KeyIndex_Array[(inputData & 0x7F)] = (inputData & 0x80) ? 0x00 : 0x80;
138 else if ( packet_index > 8 )
144 int8ToStr( packet_index, tmpStr );
145 erro_dPrint( "Big packet? Mismatched... ", tmpStr );
152 // Disable keyboard interrupt (does nothing if already off)
155 // Read the clock 8 times
158 // Mis-read packet, set back to 0
159 if ( packet_index == -1 )
162 // Append 1 bit of data
163 inputData &= ~(READ_DATA << packet_index);
166 // 8 Bits have been read
167 if ( packet_index == 8 )
169 // Wait till clock edge falls
172 // Sample both lines to make sure this is not a data value
173 // and definitely the end of packet data blip
174 uint16_t badDataCounter = 0;
175 while ( !( READ_DATA ) && !( READ_CLK ) )
178 if ( badDataCounter < 25 )
181 // Crazy Debug (Read the Scan Code)
183 hexToStr_op( inputData, tmpStr, 2 );
184 dbug_dPrint( "Read Data: 0x", tmpStr );
186 // - Map the scan code to the index array -
187 // If the 8th bit is high, remove the keypress, else, add the keypress
188 // The lower 7 bits are the array index
189 KeyIndex_Array[(inputData & 0x7F)] = (inputData & 0x80) ? 0x00 : 0x80;
191 // Even though this is a mis-read packet, we still know what the value is
197 hexToStr_op( inputData, tmpStr, 2 );
198 erro_dPrint( "Bad packet? Mismatched... 0x", tmpStr );
205 // Interrupt the keyboard, so we don't get packet pieces...
208 // Do not wait for next clock, let USB do it's thing (if desired)
212 // Wait till clock edge falls
216 // Interrupt keyboard if there is no pending packet
223 // XXX Not used with the Tandy1000
224 uint8_t scan_sendData( uint8_t dataPayload )
229 // Signal KeyIndex_Buffer that it has been properly read
231 void scan_finishedWithBuffer( void )
235 // Reset/Hold keyboard
236 // Warning! This will cause the keyboard to not send any data, so you can't disable with a keypress
237 // The Tandy 1000 keyboard has a dedicated hold/processor interrupt line
238 void scan_lockKeyboard( void )
243 void scan_unlockKeyboard( void )
249 void scan_resetKeyboard( void )
251 // TODO Tandy1000 has a dedicated reset line