Keyboard firmwares for Atmel AVR and Cortex-M
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.

matrix.c 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /* Copyright 2012 Jun Wako <[email protected]>
  2. *
  3. * This is heavily based on phantom/board.{c|h}.
  4. * https://github.com/BathroomEpiphanies/AVR-Keyboard
  5. *
  6. * Copyright (c) 2012 Fredrik Atmer, Bathroom Epiphanies Inc
  7. * http://bathroomepiphanies.com
  8. *
  9. * As for liscensing consult with the original files or its author.
  10. */
  11. #include <stdint.h>
  12. #include <stdbool.h>
  13. #include <avr/io.h>
  14. #include <util/delay.h>
  15. #include "print.h"
  16. #include "debug.h"
  17. #include "util.h"
  18. #include "matrix.h"
  19. #ifndef DEBOUNCE
  20. # define DEBOUNCE 0
  21. #endif
  22. static uint8_t debouncing = DEBOUNCE;
  23. // bit array of key state(1:on, 0:off)
  24. static matrix_row_t *matrix;
  25. static matrix_row_t *matrix_debounced;
  26. static matrix_row_t matrix0[MATRIX_ROWS];
  27. static matrix_row_t matrix1[MATRIX_ROWS];
  28. #define _DDRA (uint8_t *const)&DDRA
  29. #define _DDRB (uint8_t *const)&DDRB
  30. #define _DDRC (uint8_t *const)&DDRC
  31. #define _DDRD (uint8_t *const)&DDRD
  32. #define _DDRE (uint8_t *const)&DDRE
  33. #define _DDRF (uint8_t *const)&DDRF
  34. #define _PINA (uint8_t *const)&PINA
  35. #define _PINB (uint8_t *const)&PINB
  36. #define _PINC (uint8_t *const)&PINC
  37. #define _PIND (uint8_t *const)&PIND
  38. #define _PINE (uint8_t *const)&PINE
  39. #define _PINF (uint8_t *const)&PINF
  40. #define _PORTA (uint8_t *const)&PORTA
  41. #define _PORTB (uint8_t *const)&PORTB
  42. #define _PORTC (uint8_t *const)&PORTC
  43. #define _PORTD (uint8_t *const)&PORTD
  44. #define _PORTE (uint8_t *const)&PORTE
  45. #define _PORTF (uint8_t *const)&PORTF
  46. #define _BIT0 0x01
  47. #define _BIT1 0x02
  48. #define _BIT2 0x04
  49. #define _BIT3 0x08
  50. #define _BIT4 0x10
  51. #define _BIT5 0x20
  52. #define _BIT6 0x40
  53. #define _BIT7 0x80
  54. /* Specifies the ports and pin numbers for the rows */
  55. static
  56. uint8_t *const row_ddr[MATRIX_ROWS] = {_DDRB, _DDRB, _DDRB, _DDRB, _DDRB, _DDRB};
  57. static
  58. uint8_t *const row_port[MATRIX_ROWS] = {_PORTB, _PORTB, _PORTB, _PORTB, _PORTB, _PORTB};
  59. static
  60. uint8_t *const row_pin[MATRIX_ROWS] = {_PINB, _PINB, _PINB, _PINB, _PINB, _PINB};
  61. static
  62. const uint8_t row_bit[MATRIX_ROWS] = { _BIT0, _BIT1, _BIT2, _BIT3, _BIT4, _BIT5};
  63. /* Specifies the ports and pin numbers for the columns */
  64. static
  65. uint8_t *const col_ddr[MATRIX_COLS] = { _DDRD, _DDRC, _DDRC, _DDRD, _DDRD, _DDRE,
  66. _DDRF, _DDRF, _DDRF, _DDRF, _DDRF, _DDRF,
  67. _DDRD, _DDRD, _DDRD, _DDRD, _DDRD};
  68. static
  69. uint8_t *const col_port[MATRIX_COLS] = {_PORTD, _PORTC, _PORTC, _PORTD, _PORTD, _PORTE,
  70. _PORTF, _PORTF, _PORTF, _PORTF, _PORTF, _PORTF,
  71. _PORTD, _PORTD, _PORTD, _PORTD, _PORTD};
  72. static
  73. const uint8_t col_bit[MATRIX_COLS] = { _BIT5, _BIT7, _BIT6, _BIT4, _BIT0, _BIT6,
  74. _BIT0, _BIT1, _BIT4, _BIT5, _BIT6, _BIT7,
  75. _BIT7, _BIT6, _BIT1, _BIT2, _BIT3};
  76. static
  77. inline void pull_column(int col) {
  78. *col_port[col] &= ~col_bit[col];
  79. }
  80. static
  81. inline void release_column(int col) {
  82. *col_port[col] |= col_bit[col];
  83. }
  84. /* PORTB is set as input with pull-up resistors
  85. PORTC,D,E,F are set to high output */
  86. static
  87. void setup_io_pins(void) {
  88. uint8_t row, col;
  89. for(row = 0; row < MATRIX_ROWS; row++) {
  90. *row_ddr[row] &= ~row_bit[row];
  91. *row_port[row] |= row_bit[row];
  92. }
  93. for(col = 0; col < MATRIX_COLS; col++) {
  94. *col_ddr[col] |= col_bit[col];
  95. *col_port[col] |= col_bit[col];
  96. }
  97. }
  98. /* LEDs are on output compare pins OC1B OC1C
  99. This activates fast PWM mode on them.
  100. Prescaler 256 and 8-bit counter results in
  101. 16000000/256/256 = 244 Hz blink frequency.
  102. LED_A: Caps Lock
  103. LED_B: Scroll Lock */
  104. /* Output on PWM pins are turned off when the timer
  105. reaches the value in the output compare register,
  106. and are turned on when it reaches TOP (=256). */
  107. static
  108. void setup_leds(void) {
  109. TCCR1A |= // Timer control register 1A
  110. (1<<WGM10) | // Fast PWM 8-bit
  111. (1<<COM1B1)| // Clear OC1B on match, set at TOP
  112. (1<<COM1C1); // Clear OC1C on match, set at TOP
  113. TCCR1B |= // Timer control register 1B
  114. (1<<WGM12) | // Fast PWM 8-bit
  115. (1<<CS12); // Prescaler 256
  116. OCR1B = 250; // Output compare register 1B
  117. OCR1C = 250; // Output compare register 1C
  118. // LEDs: LED_A -> PORTB6, LED_B -> PORTB7
  119. DDRB &= 0x3F;
  120. PORTB &= 0x3F;
  121. }
  122. inline
  123. uint8_t matrix_rows(void)
  124. {
  125. return MATRIX_ROWS;
  126. }
  127. inline
  128. uint8_t matrix_cols(void)
  129. {
  130. return MATRIX_COLS;
  131. }
  132. void matrix_init(void)
  133. {
  134. // To use PORTF disable JTAG with writing JTD bit twice within four cycles.
  135. MCUCR |= (1<<JTD);
  136. MCUCR |= (1<<JTD);
  137. // initialize row and col
  138. setup_io_pins();
  139. setup_leds();
  140. // initialize matrix state: all keys off
  141. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  142. matrix0[i] = 0;
  143. matrix1[i] = 0;
  144. }
  145. matrix = matrix0;
  146. matrix_debounced = matrix1;
  147. }
  148. uint8_t matrix_scan(void)
  149. {
  150. for (uint8_t col = 0; col < MATRIX_COLS; col++) { // 0-16
  151. pull_column(col); // output hi on theline
  152. _delay_us(3); // without this wait it won't read stable value.
  153. for (uint8_t row = 0; row < MATRIX_ROWS; row++) { // 0-5
  154. bool prev_bit = matrix[row] & ((matrix_row_t)1<<col);
  155. bool curr_bit = !(*row_pin[row] & row_bit[row]);
  156. if (prev_bit != curr_bit) {
  157. matrix[row] ^= ((matrix_row_t)1<<col);
  158. if (debouncing) {
  159. debug("bounce!: "); debug_hex(debouncing); print("\n");
  160. }
  161. debouncing = DEBOUNCE;
  162. }
  163. }
  164. release_column(col);
  165. }
  166. if (debouncing) {
  167. if (--debouncing) {
  168. _delay_ms(1);
  169. } else {
  170. matrix_row_t *tmp = matrix_debounced;
  171. matrix_debounced = matrix;
  172. matrix = tmp;
  173. }
  174. }
  175. return 1;
  176. }
  177. bool matrix_is_modified(void)
  178. {
  179. // NOTE: no longer used
  180. return true;
  181. }
  182. inline
  183. bool matrix_is_on(uint8_t row, uint8_t col)
  184. {
  185. return (matrix_debounced[row] & ((matrix_row_t)1<<col));
  186. }
  187. inline
  188. matrix_row_t matrix_get_row(uint8_t row)
  189. {
  190. return matrix_debounced[row];
  191. }
  192. void matrix_print(void)
  193. {
  194. print("\nr/c 01234567\n");
  195. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  196. phex(row); print(": ");
  197. pbin_reverse(matrix_get_row(row));
  198. print("\n");
  199. }
  200. }
  201. uint8_t matrix_key_count(void)
  202. {
  203. uint8_t count = 0;
  204. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  205. for (uint8_t j = 0; j < MATRIX_COLS; j++) {
  206. if (matrix_is_on(i, j))
  207. count++;
  208. }
  209. }
  210. return count;
  211. }