aboutsummaryrefslogtreecommitdiff
path: root/keyboards/dc01/left/matrix.c
diff options
context:
space:
mode:
Diffstat (limited to 'keyboards/dc01/left/matrix.c')
-rw-r--r--keyboards/dc01/left/matrix.c92
1 files changed, 41 insertions, 51 deletions
diff --git a/keyboards/dc01/left/matrix.c b/keyboards/dc01/left/matrix.c
index 0e7b591f8..9ae9b1098 100644
--- a/keyboards/dc01/left/matrix.c
+++ b/keyboards/dc01/left/matrix.c
@@ -31,14 +31,11 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
31#include "timer.h" 31#include "timer.h"
32#include "i2c_master.h" 32#include "i2c_master.h"
33 33
34#define SLAVE_I2C_ADDRESS_RIGHT 0x19 34#define SLAVE_I2C_ADDRESS_RIGHT 0x32
35#define SLAVE_I2C_ADDRESS_NUMPAD 0x21 35#define SLAVE_I2C_ADDRESS_NUMPAD 0x36
36#define SLAVE_I2C_ADDRESS_ARROW 0x23 36#define SLAVE_I2C_ADDRESS_ARROW 0x40
37 37
38#define ERROR_DISCONNECT_COUNT 5 38#define ERROR_DISCONNECT_COUNT 5
39static uint8_t error_count_right = 0;
40static uint8_t error_count_numpad = 0;
41static uint8_t error_count_arrow = 0;
42 39
43/* Set 0 if debouncing isn't needed */ 40/* Set 0 if debouncing isn't needed */
44 41
@@ -210,38 +207,23 @@ uint8_t matrix_scan(void)
210 } 207 }
211# endif 208# endif
212 209
213 if (i2c_transaction(SLAVE_I2C_ADDRESS_RIGHT, 0x3F, 0)){ //error has occured for main right half 210if (i2c_transaction(SLAVE_I2C_ADDRESS_RIGHT, 0x3F, 0)) {
214 error_count_right++; 211 for (uint8_t i = 0; i < MATRIX_ROWS ; i++) {
215 if (error_count_right > ERROR_DISCONNECT_COUNT){ //disconnect half 212 matrix[i] &= 0x3F; //mask bits to keep
216 for (uint8_t i = 0; i < MATRIX_ROWS ; i++) {
217 matrix[i] &= 0x3F; //mask bits to keep
218 }
219 }
220 }else{ //no error
221 error_count_right = 0;
222 } 213 }
214}
223 215
224 if (i2c_transaction(SLAVE_I2C_ADDRESS_ARROW, 0X3FFF, 8)){ //error has occured for arrow cluster 216if (i2c_transaction(SLAVE_I2C_ADDRESS_ARROW, 0X3FFF, 8)) {
225 error_count_arrow++; 217 for (uint8_t i = 0; i < MATRIX_ROWS ; i++) {
226 if (error_count_arrow > ERROR_DISCONNECT_COUNT){ //disconnect arrow cluster 218 matrix[i] &= 0x3FFF; //mask bits to keep
227 for (uint8_t i = 0; i < MATRIX_ROWS ; i++) {
228 matrix[i] &= 0x3FFF; //mask bits to keep
229 }
230 }
231 }else{ //no error
232 error_count_arrow = 0;
233 } 219 }
220}
234 221
235 if (i2c_transaction(SLAVE_I2C_ADDRESS_NUMPAD, 0x1FFFF, 11)){ //error has occured for numpad 222if (i2c_transaction(SLAVE_I2C_ADDRESS_NUMPAD, 0x1FFFF, 11)) {
236 error_count_numpad++; 223 for (uint8_t i = 0; i < MATRIX_ROWS ; i++) {
237 if (error_count_numpad > ERROR_DISCONNECT_COUNT){ //disconnect numpad 224 matrix[i] &= 0x1FFFF; //mask bits to keep
238 for (uint8_t i = 0; i < MATRIX_ROWS ; i++) {
239 matrix[i] &= 0x1FFFF; //mask bits to keep
240 }
241 }
242 }else{ //no error
243 error_count_numpad = 0;
244 } 225 }
226}
245 227
246 matrix_scan_quantum(); 228 matrix_scan_quantum();
247 return 1; 229 return 1;
@@ -436,29 +418,37 @@ static void unselect_cols(void)
436 418
437// Complete rows from other modules over i2c 419// Complete rows from other modules over i2c
438i2c_status_t i2c_transaction(uint8_t address, uint32_t mask, uint8_t col_offset) { 420i2c_status_t i2c_transaction(uint8_t address, uint32_t mask, uint8_t col_offset) {
439 i2c_status_t err = i2c_start((address << 1) | I2C_WRITE, 10); 421 i2c_status_t status = i2c_start(address, 50);
440 i2c_write(0x01, 10); //request data in address 1 422 if (status < 0) {
423 goto error;
424 }
441 425
442 i2c_start((address << 1) | I2C_READ, 5); 426 status = i2c_write(0x01, 50);
427 if (status < 0) {
428 goto error;
429 }
443 430
444 err = i2c_read_ack(10); 431 status = i2c_start(address | I2C_READ, 50);
445 if (err == 0x55) { //synchronization byte
446 432
447 for (uint8_t i = 0; i < MATRIX_ROWS-1 ; i++) { //assemble slave matrix in main matrix 433 status = i2c_read_ack(50);
448 matrix[i] &= mask; //mask bits to keep 434 if (status != 0x55) { //synchronization byte
449 err = i2c_read_ack(10); 435 goto error;
450 matrix[i] |= ((uint32_t)err << (MATRIX_COLS_SCANNED + col_offset)); //add new bits at the end 436 }
451 }
452 //last read request must be followed by a NACK
453 matrix[MATRIX_ROWS - 1] &= mask; //mask bits to keep
454 err = i2c_read_nack(10);
455 matrix[MATRIX_ROWS - 1] |= ((uint32_t)err << (MATRIX_COLS_SCANNED + col_offset)); //add new bits at the end
456 437
457 } else { 438 for (uint8_t i = 0; i < MATRIX_ROWS-1 && status >= 0; i++) { //assemble slave matrix in main matrix
458 i2c_stop(); 439 matrix[i] &= mask; //mask bits to keep
459 return 1; 440 status = i2c_read_ack(50);
441 matrix[i] |= ((uint32_t)status << (MATRIX_COLS_SCANNED + col_offset)); //add new bits at the end
442 }
443 //last read request must be followed by a NACK
444 if (status >= 0) {
445 matrix[MATRIX_ROWS - 1] &= mask; //mask bits to keep
446 status = i2c_read_nack(50);
447 matrix[MATRIX_ROWS - 1] |= ((uint32_t)status << (MATRIX_COLS_SCANNED + col_offset)); //add new bits at the end
460 } 448 }
461 449
450error:
462 i2c_stop(); 451 i2c_stop();
463 return 0; 452
453 return (status < 0) ? status : I2C_STATUS_SUCCESS;
464} \ No newline at end of file 454} \ No newline at end of file