1 /* Copyright (C) 2014-2016 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>
35 #include "matrix_scan.h"
37 // Matrix Configuration
42 // ----- Defines -----
44 #if ( DebounceThrottleDiv_define > 0 )
45 nat_ptr_t Matrix_divCounter = 0;
50 // ----- Function Declarations -----
53 void cliFunc_matrixDebug( char* args );
54 void cliFunc_matrixState( char* args );
58 // ----- Variables -----
60 // Scan Module command dictionary
61 CLIDict_Entry( matrixDebug, "Enables matrix debug mode, prints out each scan code." NL "\t\tIf argument \033[35mT\033[0m is given, prints out each scan code state transition." );
62 CLIDict_Entry( matrixState, "Prints out the current scan table N times." NL "\t\t \033[1mO\033[0m - Off, \033[1;33mP\033[0m - Press, \033[1;32mH\033[0m - Hold, \033[1;35mR\033[0m - Release, \033[1;31mI\033[0m - Invalid" );
64 CLIDict_Def( matrixCLIDict, "Matrix Module Commands" ) = {
65 CLIDict_Item( matrixDebug ),
66 CLIDict_Item( matrixState ),
67 { 0, 0, 0 } // Null entry for dictionary end
71 KeyState Matrix_scanArray[ Matrix_colsNum * Matrix_rowsNum ];
74 #ifdef GHOSTING_MATRIX
75 KeyGhost Matrix_ghostArray[ Matrix_colsNum * Matrix_rowsNum ];
77 uint8_t col_use[Matrix_colsNum], row_use[Matrix_rowsNum]; // used count
78 uint8_t col_ghost[Matrix_colsNum], row_ghost[Matrix_rowsNum]; // marked as having ghost if 1
82 // Matrix debug flag - If set to 1, for each keypress the scan code is displayed in hex
83 // If set to 2, for each key state change, the scan code is displayed along with the state
84 uint8_t matrixDebugMode = 0;
86 // Matrix State Table Debug Counter - If non-zero display state table after every matrix scan
87 uint16_t matrixDebugStateCounter = 0;
89 // Matrix Scan Counters
90 uint16_t matrixMaxScans = 0;
91 uint16_t matrixCurScans = 0;
92 uint16_t matrixPrevScans = 0;
94 // System Timer used for delaying debounce decisions
95 extern volatile uint32_t systick_millis_count;
99 // ----- Functions -----
101 // Pin action (Strobe, Sense, Strobe Setup, Sense Setup)
102 // NOTE: This function is highly dependent upon the organization of the register map
103 // Only guaranteed to work with Freescale MK20 series uCs
104 uint8_t Matrix_pin( GPIO_Pin gpio, Type type )
106 // Register width is defined as size of a pointer
107 unsigned int gpio_offset = gpio.port * 0x40 / sizeof(unsigned int*);
108 unsigned int port_offset = gpio.port * 0x1000 / sizeof(unsigned int*) + gpio.pin;
110 // Assumes 0x40 between GPIO Port registers and 0x1000 between PORT pin registers
112 volatile unsigned int *GPIO_PDDR = (unsigned int*)(&GPIOA_PDDR) + gpio_offset;
113 volatile unsigned int *GPIO_PSOR = (unsigned int*)(&GPIOA_PSOR) + gpio_offset;
114 volatile unsigned int *GPIO_PCOR = (unsigned int*)(&GPIOA_PCOR) + gpio_offset;
115 volatile unsigned int *GPIO_PDIR = (unsigned int*)(&GPIOA_PDIR) + gpio_offset;
116 volatile unsigned int *PORT_PCR = (unsigned int*)(&PORTA_PCR0) + port_offset;
118 // Operation depends on Type
122 *GPIO_PSOR |= (1 << gpio.pin);
123 #ifdef GHOSTING_MATRIX
124 *GPIO_PDDR |= (1 << gpio.pin); // output
129 *GPIO_PCOR |= (1 << gpio.pin);
130 #ifdef GHOSTING_MATRIX
131 // Ghosting martix needs to put not used (off) strobes in high impedance state
132 *GPIO_PDDR &= ~(1 << gpio.pin); // input, high Z state
136 case Type_StrobeSetup:
138 *GPIO_PDDR |= (1 << gpio.pin);
140 // Configure pin with slow slew, high drive strength and GPIO mux
141 *PORT_PCR = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
143 // Enabling open-drain if specified
144 switch ( Matrix_type )
146 case Config_Opendrain:
147 *PORT_PCR |= PORT_PCR_ODE;
150 // Do nothing otherwise
157 return *GPIO_PDIR & (1 << gpio.pin) ? 1 : 0;
159 case Type_SenseSetup:
161 *GPIO_PDDR &= ~(1 << gpio.pin);
163 // Configure pin with passive filter and GPIO mux
164 *PORT_PCR = PORT_PCR_PFE | PORT_PCR_MUX(1);
166 // Pull resistor config
167 switch ( Matrix_type )
170 *PORT_PCR |= PORT_PCR_PE | PORT_PCR_PS;
173 case Config_Pulldown:
174 *PORT_PCR |= PORT_PCR_PE;
177 // Do nothing otherwise
187 // Setup GPIO pins for matrix scanning
190 // Register Matrix CLI dictionary
191 CLI_registerDictionary( matrixCLIDict, matrixCLIDictName );
193 info_msg("Columns: ");
194 printHex( Matrix_colsNum );
197 for ( uint8_t pin = 0; pin < Matrix_colsNum; pin++ )
199 Matrix_pin( Matrix_cols[ pin ], Type_StrobeSetup );
204 printHex( Matrix_rowsNum );
207 for ( uint8_t pin = 0; pin < Matrix_rowsNum; pin++ )
209 Matrix_pin( Matrix_rows[ pin ], Type_SenseSetup );
213 info_msg("Max Keys: ");
214 printHex( Matrix_maxKeys );
217 // Clear out Debounce Array
218 for ( uint8_t item = 0; item < Matrix_maxKeys; item++ )
220 Matrix_scanArray[ item ].prevState = KeyState_Off;
221 Matrix_scanArray[ item ].curState = KeyState_Off;
222 Matrix_scanArray[ item ].activeCount = 0;
223 Matrix_scanArray[ item ].inactiveCount = DebounceDivThreshold_define; // Start at 'off' steady state
224 Matrix_scanArray[ item ].prevDecisionTime = 0;
225 #ifdef GHOSTING_MATRIX
226 Matrix_ghostArray[ item ].prev = KeyState_Off;
227 Matrix_ghostArray[ item ].cur = KeyState_Off;
228 Matrix_ghostArray[ item ].saved = KeyState_Off;
232 // Clear scan stats counters
237 void Matrix_keyPositionDebug( KeyPosition pos )
239 // Depending on the state, use a different flag + color
243 print("\033[1mO\033[0m");
247 print("\033[1;33mP\033[0m");
251 print("\033[1;32mH\033[0m");
254 case KeyState_Release:
255 print("\033[1;35mR\033[0m");
258 case KeyState_Invalid:
260 print("\033[1;31mI\033[0m");
266 // Scan the matrix for keypresses
267 // NOTE: scanNum should be reset to 0 after a USB send (to reset all the counters)
268 void Matrix_scan( uint16_t scanNum )
270 #if ( DebounceThrottleDiv_define > 0 )
271 // Scan-rate throttling
272 // By scanning using a divider, the scan rate slowed down
273 // DebounceThrottleDiv_define == 1 means -> /2 or half scan rate
274 // This helps with bouncy switches on fast uCs
275 if ( !( Matrix_divCounter++ & (1 << ( DebounceThrottleDiv_define - 1 )) ) )
279 // Increment stats counters
280 if ( scanNum > matrixMaxScans ) matrixMaxScans = scanNum;
283 matrixPrevScans = matrixCurScans;
291 // Read systick for event scheduling
292 uint8_t currentTime = (uint8_t)systick_millis_count;
294 // For each strobe, scan each of the sense pins
295 for ( uint8_t strobe = 0; strobe < Matrix_colsNum; strobe++ )
298 Matrix_pin( Matrix_cols[ strobe ], Type_StrobeOn );
300 // Scan each of the sense pins
301 for ( uint8_t sense = 0; sense < Matrix_rowsNum; sense++ )
304 uint8_t key = Matrix_colsNum * sense + strobe;
305 KeyState *state = &Matrix_scanArray[ key ];
307 // If first scan, reset state
310 // Set previous state, and reset current state
311 state->prevState = state->curState;
312 state->curState = KeyState_Invalid;
316 // Increment count and right shift opposing count
317 // This means there is a maximum of scan 13 cycles on a perfect off to on transition
318 // (coming from a steady state 0xFFFF off scans)
319 // Somewhat longer with switch bounciness
320 // The advantage of this is that the count is ongoing and never needs to be reset
321 // State still needs to be kept track of to deal with what to send to the Macro module
322 if ( Matrix_pin( Matrix_rows[ sense ], Type_Sense ) )
324 // Only update if not going to wrap around
325 if ( state->activeCount < DebounceDivThreshold_define ) state->activeCount += 1;
326 state->inactiveCount >>= 1;
328 // Signal Not Detected
331 // Only update if not going to wrap around
332 if ( state->inactiveCount < DebounceDivThreshold_define ) state->inactiveCount += 1;
333 state->activeCount >>= 1;
336 // Check for state change if it hasn't been set
337 // But only if enough time has passed since last state change
338 // Only check if the minimum number of scans has been met
339 // the current state is invalid
340 // and either active or inactive count is over the debounce threshold
341 if ( state->curState == KeyState_Invalid )
343 // Determine time since last decision
344 uint8_t lastTransition = currentTime - state->prevDecisionTime;
346 // Attempt state transition
347 switch ( state->prevState )
351 if ( state->activeCount > state->inactiveCount )
353 state->curState = KeyState_Hold;
357 // If not enough time has passed since Hold
358 // Keep previous state
359 if ( lastTransition < MinDebounceTime_define )
361 //warn_print("FAST Release stopped");
362 state->curState = state->prevState;
366 state->curState = KeyState_Release;
370 case KeyState_Release:
372 if ( state->activeCount > state->inactiveCount )
374 // If not enough time has passed since Hold
375 // Keep previous state
376 if ( lastTransition < MinDebounceTime_define )
378 //warn_print("FAST Press stopped");
379 state->curState = state->prevState;
383 state->curState = KeyState_Press;
387 state->curState = KeyState_Off;
391 case KeyState_Invalid:
393 erro_print("Matrix scan bug!! Report me!");
397 // Update decision time
398 state->prevDecisionTime = currentTime;
400 // Send keystate to macro module
401 #ifndef GHOSTING_MATRIX
402 Macro_keyState( key, state->curState );
405 // Matrix Debug, only if there is a state change
406 if ( matrixDebugMode && state->curState != state->prevState )
408 // Basic debug output
409 if ( matrixDebugMode == 1 && state->curState == KeyState_Press )
414 // State transition debug output
415 else if ( matrixDebugMode == 2 )
418 Matrix_keyPositionDebug( state->curState );
426 Matrix_pin( Matrix_cols[ strobe ], Type_StrobeOff );
430 // Matrix ghosting check and elimination
431 // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
432 #ifdef GHOSTING_MATRIX
433 // strobe = column, sense = row
435 // Count (rows) use for columns
437 for ( uint8_t col = 0; col < Matrix_colsNum; col++ )
440 for ( uint8_t row = 0; row < Matrix_rowsNum; row++ )
442 uint8_t key = Matrix_colsNum * row + col;
443 KeyState *state = &Matrix_scanArray[ key ];
444 if ( keyOn(state->curState) )
449 col_ghost[col] = 0; // clear
452 // Count (columns) use for rows
454 for ( uint8_t row = 0; row < Matrix_rowsNum; row++ )
457 for ( uint8_t col = 0; col < Matrix_colsNum; col++ )
459 uint8_t key = Matrix_colsNum * row + col;
460 KeyState *state = &Matrix_scanArray[ key ];
461 if ( keyOn(state->curState) )
466 row_ghost[row] = 0; // clear
469 // Check if matrix has ghost
470 // Happens when key is pressed and some other key is pressed in same row and another in same column
472 for ( uint8_t col = 0; col < Matrix_colsNum; col++ )
474 for ( uint8_t row = 0; row < Matrix_rowsNum; row++ )
476 uint8_t key = Matrix_colsNum * row + col;
477 KeyState *state = &Matrix_scanArray[ key ];
478 if ( keyOn(state->curState) && col_use[col] >= 2 && row_use[row] >= 2 )
480 // mark col and row as having ghost
483 //print(" "); printInt8(col); print(","); printInt8(row);
490 for ( uint8_t col = 0; col < Matrix_colsNum; col++ )
492 for ( uint8_t row = 0; row < Matrix_rowsNum; row++ )
494 uint8_t key = Matrix_colsNum * row + col;
495 KeyState *state = &Matrix_scanArray[ key ];
496 KeyGhost *st = &Matrix_ghostArray[ key ];
498 // col or row is ghosting (crossed)
499 uint8_t ghost = (col_ghost[col] > 0 || row_ghost[row] > 0) ? 1 : 0;
501 st->prev = st->cur; // previous
502 // save state if no ghost or outside ghosted area
504 st->saved = state->curState; // save state if no ghost
506 // use saved state if ghosting, or current if not
507 st->cur = ghost > 0 ? st->saved : state->curState;
509 // Send keystate to macro module
510 KeyPosition k = !st->cur
511 ? (!st->prev ? KeyState_Off : KeyState_Release)
512 : ( st->prev ? KeyState_Hold : KeyState_Press);
513 //if (!st->cur && !st->prev) k = KeyState_Off; else
514 //if ( st->cur && st->prev) k = KeyState_Hold; else
515 //if ( st->cur && !st->prev) k = KeyState_Press; else
516 //if (!st->cur && st->prev) k = KeyState_Release;
517 Macro_keyState( key, k );
521 // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
524 // State Table Output Debug
525 if ( matrixDebugStateCounter > 0 )
528 matrixDebugStateCounter--;
530 // Output stats on number of scans being done per USB send
532 info_msg("Max scans: ");
533 printHex( matrixMaxScans );
535 info_msg("Previous scans: ");
536 printHex( matrixPrevScans );
539 // Output current scan number
540 info_msg("Scan Number: ");
544 // Display the state info for each key
545 print("<key>:<previous state><current state> <active count> <inactive count>");
546 for ( uint8_t key = 0; key < Matrix_maxKeys; key++ )
548 // Every 4 keys, put a newline
553 printHex_op( key, 2 );
556 Matrix_keyPositionDebug( Matrix_scanArray[ key ].prevState );
557 Matrix_keyPositionDebug( Matrix_scanArray[ key ].curState );
559 printHex_op( Matrix_scanArray[ key ].activeCount, 4 );
561 printHex_op( Matrix_scanArray[ key ].inactiveCount, 4 );
570 // Called by parent scan module whenever the available current changes
572 void Matrix_currentChange( unsigned int current )
574 // TODO - Any potential power savings?
579 // ----- CLI Command Functions -----
581 void cliFunc_matrixDebug ( char* args )
583 // Parse number from argument
584 // NOTE: Only first argument is used
587 CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
589 // Set the matrix debug flag depending on the argument
590 // If no argument, set to scan code only
591 // If set to T, set to state transition
592 switch ( arg1Ptr[0] )
597 matrixDebugMode = matrixDebugMode != 2 ? 2 : 0;
602 matrixDebugMode = matrixDebugMode != 1 ? 1 : 0;
611 info_msg("Matrix Debug Mode: ");
612 printInt8( matrixDebugMode );
615 void cliFunc_matrixState ( char* args )
617 // Parse number from argument
618 // NOTE: Only first argument is used
621 CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
623 // Default to 1 if no argument is given
624 matrixDebugStateCounter = 1;
626 if ( arg1Ptr[0] != '\0' )
628 matrixDebugStateCounter = (uint16_t)numToInt( arg1Ptr );