From d621c06d68c85d03ce26785e0e03a31eea6caacf Mon Sep 17 00:00:00 2001 From: Kai Ryu Date: Thu, 31 Mar 2016 19:08:50 +0900 Subject: [PATCH] kimera: Restructure i2c init and force stop --- keyboard/kimera/Makefile | 1 + keyboard/kimera/i2c_wrapper.c | 95 +++++++++++++++++++++++++++++++++++ keyboard/kimera/i2c_wrapper.h | 25 +++++++++ keyboard/kimera/kimera.c | 63 ----------------------- keyboard/kimera/matrix.c | 5 ++ 5 files changed, 126 insertions(+), 63 deletions(-) create mode 100644 keyboard/kimera/i2c_wrapper.c create mode 100644 keyboard/kimera/i2c_wrapper.h diff --git a/keyboard/kimera/Makefile b/keyboard/kimera/Makefile index 74264b49..227af26a 100644 --- a/keyboard/kimera/Makefile +++ b/keyboard/kimera/Makefile @@ -54,6 +54,7 @@ SRC = keymap_common.c \ backlight.c \ ledmap.c \ twimaster.c \ + i2c_wrapper.c \ kimera.c \ light_ws2812.c \ rgb.c diff --git a/keyboard/kimera/i2c_wrapper.c b/keyboard/kimera/i2c_wrapper.c new file mode 100644 index 00000000..07f87838 --- /dev/null +++ b/keyboard/kimera/i2c_wrapper.c @@ -0,0 +1,95 @@ +/* +Copyright 2016 Kai Ryu + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +#include +#include +#include +#include "i2cmaster.h" +#include "i2c_wrapper.h" +#include "debug.h" + +#define wdt_intr_enable(value) \ +__asm__ __volatile__ ( \ + "in __tmp_reg__,__SREG__" "\n\t" \ + "cli" "\n\t" \ + "wdr" "\n\t" \ + "sts %0,%1" "\n\t" \ + "out __SREG__,__tmp_reg__" "\n\t" \ + "sts %0,%2" "\n\t" \ + : /* no outputs */ \ + : "M" (_SFR_MEM_ADDR(_WD_CONTROL_REG)), \ + "r" (_BV(_WD_CHANGE_BIT) | _BV(WDE)), \ + "r" ((uint8_t) ((value & 0x08 ? _WD_PS3_MASK : 0x00) | \ + _BV(WDIE) | (value & 0x07)) ) \ + : "r0" \ +) + +#define SCL_CLOCK 400000L +#define SCL_DURATION (1000000L/SCL_CLOCK)/2 + +static uint8_t i2c_wdt_enabled = 0; + +extern uint8_t i2c_force_stop; + +static void wdt_init(void); + +void i2c_wrapper_init(void) +{ + /* init i2c */ + i2c_init(); + + /* init watch dog */ + wdt_init(); +} + +void i2c_wrapper_task(void) +{ + /* reset watch dog counter */ + wdt_reset(); +} + +void wdt_init(void) +{ + cli(); + wdt_reset(); + wdt_intr_enable(WDTO_2S); + sei(); +} + +ISR(WDT_vect) +{ + xprintf("i2c timeout\n"); + + /* let slave to release SDA */ + TWCR = 0; + DDRD |= (1< + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +#ifndef I2C_WRAPPER_H +#define I2C_WRAPPER_H + +void i2c_wrapper_init(void); +void i2c_wrapper_task(void); + +#endif + diff --git a/keyboard/kimera/kimera.c b/keyboard/kimera/kimera.c index 66fbeeb6..af85685c 100644 --- a/keyboard/kimera/kimera.c +++ b/keyboard/kimera/kimera.c @@ -19,34 +19,11 @@ along with this program. If not, see . #include #include -#include -#include -#include #include "action.h" #include "i2cmaster.h" #include "kimera.h" #include "debug.h" -#define wdt_intr_enable(value) \ -__asm__ __volatile__ ( \ - "in __tmp_reg__,__SREG__" "\n\t" \ - "cli" "\n\t" \ - "wdr" "\n\t" \ - "sts %0,%1" "\n\t" \ - "out __SREG__,__tmp_reg__" "\n\t" \ - "sts %0,%2" "\n\t" \ - : /* no outputs */ \ - : "M" (_SFR_MEM_ADDR(_WD_CONTROL_REG)), \ - "r" (_BV(_WD_CHANGE_BIT) | _BV(WDE)), \ - "r" ((uint8_t) ((value & 0x08 ? _WD_PS3_MASK : 0x00) | \ - _BV(WDIE) | (value & 0x07)) ) \ - : "r0" \ -) - -#define SCL_CLOCK 400000L -#define SCL_DURATION (1000000L/SCL_CLOCK)/2 -extern uint8_t i2c_force_stop; - static uint8_t row_mapping[PX_COUNT] = { #ifndef TWO_HEADED_KIMERA 0, 1, 2, 3, 4, 5, 6, 7, @@ -106,24 +83,10 @@ void kimera_init(void) write_matrix_mapping(); } - /* init i2c */ - i2c_init(); - - /* init watch dog */ - wdt_init(); - /* init i/o expanders */ kimera_scan(); } -void wdt_init(void) -{ - cli(); - wdt_reset(); - wdt_intr_enable(WDTO_1S); - sei(); -} - uint8_t read_matrix_mapping(void) { uint8_t error = 0; @@ -396,7 +359,6 @@ void expander_init(uint8_t exp) uint8_t expander_write(uint8_t exp, uint8_t command, uint8_t *data) { - wdt_reset(); if ((exp_online & (1<. #include "debug.h" #include "util.h" #include "matrix.h" +#include "i2c_wrapper.h" #include "kimera.h" #include "keymap_in_eeprom.h" #include "timer.h" @@ -70,6 +71,9 @@ void matrix_init(void) MCUCR = (1<