From bad56a4f2b91fc8591f6d33a1710ea0050abcfbf Mon Sep 17 00:00:00 2001 From: Jack Humbert Date: Tue, 12 Jun 2018 23:37:06 -0400 Subject: [PATCH] adds timeout to avr i2c --- drivers/avr/i2c_master.c | 89 ++++++++++++++++++++++++++++++----- drivers/avr/i2c_master.h | 2 +- drivers/avr/is31fl3731.c | 56 ++++++++++++---------- drivers/avr/is31fl3731.h | 10 ++-- keyboards/ergodox_ez/config.h | 2 + quantum/quantum.c | 2 +- quantum/rgb_matrix.c | 79 +++++++++++++++++-------------- quantum/rgb_matrix.h | 3 +- 8 files changed, 163 insertions(+), 80 deletions(-) diff --git a/drivers/avr/i2c_master.c b/drivers/avr/i2c_master.c index cd3c2e1c8..97f690043 100755 --- a/drivers/avr/i2c_master.c +++ b/drivers/avr/i2c_master.c @@ -6,6 +6,7 @@ #include #include "i2c_master.h" +#include "timer.h" #define F_SCL 400000UL // SCL frequency #define Prescaler 1 @@ -24,8 +25,18 @@ uint8_t i2c_start(uint8_t address) TWCR = 0; // transmit START condition TWCR = (1< I2C_TIMEOUT) { + return 2; // should make these codes standard + } + } + #else + // wait for end of transmission + while( !(TWCR & (1< I2C_TIMEOUT) { + return 2; // should make these codes standard + } + } + #else + // wait for end of transmission + while( !(TWCR & (1< I2C_TIMEOUT) { + return 2; // should make these codes standard + } + } + #else // wait for end of transmission - while( !(TWCR & (1< I2C_TIMEOUT) { + return 2; // should make these codes standard + } + } + #else + // wait for end of transmission + while( !(TWCR & (1< I2C_TIMEOUT) { + return 2; // should make these codes standard + } + } + #else + // wait for end of transmission + while( !(TWCR & (1< I2C_TIMEOUT) { + return 2; // should make these codes standard + } + } + #else + // wait for end of transmission + while(TWCR & (1<. //#define NO_ACTION_FUNCTION //#define DEBUG_MATRIX_SCAN_RATE +#define I2C_TIMEOUT 1000 + #endif diff --git a/quantum/quantum.c b/quantum/quantum.c index cfa3df741..5abd222d1 100644 --- a/quantum/quantum.c +++ b/quantum/quantum.c @@ -854,7 +854,7 @@ void matrix_init_quantum() { audio_init(); #endif #ifdef RGB_MATRIX_ENABLE - rgb_matrix_init_drivers(); + rgb_matrix_init(); #endif matrix_init_kb(); } diff --git a/quantum/rgb_matrix.c b/quantum/rgb_matrix.c index 8c41fc54d..d71283f7c 100644 --- a/quantum/rgb_matrix.c +++ b/quantum/rgb_matrix.c @@ -101,10 +101,14 @@ void map_row_column_to_led( uint8_t row, uint8_t column, uint8_t *led_i, uint8_t } } - void rgb_matrix_update_pwm_buffers(void) { - IS31FL3731_update_pwm_buffers( DRIVER_ADDR_1, DRIVER_ADDR_2 ); - IS31FL3731_update_led_control_registers( DRIVER_ADDR_1, DRIVER_ADDR_2 ); + uint8_t ret = IS31FL3731_update_pwm_buffers( DRIVER_ADDR_1, DRIVER_ADDR_2 ); + ret |= IS31FL3731_update_led_control_registers( DRIVER_ADDR_1, DRIVER_ADDR_2 ); + if (ret == 2) { + wait_ms(1000); + i2c_stop(); + rgb_matrix_setup_drivers(); + } } void rgb_matrix_set_color( int index, uint8_t red, uint8_t green, uint8_t blue ) { @@ -115,7 +119,6 @@ void rgb_matrix_set_color_all( uint8_t red, uint8_t green, uint8_t blue ) { IS31FL3731_set_color_all( red, green, blue ); } - bool process_rgb_matrix(uint16_t keycode, keyrecord_t *record) { if ( record->event.pressed ) { uint8_t led[8], led_count; @@ -218,7 +221,7 @@ void rgb_matrix_single_LED_test(void) { } // All LEDs off -void rgb_matrix_all_off(void) { +void rgb_matrix_all_off(void) { rgb_matrix_set_color_all( 0, 0, 0 ); } @@ -244,7 +247,7 @@ void rgb_matrix_solid_reactive(void) { // alphas = color1, mods = color2 void rgb_matrix_alphas_mods(void) { - + RGB rgb1 = hsv_to_rgb( (HSV){ .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val } ); RGB rgb2 = hsv_to_rgb( (HSV){ .h = (rgb_matrix_config.hue + 180) % 360, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val } ); @@ -722,40 +725,44 @@ void rgb_matrix_indicators_user(void) {} // } // } -void rgb_matrix_init_drivers(void) { - // Initialize TWI - i2c_init(); - IS31FL3731_init( DRIVER_ADDR_1 ); - IS31FL3731_init( DRIVER_ADDR_2 ); +void rgb_matrix_init(void) { + rgb_matrix_setup_drivers(); - for ( int index = 0; index < DRIVER_LED_TOTAL; index++ ) { - bool enabled = true; - // This only caches it for later - IS31FL3731_set_led_control_register( index, enabled, enabled, enabled ); - } - // This actually updates the LED drivers - IS31FL3731_update_led_control_registers( DRIVER_ADDR_1, DRIVER_ADDR_2 ); + // TODO: put the 1 second startup delay here? - // TODO: put the 1 second startup delay here? - - // clear the key hits - for ( int led=0; led