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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

hhkb_avr.h 4.1KB

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