7 static uint8_t expander_status = 0;
8 static uint8_t expander_input = 0;
10 void expander_config(void);
11 uint8_t expander_write(uint8_t reg, uint8_t data);
12 uint8_t expander_read(uint8_t reg, uint8_t *data);
14 void expander_init(void)
20 void expander_scan(void)
22 dprintf("expander status: %d ... ", expander_status);
23 uint8_t ret = i2c_start(EXPANDER_ADDR | I2C_WRITE);
26 if (expander_status == 0) {
27 dprintf("attached\n");
34 if (expander_status == 1) {
35 dprintf("detached\n");
40 dprintf("%d\n", expander_status);
43 void expander_read_cols(void)
45 expander_read(EXPANDER_REG_GPIOA, &expander_input);
48 uint8_t expander_get_col(uint8_t col)
53 return expander_input & (1<<col) ? 1 : 0;
56 matrix_row_t expander_read_row(void)
61 matrix_row_t cols = 0;
62 for (uint8_t col = 0; col < MATRIX_COLS; col++) {
63 if (expander_get_col(col)) {
64 cols |= (1UL << (MATRIX_COLS - 1 - col));
71 void expander_unselect_rows(void)
73 expander_write(EXPANDER_REG_IODIRB, 0xFF);
76 void expander_select_row(uint8_t row)
78 expander_write(EXPANDER_REG_IODIRB, ~(1<<(row+1)));
81 void expander_config(void)
83 expander_write(EXPANDER_REG_IPOLA, 0xFF);
84 expander_write(EXPANDER_REG_GPPUA, 0xFF);
85 expander_write(EXPANDER_REG_IODIRB, 0xFF);
88 uint8_t expander_write(uint8_t reg, uint8_t data)
90 if (expander_status == 0) {
94 ret = i2c_start(EXPANDER_ADDR | I2C_WRITE);
98 ret = i2c_write(data);
104 uint8_t expander_read(uint8_t reg, uint8_t *data)
106 if (expander_status == 0) {
110 ret = i2c_start(EXPANDER_ADDR | I2C_WRITE);
112 ret = i2c_write(reg);
114 ret = i2c_rep_start(EXPANDER_ADDR | I2C_READ);
116 *data = i2c_readNak();