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>
32 #include "scan_loop.h"
36 // ----- Defines -----
40 #define CLK_PORT PORTD
44 #define DATA_READ PIND
45 #define DATA_PORT PORTD
49 #define INTR_PORT PORTD
56 #define READ_CLK CLK_READ & (1 << CLK_PIN) ? 1 : 0
57 #define READ_DATA DATA_READ & (1 << DATA_PIN) ? 0 : 1
59 #define UNSET_INTR() INTR_DDR &= ~(1 << INTR_PIN)
60 #define SET_INTR() INTR_DDR |= (1 << INTR_PIN)
64 // ----- Variables -----
66 uint8_t KeyIndex_Array[KEYBOARD_SIZE + 1];
68 // Scan Code Retrieval Variables
69 uint8_t inputData = 0xFF;
70 uint8_t packet_index = 0;
74 // ----- Functions -----
77 inline void scan_setup()
80 CLK_DDR &= ~(1 << CLK_PIN);
81 DATA_DDR &= ~(1 << DATA_PIN);
84 CLK_PORT &= ~(1 << CLK_PIN); // (CLK)
85 DATA_PORT &= ~(1 << DATA_PIN); // (/DATA)
87 // Setup Keyboard Interrupt
88 INTR_DDR &= ~(1 << INTR_PIN);
89 INTR_PORT &= ~(1 << INTR_PIN);
91 /* Interrupt Style (Not working fully)
93 // Setup interrupt on the CLK pin TODO Better defines
94 EICRA |= 0x03; // Rising Edge Interrupt
97 // Setup interrupt on the DATA pin TODO Better defines
98 EICRA |= 0x08; // Falling Edge Interrupt
105 // Main Detection Loop
106 inline uint8_t scan_loop()
110 if ( packet_index == 8 )
112 // Disable Error LED, proper key found
116 // Crazy Debug (Read the Scan Code)
118 hexToStr_op( inputData, tmpStr, 2 );
119 dPrintStrsNL( "Read Data: 0x", tmpStr );
121 // - Map the scan code to the index array -
122 // If the 8th bit is high, remove the keypress, else, add the keypress
123 // The lower 7 bits are the array index
124 KeyIndex_Array[(inputData & 0x7F)] = (inputData & 0x80) ? 0x00 : 0x80;
131 else if ( packet_index > 8 )
137 int8ToStr( packet_index, tmpStr );
138 erro_dPrint( "Big packet? Mismatched... ", tmpStr );
145 // Disable keyboard interrupt (does nothing if already off)
148 // Read the clock 8 times
151 // Mis-read packet, set back to 0
152 if ( packet_index == -1 )
155 // Append 1 bit of data
156 inputData &= ~(READ_DATA << packet_index);
159 // 8 Bits have been read
160 if ( packet_index == 8 )
162 // Wait till clock edge falls
165 // Sample both lines to make sure this is not a data value
166 // and definitely the end of packet data blip
167 uint16_t badDataCounter = 0;
168 while ( !( READ_DATA ) && !( READ_CLK ) )
171 if ( badDataCounter < 25 )
174 // Crazy Debug (Read the Scan Code)
176 hexToStr_op( inputData, tmpStr, 2 );
177 dbug_dPrint( "Read Data: 0x", tmpStr );
179 // - Map the scan code to the index array -
180 // If the 8th bit is high, remove the keypress, else, add the keypress
181 // The lower 7 bits are the array index
182 KeyIndex_Array[(inputData & 0x7F)] = (inputData & 0x80) ? 0x00 : 0x80;
184 // Even though this is a mis-read packet, we still know what the value is
190 hexToStr_op( inputData, tmpStr, 2 );
191 erro_dPrint( "Bad packet? Mismatched... 0x", tmpStr );
198 // Interrupt the keyboard, so we don't get packet pieces...
201 // Do not wait for next clock, let USB do it's thing (if desired)
205 // Wait till clock edge falls
209 // Interrupt keyboard if there is no pending packet
215 // Detection interrupt, signalled by a clock pulse from CLK_PIN
218 //cli(); // Disable Interrupts
220 // Append 1 bit of data
221 //inputData &= ~(READ_DATA << packet_index);
224 //sei(); // Re-enable Interrupts
230 // Append 1 bit of data
231 inputData &= ~(1 << packet_index);
234 // Disable Clk Signal (Not needed if there's a data signal)
235 EIFR |= (1 << INTF0);