Keyboard firmwares for Atmel AVR and Cortex-M
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

hhkb_avr.h 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. #ifdef HHKB_POWER_SAVING
  38. static inline void KEY_POWER_ON(void) {
  39. _delay_ms(10); // TODO: sleep to save power
  40. DDRB = 0xFF; PORTB = 0x40; // change pins output
  41. DDRD |= (1<<4); PORTD |= (1<<4); // MOS FET switch on
  42. /* Without this wait you will miss or get false key events. */
  43. _delay_ms(1); // wait for powering up
  44. }
  45. static inline void KEY_POWER_OFF(void) {
  46. /* input with pull-up consumes less than without it when pin is open. */
  47. DDRB = 0x00; PORTB = 0xFF; // change pins input with pull-up
  48. DDRD |= (1<<4); PORTD &= ~(1<<4); // MOS FET switch off
  49. }
  50. #else
  51. static inline void KEY_POWER_ON(void) {}
  52. static inline void KEY_POWER_OFF(void) {}
  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. #ifdef HHKB_JP
  63. /* row extention for HHKB JP */
  64. DDRC |= (1<<6|1<<7);
  65. PORTC |= (1<<6|1<<7);
  66. #endif
  67. KEY_UNABLE();
  68. KEY_PREV_OFF();
  69. KEY_POWER_OFF();
  70. }
  71. static inline void KEY_SELECT(uint8_t ROW, uint8_t COL)
  72. {
  73. PORTB = (PORTB & 0xC0) | (((COL) & 0x07)<<3) | ((ROW) & 0x07);
  74. #ifdef HHKB_JP
  75. if ((ROW) & 0x08) PORTC = (PORTC & ~(1<<6|1<<7)) | (1<<6);
  76. else PORTC = (PORTC & ~(1<<6|1<<7)) | (1<<7);
  77. #endif
  78. }
  79. #elif defined(__AVR_AT90USB1286__)
  80. /*
  81. * For Teensy++(AT90USB1286)
  82. *
  83. * HHKB pro HHKB pro2
  84. * row: PB0-2 (6-8) (5-7)
  85. * col: PB3-5,6 (9-12) (8-11)
  86. * key: PE6(pull-uped) (4) (3)
  87. * prev: PE7 (5) (4)
  88. *
  89. * TODO: convert into 'staitc inline' function
  90. */
  91. #define KEY_INIT() do { \
  92. DDRB |= 0x7F; \
  93. DDRE |= (1<<7); \
  94. DDRE &= ~(1<<6); \
  95. PORTE |= (1<<6); \
  96. } while (0)
  97. #define KEY_SELECT(ROW, COL) (PORTB = (PORTB & 0xC0) | \
  98. (((COL) & 0x07)<<3) | \
  99. ((ROW) & 0x07))
  100. #define KEY_ENABLE() (PORTB &= ~(1<<6))
  101. #define KEY_UNABLE() (PORTB |= (1<<6))
  102. #define KEY_STATE() (PINE & (1<<6))
  103. #define KEY_PREV_ON() (PORTE |= (1<<7))
  104. #define KEY_PREV_OFF() (PORTE &= ~(1<<7))
  105. #define KEY_POWER_ON()
  106. #define KEY_POWER_OFF()
  107. #else
  108. # error "define code for matrix scan"
  109. #endif
  110. #if 0
  111. // For ATMega328P with V-USB
  112. //
  113. // #elif defined(__AVR_ATmega328P__)
  114. // Ports for V-USB
  115. // key: PB0(pull-uped)
  116. // prev: PB1
  117. // row: PB2-4
  118. // col: PC0-2,3
  119. // power: PB5(Low:on/Hi-z:off)
  120. #define KEY_INIT() do { \
  121. DDRB |= 0x3E; \
  122. DDRB &= ~(1<<0); \
  123. PORTB |= 1<<0; \
  124. DDRC |= 0x0F; \
  125. KEY_UNABLE(); \
  126. KEY_PREV_OFF(); \
  127. } while (0)
  128. #define KEY_SELECT(ROW, COL) do { \
  129. PORTB = (PORTB & 0xE3) | ((ROW) & 0x07)<<2; \
  130. PORTC = (PORTC & 0xF8) | ((COL) & 0x07); \
  131. } while (0)
  132. #define KEY_ENABLE() (PORTC &= ~(1<<3))
  133. #define KEY_UNABLE() (PORTC |= (1<<3))
  134. #define KEY_STATE() (PINB & (1<<0))
  135. #define KEY_PREV_ON() (PORTB |= (1<<1))
  136. #define KEY_PREV_OFF() (PORTB &= ~(1<<1))
  137. // Power supply switching
  138. #define KEY_POWER_ON() do { \
  139. KEY_INIT(); \
  140. PORTB &= ~(1<<5); \
  141. _delay_ms(1); \
  142. } while (0)
  143. #define KEY_POWER_OFF() do { \
  144. DDRB &= ~0x3F; \
  145. PORTB &= ~0x3F; \
  146. DDRC &= ~0x0F; \
  147. PORTC &= ~0x0F; \
  148. } while (0)
  149. #endif
  150. #endif