qmk_firmware

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

hhkb_avr.h (6524B)


      1 #pragma once
      2 
      3 #include <stdint.h>
      4 #include <stdbool.h>
      5 #include <avr/io.h>
      6 #include <avr/interrupt.h>
      7 #include <util/delay.h>
      8 
      9 
     10 // Timer resolution check
     11 #if (1000000/TIMER_RAW_FREQ > 20)
     12 #   error "Timer resolution(>20us) is not enough for HHKB matrix scan tweak on V-USB."
     13 #endif
     14 
     15 
     16 /*
     17  * HHKB Matrix I/O
     18  *
     19  * row:     HC4051[A,B,C]  selects scan row0-7
     20  * row-ext: [En0,En1] row extention for JP
     21  * col:     LS145[A,B,C,D] selects scan col0-7 and enable(D)
     22  * key:     on: 0/off: 1
     23  * prev:    hysteresis control: assert(1) when previous key state is on
     24  */
     25 
     26 
     27 #if defined(__AVR_ATmega32U4__)
     28 /*
     29  * For TMK HHKB alt controller(ATMega32U4)
     30  *
     31  * row:     PB0-2
     32  * col:     PB3-5,6
     33  * key:     PD7(pull-uped)
     34  * prev:    PB7
     35  * power:   PD4(L:off/H:on)
     36  * row-ext: PC6,7 for HHKB JP(active low)
     37  */
     38 static inline void KEY_ENABLE(void) { (PORTB &= ~(1<<6)); }
     39 static inline void KEY_UNABLE(void) { (PORTB |=  (1<<6)); }
     40 static inline bool KEY_STATE(void) { return (PIND & (1<<7)); }
     41 static inline void KEY_PREV_ON(void) { (PORTB |=  (1<<7)); }
     42 static inline void KEY_PREV_OFF(void) { (PORTB &= ~(1<<7)); }
     43 #ifdef HHKB_POWER_SAVING
     44 static inline void KEY_POWER_ON(void) {
     45     DDRB = 0xFF; PORTB = 0x40;          // change pins output
     46     DDRD |= (1<<4); PORTD |= (1<<4);    // MOS FET switch on
     47     /* Without this wait you will miss or get false key events. */
     48     _delay_ms(5);                       // wait for powering up
     49 }
     50 static inline void KEY_POWER_OFF(void) {
     51     /* input with pull-up consumes less than without it when pin is open. */
     52     DDRB = 0x00; PORTB = 0xFF;          // change pins input with pull-up
     53     DDRD |= (1<<4); PORTD &= ~(1<<4);   // MOS FET switch off
     54 }
     55 static inline bool KEY_POWER_STATE(void) { return PORTD & (1<<4); }
     56 #else
     57 static inline void KEY_POWER_ON(void) {}
     58 static inline void KEY_POWER_OFF(void) {}
     59 static inline bool KEY_POWER_STATE(void) { return true; }
     60 #endif
     61 static inline void KEY_INIT(void)
     62 {
     63     /* row,col,prev: output */
     64     DDRB  = 0xFF;
     65     PORTB = 0x40;   // unable
     66     /* key: input with pull-up */
     67     DDRD  &= ~0x80;
     68     PORTD |=  0x80;
     69 
     70     KEY_UNABLE();
     71     KEY_PREV_OFF();
     72 
     73     KEY_POWER_OFF();
     74 }
     75 static inline void KEY_SELECT(uint8_t ROW, uint8_t COL)
     76 {
     77     PORTB = (PORTB & 0xC0) | (((COL) & 0x07)<<3) | ((ROW) & 0x07);
     78 
     79 }
     80 
     81 #elif defined(__AVR_ATmega32U2__)
     82 /*
     83  * For TMK HHKB alt controller(ATMega32U4)
     84  *
     85  * row:     PB0-2
     86  * col:     PB3-5,6
     87  * key:     PD3(pull-uped)
     88  * prev:    PB7
     89  * power:   PD4(L:off/H:on)
     90  * row-ext: PC6,7 for HHKB JP(active low)
     91  */
     92 static inline void KEY_ENABLE(void) { (PORTB &= ~(1<<6)); }
     93 static inline void KEY_UNABLE(void) { (PORTB |=  (1<<6)); }
     94 static inline bool KEY_STATE(void) { return (PIND & (1<<3)); }
     95 static inline void KEY_PREV_ON(void) { (PORTB |=  (1<<7)); }
     96 static inline void KEY_PREV_OFF(void) { (PORTB &= ~(1<<7)); }
     97 #ifdef HHKB_POWER_SAVING
     98 static inline void KEY_POWER_ON(void) {
     99     DDRB = 0xFF; PORTB = 0x40;          // change pins output
    100     DDRD |= (1<<4); PORTD |= (1<<4);    // MOS FET switch on
    101     /* Without this wait you will miss or get false key events. */
    102     _delay_ms(5);                       // wait for powering up
    103 }
    104 static inline void KEY_POWER_OFF(void) {
    105     /* input with pull-up consumes less than without it when pin is open. */
    106     DDRB = 0x00; PORTB = 0xFF;          // change pins input with pull-up
    107     DDRD |= (1<<4); PORTD &= ~(1<<4);   // MOS FET switch off
    108 }
    109 static inline bool KEY_POWER_STATE(void) { return PORTD & (1<<4); }
    110 #else
    111 static inline void KEY_POWER_ON(void) {}
    112 static inline void KEY_POWER_OFF(void) {}
    113 static inline bool KEY_POWER_STATE(void) { return true; }
    114 #endif
    115 static inline void KEY_INIT(void)
    116 {
    117     /* row,col,prev: output */
    118     DDRB  = 0xFF;
    119     PORTB = 0x40;   // unable
    120     /* key: input with pull-up */
    121     DDRD  &= ~0x80;
    122     PORTD |=  0x80;
    123 
    124     KEY_UNABLE();
    125     KEY_PREV_OFF();
    126 
    127     KEY_POWER_OFF();
    128 }
    129 static inline void KEY_SELECT(uint8_t ROW, uint8_t COL)
    130 {
    131     PORTB = (PORTB & 0xC0) | (((COL) & 0x07)<<3) | ((ROW) & 0x07);
    132 
    133 }
    134 
    135 
    136 #elif defined(__AVR_AT90USB1286__)
    137 /*
    138  * For Teensy++(AT90USB1286)
    139  *
    140  *                          HHKB pro    HHKB pro2
    141  * row:     PB0-2           (6-8)       (5-7)
    142  * col:     PB3-5,6         (9-12)      (8-11)
    143  * key:     PE6(pull-uped)  (4)         (3)
    144  * prev:    PE7             (5)         (4)
    145  *
    146  * TODO: convert into 'staitc inline' function
    147  */
    148 #define KEY_INIT()              do {    \
    149     DDRB |= 0x7F;                       \
    150     DDRE |=  (1<<7);                    \
    151     DDRE &= ~(1<<6);                    \
    152     PORTE |= (1<<6);                    \
    153 } while (0)
    154 #define KEY_SELECT(ROW, COL)    (PORTB = (PORTB & 0xC0) |       \
    155                                          (((COL) & 0x07)<<3) |  \
    156                                          ((ROW) & 0x07))
    157 #define KEY_ENABLE()            (PORTB &= ~(1<<6))
    158 #define KEY_UNABLE()            (PORTB |=  (1<<6))
    159 #define KEY_STATE()             (PINE & (1<<6))
    160 #define KEY_PREV_ON()           (PORTE |=  (1<<7))
    161 #define KEY_PREV_OFF()          (PORTE &= ~(1<<7))
    162 #define KEY_POWER_ON()
    163 #define KEY_POWER_OFF()
    164 #define KEY_POWER_STATE()       true
    165 
    166 
    167 #else
    168 #   error "define code for matrix scan"
    169 #endif
    170 
    171 
    172 #if 0
    173 // For ATMega328P with V-USB
    174 //
    175 // #elif defined(__AVR_ATmega328P__)
    176 // Ports for V-USB
    177 // key:     PB0(pull-uped)
    178 // prev:    PB1
    179 // row:     PB2-4
    180 // col:     PC0-2,3
    181 // power:   PB5(Low:on/Hi-z:off)
    182 #define KEY_INIT()              do {    \
    183     DDRB  |= 0x3E;                      \
    184     DDRB  &= ~(1<<0);                   \
    185     PORTB |= 1<<0;                      \
    186     DDRC  |= 0x0F;                      \
    187     KEY_UNABLE();                       \
    188     KEY_PREV_OFF();                     \
    189 } while (0)
    190 #define KEY_SELECT(ROW, COL)    do {    \
    191     PORTB = (PORTB & 0xE3) | ((ROW) & 0x07)<<2; \
    192     PORTC = (PORTC & 0xF8) | ((COL) & 0x07);    \
    193 } while (0)
    194 #define KEY_ENABLE()            (PORTC &= ~(1<<3))
    195 #define KEY_UNABLE()            (PORTC |=  (1<<3))
    196 #define KEY_STATE()             (PINB & (1<<0))
    197 #define KEY_PREV_ON()           (PORTB |=  (1<<1))
    198 #define KEY_PREV_OFF()          (PORTB &= ~(1<<1))
    199 // Power supply switching
    200 #define KEY_POWER_ON()          do {    \
    201     KEY_INIT();                         \
    202     PORTB &= ~(1<<5);                   \
    203     _delay_ms(1);                       \
    204 } while (0)
    205 #define KEY_POWER_OFF()         do {    \
    206     DDRB  &= ~0x3F;                     \
    207     PORTB &= ~0x3F;                     \
    208     DDRC  &= ~0x0F;                     \
    209     PORTC &= ~0x0F;                     \
    210 } while (0)
    211 #endif