aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoel Challis <git@zvecr.com>2021-09-07 16:35:13 +0100
committerGitHub <noreply@github.com>2021-09-07 16:35:13 +0100
commit04c0704b280c4847c43b164335e9741b19219131 (patch)
tree540b9c98899afb568644ddfcef04bc74669be4e3
parente7a5c006d9777a4009da934f408961aa2d2e6fb1 (diff)
downloadqmk_firmware-04c0704b280c4847c43b164335e9741b19219131.tar.gz
qmk_firmware-04c0704b280c4847c43b164335e9741b19219131.zip
3w6 - Refactor use of AVR only I2C functions (#14339)
* Refactor use of legacy i2c functions * Align rev2 * Review fixes
-rw-r--r--keyboards/3w6/rev1/matrix.c88
-rw-r--r--keyboards/3w6/rev2/matrix.c81
2 files changed, 64 insertions, 105 deletions
diff --git a/keyboards/3w6/rev1/matrix.c b/keyboards/3w6/rev1/matrix.c
index 7262fd22e..af3d21067 100644
--- a/keyboards/3w6/rev1/matrix.c
+++ b/keyboards/3w6/rev1/matrix.c
@@ -35,8 +35,6 @@ extern i2c_status_t tca9555_status;
35// | 0 | 1 | 0 | 0 | A2 | A1 | A0 | 35// | 0 | 1 | 0 | 0 | A2 | A1 | A0 |
36// | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 36// | 0 | 1 | 0 | 0 | 0 | 0 | 0 |
37#define I2C_ADDR 0b0100000 37#define I2C_ADDR 0b0100000
38#define I2C_ADDR_WRITE ((I2C_ADDR << 1) | I2C_WRITE)
39#define I2C_ADDR_READ ((I2C_ADDR << 1) | I2C_READ)
40 38
41// Register addresses 39// Register addresses
42#define IODIRA 0x06 // i/o direction register 40#define IODIRA 0x06 // i/o direction register
@@ -64,19 +62,14 @@ uint8_t init_tca9555(void) {
64 // - unused : input : 1 62 // - unused : input : 1
65 // - input : input : 1 63 // - input : input : 1
66 // - driving : output : 0 64 // - driving : output : 0
67 tca9555_status = i2c_start(I2C_ADDR_WRITE, I2C_TIMEOUT); 65 uint8_t conf[2] = {
68 if (tca9555_status) goto out; 66 // This means: write on pin 5 of port 0, read on rest
69 tca9555_status = i2c_write(IODIRA, I2C_TIMEOUT); 67 0b11011111,
70 if (tca9555_status) goto out; 68 // This means: we will write on pins 0 to 2 on port 1. read rest
71 // This means: write on pin 5 of port 0, read on rest 69 0b11111000,
72 tca9555_status = i2c_write(0b11011111, I2C_TIMEOUT); 70 };
73 if (tca9555_status) goto out; 71 tca9555_status = i2c_writeReg(I2C_ADDR, IODIRA, conf, 2, I2C_TIMEOUT);
74 // This means: we will write on pins 0 to 2 on port 1. read rest 72
75 tca9555_status = i2c_write(0b11111000, I2C_TIMEOUT);
76 if (tca9555_status) goto out;
77
78out:
79 i2c_stop();
80 return tca9555_status; 73 return tca9555_status;
81} 74}
82 75
@@ -192,36 +185,29 @@ static matrix_row_t read_cols(uint8_t row) {
192 if (tca9555_status) { // if there was an error 185 if (tca9555_status) { // if there was an error
193 return 0; 186 return 0;
194 } else { 187 } else {
195 uint8_t data = 0; 188 uint8_t data = 0;
196 uint8_t port0 = 0; 189 uint8_t ports[2] = {0};
197 uint8_t port1 = 0; 190 tca9555_status = i2c_readReg(I2C_ADDR, IREGP0, ports, 2, I2C_TIMEOUT);
198 tca9555_status = i2c_start(I2C_ADDR_WRITE, I2C_TIMEOUT); 191 if (tca9555_status) { // if there was an error
199 if (tca9555_status) goto out; 192 // do nothing
200 tca9555_status = i2c_write(IREGP0, I2C_TIMEOUT); 193 return 0;
201 if (tca9555_status) goto out; 194 } else {
202 tca9555_status = i2c_start(I2C_ADDR_READ, I2C_TIMEOUT); 195 uint8_t port0 = ports[0];
203 if (tca9555_status) goto out; 196 uint8_t port1 = ports[1];
204 tca9555_status = i2c_read_ack(I2C_TIMEOUT); 197
205 if (tca9555_status < 0) goto out; 198 // The initial state was all ones and any depressed key at a given column for the currently selected row will have its bit flipped to zero.
206 port0 = (uint8_t)tca9555_status; 199 // The return value is a row as represented in the generic matrix code were the rightmost bits represent the lower columns and zeroes represent non-depressed keys while ones represent depressed keys.
207 tca9555_status = i2c_read_nack(I2C_TIMEOUT); 200 // Since the pins are not ordered sequentially, we have to build the correct dataset from the two ports. Refer to the schematic to see where every pin is connected.
208 if (tca9555_status < 0) goto out; 201 data |= ( port0 & 0x01 );
209 port1 = (uint8_t)tca9555_status; 202 data |= ( port0 & 0x02 );
210 203 data |= ( port1 & 0x10 ) >> 2;
211 // The initial state was all ones and any depressed key at a given column for the currently selected row will have its bit flipped to zero. 204 data |= ( port1 & 0x08 );
212 // The return value is a row as represented in the generic matrix code were the rightmost bits represent the lower columns and zeroes represent non-depressed keys while ones represent depressed keys. 205 data |= ( port0 & 0x40 ) >> 2;
213 // Since the pins are not ordered sequentially, we have to build the correct dataset from the two ports. Refer to the schematic to see where every pin is connected. 206 data = ~(data);
214 data |= ( port0 & 0x01 ); 207
215 data |= ( port0 & 0x02 ); 208 tca9555_status = I2C_STATUS_SUCCESS;
216 data |= ( port1 & 0x10 ) >> 2; 209 return data;
217 data |= ( port1 & 0x08 ); 210 }
218 data |= ( port0 & 0x40 ) >> 2;
219 data = ~(data);
220
221 tca9555_status = I2C_STATUS_SUCCESS;
222 out:
223 i2c_stop();
224 return data;
225 } 211 }
226 } 212 }
227} 213}
@@ -263,18 +249,10 @@ static void select_row(uint8_t row) {
263 default: break; 249 default: break;
264 } 250 }
265 251
266 tca9555_status = i2c_start(I2C_ADDR_WRITE, I2C_TIMEOUT); 252 uint8_t ports[2] = {port0, port1};
267 if (tca9555_status) goto out; 253 tca9555_status = i2c_writeReg(I2C_ADDR, OREGP0, ports, 2, I2C_TIMEOUT);
268 tca9555_status = i2c_write(OREGP0, I2C_TIMEOUT);
269 if (tca9555_status) goto out;
270 tca9555_status = i2c_write(port0, I2C_TIMEOUT);
271 if (tca9555_status) goto out;
272 tca9555_status = i2c_write(port1, I2C_TIMEOUT);
273 if (tca9555_status) goto out;
274 // Select the desired row by writing a byte for the entire GPIOB bus where only the bit representing the row we want to select is a zero (write instruction) and every other bit is a one. 254 // Select the desired row by writing a byte for the entire GPIOB bus where only the bit representing the row we want to select is a zero (write instruction) and every other bit is a one.
275 // Note that the row - MATRIX_ROWS_PER_SIDE reflects the fact that being on the right hand, the columns are numbered from MATRIX_ROWS_PER_SIDE to MATRIX_ROWS, but the pins we want to write to are indexed from zero up on the GPIOB bus. 255 // Note that the row - MATRIX_ROWS_PER_SIDE reflects the fact that being on the right hand, the columns are numbered from MATRIX_ROWS_PER_SIDE to MATRIX_ROWS, but the pins we want to write to are indexed from zero up on the GPIOB bus.
276 out:
277 i2c_stop();
278 } 256 }
279 } 257 }
280} 258}
diff --git a/keyboards/3w6/rev2/matrix.c b/keyboards/3w6/rev2/matrix.c
index 5bc967bed..4df161b89 100644
--- a/keyboards/3w6/rev2/matrix.c
+++ b/keyboards/3w6/rev2/matrix.c
@@ -35,8 +35,6 @@ extern i2c_status_t tca9555_status;
35// | 0 | 1 | 0 | 0 | A2 | A1 | A0 | 35// | 0 | 1 | 0 | 0 | A2 | A1 | A0 |
36// | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 36// | 0 | 1 | 0 | 0 | 0 | 0 | 0 |
37#define I2C_ADDR 0b0100000 37#define I2C_ADDR 0b0100000
38#define I2C_ADDR_WRITE ((I2C_ADDR << 1) | I2C_WRITE)
39#define I2C_ADDR_READ ((I2C_ADDR << 1) | I2C_READ)
40 38
41// Register addresses 39// Register addresses
42#define IODIRA 0x06 // i/o direction register 40#define IODIRA 0x06 // i/o direction register
@@ -64,19 +62,14 @@ uint8_t init_tca9555(void) {
64 // - unused : input : 1 62 // - unused : input : 1
65 // - input : input : 1 63 // - input : input : 1
66 // - driving : output : 0 64 // - driving : output : 0
67 tca9555_status = i2c_start(I2C_ADDR_WRITE, I2C_TIMEOUT); 65 uint8_t conf[2] = {
68 if (tca9555_status) goto out; 66 // This means: read all pins of port 0
69 tca9555_status = i2c_write(IODIRA, I2C_TIMEOUT); 67 0b11111111,
70 if (tca9555_status) goto out; 68 // This means: we will write on pins 0 to 3 on port 1. read rest
71 // This means: read all pins of port 0 69 0b11110000,
72 tca9555_status = i2c_write(0b11111111, I2C_TIMEOUT); 70 };
73 if (tca9555_status) goto out; 71 tca9555_status = i2c_writeReg(I2C_ADDR, IODIRA, conf, 2, I2C_TIMEOUT);
74 // This means: we will write on pins 0 to 3 on port 1. read rest 72
75 tca9555_status = i2c_write(0b11110000, I2C_TIMEOUT);
76 if (tca9555_status) goto out;
77
78out:
79 i2c_stop();
80 return tca9555_status; 73 return tca9555_status;
81} 74}
82 75
@@ -194,32 +187,27 @@ static matrix_row_t read_cols(uint8_t row) {
194 } else { 187 } else {
195 uint8_t data = 0; 188 uint8_t data = 0;
196 uint8_t port0 = 0; 189 uint8_t port0 = 0;
197 tca9555_status = i2c_start(I2C_ADDR_WRITE, I2C_TIMEOUT); 190 tca9555_status = i2c_readReg(I2C_ADDR, IREGP0, port0, 1, I2C_TIMEOUT);
198 if (tca9555_status) goto out; 191 if (tca9555_status) { // if there was an error
199 tca9555_status = i2c_write(IREGP0, I2C_TIMEOUT); 192 // do nothing
200 if (tca9555_status) goto out; 193 return 0;
201 tca9555_status = i2c_start(I2C_ADDR_READ, I2C_TIMEOUT); 194 } else {
202 if (tca9555_status) goto out; 195 uint8_t port0 = ports[0];
203 tca9555_status = i2c_read_nack(I2C_TIMEOUT); 196 uint8_t port1 = ports[1];
204 if (tca9555_status < 0) goto out; 197
205 198 // We read all the pins on GPIOA.
206 port0 = ~(uint8_t)tca9555_status; 199 // The initial state was all ones and any depressed key at a given column for the currently selected row will have its bit flipped to zero.
207 200 // The return value is a row as represented in the generic matrix code were the rightmost bits represent the lower columns and zeroes represent non-depressed keys while ones represent depressed keys.
208 // We read all the pins on GPIOA. 201 // the pins connected to eact columns are sequential, but in reverse order, and counting from zero down (col 5 -> GPIO04, col6 -> GPIO03 and so on).
209 // The initial state was all ones and any depressed key at a given column for the currently selected row will have its bit flipped to zero. 202 data |= ( port0 & 0x01 ) << 4;
210 // The return value is a row as represented in the generic matrix code were the rightmost bits represent the lower columns and zeroes represent non-depressed keys while ones represent depressed keys. 203 data |= ( port0 & 0x02 ) << 2;
211 // the pins connected to eact columns are sequential, but in reverse order, and counting from zero down (col 5 -> GPIO04, col6 -> GPIO03 and so on). 204 data |= ( port0 & 0x04 );
212 data |= ( port0 & 0x01 ) << 4; 205 data |= ( port0 & 0x08 ) >> 2;
213 data |= ( port0 & 0x02 ) << 2; 206 data |= ( port0 & 0x10 ) >> 4;
214 data |= ( port0 & 0x04 ); 207
215 data |= ( port0 & 0x08 ) >> 2; 208 tca9555_status = I2C_STATUS_SUCCESS;
216 data |= ( port0 & 0x10 ) >> 4; 209 return data;
217 210 }
218 tca9555_status = I2C_STATUS_SUCCESS;
219 out:
220 i2c_stop();
221
222 return data;
223 } 211 }
224 } 212 }
225} 213}
@@ -256,20 +244,13 @@ static void select_row(uint8_t row) {
256 case 4: port1 &= ~(1 << 0); break; 244 case 4: port1 &= ~(1 << 0); break;
257 case 5: port1 &= ~(1 << 1); break; 245 case 5: port1 &= ~(1 << 1); break;
258 case 6: port1 &= ~(1 << 2); break; 246 case 6: port1 &= ~(1 << 2); break;
259 case 7: port1 &= ~(1 << 3); break; 247 case 7: port0 &= ~(1 << 5); break;
260 default: break; 248 default: break;
261 } 249 }
262 250
251 tca9555_status = i2c_writeReg(I2C_ADDR, OREGP1, port1, 2, I2C_TIMEOUT);
263 // Select the desired row by writing a byte for the entire GPIOB bus where only the bit representing the row we want to select is a zero (write instruction) and every other bit is a one. 252 // Select the desired row by writing a byte for the entire GPIOB bus where only the bit representing the row we want to select is a zero (write instruction) and every other bit is a one.
264 // Note that the row - MATRIX_ROWS_PER_SIDE reflects the fact that being on the right hand, the columns are numbered from MATRIX_ROWS_PER_SIDE to MATRIX_ROWS, but the pins we want to write to are indexed from zero up on the GPIOB bus. 253 // Note that the row - MATRIX_ROWS_PER_SIDE reflects the fact that being on the right hand, the columns are numbered from MATRIX_ROWS_PER_SIDE to MATRIX_ROWS, but the pins we want to write to are indexed from zero up on the GPIOB bus.
265 tca9555_status = i2c_start(I2C_ADDR_WRITE, I2C_TIMEOUT);
266 if (tca9555_status) goto out;
267 tca9555_status = i2c_write(OREGP1, I2C_TIMEOUT);
268 if (tca9555_status) goto out;
269 tca9555_status = i2c_write(port1, I2C_TIMEOUT);
270 if (tca9555_status) goto out;
271 out:
272 i2c_stop();
273 } 254 }
274 } 255 }
275} 256}