qmk_firmware

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

matrix.c (6509B)


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