You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

157 lines
4.7 KiB

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