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 /* XXX OLD CODE - Somewhat worked, has glitches, and is not compatible with the current API
157 // Read the clock 8 times
160 // Mis-read packet, set back to 0
161 if ( packet_index == -1 )
164 // Append 1 bit of data
165 inputData &= ~(READ_DATA << packet_index);
168 // 8 Bits have been read
169 if ( packet_index == 8 )
171 // Wait till clock edge falls
174 // Sample both lines to make sure this is not a data value
175 // and definitely the end of packet data blip
176 uint16_t badDataCounter = 0;
177 while ( !( READ_DATA ) && !( READ_CLK ) )
180 if ( badDataCounter < 25 )
183 // Crazy Debug (Read the Scan Code)
185 hexToStr_op( inputData, tmpStr, 2 );
186 dbug_dPrint( "Read Data: 0x", tmpStr );
188 // - Map the scan code to the index array -
189 // If the 8th bit is high, remove the keypress, else, add the keypress
190 // The lower 7 bits are the array index
191 KeyIndex_Array[(inputData & 0x7F)] = (inputData & 0x80) ? 0x00 : 0x80;
193 // Even though this is a mis-read packet, we still know what the value is
199 hexToStr_op( inputData, tmpStr, 2 );
200 erro_dPrint( "Bad packet? Mismatched... 0x", tmpStr );
207 // Interrupt the keyboard, so we don't get packet pieces...
210 // Do not wait for next clock, let USB do it's thing (if desired)
214 // Wait till clock edge falls
220 // Interrupt keyboard if there is no pending packet
227 // XXX Not used with the Tandy1000
228 uint8_t scan_sendData( uint8_t dataPayload )
233 // Signal KeyIndex_Buffer that it has been properly read
235 void scan_finishedWithBuffer( void )
239 // Signal that the keys have been properly sent over USB
240 void scan_finishedWithUSBBuffer( void )
244 // Reset/Hold keyboard
245 // Warning! This will cause the keyboard to not send any data, so you can't disable with a keypress
246 // The Tandy 1000 keyboard has a dedicated hold/processor interrupt line
247 void scan_lockKeyboard( void )
252 void scan_unlockKeyboard( void )
258 void scan_resetKeyboard( void )
260 // TODO Tandy1000 has a dedicated reset line