qmk_firmware

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

matrix.c (7160B)


      1 /*
      2 Copyright 2019 MechMerlin <mechmerlin@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(uint8_t col);
     36 static void init_rows(void);
     37 static void unselect_cols(void);
     38 static void select_col(uint8_t col);
     39 
     40 
     41 __attribute__ ((weak))
     42 void matrix_init_kb(void) {
     43     matrix_init_user();
     44 }
     45 
     46 __attribute__ ((weak))
     47 void matrix_scan_kb(void) {
     48     matrix_scan_user();
     49 }
     50 
     51 __attribute__ ((weak))
     52 void matrix_init_user(void) {
     53 }
     54 
     55 __attribute__ ((weak))
     56 void matrix_scan_user(void) {
     57 }
     58 
     59 void backlight_init_ports(void)
     60 {
     61   // DDRD  |=  0b11010000;
     62   // PORTD &= ~0b01010000;
     63   // PORTD |=  0b10000000;
     64   // DDRB  |=  0b00011111;
     65   // PORTB &= ~0b00001110;
     66   // PORTB |=  0b00010001;
     67   // DDRE  |=  0b01000000;
     68   // PORTE &= ~0b01000000;
     69 }
     70 
     71 void matrix_init(void) {
     72   backlight_init_ports();
     73   unselect_cols();
     74   init_rows();
     75 
     76   for (uint8_t i=0; i < MATRIX_ROWS; i++)  {
     77     matrix[i] = 0;
     78     matrix_debouncing[i] = 0;
     79   }
     80 
     81   matrix_init_kb();
     82 }
     83 
     84 uint8_t matrix_scan(void) {
     85   for (uint8_t col = 0; col < MATRIX_COLS; col++) {
     86     select_col(col);
     87     _delay_us(3);
     88 
     89     uint8_t rows = read_rows(col);
     90 
     91     for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
     92       bool prev_bit = matrix_debouncing[row] & ((matrix_row_t)1<<col);
     93       bool curr_bit = rows & (1<<row);
     94       if (prev_bit != curr_bit) {
     95         matrix_debouncing[row] ^= ((matrix_row_t)1<<col);
     96         if (debouncing) {
     97             dprint("bounce!: "); dprintf("%02X", debouncing); dprintln();
     98         }
     99         debouncing = DEBOUNCE;
    100       }
    101     }
    102     unselect_cols();
    103   }
    104 
    105   if (debouncing) {
    106     if (--debouncing) {
    107       _delay_ms(1);
    108     } else {
    109       for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
    110         matrix[i] = matrix_debouncing[i];
    111       }
    112     }
    113   }
    114 
    115   matrix_scan_kb();
    116   return 1;
    117 }
    118 
    119 inline matrix_row_t matrix_get_row(uint8_t row) {
    120   return matrix[row];
    121 }
    122 
    123 void matrix_print(void) {
    124   print("\nr/c 0123456789ABCDEF\n");
    125   for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
    126     xprintf("%02X: %032lb\n", row, bitrev32(matrix_get_row(row)));
    127   }
    128 }
    129 /* Row pin configuration
    130  * row: 0    1    2    3    4    5
    131  * pin: PB7  PD0  PD1  PD2  PD3  PD5
    132  *
    133  * Reset Key uses its own pin PE2
    134  */
    135 static void init_rows(void) {
    136     DDRD  &= ~0b00101111;
    137     PORTD &= ~0b00101111;
    138 
    139     DDRB  &= ~0b10000000;
    140     PORTB &= ~0b10000000;
    141 
    142     DDRE  &= ~0b00000100;
    143     PORTE |=  0b00000100;
    144 }
    145 
    146 static uint8_t read_rows(uint8_t col) {
    147   if (col == 19) {
    148     return PINE&(1<<2) ? 0 : (1<<0);
    149   } else {
    150       return (PIND&(1<<0) ? (1<<1) : 0) |
    151              (PIND&(1<<1) ? (1<<2) : 0) |
    152              (PIND&(1<<2) ? (1<<3) : 0) |
    153              (PIND&(1<<3) ? (1<<4) : 0) |
    154              (PIND&(1<<5) ? (1<<5) : 0) |
    155              (PINB&(1<<7) ? (1<<0) : 0);
    156     }
    157 }
    158 
    159 
    160 /* Columns 0 - G
    161  * These columns uses two 74HC237D 3 to 8 bit demultiplexers.
    162  *
    163  * atmega32u4    decoder    pin
    164  *    C6           U1       E2
    165  *    B6           U2       E2
    166  *    F0          U1, U2    A0
    167  *    F1          U1, U2    A1
    168  *    C7          U1, U2    A2
    169  *
    170  * col 0: B4
    171  * col 1: D7
    172  * col I: B5
    173  *
    174  * col / pin:    PC6  PB6  PF0  PF1  PC7  Decoder Pin
    175  * 2:             1    0    0    0    0     U1    Y0
    176  * 3:             1    0    1    0    0     U1    Y1
    177  * 4:             1    0    0    1    0     U1    Y2
    178  * 5:             1    0    1    1    0     U1    Y3
    179  * 6:             1    0    0    0    1     U1    Y4
    180  * 7:             1    0    1    0    1     U1    Y5
    181  * 8:             1    0    0    1    1     U1    Y6
    182  * 9:             1    0    1    1    1     U1    Y7
    183  * A:             0    1    0    0    0     U2    Y0
    184  * B:             0    1    1    0    0     U2    Y1
    185  * C:             0    1    0    1    0     U2    Y2
    186  * D:             0    1    1    1    0     U2    Y3
    187  * E:             0    1    0    0    1     U2    Y4
    188  * F:             0    1    1    0    1     U2    Y5
    189  * G:             0    1    0    1    1     U2    Y6
    190  * H:             0    1    1    1    1     U2    Y7
    191  *
    192  */
    193 static void unselect_cols(void) {
    194   DDRB  |=  0b01110000;
    195   PORTB &= ~0b01110000;
    196 
    197   DDRC  |=  0b11000000;
    198   PORTC &= ~0b11000000;
    199 
    200   DDRD  |=  0b10000000;
    201   PORTD &= ~0b10000000;
    202 
    203   DDRF  |=  0b00000011;
    204   PORTF &= ~0b00000011;
    205 }
    206 
    207 static void select_col(uint8_t col) {
    208  
    209    switch (col) {
    210         case 0:
    211             PORTB |= 0b00010000;
    212             break;
    213         case 1:
    214             PORTD |= 0b10000000;
    215             break;
    216         case 2: // U1 Y0
    217             PORTC |= 0b01000000;
    218             break;
    219         case 3: // U1 Y1
    220             PORTC |= 0b01000000;
    221             PORTF |= 0b00000001;
    222             break;
    223         case 4: // U1 Y2
    224             PORTC |= 0b01000000;
    225             PORTF |= 0b00000010;
    226             break;
    227         case 5: // U1 Y3
    228             PORTC |= 0b01000000;
    229             PORTF |= 0b00000011;
    230             break;
    231         case 6: // U1 Y4
    232             PORTC |= 0b11000000;
    233             break;
    234         case 7: // U1 Y5
    235             PORTC |= 0b11000000;
    236             PORTF |= 0b00000001;
    237             break;
    238         case 8: // U1 Y6
    239             PORTC |= 0b11000000;
    240             PORTF |= 0b00000010;
    241             break;
    242         case 9: // U1 Y7
    243             PORTC |= 0b11000000;
    244             PORTF |= 0b00000011;
    245             break;
    246         case 10: // U2 Y0
    247             PORTB |= 0b01000000;
    248             break;
    249         case 11: // U2 Y1
    250             PORTB |= 0b01000000;
    251             PORTF |= 0b00000001;
    252             break;
    253         case 12: // U2 Y2
    254             PORTB |= 0b01000000;
    255             PORTF |= 0b00000010;
    256             break;
    257         case 13: // U2 Y3
    258             PORTB |= 0b01000000;
    259             PORTF |= 0b00000011;
    260             break;
    261         case 14: // U2 Y4
    262             PORTB |= 0b01000000;
    263             PORTC |= 0b10000000;
    264             break;
    265         case 15: // U2 Y5
    266             PORTB |= 0b01000000;
    267             PORTC |= 0b10000000;
    268             PORTF |= 0b00000001;
    269             break;
    270         case 16: // U2 Y6
    271             PORTB |= 0b01000000;
    272             PORTC |= 0b10000000;
    273             PORTF |= 0b00000010;
    274             break;
    275         case 17: // U2 Y7
    276             PORTB |= 0b01000000;
    277             PORTC |= 0b10000000;
    278             PORTF |= 0b00000011;
    279             break;
    280         case 18:
    281             PORTB |= 0b00100000;
    282             break;
    283     }
    284 }