From 399ad8cc3e578647e947e26165c082b9261aa26b Mon Sep 17 00:00:00 2001 From: Matt Books Date: Thu, 7 Dec 2017 21:23:17 -0800 Subject: [PATCH] Some progress on firmware --- keyboards/hbno2/hbno2.c | 28 +++---- keyboards/hbno2/hbno2.h | 5 +- keyboards/hbno2/matrix.c | 148 +++++++++++++++++------------------- keyboards/hbno2/twimaster.c | 125 +++++++++++++----------------- 4 files changed, 137 insertions(+), 169 deletions(-) diff --git a/keyboards/hbno2/hbno2.c b/keyboards/hbno2/hbno2.c index 166bd8e78e68..db985c278464 100644 --- a/keyboards/hbno2/hbno2.c +++ b/keyboards/hbno2/hbno2.c @@ -25,23 +25,17 @@ void matrix_init_kb(void) { /* matrix_init_user(); */ init_mcp23018(); - - print("matrix init kb\n"); } uint8_t init_mcp23018(void) { mcp23018_status = 0x20; - print("init mcp23018\n"); - // I2C subsystem // uint8_t sreg_prev; // sreg_prev=SREG; // cli(); - print("i2c initializing\n"); i2c_init(); // on pins D(1,0) - print("i2c initialized\n"); i2c_initialized = true; _delay_ms(1000); @@ -49,23 +43,23 @@ uint8_t init_mcp23018(void) { /* // - unused : input : 1 */ /* // - input : input : 1 */ /* // - driving : output : 0 */ - /* mcp23018_status = i2c_start(I2C_ADDR_WRITE); if (mcp23018_status) goto out; */ - /* mcp23018_status = i2c_write(IODIRA); if (mcp23018_status) goto out; */ - /* mcp23018_status = i2c_write(0b00000000); if (mcp23018_status) goto out; */ - /* mcp23018_status = i2c_write(0b11111111); if (mcp23018_status) goto out; */ - /* i2c_stop(); */ + mcp23018_status = i2c_start(I2C_ADDR_WRITE); if (mcp23018_status) goto out; + mcp23018_status = i2c_write(IODIRA); if (mcp23018_status) goto out; + mcp23018_status = i2c_write(0b11111111); if (mcp23018_status) goto out; + mcp23018_status = i2c_write(0b11111111); if (mcp23018_status) goto out; + i2c_stop(); /* // set pull-up */ /* // - unused : on : 1 */ /* // - input : on : 1 */ /* // - driving : off : 0 */ -/* mcp23018_status = i2c_start(I2C_ADDR_WRITE); if (mcp23018_status) goto out; */ -/* mcp23018_status = i2c_write(GPPUA); if (mcp23018_status) goto out; */ -/* mcp23018_status = i2c_write(0b11111111); if (mcp23018_status) goto out; */ -/* mcp23018_status = i2c_write(0b11111111); if (mcp23018_status) goto out; */ + mcp23018_status = i2c_start(I2C_ADDR_WRITE); if (mcp23018_status) goto out; + mcp23018_status = i2c_write(GPPUA); if (mcp23018_status) goto out; + mcp23018_status = i2c_write(0b00000000); if (mcp23018_status) goto out; + mcp23018_status = i2c_write(0b00000000); if (mcp23018_status) goto out; -/* out: */ -/* i2c_stop(); */ +out: + i2c_stop(); // SREG=sreg_prev; diff --git a/keyboards/hbno2/hbno2.h b/keyboards/hbno2/hbno2.h index bc147028d023..c1a5b6467e1b 100644 --- a/keyboards/hbno2/hbno2.h +++ b/keyboards/hbno2/hbno2.h @@ -11,9 +11,8 @@ #define CPU_16MHz 0x00 // I2C aliases and register addresses (see "mcp23018.md") -#define I2C_ADDR 0b0100000 -#define I2C_ADDR_WRITE ( (I2C_ADDR<<1) | I2C_WRITE ) -#define I2C_ADDR_READ ( (I2C_ADDR<<1) | I2C_READ ) +#define I2C_ADDR_WRITE 0b01000000 +#define I2C_ADDR_READ 0b01000001 #define IODIRA 0x00 // i/o direction register #define IODIRB 0x01 #define GPPUA 0x0C // GPIO pull-up resistor register diff --git a/keyboards/hbno2/matrix.c b/keyboards/hbno2/matrix.c index 557171cddd43..036b8abc90ed 100644 --- a/keyboards/hbno2/matrix.c +++ b/keyboards/hbno2/matrix.c @@ -39,9 +39,6 @@ along with this program. If not, see . #include "timer.h" #endif -#define SIDE_LEFT 1 -#define SIDE_RIGHT 0 - #define DEBUG_MATRIX_SCAN_RATE 1 /* @@ -69,11 +66,11 @@ static matrix_row_t matrix[MATRIX_ROWS]; // already changed in the last DEBOUNCE scans. static uint8_t debounce_matrix[MATRIX_ROWS * MATRIX_COLS]; -/* static matrix_row_t read_cols(uint8_t row, uint8_t side); */ +static matrix_row_t read_cols(uint8_t row); static void unselect_rows(void); -/* static void select_row(uint8_t row); */ +static void select_row(uint8_t row); -/* static uint8_t mcp23018_reset_loop; */ +static uint8_t mcp23018_reset_loop; #ifdef DEBUG_MATRIX_SCAN_RATE uint32_t matrix_timer; @@ -113,8 +110,6 @@ void matrix_init(void) { // initialize row and col - print("HELLO! matrix init\n"); - mcp23018_status = init_mcp23018(); unselect_rows(); @@ -127,6 +122,8 @@ void matrix_init(void) } } + DDRB = 1<<0 | 1<<1 | 1<<2 | 1<<3 | 1<<4; + /* #ifdef DEBUG_MATRIX_SCAN_RATE */ /* matrix_timer = timer_read32(); */ /* matrix_scan_count = 0; */ @@ -137,7 +134,7 @@ void matrix_init(void) } void matrix_power_up(void) { - /* mcp23018_status = init_mcp23018(); */ + mcp23018_status = init_mcp23018(); /* unselect_rows(); */ @@ -178,63 +175,56 @@ void debounce_report(matrix_row_t change, uint8_t row) { uint8_t matrix_scan(void) { - /* matrix_print(); */ + matrix_print(); /* uint8_t data = 0; */ - print("Start i2c\n"); - - mcp23018_status = i2c_start(I2C_ADDR_READ); - if (mcp23018_status) { - print("ERR\n"); - /* goto out; */ - } + /* mcp23018_status = i2c_start(I2C_ADDR_READ); */ + /* if (mcp23018_status) { */ + /* goto out; */ + /* } */ - /* print("Read i2c\n"); */ /* data = i2c_readNak(); */ - /* printf("Data: %x\n", data); */ /* out: */ - /* print("Stop i2c\n"); */ /* i2c_stop(); */ -/* if (mcp23018_status) { // if there was an error */ -/* if (++mcp23018_reset_loop == 0) { */ -/* // since mcp23018_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans */ -/* // this will be approx bit more frequent than once per second */ -/* print("trying to reset mcp23018\n"); */ -/* mcp23018_status = init_mcp23018(); */ -/* if (mcp23018_status) { */ -/* print("left side not responding\n"); */ -/* } else { */ -/* print("left side attached\n"); */ -/* } */ -/* } */ -/* } */ + if (mcp23018_status) { // if there was an error + if (++mcp23018_reset_loop == 0) { + // since mcp23018_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans + // this will be approx bit more frequent than once per second + print("trying to reset mcp23018\n"); + mcp23018_status = init_mcp23018(); + if (mcp23018_status) { + print("left side not responding\n"); + } else { + print("left side attached\n"); + } + } + } -/* #ifdef DEBUG_MATRIX_SCAN_RATE */ -/* matrix_scan_count++; */ +#ifdef DEBUG_MATRIX_SCAN_RATE + matrix_scan_count++; -/* uint32_t timer_now = timer_read32(); */ -/* if (TIMER_DIFF_32(timer_now, matrix_timer)>1000) { */ -/* print("matrix scan frequency: "); */ -/* pdec(matrix_scan_count); */ -/* print("\n"); */ + uint32_t timer_now = timer_read32(); + if (TIMER_DIFF_32(timer_now, matrix_timer)>1000) { + print("matrix scan frequency: "); + pdec(matrix_scan_count); + print("\n"); -/* matrix_timer = timer_now; */ -/* matrix_scan_count = 0; */ -/* } */ -/* #endif */ + matrix_timer = timer_now; + matrix_scan_count = 0; + } +#endif -/* for (uint8_t i = 0; i < MATRIX_ROWS; i++) { */ -/* select_row(i); */ -/* matrix_row_t mask = debounce_mask(i); */ -/* matrix_row_t cols = (read_cols(i, SIDE_LEFT) & mask) << MATRIX_COLS_RIGHT; */ -/* cols = cols | (read_cols(i, SIDE_RIGHT) & mask) | (matrix[i] & ~mask); */ -/* debounce_report(cols ^ matrix[i], i); */ -/* matrix[i] = cols; */ -/* unselect_rows(); */ -/* } */ + for (uint8_t i = 0; i < MATRIX_ROWS; i++) { + select_row(i); + matrix_row_t mask = debounce_mask(i); + matrix_row_t cols = (read_cols(i) & mask); + debounce_report(cols ^ matrix[i], i); + matrix[i] = cols; + unselect_rows(); + } matrix_scan_quantum(); @@ -261,6 +251,7 @@ void matrix_print(void) pbin_reverse16(matrix_get_row(row)); print("\n"); } + _delay_ms(1000); } uint8_t matrix_key_count(void) @@ -272,34 +263,37 @@ uint8_t matrix_key_count(void) return count; } -/* static matrix_row_t read_cols(uint8_t row, uint8_t side) */ -/* { */ -/* uint8_t mcp_side = side == SIDE_LEFT ? GPIOB : GPIOA; */ - -/* if (mcp23018_status) { // if there was an error */ -/* return 0; */ -/* } else { */ -/* uint8_t data = 0; */ -/* mcp23018_status = i2c_start(I2C_ADDR_WRITE); if (mcp23018_status) goto out; */ -/* mcp23018_status = i2c_write(mcp_side); if (mcp23018_status) goto out; */ -/* mcp23018_status = i2c_start(I2C_ADDR_READ); if (mcp23018_status) goto out; */ -/* data = i2c_readNak(); */ -/* data = ~data; */ -/* out: */ -/* i2c_stop(); */ -/* return data; */ -/* } */ -/* } */ +static matrix_row_t read_cols(uint8_t row) +{ + if (mcp23018_status) { // if there was an error + print("ERR\n"); + return 0; + } else { + uint8_t data = 0; + mcp23018_status = i2c_start(I2C_ADDR_WRITE); if (mcp23018_status) goto out; + mcp23018_status = i2c_write(GPIOA); if (mcp23018_status) goto out; + mcp23018_status = i2c_start(I2C_ADDR_READ); if (mcp23018_status) goto out; + data = i2c_readNak() & 0b01111111; + i2c_stop(); + mcp23018_status = i2c_start(I2C_ADDR_WRITE); if (mcp23018_status) goto out; + mcp23018_status = i2c_write(GPIOB); if (mcp23018_status) goto out; + mcp23018_status = i2c_start(I2C_ADDR_READ); if (mcp23018_status) goto out; + data |= (i2c_readNak() & 0b00111111) << 7; + + xprintf("ROW: %013b\n", data); + out: + i2c_stop(); + return data; + } +} static void unselect_rows(void) { - DDRB &= ~(1<<0 | 1<<1 | 1<<2 | 1<<3 | 1<<4); PORTB &= ~(1<<0 | 1<<1 | 1<<2 | 1<<3 | 1<<4); } -/* static void select_row(uint8_t row) */ -/* { */ -/* DDRB |= (1< http://jump.to/fleury -* File: $Id: twimaster.c,v 1.3 2005/07/02 11:14:21 Peter Exp $ +* File: $Id: twimaster.c,v 1.4 2015/01/17 12:16:05 peter Exp $ * Software: AVR-GCC 3.4.3 / avr-libc 1.2.3 * Target: any AVR device with hardware TWI * Usage: API compatible with I2C Software Library i2cmaster.h **************************************************************************/ -#include QMK_KEYBOARD_H #include #include -#include "i2cmaster.h" +#include +#include QMK_KEYBOARD_H -/* define CPU frequency in Mhz here if not defined in Makefile */ +/* define CPU frequency in hz here if not defined in Makefile */ #ifndef F_CPU -#define F_CPU 16000000UL +#define F_CPU 4000000UL #endif /* I2C clock in Hz */ @@ -27,34 +27,14 @@ *************************************************************************/ void i2c_init(void) { - /* initialize TWI clock - * minimal values in Bit Rate Register (TWBR) and minimal Prescaler - * bits in the TWI Status Register should give us maximal possible - * I2C bus speed - about 444 kHz - * - * for more details, see 20.5.2 in ATmega16/32 secification - */ - print("Waiting before i2c init\n"); - - _delay_ms(10000); - print("Doing i2c init\n"); - - // Enable pull-up resistors on PD0 + PD1 - DDRD |= 0x3; - PORTD ^= ~0x3; - - print("Sleeping with SDA/SCL pulled low"); - _delay_ms(10000); + /* initialize TWI clock: 100 kHz clock, TWPS = 0 => prescaler = 1 */ - TWCR = 1< 10 for stable operation */ }/* i2c_init */ + /************************************************************************* Issues a start condition and sends address and transfer direction. return 0 = device accessible, 1= failed to access device @@ -63,24 +43,19 @@ unsigned char i2c_start(unsigned char address) { uint8_t twst; - TWCR = (1<