qmk_firmware

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

matrix.c (5459B)


      1 /*
      2 Copyright 2011 Jun Wako <wakojun@gmail.com>
      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  * scan matrix
     20  */
     21 #include <stdint.h>
     22 #include <stdbool.h>
     23 #include <util/delay.h>
     24 #include "print.h"
     25 #include "debug.h"
     26 #include "util.h"
     27 #include "timer.h"
     28 #include "matrix.h"
     29 #include "avr/timer_avr.h"
     30 #include "hhkb_avr.h"
     31 #include <avr/wdt.h>
     32 #include "suspend.h"
     33 #include "lufa.h"
     34 
     35 
     36 // matrix power saving
     37 #define MATRIX_POWER_SAVE       10000
     38 static uint32_t matrix_last_modified = 0;
     39 
     40 // matrix state buffer(1:on, 0:off)
     41 static matrix_row_t *matrix;
     42 static matrix_row_t *matrix_prev;
     43 static matrix_row_t _matrix0[MATRIX_ROWS];
     44 static matrix_row_t _matrix1[MATRIX_ROWS];
     45 
     46 
     47 inline
     48 uint8_t matrix_rows(void)
     49 {
     50     return MATRIX_ROWS;
     51 }
     52 
     53 inline
     54 uint8_t matrix_cols(void)
     55 {
     56     return MATRIX_COLS;
     57 }
     58 
     59 void matrix_init(void)
     60 {
     61 #ifdef DEBUG
     62     debug_enable = true;
     63     debug_keyboard = true;
     64 #endif
     65 
     66     KEY_INIT();
     67 
     68     // initialize matrix state: all keys off
     69     for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix0[i] = 0x00;
     70     for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix1[i] = 0x00;
     71     matrix = _matrix0;
     72     matrix_prev = _matrix1;
     73 
     74     matrix_init_kb();
     75 }
     76 
     77 __attribute__((weak)) void matrix_init_kb(void) { matrix_init_user(); }
     78 
     79 __attribute__((weak)) void matrix_scan_kb(void) { matrix_scan_user(); }
     80 
     81 __attribute__((weak)) void matrix_init_user(void) {}
     82 
     83 __attribute__((weak)) void matrix_scan_user(void) {}
     84 
     85 uint8_t matrix_scan(void)
     86 {
     87     uint8_t *tmp;
     88 
     89     tmp = matrix_prev;
     90     matrix_prev = matrix;
     91     matrix = tmp;
     92 
     93     // power on
     94     if (!KEY_POWER_STATE()) KEY_POWER_ON();
     95     for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
     96         for (uint8_t col = 0; col < MATRIX_COLS; col++) {
     97             KEY_SELECT(row, col);
     98             _delay_us(5);
     99 
    100             // Not sure this is needed. This just emulates HHKB controller's behaviour.
    101             if (matrix_prev[row] & (1<<col)) {
    102                 KEY_PREV_ON();
    103             }
    104             _delay_us(10);
    105 
    106             // NOTE: KEY_STATE is valid only in 20us after KEY_ENABLE.
    107             // If V-USB interrupts in this section we could lose 40us or so
    108             // and would read invalid value from KEY_STATE.
    109             uint8_t last = TIMER_RAW;
    110 
    111             KEY_ENABLE();
    112 
    113             // Wait for KEY_STATE outputs its value.
    114             // 1us was ok on one HHKB, but not worked on another.
    115             // no   wait doesn't work on Teensy++ with pro(1us works)
    116             // no   wait does    work on tmk PCB(8MHz) with pro2
    117             // 1us  wait does    work on both of above
    118             // 1us  wait doesn't work on tmk(16MHz)
    119             // 5us  wait does    work on tmk(16MHz)
    120             // 5us  wait does    work on tmk(16MHz/2)
    121             // 5us  wait does    work on tmk(8MHz)
    122             // 10us wait does    work on Teensy++ with pro
    123             // 10us wait does    work on 328p+iwrap with pro
    124             // 10us wait doesn't work on tmk PCB(8MHz) with pro2(very lagged scan)
    125             _delay_us(5);
    126 
    127             if (KEY_STATE()) {
    128                 matrix[row] &= ~(1<<col);
    129             } else {
    130                 matrix[row] |= (1<<col);
    131             }
    132 
    133             // Ignore if this code region execution time elapses more than 20us.
    134             // MEMO: 20[us] * (TIMER_RAW_FREQ / 1000000)[count per us]
    135             // MEMO: then change above using this rule: a/(b/c) = a*1/(b/c) = a*(c/b)
    136             if (TIMER_DIFF_RAW(TIMER_RAW, last) > 20/(1000000/TIMER_RAW_FREQ)) {
    137                 matrix[row] = matrix_prev[row];
    138             }
    139 
    140             _delay_us(5);
    141             KEY_PREV_OFF();
    142             KEY_UNABLE();
    143 
    144             // NOTE: KEY_STATE keep its state in 20us after KEY_ENABLE.
    145             // This takes 25us or more to make sure KEY_STATE returns to idle state.
    146 
    147             // Looks like JP needs faster scan due to its twice larger matrix
    148             // or it can drop keys in fast key typing
    149             _delay_us(30);
    150         }
    151         if (matrix[row] ^ matrix_prev[row]) matrix_last_modified = timer_read32();
    152     }
    153     // power off
    154     if (KEY_POWER_STATE() &&
    155             (USB_DeviceState == DEVICE_STATE_Suspended ||
    156              USB_DeviceState == DEVICE_STATE_Unattached ) &&
    157             timer_elapsed32(matrix_last_modified) > MATRIX_POWER_SAVE) {
    158         KEY_POWER_OFF();
    159         suspend_power_down();
    160     }
    161 
    162     matrix_scan_kb();
    163 
    164     return 1;
    165 }
    166 
    167 inline
    168 bool matrix_has_ghost(void)
    169 {
    170     return false;
    171 }
    172 
    173 inline
    174 bool matrix_is_on(uint8_t row, uint8_t col)
    175 {
    176     return (matrix[row] & (1<<col));
    177 }
    178 
    179 inline
    180 matrix_row_t matrix_get_row(uint8_t row)
    181 {
    182     return matrix[row];
    183 }
    184 
    185 void matrix_print(void)
    186 {
    187     print("\nr/c 01234567\n");
    188     for (uint8_t row = 0; row < matrix_rows(); row++) {
    189         xprintf("%02X: %08b\n", row, bitrev(matrix_get_row(row)));
    190     }
    191 }
    192 
    193 void matrix_power_up(void) {
    194     KEY_POWER_ON();
    195 }
    196 void matrix_power_down(void) {
    197     KEY_POWER_OFF();
    198 }