1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
/*
Copyright 2017 Luiz Ribeiro <luizribeiro@gmail.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <string.h>
#include <stdio.h>
#include "quantum.h"
#include "i2c_master.h"
#include "split78.h"
#define RIGHT_HALF
void matrix_set_row_status(uint8_t row);
#if defined(RIGHT_HALF)
#define I2C_TIMEOUT 10
#define MCP23018_TWI_ADDRESS 0b0100000
#define TW_READ 1
#define TW_WRITE 0
#define TWI_ADDR_WRITE ( (MCP23018_TWI_ADDRESS<<1) | TW_WRITE )
#define TWI_ADDR_READ ( (MCP23018_TWI_ADDRESS<<1) | TW_READ )
#define IODIRA 0x00 // i/o direction register
#define IODIRB 0x01
#define IODIRA 0x00 // i/o direction register
#define IODIRB 0x01
#define GPPUA 0x0C // GPIO pull-up resistor register
#define GPPUB 0x0D
#define GPIOA 0x12 // general purpose i/o port register (write modifies OLAT)
#define GPIOB 0x13
#define OLATA 0x14 // output latch register
#define OLATB 0x15
#define MCP_ROWS_START 8
static uint8_t mcp23018_init(void) {
uint8_t ret;
uint8_t data[3];
// set pin direction
// - unused : input : 1
// - input : input : 1
// - driving : output : 0
data[0] = IODIRA;
data[1] = 0b00000000; // IODIRA
data[2] = (0b11111111); // IODIRB
ret = i2c_transmit(TWI_ADDR_WRITE, (uint8_t *)data, 3, I2C_TIMEOUT);
if (ret) goto out; // make sure we got an ACK
// set pull-up
// - unused : on : 1
// - input : on : 1
// - driving : off : 0
data[0] = GPPUA;
data[1] = 0b00000000; // IODIRA
data[2] = (0b11111111); // IODIRB
ret = i2c_transmit(TWI_ADDR_WRITE, (uint8_t *)data, 3, I2C_TIMEOUT);
if (ret) goto out; // make sure we got an ACK
// set logical value (doesn't matter on inputs)
// - unused : hi-Z : 1
// - input : hi-Z : 1
// - driving : hi-Z : 1
data[0] = OLATA;
data[1] = 0b11111111; // IODIRA
data[2] = (0b11111111); // IODIRB
ret = i2c_transmit(TWI_ADDR_WRITE, (uint8_t *)data, 3, I2C_TIMEOUT);
out:
return ret;
}
#endif
void matrix_init_custom(void) {
// Set rows as output starting high
DDRB = 0xFF;
PORTB = 0xFF;
// Set columns as inputs with pull-up enabled
DDRA = 0x00;
PORTA = 0xFF;
// Initialize i2c communication
i2c_init();
#if defined(RIGHT_HALF)
// Initialize the chip on the other half
mcp23018_init();
#endif
}
static i2c_status_t mcp23018_status = I2C_STATUS_SUCCESS;
bool matrix_scan_custom(matrix_row_t current_matrix[]) {
bool matrix_has_changed = false;
#if defined(RIGHT_HALF)
if (mcp23018_status != I2C_STATUS_SUCCESS) {
mcp23018_status = mcp23018_init();
}
#endif
for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
// Store last value of row prior to reading
matrix_row_t last_row_value = current_matrix[row];
matrix_row_t cols = 0;
// Select the row to scan
matrix_set_row_status(row);
matrix_io_delay();
//Set the local row
#if defined(RIGHT_HALF)
// Initialize to 0x7F in case I2C read fails,
// as 0x75 would be no keys pressed
uint8_t data = 0x7F;
// Receive the columns from right half
if (mcp23018_status == I2C_STATUS_SUCCESS) {
mcp23018_status = i2c_receive(TWI_ADDR_WRITE, &data, 1, I2C_TIMEOUT);
}
#endif
cols |= ((~(PINA | 0x80)) & 0x7F);
#if defined(RIGHT_HALF)
cols |= (((~(data | 0x80)) & 0x7F) << 7);
#endif
current_matrix[row] = cols;
matrix_has_changed |= (last_row_value != current_matrix[row]);
}
return matrix_has_changed;
}
void matrix_set_row_status(uint8_t row) {
#if defined(RIGHT_HALF)
uint8_t txdata[3];
//Set the remote row on port A
txdata[0] = (GPIOA);
txdata[1] = ( 0xFF & ~(1<<row) );
if (mcp23018_status == I2C_STATUS_SUCCESS) {
mcp23018_status = i2c_transmit(TWI_ADDR_WRITE, (uint8_t *)txdata, 2, I2C_TIMEOUT);
}
#endif
//Set the local row on port B
DDRB = (1 << row);
PORTB = ~(1 << row);
}
|