Skip to content

Commit

Permalink
Testing issue qmk#1191 (n-column split i2c slave)
Browse files Browse the repository at this point in the history
Based on initial OrthoDox (serial) config by @reddragond and others,
this attempts to add TWI (I2C) support.
Relevant: <qmk#1191>

- per @ahtn recommendation, using memcpy for moving slave matrix
  into slave sending buffer
- slave buffer has been enlarged using sizeof(matrix_row_t)
- note: i2c.h now includes matrix.h
- note: matrix.c includes <string.h>
  • Loading branch information
utrrrongeeb committed Jun 10, 2017
1 parent eeaec74 commit 8ee4592
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
3 changes: 2 additions & 1 deletion keyboards/orthodox/i2c.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define I2C_H

#include <stdint.h>
#include "matrix.h"

#ifndef F_CPU
#define F_CPU 16000000UL
Expand All @@ -13,7 +14,7 @@
#define I2C_ACK 1
#define I2C_NACK 0

#define SLAVE_BUFFER_SIZE 0x10
#define SLAVE_BUFFER_SIZE (MATRIX_ROWS/2*sizeof(matrix_row_t))

// i2c SCL clock frequency
#define SCL_CLOCK 400000L
Expand Down
20 changes: 12 additions & 8 deletions keyboards/orthodox/matrix.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdint.h>
#include <stdbool.h>
#ifdef USE_I2C
// provides memcpy for copying TWI slave buffer
#include <string.h>
#endif
#include <avr/io.h>
#include <avr/wdt.h>
#include <avr/interrupt.h>
Expand Down Expand Up @@ -167,11 +171,13 @@ int i2c_transaction(void) {
if (err) goto i2c_error;

if (!err) {
int i;
for (i = 0; i < ROWS_PER_HAND-1; ++i) {
matrix[slaveOffset+i] = i2c_master_read(I2C_ACK);
// 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);
}
matrix[slaveOffset+i] = i2c_master_read(I2C_NACK);
// last byte to be read / end of chunk
*((uint8_t*)&matrix[slaveOffset]+i) = i2c_master_read(I2C_NACK);
i2c_master_stop();
} else {
i2c_error: // the cable is disconnceted, or something else went wrong
Expand Down Expand Up @@ -236,10 +242,8 @@ void matrix_slave_scan(void) {
int offset = (isLeftHand) ? 0 : (MATRIX_ROWS / 2);

#ifdef USE_I2C
for (int i = 0; i < ROWS_PER_HAND; ++i) {
/* i2c_slave_buffer[i] = matrix[offset+i]; */
i2c_slave_buffer[i] = matrix[offset+i];
}
// SLAVE_BUFFER_SIZE is from i2c.h
memcpy((void*)i2c_slave_buffer, (const void*)&matrix[offset], SLAVE_BUFFER_SIZE);
#else // USE_SERIAL
for (int i = 0; i < ROWS_PER_HAND; ++i) {
serial_slave_buffer[i] = matrix[offset+i];
Expand Down

0 comments on commit 8ee4592

Please sign in to comment.