1 /* Copyright 2012 Jun Wako <wakojun@gmail.com>
3 * This is heavily based on phantom/board.{c|h}.
4 * https://github.com/BathroomEpiphanies/AVR-Keyboard
6 * Copyright (c) 2012 Fredrik Atmer, Bathroom Epiphanies Inc
7 * http://bathroomepiphanies.com
9 * As for liscensing consult with the original files or its author.
14 #include <util/delay.h>
24 static uint8_t debouncing = DEBOUNCE;
26 // bit array of key state(1:on, 0:off)
27 static matrix_row_t matrix[MATRIX_ROWS];
28 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
31 #define _DDRA (uint8_t *const)&DDRA
32 #define _DDRB (uint8_t *const)&DDRB
33 #define _DDRC (uint8_t *const)&DDRC
34 #define _DDRD (uint8_t *const)&DDRD
35 #define _DDRE (uint8_t *const)&DDRE
36 #define _DDRF (uint8_t *const)&DDRF
38 #define _PINA (uint8_t *const)&PINA
39 #define _PINB (uint8_t *const)&PINB
40 #define _PINC (uint8_t *const)&PINC
41 #define _PIND (uint8_t *const)&PIND
42 #define _PINE (uint8_t *const)&PINE
43 #define _PINF (uint8_t *const)&PINF
45 #define _PORTA (uint8_t *const)&PORTA
46 #define _PORTB (uint8_t *const)&PORTB
47 #define _PORTC (uint8_t *const)&PORTC
48 #define _PORTD (uint8_t *const)&PORTD
49 #define _PORTE (uint8_t *const)&PORTE
50 #define _PORTF (uint8_t *const)&PORTF
61 /* Specifies the ports and pin numbers for the rows */
63 uint8_t *const row_ddr[MATRIX_ROWS] = {_DDRB, _DDRB, _DDRB, _DDRB, _DDRB, _DDRB};
66 uint8_t *const row_port[MATRIX_ROWS] = {_PORTB, _PORTB, _PORTB, _PORTB, _PORTB, _PORTB};
69 uint8_t *const row_pin[MATRIX_ROWS] = {_PINB, _PINB, _PINB, _PINB, _PINB, _PINB};
72 const uint8_t row_bit[MATRIX_ROWS] = { _BIT0, _BIT1, _BIT2, _BIT3, _BIT4, _BIT5};
74 /* Specifies the ports and pin numbers for the columns */
76 uint8_t *const col_ddr[MATRIX_COLS] = { _DDRD, _DDRC, _DDRC, _DDRD, _DDRD, _DDRE,
77 _DDRF, _DDRF, _DDRF, _DDRF, _DDRF, _DDRF,
78 _DDRD, _DDRD, _DDRD, _DDRD, _DDRD};
81 uint8_t *const col_port[MATRIX_COLS] = {_PORTD, _PORTC, _PORTC, _PORTD, _PORTD, _PORTE,
82 _PORTF, _PORTF, _PORTF, _PORTF, _PORTF, _PORTF,
83 _PORTD, _PORTD, _PORTD, _PORTD, _PORTD};
86 const uint8_t col_bit[MATRIX_COLS] = { _BIT5, _BIT7, _BIT6, _BIT4, _BIT0, _BIT6,
87 _BIT0, _BIT1, _BIT4, _BIT5, _BIT6, _BIT7,
88 _BIT7, _BIT6, _BIT1, _BIT2, _BIT3};
91 inline void pull_column(int col) {
92 *col_port[col] &= ~col_bit[col];
96 inline void release_column(int col) {
97 *col_port[col] |= col_bit[col];
100 /* PORTB is set as input with pull-up resistors
101 PORTC,D,E,F are set to high output */
104 void setup_io_pins(void) {
106 for(row = 0; row < MATRIX_ROWS; row++) {
107 *row_ddr[row] &= ~row_bit[row];
108 *row_port[row] |= row_bit[row];
110 for(col = 0; col < MATRIX_COLS; col++) {
111 *col_ddr[col] |= col_bit[col];
112 *col_port[col] |= col_bit[col];
116 /* LEDs are on output compare pins OC1B OC1C
117 This activates fast PWM mode on them.
118 Prescaler 256 and 8-bit counter results in
119 16000000/256/256 = 244 Hz blink frequency.
121 LED_B: Scroll Lock */
122 /* Output on PWM pins are turned off when the timer
123 reaches the value in the output compare register,
124 and are turned on when it reaches TOP (=256). */
126 void setup_leds(void) {
127 TCCR1A |= // Timer control register 1A
128 (1<<WGM10) | // Fast PWM 8-bit
129 (1<<COM1B1)| // Clear OC1B on match, set at TOP
130 (1<<COM1C1); // Clear OC1C on match, set at TOP
131 TCCR1B |= // Timer control register 1B
132 (1<<WGM12) | // Fast PWM 8-bit
133 (1<<CS12); // Prescaler 256
134 OCR1B = 250; // Output compare register 1B
135 OCR1C = 250; // Output compare register 1C
136 // LEDs: LED_A -> PORTB6, LED_B -> PORTB7
143 uint8_t matrix_rows(void)
149 uint8_t matrix_cols(void)
154 void matrix_init(void)
156 // To use PORTF disable JTAG with writing JTD bit twice within four cycles.
160 // initialize row and col
164 // initialize matrix state: all keys off
165 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
167 matrix_debouncing[i] = 0;
171 uint8_t matrix_scan(void)
173 for (uint8_t col = 0; col < MATRIX_COLS; col++) { // 0-16
174 pull_column(col); // output hi on theline
175 _delay_us(3); // without this wait it won't read stable value.
176 for (uint8_t row = 0; row < MATRIX_ROWS; row++) { // 0-5
177 bool prev_bit = matrix_debouncing[row] & ((matrix_row_t)1<<col);
178 bool curr_bit = !(*row_pin[row] & row_bit[row]);
179 if (prev_bit != curr_bit) {
180 matrix_debouncing[row] ^= ((matrix_row_t)1<<col);
182 debug("bounce!: "); debug_hex(debouncing); print("\n");
184 debouncing = DEBOUNCE;
194 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
195 matrix[i] = matrix_debouncing[i];
203 bool matrix_is_modified(void)
205 // NOTE: no longer used
210 bool matrix_is_on(uint8_t row, uint8_t col)
212 return (matrix[row] & ((matrix_row_t)1<<col));
216 matrix_row_t matrix_get_row(uint8_t row)
221 void matrix_print(void)
223 print("\nr/c 01234567\n");
224 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
225 phex(row); print(": ");
226 pbin_reverse(matrix_get_row(row));
231 uint8_t matrix_key_count(void)
234 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
235 for (uint8_t j = 0; j < MATRIX_COLS; j++) {
236 if (matrix_is_on(i, j))