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.

matrix.c 6.1KB

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