Skip to content

Commit

Permalink
Some progress on firmware
Browse files Browse the repository at this point in the history
  • Loading branch information
mattbooks committed Dec 8, 2017
1 parent bec8540 commit 399ad8c
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 169 deletions.
28 changes: 11 additions & 17 deletions keyboards/hbno2/hbno2.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,47 +25,41 @@ 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);

/* // set pin direction */
/* // - 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;

Expand Down
5 changes: 2 additions & 3 deletions keyboards/hbno2/hbno2.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
148 changes: 71 additions & 77 deletions keyboards/hbno2/matrix.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "timer.h"
#endif

#define SIDE_LEFT 1
#define SIDE_RIGHT 0

#define DEBUG_MATRIX_SCAN_RATE 1

/*
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -113,8 +110,6 @@ void matrix_init(void)
{
// initialize row and col

print("HELLO! matrix init\n");

mcp23018_status = init_mcp23018();

unselect_rows();
Expand All @@ -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; */
Expand All @@ -137,7 +134,7 @@ void matrix_init(void)
}

void matrix_power_up(void) {
/* mcp23018_status = init_mcp23018(); */
mcp23018_status = init_mcp23018();

/* unselect_rows(); */

Expand Down Expand Up @@ -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();

Expand All @@ -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)
Expand All @@ -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<<row); */
/* PORTB &= ~(1<<row); */
/* } */
static void select_row(uint8_t row)
{
PORTB |= 1<<row;
}

Loading

0 comments on commit 399ad8c

Please sign in to comment.