diff --git a/drivers/arm/twi2c.c b/drivers/arm/twi2c.c index b6dc2d27e..561a601ea 100644 --- a/drivers/arm/twi2c.c +++ b/drivers/arm/twi2c.c @@ -21,6 +21,7 @@ #include "chprintf.h" #include "memstreams.h" #include "printf.h" +#include "matrix.h" #ifndef I2C_DRIVER #define I2C_DRIVER I2CD1 @@ -37,13 +38,7 @@ * Stretches clock until reply available. */ - -#define slaveI2Caddress 0x30 /* Address in our terms - halved by later code */ -//#define myOtherI2Caddress 0x19 - -I2CSlaveMsgCB twi2c_slave_message_process, catchError, clearAfterSend; - -static const I2CConfig slaveI2Cconfig = { +static const I2CConfig uniI2CConfig = { STM32_TIMINGR_PRESC(15U) | STM32_TIMINGR_SCLDEL(4U) | STM32_TIMINGR_SDADEL(2U) | STM32_TIMINGR_SCLH(15U) | STM32_TIMINGR_SCLL(21U), @@ -119,11 +114,10 @@ void catchError(I2CDriver *i2cp) noteI2cError(i2cp->errors); } - +extern void matrix_copy(matrix_row_t * copy); const char hexString[16] = "0123456789abcdef"; - /** * Message processor - looks at received message, determines reply as quickly as possible * @@ -132,36 +126,18 @@ const char hexString[16] = "0123456789abcdef"; * Note: Called in interrupt context, so need to be quick! */ void twi2c_slave_message_process(I2CDriver *i2cp) { - uint8_t i; - uint8_t *txPtr = txBody + 8; + uint8_t *txPtr = txBody; uint8_t txLen; - uint32_t curCount; size_t len = i2cSlaveBytes(i2cp); // Number of bytes received - if (len >= sizeof(rxBody)) - len = sizeof(rxBody)-1; - rxBody[len]=0; // String termination sometimes useful - /* A real-world application would read and decode the message in rxBody, then generate an appropriate reply in txBody */ - - curCount = ++messageCounter; - txLen = len + 11; // Add in the overhead - - for (i = 0; i < 8; i++) - { - *--txPtr = hexString[curCount & 0xf]; - curCount = curCount >> 4; + if (len >= 2 && rxBody[0] == 0x01 && rxBody[1] == 0x00) { + matrix_row_t matrix[MATRIX_ROWS / 2]; + matrix_copy(matrix); + memcpy(txPtr, matrix, MATRIX_ROWS / 2); + txLen = MATRIX_ROWS / 2; } - txPtr = txBody + 8; - *txPtr++ = ' '; - *txPtr++ = '['; - memcpy(txPtr, rxBody, len); // Echo received message - txPtr += len; - *txPtr++ = ']'; - *txPtr = '\0'; - - /** Message ready to go here */ echoReply.size = txLen; i2cSlaveReplyI(i2cp, &echoReply); } @@ -193,7 +169,7 @@ void twi2c_slave_init(void) { palSetPadMode(GPIOB, 8, PAL_MODE_ALTERNATE(4) | PAL_STM32_OTYPE_OPENDRAIN | PAL_STM32_PUPDR_PULLUP); - i2cStart(&I2C_DRIVER, &slaveI2Cconfig); + i2cStart(&I2C_DRIVER, &uniI2CConfig); #if HAL_USE_I2C_SLAVE I2C_DRIVER.slaveTimeout = MS2ST(100); // Time for complete message #endif @@ -215,3 +191,11 @@ void twi2c_slave_task(void) { printf("I2cError: %04x\r\n", lastI2cErrorFlags); } } + +void twi2c_master_init(void) { + i2cStart(&I2C_DRIVER, &uniI2CConfig); +} + +msg_t twi2c_master_send(i2caddr_t address, const uint8_t * txbuf, uint8_t * rxbuf, systime_t timeout) { + return i2cMasterTransmitTimeout(&I2C_DRIVER, address, txbuf, sizeof(txbuf), rxbuf, sizeof(rxbuf), timeout); +} diff --git a/drivers/arm/twi2c.h b/drivers/arm/twi2c.h index a1847d436..175572189 100644 --- a/drivers/arm/twi2c.h +++ b/drivers/arm/twi2c.h @@ -15,4 +15,14 @@ */ #include "ch.h" -#include "hal.h" \ No newline at end of file +#include "hal.h" + +#define slaveI2Caddress 0x30 /* Address in our terms - halved by later code */ +//#define myOtherI2Caddress 0x19 + +I2CSlaveMsgCB twi2c_slave_message_process, catchError, clearAfterSend; + +void twi2c_slave_init(void); + +void twi2c_master_init(void); +msg_t twi2c_master_send(i2caddr_t address, const uint8_t * txbuf, uint8_t * rxbuf, systime_t timeout); diff --git a/keyboards/_qmk_handwire/keymaps/default/keymap.c b/keyboards/_qmk_handwire/keymaps/default/keymap.c index d0e727825..2611f570c 100644 --- a/keyboards/_qmk_handwire/keymaps/default/keymap.c +++ b/keyboards/_qmk_handwire/keymaps/default/keymap.c @@ -35,14 +35,14 @@ enum custom_keycodes { const uint16_t keymaps[][MATRIX_ROWS][MATRIX_COLS] = { [0] = KEYMAP( KC_GRV, KC_1, KC_2, KC_3, KC_4, KC_0, KC_DEL, - KC_TAB, KC_Q, KC_W, KC_E, KC_R, KC_T, KC_HOME, + KC_TAB, DEBUG, KC_W, KC_E, KC_R, KC_T, KC_HOME, KC_ESC, KC_A, KC_S, KC_D, KC_F, KC_G, KC_END, KC_LSFT, KC_Z, KC_X, KC_C, KC_V, KC_N, KC_RCTL, KC_LEFT, KC_DOWN, KC_UP, KC_LGUI, KC_SPC, MO(2), MO(1), KC_INS, KC_6, KC_7, KC_8, KC_9, KC_0, KC_BSPC, - KC_PGUP, KC_Y, KC_U, KC_I, KC_O, KC_P, KC_DEL, + KC_PGUP, KC_Y, KC_U, KC_I, KC_O, DEBUG, KC_DEL, KC_PGDN, KC_H, KC_J, KC_K, KC_L, KC_COLN, KC_QUOT, KC_N, KC_M, KC_COMM, KC_DOT, KC_SLSH, KC_ENT, KC_RCTL, KC_LEFT, KC_DOWN, KC_UP, KC_RGHT, diff --git a/keyboards/_qmk_handwire/matrix.c b/keyboards/_qmk_handwire/matrix.c index 8800183aa..1d67851ba 100644 --- a/keyboards/_qmk_handwire/matrix.c +++ b/keyboards/_qmk_handwire/matrix.c @@ -9,6 +9,7 @@ #include "matrix.h" #include "usb_main.h" +#include "twi2c.h" /* QMK Handwire * @@ -46,7 +47,7 @@ void matrix_scan_kb(void) { void matrix_init(void) { printf("matrix init\n"); - //debug_matrix = true; + // debug_matrix = true; // C13 is connected to VCC on the right hand palSetPadMode(GPIOC, 13, PAL_MODE_INPUT); @@ -56,6 +57,12 @@ void matrix_init(void) { // if USB is active, this is the master master = (USB_DRIVER.state == USB_ACTIVE); + if (master) { + twi2c_master_init(); + } else { + twi2c_slave_init(); + } + /* Column(sense) */ palSetPadMode(GPIOA, 13, PAL_MODE_INPUT_PULLDOWN); palSetPadMode(GPIOA, 14, PAL_MODE_INPUT_PULLDOWN); @@ -120,19 +127,31 @@ matrix_row_t matrix_scan_common(uint8_t row) { return data; } -uint8_t matrix_scan_master(void) { +void matrix_scan_master(void) { -} + const uint8_t command[2] = { 0x01, 0x00 }; + uint8_t other_matrix[MATRIX_ROWS]; -uint8_t matrix_scan_slave(void) { + msg_t resp; + resp = twi2c_master_send(slaveI2Caddress/2, command, other_matrix, TIME_INFINITE); + printf("%x\n", resp); + uint8_t * matrix_pointer; + if (right_hand) { + matrix_pointer = matrix; + } else { + matrix_pointer = matrix + (MATRIX_ROWS / 2); + } + + memcpy(matrix_pointer, other_matrix, MATRIX_ROWS / 2); } uint8_t matrix_scan(void) { + for (int row = 0; row < MATRIX_ROWS; row++) { matrix_row_t data = 0; - if (right_hand && row >= 6) { + if ((right_hand && row >= 6) || (!right_hand && row < 6)) { data = matrix_scan_common(row % 6); } @@ -149,6 +168,11 @@ uint8_t matrix_scan(void) { } debouncing = false; } + + if (master) { + matrix_scan_master(); + } + matrix_scan_quantum(); return 1; @@ -162,6 +186,16 @@ matrix_row_t matrix_get_row(uint8_t row) { return matrix[row]; } +void matrix_copy(matrix_row_t * copy) { + uint8_t * matrix_pointer; + if (right_hand) { + matrix_pointer = matrix + (MATRIX_ROWS / 2); + } else { + matrix_pointer = matrix; + } + memcpy(copy, matrix_pointer, MATRIX_ROWS / 2); +} + void matrix_print(void) { printf("\nr/c 01234567\n"); for (uint8_t row = 0; row < MATRIX_ROWS; row++) {