From 7fefcc0b1ee7ae23ef8cba1334945dc3a61793e3 Mon Sep 17 00:00:00 2001 From: Art Ortenburger Date: Sun, 25 Jun 2017 21:15:33 -0300 Subject: [PATCH] orthodox: TWI (i2c) is working, kludge for col9 The TWI interconnect ("i2c" in directories and build config) is now working for the Orthodox, including the slave half's column #9. This is intended as an interim solution, as it's a kludge, not a fix. Rather than a working multi-byte implementation, the two col9 keys' bits are packed-into and unpacked-from the two unused bits in row1. Furthermore, the TWI clock constant has been reduced to 100000 from 400000, as testing revealed the higher value just didn't work. Testing also found that (with this kludge) increasing the TWI buffer was not necessary. This commit leaves many commented-out lines in matrix.c from previous testing, which will be removed in a future commit once the interconnects' multi-byte problems have been debugged more thoroughly. --- keyboards/orthodox/i2c.h | 2 +- keyboards/orthodox/matrix.c | 33 +++++++++++++++++++++------------ 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/keyboards/orthodox/i2c.h b/keyboards/orthodox/i2c.h index 5326d3cc18c9..2af843ff60ad 100644 --- a/keyboards/orthodox/i2c.h +++ b/keyboards/orthodox/i2c.h @@ -14,7 +14,7 @@ #define I2C_ACK 1 #define I2C_NACK 0 -#define SLAVE_BUFFER_SIZE 0x20 +#define SLAVE_BUFFER_SIZE 0x10 // i2c SCL clock frequency #define SCL_CLOCK 100000UL diff --git a/keyboards/orthodox/matrix.c b/keyboards/orthodox/matrix.c index c6a44193df00..3b60cead84b5 100644 --- a/keyboards/orthodox/matrix.c +++ b/keyboards/orthodox/matrix.c @@ -171,22 +171,27 @@ int i2c_transaction(void) { if (err) goto i2c_error; if (!err) { - /* - // read from TWI byte-by-byte into matrix_row_t memory space + /* + // read from TWI byte-by-byte into matrix_row_t memory space size_t i; for (i = 0; i < SLAVE_BUFFER_SIZE-1; ++i) { *((uint8_t*)&matrix[slaveOffset]+i) = i2c_master_read(I2C_ACK); } - // last byte to be read / end of chunk + // last byte to be read / end of chunk *((uint8_t*)&matrix[slaveOffset]+i) = i2c_master_read(I2C_NACK); - */ - - // i2c_master_read(I2C_ACK); - matrix[slaveOffset+0] = i2c_master_read(I2C_ACK); - // i2c_master_read(I2C_ACK); - matrix[slaveOffset+1] = i2c_master_read(I2C_ACK); - // i2c_master_read(I2C_ACK); - matrix[slaveOffset+2] = i2c_master_read(I2C_NACK); + */ + + // kludge for column #9: unpack bits for keys (2,9) and (3,9) from (1,7) and (1,8) + // i2c_master_read(I2C_ACK); + matrix[slaveOffset+0] = i2c_master_read(I2C_ACK); + // i2c_master_read(I2C_ACK); + matrix[slaveOffset+1] = (matrix_row_t)i2c_master_read(I2C_ACK)\ + | (matrix[slaveOffset+0]&0x40U)<<2; + // i2c_master_read(I2C_ACK); + matrix[slaveOffset+2] = (matrix_row_t)i2c_master_read(I2C_NACK)\ + | (matrix[slaveOffset+0]&0x80U)<<1; + // clear highest two bits on row 1, where the col9 bits were transported + matrix[slaveOffset+0] &= 0x3F; i2c_master_stop(); } else { @@ -255,7 +260,11 @@ void matrix_slave_scan(void) { // SLAVE_BUFFER_SIZE is from i2c.h // (MATRIX_ROWS/2*sizeof(matrix_row_t)) // memcpy((void*)i2c_slave_buffer, (const void*)&matrix[offset], (ROWS_PER_HAND*sizeof(matrix_row_t))); - i2c_slave_buffer[0] = (uint8_t)(matrix[offset+0]); + + // kludge for column #9: put bits for keys (2,9) and (3,9) into (1,7) and (1,8) + i2c_slave_buffer[0] = (uint8_t)(matrix[offset+0])\ + | (matrix[offset+1]&0x100U)>>2\ + | (matrix[offset+2]&0x100U)>>1; i2c_slave_buffer[1] = (uint8_t)(matrix[offset+1]); i2c_slave_buffer[2] = (uint8_t)(matrix[offset+2]); // note: looks like a possible operator-precedence bug here, in last version?