1
0

kimera: Implement i2c auto-scan and timeout

This commit is contained in:
Kai Ryu 2014-10-31 15:30:32 +09:00
parent 4ff4331e84
commit 93b017b095
5 changed files with 194 additions and 120 deletions

View File

@ -140,7 +140,7 @@ NKRO_ENABLE = yes # USB Nkey Rollover - not yet supported in LUFA
#PS2_USE_BUSYWAIT = yes
BACKLIGHT_ENABLE = yes # Enable keyboard backlight functionality
KEYMAP_IN_EEPROM_ENABLE = yes # External keymap in eeprom
KEYMAP_SECTION_ENABLE = yes # Fixed address keymap for keymap editor
#KEYMAP_SECTION_ENABLE = yes # Fixed address keymap for keymap editor
BREATHING_LED_ENABLE = yes # Enable breathing backlight
# Optimize size but this may cause error "relocation truncated to fit"

View File

@ -19,11 +19,18 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <stdbool.h>
#include <avr/eeprom.h>
#include <avr/interrupt.h>
#include <avr/wdt.h>
#include <util/delay.h>
#include "action.h"
#include "suspend.h"
#include "i2cmaster.h"
#include "kimera.h"
#include "debug.h"
#define SCL_CLOCK 400000L
extern uint8_t i2c_force_stop;
uint8_t row_mapping[PX_COUNT] = {
0, 1, 2, 3, 4, 5, 6, 7,
UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED, UNCONFIGURED,
@ -39,6 +46,7 @@ uint8_t col_mapping[PX_COUNT] = {
uint8_t row_count = 8;
uint8_t col_count = 24;
uint8_t data[EXP_COUNT][EXP_PORT_COUNT];
uint8_t exp_status = 0;
void kimera_init(void)
{
@ -51,8 +59,19 @@ void kimera_init(void)
/* init i2c */
i2c_init();
/* init i/o expander */
expander_init();
/* init i/o expanders */
kimera_scan();
/* init watch dog */
wdt_init();
}
void wdt_init(void)
{
cli();
wdt_reset();
wdt_intr_enable(WDTO_1S);
sei();
}
uint8_t read_matrix_mapping(void)
@ -110,9 +129,34 @@ void write_matrix_mapping(void)
}
}
void kimera_scan(void)
{
wdt_reset();
uint8_t ret;
for (uint8_t exp = 0; exp < EXP_COUNT; exp++) {
ret = i2c_start(EXP_ADDR(exp) | I2C_READ);
if (exp_status & (1<<exp)) {
if (ret) {
dprintf("lost: %d\n", exp);
exp_status &= ~(1<<exp);
clear_keyboard();
}
}
else {
if (!ret) {
dprintf("found: %d\n", exp);
exp_status |= (1<<exp);
i2c_stop();
expander_init(exp);
clear_keyboard();
}
}
}
}
matrix_row_t read_cols(void)
{
init_data(0x00);
init_data(0xFF);
/* read all input registers */
for (uint8_t exp = 0; exp < EXP_COUNT; exp++) {
@ -124,7 +168,7 @@ matrix_row_t read_cols(void)
for (uint8_t col = 0; col < col_count; col++) {
uint8_t px = col_mapping[col];
if (px != UNCONFIGURED) {
if (data[PX_TO_EXP(px)][PX_TO_PORT(px)] & (1 << PX_TO_PIN(px))) {
if (!(data[PX_TO_EXP(px)][PX_TO_PORT(px)] & (1 << PX_TO_PIN(px)))) {
cols |= (1UL << col);
}
}
@ -153,14 +197,16 @@ void select_row(uint8_t row)
}
}
void expander_init(void)
void expander_init(uint8_t exp)
{
init_data(0xFF);
/* write inversion register */
/*
for (uint8_t exp = 0; exp < EXP_COUNT; exp++) {
expander_write_inversion(exp, data[exp]);
}
*/
/* set output bit */
for (uint8_t row = 0; row < row_count; row++) {
@ -171,13 +217,12 @@ void expander_init(void)
}
/* write config registers */
for (uint8_t exp = 0; exp < EXP_COUNT; exp++) {
expander_write_config(exp, data[exp]);
}
}
uint8_t expander_write(uint8_t exp, uint8_t command, uint8_t *data)
{
wdt_reset();
uint8_t addr = EXP_ADDR(exp);
uint8_t ret;
ret = i2c_start(addr | I2C_WRITE);
@ -194,6 +239,7 @@ stop:
uint8_t expander_read(uint8_t exp, uint8_t command, uint8_t *data)
{
wdt_reset();
uint8_t addr = EXP_ADDR(exp);
uint8_t ret;
ret = i2c_start(addr | I2C_WRITE);
@ -240,3 +286,24 @@ void init_data(uint8_t value)
}
}
}
ISR(WDT_vect)
{
dprintf("i2c timeout\n");
/* send stop condition */
TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
TWCR = 0;
/* let slave to release SDA */
DDRD |= (1<<PD0);
for (uint8_t i = 0; i < 9; i++) {
PORTD &= ~(1<<PD0);
_delay_us((F_CPU / SCL_CLOCK - 16) / 2);
PORTD |= (1<<PD0);
_delay_us((F_CPU / SCL_CLOCK - 16) / 2);
}
/* escape from loop */
i2c_force_stop = 1;
}

View File

@ -120,13 +120,14 @@ const uint16_t PROGMEM dummy[] = {
/* Functions */
void kimera_init(void);
void wdt_init(void);
uint8_t read_matrix_mapping(void);
void write_matrix_mapping(void);
void shift_out_word(uint16_t word);
void kimera_scan(void);
matrix_row_t read_cols(void);
void unselect_rows(void);
void select_row(uint8_t row);
void expander_init(void);
void expander_init(uint8_t exp);
uint8_t expander_write(uint8_t exp, uint8_t command, uint8_t *data);
uint8_t expander_read(uint8_t exp, uint8_t command, uint8_t *data);
uint8_t expander_write_output(uint8_t exp, uint8_t *data);

View File

@ -71,10 +71,14 @@ void matrix_init(void)
matrix_debouncing[i] = 0;
}
PORTD &= ~(1<<PD4);
}
uint8_t matrix_scan(void)
{
kimera_scan();
for (uint8_t i = 0; i < matrix_rows(); i++) {
select_row(i);
_delay_us(30); // without this wait read unstable value.

View File

@ -21,6 +21,8 @@
/* I2C clock in Hz */
#define SCL_CLOCK 400000L
volatile uint8_t i2c_force_stop = 0;
#define CHECK_FORCE_STOP() if(i2c_force_stop){i2c_force_stop=0;break;}
/*************************************************************************
Initialization of the I2C bus interface. Need to be called only once
@ -47,7 +49,7 @@ unsigned char i2c_start(unsigned char address)
TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);
// wait until transmission completed
while(!(TWCR & (1<<TWINT)));
while(!(TWCR & (1<<TWINT))) { CHECK_FORCE_STOP(); };
// check value of TWI Status Register. Mask prescaler bits.
twst = TW_STATUS & 0xF8;
@ -58,7 +60,7 @@ unsigned char i2c_start(unsigned char address)
TWCR = (1<<TWINT) | (1<<TWEN);
// wail until transmission completed and ACK/NACK has been received
while(!(TWCR & (1<<TWINT)));
while(!(TWCR & (1<<TWINT))) { CHECK_FORCE_STOP(); };
// check value of TWI Status Register. Mask prescaler bits.
twst = TW_STATUS & 0xF8;
@ -107,7 +109,7 @@ void i2c_start_wait(unsigned char address)
TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
// wait until stop condition is executed and bus released
while(TWCR & (1<<TWSTO));
while(TWCR & (1<<TWSTO)) { CHECK_FORCE_STOP(); };
continue;
}
@ -142,7 +144,7 @@ void i2c_stop(void)
TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
// wait until stop condition is executed and bus released
while(TWCR & (1<<TWSTO));
while(TWCR & (1<<TWSTO)) { CHECK_FORCE_STOP(); };
}/* i2c_stop */
@ -163,7 +165,7 @@ unsigned char i2c_write( unsigned char data )
TWCR = (1<<TWINT) | (1<<TWEN);
// wait until transmission completed
while(!(TWCR & (1<<TWINT)));
while(!(TWCR & (1<<TWINT))) { CHECK_FORCE_STOP() };
// check value of TWI Status Register. Mask prescaler bits
twst = TW_STATUS & 0xF8;
@ -181,7 +183,7 @@ unsigned char i2c_write( unsigned char data )
unsigned char i2c_readAck(void)
{
TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWEA);
while(!(TWCR & (1<<TWINT)));
while(!(TWCR & (1<<TWINT))) { CHECK_FORCE_STOP(); };
return TWDR;
@ -196,7 +198,7 @@ unsigned char i2c_readAck(void)
unsigned char i2c_readNak(void)
{
TWCR = (1<<TWINT) | (1<<TWEN);
while(!(TWCR & (1<<TWINT)));
while(!(TWCR & (1<<TWINT))) { CHECK_FORCE_STOP(); };
return TWDR;