qmk_firmware

QMK firmware for my keyboards (Corne, Sweep Ferris) and trackball (Ploopy Adept)
Log | Files | Refs | Submodules | LICENSE

matrix.c (10020B)


      1 /*
      2 Copyright 2012-2018 Jun Wako, Jack Humbert, Mike Roberts
      3 
      4 This program is free software: you can redistribute it and/or modify
      5 it under the terms of the GNU General Public License as published by
      6 the Free Software Foundation, either version 2 of the License, or
      7 (at your option) any later version.
      8 
      9 This program is distributed in the hope that it will be useful,
     10 but WITHOUT ANY WARRANTY; without even the implied warranty of
     11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     12 GNU General Public License for more details.
     13 
     14 You should have received a copy of the GNU General Public License
     15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
     16 */
     17 
     18 /*
     19  * This matrix.c has been hacked up to support some columns being on an ex pander in ROW2COL mode.
     20  * The columns are only ever selected and unselected, never read. Unselecting a single column via the expander is not
     21  * implemented because updating one column costs the same as updating all the columns in a bank. Currently both banks
     22  * are unselected but two i2c transactions could be removed if we only unselect the the proper half.
     23  */
     24 
     25 #include <stdint.h>
     26 #include <stdbool.h>
     27 #if defined(__AVR__)
     28 #include <avr/io.h>
     29 #endif
     30 #include "wait.h"
     31 #include "print.h"
     32 #include "debug.h"
     33 #include "util.h"
     34 #include "matrix.h"
     35 #include "timer.h"
     36 #include "mcp23017.h"
     37 #include "outputselect.h"
     38 
     39 /* Set 0 if debouncing isn't needed */
     40 
     41 #ifndef DEBOUNCE
     42 #   define DEBOUNCE 5
     43 #endif
     44 
     45 #if (DEBOUNCE > 0)
     46     static uint16_t debouncing_time;
     47     static bool debouncing = false;
     48 #endif
     49 
     50 #if (MATRIX_COLS <= 8)
     51 #    define print_matrix_header()  print("\nr/c 01234567\n")
     52 #    define print_matrix_row(row)  print_bin_reverse8(matrix_get_row(row))
     53 #    define ROW_SHIFTER ((uint8_t)1)
     54 #elif (MATRIX_COLS <= 16)
     55 #    define print_matrix_header()  print("\nr/c 0123456789ABCDEF\n")
     56 #    define print_matrix_row(row)  print_bin_reverse16(matrix_get_row(row))
     57 #    define ROW_SHIFTER ((uint16_t)1)
     58 #elif (MATRIX_COLS <= 32)
     59 #    define print_matrix_header()  print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
     60 #    define print_matrix_row(row)  print_bin_reverse32(matrix_get_row(row))
     61 #    define ROW_SHIFTER  ((uint32_t)1)
     62 #endif
     63 
     64 #ifdef MATRIX_MASKED
     65     extern const matrix_row_t matrix_mask[];
     66 #endif
     67 
     68 #if (DIODE_DIRECTION == ROW2COL) || (DIODE_DIRECTION == COL2ROW)
     69 static const uint8_t row_pins[MATRIX_ROWS] = NEK_MATRIX_ROW_PINS;
     70 static const uint8_t col_pins[MATRIX_COLS] = NEK_MATRIX_COL_PINS;
     71 static const bool col_expanded[MATRIX_COLS] = COL_EXPANDED;
     72 #endif
     73 
     74 /* matrix state(1:on, 0:off) */
     75 static matrix_row_t matrix[MATRIX_ROWS];
     76 
     77 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
     78 
     79 
     80 #if (DIODE_DIRECTION == COL2ROW)
     81     static void init_cols(void);
     82     static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row);
     83     static void unselect_rows(void);
     84     static void select_row(uint8_t row);
     85     static void unselect_row(uint8_t row);
     86 #elif (DIODE_DIRECTION == ROW2COL)
     87     static void init_rows(void);
     88     static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col);
     89     static void unselect_cols(void);
     90     static void unselect_col(uint8_t col);
     91     static void select_col(uint8_t col);
     92 #endif
     93 
     94 __attribute__ ((weak))
     95 void matrix_init_kb(void) {
     96     matrix_init_user();
     97 }
     98 
     99 __attribute__ ((weak))
    100 void matrix_scan_kb(void) {
    101     matrix_scan_user();
    102 }
    103 
    104 __attribute__ ((weak))
    105 void matrix_init_user(void) {
    106 }
    107 
    108 __attribute__ ((weak))
    109 void matrix_scan_user(void) {
    110 }
    111 
    112 inline
    113 uint8_t matrix_rows(void) {
    114     return MATRIX_ROWS;
    115 }
    116 
    117 inline
    118 uint8_t matrix_cols(void) {
    119     return MATRIX_COLS;
    120 }
    121 
    122 void matrix_init(void) {
    123     // initialize row and col
    124 #if (DIODE_DIRECTION == COL2ROW)
    125     unselect_rows();
    126     init_cols();
    127 #elif (DIODE_DIRECTION == ROW2COL)
    128     unselect_cols();
    129     init_rows();
    130 #endif
    131 
    132     // initialize matrix state: all keys off
    133     for (uint8_t i=0; i < MATRIX_ROWS; i++) {
    134         matrix[i] = 0;
    135         matrix_debouncing[i] = 0;
    136     }
    137 
    138     matrix_init_kb();
    139     set_output(OUTPUT_AUTO);
    140 }
    141 
    142 uint8_t matrix_scan(void)
    143 {
    144 
    145 #if (DIODE_DIRECTION == COL2ROW)
    146 
    147     // Set row, read cols
    148     for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
    149 #       if (DEBOUNCE > 0)
    150             bool matrix_changed = read_cols_on_row(matrix_debouncing, current_row);
    151 
    152             if (matrix_changed) {
    153                 debouncing = true;
    154                 debouncing_time = timer_read();
    155             }
    156 
    157 #       else
    158             read_cols_on_row(matrix, current_row);
    159 #       endif
    160 
    161     }
    162 
    163 #elif (DIODE_DIRECTION == ROW2COL)
    164 
    165     // Set col, read rows
    166     for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
    167 #       if (DEBOUNCE > 0)
    168             bool matrix_changed = read_rows_on_col(matrix_debouncing, current_col);
    169             if (matrix_changed) {
    170                 debouncing = true;
    171                 debouncing_time = timer_read();
    172             }
    173 #       else
    174              read_rows_on_col(matrix, current_col);
    175 #       endif
    176 
    177     }
    178 
    179 #endif
    180 
    181 #   if (DEBOUNCE > 0)
    182         if (debouncing && (timer_elapsed(debouncing_time) > DEBOUNCE)) {
    183             for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
    184                 matrix[i] = matrix_debouncing[i];
    185             }
    186             debouncing = false;
    187         }
    188 #   endif
    189 
    190     matrix_scan_kb();
    191     return 1;
    192 }
    193 
    194 inline
    195 bool matrix_is_on(uint8_t row, uint8_t col)
    196 {
    197     return (matrix[row] & ((matrix_row_t)1<col));
    198 }
    199 
    200 inline
    201 matrix_row_t matrix_get_row(uint8_t row)
    202 {
    203     // Matrix mask lets you disable switches in the returned matrix data. For example, if you have a
    204     // switch blocker installed and the switch is always pressed.
    205 #ifdef MATRIX_MASKED
    206     return matrix[row] & matrix_mask[row];
    207 #else
    208     return matrix[row];
    209 #endif
    210 }
    211 
    212 void matrix_print(void)
    213 {
    214     print_matrix_header();
    215 
    216     for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
    217         print_hex8(row); print(": ");
    218         print_matrix_row(row);
    219         print("\n");
    220     }
    221 }
    222 
    223 #if (DIODE_DIRECTION == COL2ROW)
    224 
    225 static void init_cols(void)
    226 {
    227     for(uint8_t x = 0; x < MATRIX_COLS; x++) {
    228         uint8_t pin = col_pins[x];
    229         _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
    230         _SFR_IO8((pin >> 4) + 2) |=  _BV(pin & 0xF); // HI
    231     }
    232 }
    233 
    234 static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
    235 {
    236     // Store last value of row prior to reading
    237     matrix_row_t last_row_value = current_matrix[current_row];
    238 
    239     // Clear data in matrix row
    240     current_matrix[current_row] = 0;
    241 
    242     // Select row and wait for row selecton to stabilize
    243     select_row(current_row);
    244     wait_us(30);
    245 
    246     // For each col...
    247     for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
    248 
    249         // Select the col pin to read (active low)
    250         uint8_t pin = col_pins[col_index];
    251         uint8_t pin_state = (_SFR_IO8(pin >> 4) & _BV(pin & 0xF));
    252 
    253         // Populate the matrix row with the state of the col pin
    254         current_matrix[current_row] |=  pin_state ? 0 : (ROW_SHIFTER << col_index);
    255     }
    256 
    257     // Unselect row
    258     unselect_row(current_row);
    259 
    260     return (last_row_value != current_matrix[current_row]);
    261 }
    262 
    263 static void select_row(uint8_t row)
    264 {
    265     uint8_t pin = row_pins[row];
    266     _SFR_IO8((pin >> 4) + 1) |=  _BV(pin & 0xF); // OUT
    267     _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
    268 }
    269 
    270 static void unselect_row(uint8_t row)
    271 {
    272     uint8_t pin = row_pins[row];
    273     _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
    274     _SFR_IO8((pin >> 4) + 2) |=  _BV(pin & 0xF); // HI
    275 }
    276 
    277 static void unselect_rows(void)
    278 {
    279     for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
    280         uint8_t pin = row_pins[x];
    281         _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
    282         _SFR_IO8((pin >> 4) + 2) |=  _BV(pin & 0xF); // HI
    283     }
    284 }
    285 
    286 #elif (DIODE_DIRECTION == ROW2COL)
    287 
    288 static void init_rows(void)
    289 {
    290     for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
    291         uint8_t pin = row_pins[x];
    292         _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
    293         _SFR_IO8((pin >> 4) + 2) |=  _BV(pin & 0xF); // HI
    294     }
    295 }
    296 
    297 static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
    298 {
    299     bool matrix_changed = false;
    300 
    301     // Select col and wait for col selecton to stabilize
    302     select_col(current_col);
    303     wait_us(30);
    304 
    305     // For each row...
    306     for(uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++)
    307     {
    308         // Store last value of row prior to reading
    309         matrix_row_t last_row_value = current_matrix[row_index];
    310 
    311         // Check row pin state
    312         if ((_SFR_IO8(row_pins[row_index] >> 4) & _BV(row_pins[row_index] & 0xF)) == 0)
    313         {
    314             // Pin LO, set col bit
    315             current_matrix[row_index] |= (ROW_SHIFTER << current_col);
    316         }
    317         else
    318         {
    319             // Pin HI, clear col bit
    320             current_matrix[row_index] &= ~(ROW_SHIFTER << current_col);
    321         }
    322 
    323         // Determine if the matrix changed state
    324         if ((last_row_value != current_matrix[row_index]) && !(matrix_changed))
    325         {
    326             matrix_changed = true;
    327         }
    328     }
    329 
    330     // Unselect col
    331     unselect_col(current_col);
    332 
    333     return matrix_changed;
    334 }
    335 
    336 static void select_col(uint8_t col)
    337 {
    338     uint8_t pin = col_pins[col];
    339     if (col_expanded[col])
    340     {
    341         expander_select(pin);
    342     }
    343     else
    344     {
    345         _SFR_IO8((pin >> 4) + 1) |=  _BV(pin & 0xF); // OUT
    346         _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
    347     }
    348 }
    349 
    350 
    351 static void unselect_col(uint8_t col)
    352 {
    353     uint8_t pin = col_pins[col];
    354     if (col_expanded[col])
    355     {
    356         expander_unselect_all();
    357     }
    358     else
    359     {
    360         _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
    361         _SFR_IO8((pin >> 4) + 2) |=  _BV(pin & 0xF); // HI
    362     }
    363 }
    364 
    365 static void unselect_cols(void)
    366 {
    367     expander_unselect_all();
    368 
    369     for(uint8_t col = 0; col < MATRIX_COLS; col++) {
    370         uint8_t pin = col_pins[col];
    371         if (!col_expanded[col])
    372         {
    373             _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
    374             _SFR_IO8((pin >> 4) + 2) |=  _BV(pin & 0xF); // HI
    375         }
    376     }
    377 }
    378 
    379 #endif