1 /* Copyright (C) 2013 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 <Lib/ScanLib.h>
32 #include "scan_loop.h"
36 // ----- Defines -----
41 // Make sure we haven't overflowed the buffer
42 #define bufferAdd(byte) \
43 if ( KeyIndex_BufferUsed < KEYBOARD_BUFFER ) \
44 KeyIndex_Buffer[KeyIndex_BufferUsed++] = byte
48 // ----- Variables -----
50 // Buffer used to inform the macro processing module which keys have been detected as pressed
51 volatile uint8_t KeyIndex_Buffer[KEYBOARD_BUFFER];
52 volatile uint8_t KeyIndex_BufferUsed;
56 // ----- Function Declarations -----
58 void processKeyValue( uint8_t valueType );
59 void removeKeyValue( uint8_t keyValue );
63 // ----- Interrupt Functions -----
65 // UART Receive Buffer Full Interrupt
66 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
68 #elif defined(_mk20dx128_) // ARM
69 void uart0_status_isr(void)
72 cli(); // Disable Interrupts
74 // Read part of the scan code (3 8bit chunks) from USART
75 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
77 #elif defined(_mk20dx128_) // ARM
78 // Exit out if nothing to do
80 if ( !(UART0_S1 & UART_S1_RDRF ) )
87 // Only doing single byte FIFO here
88 uint8_t tmp = UART0_D;
94 hexToStr( tmp, tmpStr );
95 dPrintStrsNL( tmpStr, " " ); // Debug
99 sei(); // Re-enable Interrupts
104 // ----- Functions -----
107 inline void scan_setup()
108 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
110 // Setup the the USART interface for keyboard data input
112 // Setup baud rate - 1205 Baud
113 // 16 MHz / ( 16 * Baud ) = UBRR
114 // Baud: 1205 -> 16 MHz / ( 16 * 1205 ) = 829.8755
115 // Thus baud setting = 830
116 uint16_t baud = 830; // Max setting of 4095
117 UBRR1H = (uint8_t)(baud >> 8);
118 UBRR1L = (uint8_t)baud;
120 // Enable the receiver, transmitter, and RX Complete Interrupt
121 // TODO - Only receiver, and rx interrupt
124 // Set frame format: 8 data, 1 stop bit, odd parity
125 // Asynchrounous USART mode
126 // TODO - Even parity
129 // Reset the keyboard before scanning, we might be in a wierd state
130 scan_resetKeyboard();
132 #elif defined(_mk20dx128_) // ARM
134 // Setup the the UART interface for keyboard data input
135 SIM_SCGC4 |= SIM_SCGC4_UART0; // Disable clock gating
137 // Pin Setup for UART0
138 PORTB_PCR16 = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_PFE | PORT_PCR_MUX(3); // RX Pin
139 PORTB_PCR17 = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3); // TX Pin
141 // Setup baud rate - 1205 Baud
142 // 48 MHz / ( 16 * Baud ) = BDH/L
143 // Baud: 1205 -> 48 MHz / ( 16 * 1205 ) = 2489.6266
144 // Thus baud setting = 2490
145 // NOTE: If finer baud adjustment is needed see UARTx_C4 -> BRFA in the datasheet
146 uint16_t baud = 2490; // Max setting of 8191
147 UART0_BDH = (uint8_t)(baud >> 8);
148 UART0_BDL = (uint8_t)baud;
150 // 8 bit, Even Parity, Idle Character bit after stop
151 UART0_C1 = ~UART_C1_M | UART_C1_PE | ~UART_C1_PT | UART_C1_ILT;
153 // Number of bytes in FIFO before TX Interrupt
156 // Number of bytes in FIFO before RX Interrupt
159 // TX FIFO Disabled, TX FIFO Size 1 (Max 8 datawords), RX FIFO Enabled, RX FIFO Size 1 (Max 8 datawords)
164 UART0_PFIFO = ~UART_PFIFO_TXFE | /*TXFIFOSIZE*/ (0x0 << 4) | ~UART_PFIFO_RXFE | /*RXFIFOSIZE*/ (0x0);
166 // TX Disabled, RX Enabled, RX Interrupt Enabled
167 UART0_C2 = UART_C2_TE | UART_C2_RE | UART_C2_RIE;
169 // Reciever Inversion Disabled
170 UART0_S2 = ~UART_S2_RXINV;
172 // Transmit Inversion Disabled
173 UART0_C3 = ~UART_S2_TXINV;
175 // Add interrupt to the vector table
176 NVIC_ENABLE_IRQ( IRQ_UART0_STATUS );
178 // Reset the keyboard before scanning, we might be in a wierd state
179 scan_resetKeyboard();
184 // Main Detection Loop
185 inline uint8_t scan_loop()
192 void processKeyValue( uint8_t keyValue )
194 // TODO Process ASCII
196 // Make sure the key isn't already in the buffer
197 for ( uint8_t c = 0; c < KeyIndex_BufferUsed + 1; c++ )
199 // Key isn't in the buffer yet
200 if ( c == KeyIndex_BufferUsed )
202 bufferAdd( keyValue );
206 // Key already in the buffer
207 if ( KeyIndex_Buffer[c] == keyValue )
212 void removeKeyValue( uint8_t keyValue )
214 // Check for the released key, and shift the other keys lower on the buffer
216 for ( c = 0; c < KeyIndex_BufferUsed; c++ )
218 // Key to release found
219 if ( KeyIndex_Buffer[c] == keyValue )
221 // Shift keys from c position
222 for ( uint8_t k = c; k < KeyIndex_BufferUsed - 1; k++ )
223 KeyIndex_Buffer[k] = KeyIndex_Buffer[k + 1];
226 KeyIndex_BufferUsed--;
232 // Error case (no key to release)
233 if ( c == KeyIndex_BufferUsed + 1 )
237 hexToStr( keyValue, tmpStr );
238 erro_dPrint( "Could not find key to release: ", tmpStr );
243 // NOTE: Example only, MBC-55X cannot receive user data
244 uint8_t scan_sendData( uint8_t dataPayload )
248 hexToStr( dataPayload, tmpStr );
249 info_dPrint( "Sending - ", tmpStr );
251 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
253 #elif defined(_mk20dx128_) // ARM
254 UART0_D = dataPayload;
260 // Signal KeyIndex_Buffer that it has been properly read
261 void scan_finishedWithBuffer( uint8_t sentKeys )
265 // Signal that the keys have been properly sent over USB
266 void scan_finishedWithUSBBuffer( uint8_t sentKeys )
270 // Reset/Hold keyboard
271 // NOTE: Does nothing with the FACOM6684
272 void scan_lockKeyboard( void )
276 // NOTE: Does nothing with the FACOM6684
277 void scan_unlockKeyboard( void )
282 void scan_resetKeyboard( void )
284 // Not a calculated valued...
287 KeyIndex_BufferUsed = 0;