qmk_firmware

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

matrix.c (6167B)


      1 /*
      2 Copyright 2016-2017 Ralf Schmitt <ralf@bunkertor.net> Rasmus Schults <rasmusx@gmail.com>
      3 This program is free software: you can redistribute it and/or modify
      4 it under the terms of the GNU General Public License as published by
      5 the Free Software Foundation, either version 2 of the License, or
      6 (at your option) any later version.
      7 
      8 This program is distributed in the hope that it will be useful,
      9 but WITHOUT ANY WARRANTY; without even the implied warranty of
     10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     11 GNU General Public License for more details.
     12 
     13 You should have received a copy of the GNU General Public License
     14 along with this program.  If not, see <http://www.gnu.org/licenses/>.
     15 */
     16 
     17 #include <util/delay.h>
     18 #include <avr/io.h>
     19 #include <stdio.h>
     20 #include "matrix.h"
     21 #include "util.h"
     22 #include "print.h"
     23 #include "debug.h"
     24 
     25 #ifndef DEBOUNCE
     26 #    define DEBOUNCE 5
     27 #endif
     28 
     29 static uint8_t debouncing = DEBOUNCE;
     30 
     31 /* matrix state(1:on, 0:off) */
     32 static matrix_row_t matrix[MATRIX_ROWS];
     33 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
     34 
     35 static uint8_t read_rows(void);
     36 static uint8_t read_fwkey(void);
     37 static void init_rows(void);
     38 static void unselect_cols(void);
     39 static void select_col(uint8_t col);
     40 
     41 
     42 __attribute__ ((weak))
     43 void matrix_init_kb(void) {
     44     matrix_init_user();
     45 }
     46 
     47 __attribute__ ((weak))
     48 void matrix_scan_kb(void) {
     49     matrix_scan_user();
     50 }
     51 
     52 __attribute__ ((weak))
     53 void matrix_init_user(void) {
     54 }
     55 
     56 __attribute__ ((weak))
     57 void matrix_scan_user(void) {
     58 }
     59 
     60 void matrix_init(void) {
     61   DDRD  |=  0b11010000;
     62   PORTD &= ~0b01010000;
     63   DDRB  |=  0b00011111;
     64   PORTB &= ~0b00001110;
     65   PORTB |=  0b00010001;
     66   DDRE  |=  0b01000000;
     67   PORTE &= ~0b01000000;
     68 
     69   unselect_cols();
     70   init_rows();
     71 
     72   for (uint8_t i=0; i < MATRIX_ROWS; i++)  {
     73     matrix[i] = 0;
     74     matrix_debouncing[i] = 0;
     75   }
     76 
     77   matrix_init_kb();
     78 }
     79 
     80 uint8_t matrix_scan(void) {
     81   for (uint8_t col = 0; col < MATRIX_COLS; col++) {
     82     select_col(col);
     83     _delay_us(3);
     84 
     85     uint8_t rows = read_rows();
     86     if(col == 0) {
     87       rows |= read_fwkey();
     88     }
     89     for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
     90       bool prev_bit = matrix_debouncing[row] & ((matrix_row_t)1<<col);
     91       bool curr_bit = rows & (1<<row);
     92       if (prev_bit != curr_bit) {
     93         matrix_debouncing[row] ^= ((matrix_row_t)1<<col);
     94         if (debouncing) {
     95             dprint("bounce!: "); dprintf("%02X", debouncing); dprintln();
     96         }        
     97         debouncing = DEBOUNCE;
     98       }
     99     }
    100     unselect_cols();
    101   }
    102 
    103   if (debouncing) {
    104     if (--debouncing) {
    105       _delay_ms(1);
    106     } else {
    107       for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
    108         matrix[i] = matrix_debouncing[i];
    109       }
    110     }
    111   }
    112 
    113   matrix_scan_kb();
    114   return 1;
    115 }
    116 
    117 inline matrix_row_t matrix_get_row(uint8_t row) {
    118   return matrix[row];
    119 }
    120 
    121 void matrix_print(void) {
    122   print("\nr/c 0123456789ABCDEF\n");
    123   for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
    124     xprintf("%02X: %032lb\n", row, bitrev32(matrix_get_row(row)));
    125   }
    126 }
    127 
    128 /* Row pin configuration
    129  * row: 0    1    2    3    4    5
    130  * pin: PB7  PD0  PD1  PD2  PD3  PD5
    131  *
    132  * Esc uses its own pin PE2
    133  */
    134 static void init_rows(void) {
    135   DDRB  &= ~0b10000000;
    136   PORTB &= ~0b10000000;
    137 
    138   DDRD  &= ~0b00101111;
    139   PORTD &= ~0b00101111;
    140 
    141   DDRE  &= ~0b00000100;
    142   PORTE |=  0b00000100;
    143 }
    144 
    145 static uint8_t read_rows(void) {
    146   return (PINB&(1<<7) ? (1<<0) : 0) |
    147     (PIND&(1<<0) ? (1<<1) : 0) |
    148     (PIND&(1<<1) ? (1<<2) : 0) |
    149     (PIND&(1<<2) ? (1<<3) : 0) |
    150     (PIND&(1<<3) ? (1<<4) : 0) |
    151     (PIND&(1<<5) ? (1<<5) : 0);
    152 }
    153 
    154 static uint8_t read_fwkey(void)
    155 {
    156   return PINE&(1<<2) ? 0 : (1<<0);
    157 }
    158 
    159 /* Columns 0 - 15
    160  * These columns uses two 74HC237D 3 to 8 bit demultiplexers.
    161  * col / pin:    PC6  PB6  PF0  PF1  PC7
    162  * 0:             1    0    0    0    0
    163  * 1:             1    0    1    0    0
    164  * 2:             1    0    0    1    0
    165  * 3:             1    0    1    1    0
    166  * 4:             1    0    0    0    1
    167  * 5:             1    0    1    0    1
    168  * 6:             1    0    0    1    1
    169  * 7:             1    0    1    1    1
    170  * 8:             0    1    0    0    0
    171  * 9:             0    1    1    0    0
    172  * 10:            0    1    0    1    0
    173  * 11:            0    1    1    1    0
    174  * 12:            0    1    0    0    1
    175  * 13:            0    1    1    0    1
    176  * 14:            0    1    0    1    1
    177  * 15:            0    1    1    1    1
    178  *
    179  * col: 16
    180  * pin: PB5
    181  *
    182  * col: 17
    183  * pin: PB4
    184  *
    185  * col: 18
    186  * pin: PD7
    187  */
    188 static void unselect_cols(void) {
    189   DDRB  |=  0b01110000;
    190   PORTB &= ~0b01110000;
    191 
    192   DDRC |= (1<<6) | (1<<7);
    193   PORTC &= ~((1<<6) | (1<<7));
    194 
    195   DDRF |= (1<<0) | (1<<1);
    196   PORTF &= ~((1<<0) | (1<<1));
    197 
    198   DDRD |= (1<<7);
    199   PORTD &= ~(1<<7);
    200 }
    201 
    202 static void select_col(uint8_t col) {
    203   switch (col) {
    204     case 0:
    205       PORTC |= (1<<6);
    206       break;
    207     case 1:
    208       PORTC |= (1<<6);
    209       PORTF |= (1<<0);
    210       break;
    211     case 2:
    212       PORTC |= (1<<6);
    213       PORTF |= (1<<1);
    214       break;
    215     case 3:
    216       PORTC |= (1<<6);
    217       PORTF |= (1<<0) | (1<<1);
    218       break;
    219     case 4:
    220       PORTC |= (1<<6);
    221       PORTC |= (1<<7);
    222       break;
    223     case 5:
    224       PORTC |= (1<<6);
    225       PORTF |= (1<<0);
    226       PORTC |= (1<<7);
    227       break;
    228     case 6:
    229       PORTC |= (1<<6);
    230       PORTF |= (1<<1);
    231       PORTC |= (1<<7);
    232       break;
    233     case 7:
    234       PORTC |= (1<<6);
    235       PORTF |= (1<<0) | (1<<1);
    236       PORTC |= (1<<7);
    237       break;
    238     case 8:
    239       PORTB |= (1<<6);
    240       break;
    241     case 9:
    242       PORTB |= (1<<6);
    243       PORTF |= (1<<0);
    244       break;
    245     case 10:
    246       PORTB |= (1<<6);
    247       PORTF |= (1<<1);
    248       break;
    249     case 11:
    250       PORTB |= (1<<6);
    251       PORTF |= (1<<0) | (1<<1);
    252       break;
    253     case 12:
    254       PORTB |= (1<<6);
    255       PORTC |= (1<<7);
    256       break;
    257     case 13:
    258       PORTB |= (1<<6);
    259       PORTF |= (1<<0);
    260       PORTC |= (1<<7);
    261       break;
    262     case 14:
    263       PORTB |= (1<<6);
    264       PORTF |= (1<<1);
    265       PORTC |= (1<<7);
    266       break;
    267     case 15:
    268       PORTB |= (1<<6);
    269       PORTF |= (1<<0) | (1<<1);
    270       PORTC |= (1<<7);
    271       break;
    272     case 16:
    273       PORTB |= (1<<5);
    274       break;
    275     case 17:
    276       PORTB |= (1<<4);
    277       break;
    278     case 18:
    279       PORTD |= (1<<7);
    280       break;
    281   }
    282 }