qmk_firmware

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

matrix.c (6340B)


      1 /*
      2 Copyright 2017 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 
    130 /* Row pin configuration
    131  * row: 0    1    2    3    4    5
    132  * pin: PB7  PD0  PD1  PD2  PD3  PD5
    133  *
    134  * Esc uses its own pin PE2
    135  */
    136 static void init_rows(void) {
    137     DDRD  &= ~0b00101111;
    138     PORTD &= ~0b00101111;
    139 
    140     DDRB  &= ~0b10000000;
    141     PORTB &= ~0b10000000;
    142 
    143     DDRE  &= ~0b00000100;
    144     PORTE |=  0b00000100;
    145 }
    146 
    147 static uint8_t read_rows(uint8_t col) {
    148   if (col == 16) {
    149     return PINE&(1<<2) ? 0 : (1<<0);
    150   } else {
    151       return (PIND&(1<<0) ? (1<<0) : 0) |
    152              (PIND&(1<<1) ? (1<<1) : 0) |
    153              (PIND&(1<<2) ? (1<<2) : 0) |
    154              (PIND&(1<<3) ? (1<<3) : 0) |
    155              (PIND&(1<<5) ? (1<<4) : 0) |
    156              (PINB&(1<<7) ? (1<<5) : 0);
    157     }
    158 }
    159 
    160 uint8_t read_fwkey(void)
    161 {
    162   return PINE&(1<<2) ? 0 : (1<<0);
    163 }
    164 
    165 /* Columns 0 - 15
    166  * These columns uses two 74HC237D 3 to 8 bit demultiplexers.
    167  * col / pin:    PC6  PB6  PF0  PF1  PC7
    168  * 0:             1    0    0    0    0
    169  * 1:             1    0    1    0    0
    170  * 2:             1    0    0    1    0
    171  * 3:             1    0    1    1    0
    172  * 4:             1    0    0    0    1
    173  * 5:             1    0    1    0    1
    174  * 6:             1    0    0    1    1
    175  * 7:             1    0    1    1    1
    176  * 8:             0    1    0    0    0
    177  * 9:             0    1    1    0    0
    178  * 10:            0    1    0    1    0
    179  * 11:            0    1    1    1    0
    180  * 12:            0    1    0    0    1
    181  * 13:            0    1    1    0    1
    182  * 14:            0    1    0    1    1
    183  * 15:            0    1    1    1    1
    184  *
    185  */
    186 static void unselect_cols(void) {
    187   DDRB  |=  0b01000000;
    188   PORTB &= ~0b01000000;
    189 
    190   DDRC  |=  0b11000000;
    191   PORTC &= ~0b11000000;
    192 
    193   DDRF  |=  0b00000011;
    194   PORTF &= ~0b00000011;
    195 }
    196 
    197 static void select_col(uint8_t col) {
    198  
    199    switch (col) {
    200         case 0:
    201             PORTC |= 0b01000000;
    202             break;
    203         case 1:
    204             PORTC |= 0b01000000;
    205             PORTF |= 0b00000001;
    206             break;
    207         case 2:
    208             PORTC |= 0b01000000;
    209             PORTF |= 0b00000010;
    210             break;
    211         case 3:
    212             PORTC |= 0b01000000;
    213             PORTF |= 0b00000011;
    214             break;
    215         case 4:
    216             PORTC |= 0b11000000;
    217             break;
    218         case 5:
    219             PORTC |= 0b11000000;
    220             PORTF |= 0b00000001;
    221             break;
    222         case 6:
    223             PORTC |= 0b11000000;
    224             PORTF |= 0b00000010;
    225             break;
    226         case 7:
    227             PORTC |= 0b11000000;
    228             PORTF |= 0b00000011;
    229             break;
    230         case 8:
    231             PORTB |= 0b01000000;
    232             break;
    233         case 9:
    234             PORTB |= 0b01000000;
    235             PORTF |= 0b00000001;
    236             break;
    237         case 10:
    238             PORTB |= 0b01000000;
    239             PORTF |= 0b00000010;
    240             break;
    241         case 11:
    242             PORTB |= 0b01000000;
    243             PORTF |= 0b00000011;
    244             break;
    245         case 12:
    246             PORTB |= 0b01000000;
    247             PORTC |= 0b10000000;
    248             break;
    249         case 13:
    250             PORTB |= 0b01000000;
    251             PORTF |= 0b00000001;
    252             PORTC |= 0b10000000;
    253             break;
    254         case 14:
    255             PORTB |= 0b01000000;
    256             PORTF |= 0b00000010;
    257             PORTC |= 0b10000000;
    258             break;
    259         case 15:
    260             PORTB |= 0b01000000;
    261             PORTF |= 0b00000011;
    262             PORTC |= 0b10000000;
    263             break;
    264     }
    265 }