]> git.donarmstrong.com Git - kiibohd-controller.git/blob - Scan/MBC-55X/scan_loop.c
d476bce983f8afb8fe9c05ed1661b8c478a5d71b
[kiibohd-controller.git] / Scan / MBC-55X / scan_loop.c
1 /* Copyright (C) 2013 by Jacob Alexander
2  * 
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:
9  * 
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  * 
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
19  * THE SOFTWARE.
20  */
21
22 // ----- Includes -----
23
24 // Compiler Includes
25 #include <Lib/ScanLib.h>
26
27 // Project Includes
28 #include <led.h>
29 #include <print.h>
30
31 // Local Includes
32 #include "scan_loop.h"
33
34
35
36 // ----- Defines -----
37
38
39 // ----- Macros -----
40
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
45
46
47
48 // ----- Variables -----
49
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;
53
54
55
56 // ----- Function Declarations -----
57
58 void processKeyValue( uint8_t valueType );
59 void  removeKeyValue( uint8_t keyValue );
60
61
62
63 // ----- Interrupt Functions -----
64
65 // UART Receive Buffer Full Interrupt
66 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
67 ISR(USART1_RX_vect)
68 #elif defined(_mk20dx128_) // ARM
69 void uart0_status_isr(void)
70 #endif
71 {
72         cli(); // Disable Interrupts
73
74         // Read part of the scan code (3 8bit chunks) from USART
75 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
76         uint8_t tmp = UDR1;
77 #elif defined(_mk20dx128_) // ARM
78         // Exit out if nothing to do
79         /*
80         if ( !(UART0_S1 & UART_S1_RDRF ) )
81         {
82                 sei();
83                 return;
84         }
85         */
86
87         // Only doing single byte FIFO here
88         uint8_t tmp = UART0_D;
89         print("YAYA");
90 #endif
91
92         // Debug
93         char tmpStr[6];
94         hexToStr( tmp, tmpStr );
95         dPrintStrsNL( tmpStr, " " ); // Debug
96
97         // TODO
98
99         sei(); // Re-enable Interrupts
100 }
101
102
103
104 // ----- Functions -----
105
106 // Setup
107 inline void scan_setup()
108 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
109 {
110         // Setup the the USART interface for keyboard data input
111
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;
119
120         // Enable the receiver, transmitter, and RX Complete Interrupt
121         // TODO - Only receiver, and rx interrupt
122         UCSR1B = 0x98;
123
124         // Set frame format: 8 data, 1 stop bit, odd parity
125         // Asynchrounous USART mode
126         // TODO - Even parity
127         UCSR1C = 0x36;
128
129         // Reset the keyboard before scanning, we might be in a wierd state
130         scan_resetKeyboard();
131 }
132 #elif defined(_mk20dx128_) // ARM
133 {
134         // Setup the the UART interface for keyboard data input
135         SIM_SCGC4 |= SIM_SCGC4_UART0; // Disable clock gating
136
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
140
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;
149
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;
152
153         // Number of bytes in FIFO before TX Interrupt
154         UART0_TWFIFO = 1;
155
156         // Number of bytes in FIFO before RX Interrupt
157         UART0_RWFIFO = 1;
158
159         // TX FIFO Disabled, TX FIFO Size 1 (Max 8 datawords), RX FIFO Enabled, RX FIFO Size 1 (Max 8 datawords)
160         // TX/RX FIFO Size:
161         //  0x0 - 1 dataword
162         //  0x1 - 4 dataword
163         //  0x2 - 8 dataword
164         UART0_PFIFO = ~UART_PFIFO_TXFE | /*TXFIFOSIZE*/ (0x0 << 4) | ~UART_PFIFO_RXFE | /*RXFIFOSIZE*/ (0x0);
165
166         // TX Disabled, RX Enabled, RX Interrupt Enabled
167         UART0_C2 = UART_C2_TE | UART_C2_RE | UART_C2_RIE;
168
169         // Reciever Inversion Disabled
170         UART0_S2 = ~UART_S2_RXINV;
171
172         // Transmit Inversion Disabled
173         UART0_C3 = ~UART_S2_TXINV;
174
175         // Add interrupt to the vector table
176         NVIC_ENABLE_IRQ( IRQ_UART0_STATUS );
177
178         // Reset the keyboard before scanning, we might be in a wierd state
179         scan_resetKeyboard();
180 }
181 #endif
182
183
184 // Main Detection Loop
185 inline uint8_t scan_loop()
186 {
187         UART0_D = 0x56;
188         _delay_ms( 100 );
189         return 0;
190 }
191
192 void processKeyValue( uint8_t keyValue )
193 {
194         // TODO Process ASCII
195
196         // Make sure the key isn't already in the buffer
197         for ( uint8_t c = 0; c < KeyIndex_BufferUsed + 1; c++ )
198         {
199                 // Key isn't in the buffer yet
200                 if ( c == KeyIndex_BufferUsed )
201                 {
202                         bufferAdd( keyValue );
203                         break;
204                 }
205
206                 // Key already in the buffer
207                 if ( KeyIndex_Buffer[c] == keyValue )
208                         break;
209         }
210 }
211
212 void removeKeyValue( uint8_t keyValue )
213 {
214         // Check for the released key, and shift the other keys lower on the buffer
215         uint8_t c;
216         for ( c = 0; c < KeyIndex_BufferUsed; c++ )
217         {
218                 // Key to release found
219                 if ( KeyIndex_Buffer[c] == keyValue )
220                 {
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];
224
225                         // Decrement Buffer
226                         KeyIndex_BufferUsed--;
227
228                         break;
229                 }
230         }
231
232         // Error case (no key to release)
233         if ( c == KeyIndex_BufferUsed + 1 )
234         {
235                 errorLED( 1 );
236                 char tmpStr[6];
237                 hexToStr( keyValue, tmpStr );
238                 erro_dPrint( "Could not find key to release: ", tmpStr );
239         }
240 }
241
242 // Send data
243 // NOTE: Example only, MBC-55X cannot receive user data
244 uint8_t scan_sendData( uint8_t dataPayload )
245 {
246         // Debug
247         char tmpStr[6];
248         hexToStr( dataPayload, tmpStr );
249         info_dPrint( "Sending - ", tmpStr );
250
251 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
252         UDR1 = dataPayload;
253 #elif defined(_mk20dx128_) // ARM
254         UART0_D = dataPayload;
255 #endif
256
257         return 0;
258 }
259
260 // Signal KeyIndex_Buffer that it has been properly read
261 void scan_finishedWithBuffer( uint8_t sentKeys )
262 {
263 }
264
265 // Signal that the keys have been properly sent over USB
266 void scan_finishedWithUSBBuffer( uint8_t sentKeys )
267 {
268 }
269
270 // Reset/Hold keyboard
271 // NOTE: Does nothing with the FACOM6684
272 void scan_lockKeyboard( void )
273 {
274 }
275
276 // NOTE: Does nothing with the FACOM6684
277 void scan_unlockKeyboard( void )
278 {
279 }
280
281 // Reset Keyboard
282 void scan_resetKeyboard( void )
283 {
284         // Not a calculated valued...
285         _delay_ms( 50 );
286
287         KeyIndex_BufferUsed = 0;
288 }
289