diff --git a/left/src/i2c_watchdog.c b/left/src/i2c_watchdog.c index 79a5764..fdd6fc5 100644 --- a/left/src/i2c_watchdog.c +++ b/left/src/i2c_watchdog.c @@ -3,47 +3,42 @@ #include "i2c_watchdog.h" #include "test_led.h" #include "init_peripherals.h" +#include "main.h" /* NOTE: Because of a bug in the ROM bootloader of the KL03Z, the watchdog timer is disabled and cannot be re-enabled. * See https://community.nxp.com/thread/457893 * Therefore the hardware watchdog timer cannot be used without an extra way to enter bootloader or application mode. */ - -#define WATCH_ENABLE_WATCHDOG (1) /* additionally, un-comment InitI2cWatchdog() in init_peripherals.c */ - -#if WATCH_ENABLE_WATCHDOG -static uint32_t prevWatchdogCounter = 0; +#if KEY_USE_I2C_WATCHDOG_TIMER + static uint32_t prevWatchdogCounter = 0; + static uint32_t I2cWatchdog_RecoveryCounter; /* counter for how many times we had to recover and restart */ #endif -static uint32_t I2cWatchdog_RecoveryCounter; /* counter for how many times we had to recover and restart */ void InitI2cWatchdog(void) { - lptmr_config_t lptmrConfig; - LPTMR_GetDefaultConfig(&lptmrConfig); - LPTMR_Init(LPTMR0, &lptmrConfig); - LPTMR_SetTimerPeriod(LPTMR0, USEC_TO_COUNT(LPTMR_USEC_COUNT, LPTMR_SOURCE_CLOCK)); /* set LPTM for a 100 ms period */ - LPTMR_EnableInterrupts(LPTMR0, kLPTMR_TimerInterruptEnable); - EnableIRQ(LPTMR0_IRQn); - LPTMR_StartTimer(LPTMR0); } -#if WATCH_ENABLE_WATCHDOG -void I2C_WATCHDOG_LPTMR_HANDLER(void) +#if KEY_USE_I2C_WATCHDOG_TIMER +void RunWatchdog(void) { static volatile uint32_t I2cWatchdog_WatchCounter = 0; /* counter for timer */ - TEST_LED_TOGGLE(); - I2cWatchdog_WatchCounter++; + static int cntr = 0; - if (I2cWatchdog_WatchCounter>1) { /* do not check within the first 100 ms, as I2C might not be running yet */ - if (I2C_Watchdog == prevWatchdogCounter) { // Restart I2C if there hasn't been any interrupt during 100 ms. I2C_Watchdog gets incremented for every I2C transaction - // NVIC_SystemReset(); - I2cWatchdog_RecoveryCounter++; - I2C_SlaveDeinit(I2C_BUS_BASEADDR); - InitI2c(); + cntr++; + if (cntr==100) { /* we get called from KEY_SCANNER_HANDLER() which runs at 1ms, thus scaling down by 100 here to get 100 ms period */ + cntr=0; + TEST_LED_TOGGLE(); + I2cWatchdog_WatchCounter++; + + if (I2cWatchdog_WatchCounter>10) { /* do not check within the first 1000 ms, as I2C might not be running yet */ + if (I2C_Watchdog == prevWatchdogCounter) { // Restart I2C if there hasn't been any interrupt during 100 ms. I2C_Watchdog gets incremented for every I2C transaction + // NVIC_SystemReset(); + I2cWatchdog_RecoveryCounter++; + I2C_SlaveDeinit(I2C_BUS_BASEADDR); + InitI2c(); + } } - } - prevWatchdogCounter = I2C_Watchdog; /* remember previous counter */ - - LPTMR_ClearStatusFlags(LPTMR0, kLPTMR_TimerCompareFlag); + prevWatchdogCounter = I2C_Watchdog; /* remember previous counter */ + } } #endif diff --git a/left/src/init_peripherals.c b/left/src/init_peripherals.c index 267cdaf..4f2356d 100644 --- a/left/src/init_peripherals.c +++ b/left/src/init_peripherals.c @@ -10,6 +10,7 @@ #include "slave_protocol_handler.h" #include "i2c_watchdog.h" #include "debug_over_spi.h" +#include "main.h" i2c_slave_config_t slaveConfig; i2c_slave_handle_t slaveHandle; @@ -87,5 +88,7 @@ void InitPeripherals(void) LedPwm_Init(); DebugOverSpi_Init(); InitI2c(); +#if KEY_USE_I2C_WATCHDOG_TIMER InitI2cWatchdog(); +#endif } diff --git a/left/src/key_scanner.c b/left/src/key_scanner.c index d8990ee..96499ef 100644 --- a/left/src/key_scanner.c +++ b/left/src/key_scanner.c @@ -1,10 +1,16 @@ #include "fsl_lptmr.h" #include "key_scanner.h" #include "main.h" +#if KEY_USE_I2C_WATCHDOG_TIMER + #include "i2c_watchdog.h" +#endif void KEY_SCANNER_HANDLER(void) { KeyMatrix_ScanRow(&keyMatrix); +#if KEY_USE_I2C_WATCHDOG_TIMER + RunWatchdog(); +#endif LPTMR_ClearStatusFlags(KEY_SCANNER_LPTMR_BASEADDR, kLPTMR_TimerCompareFlag); } diff --git a/left/src/main.h b/left/src/main.h index 7df0c8c..0397f7a 100644 --- a/left/src/main.h +++ b/left/src/main.h @@ -14,4 +14,6 @@ extern key_matrix_t keyMatrix; + #define KEY_USE_I2C_WATCHDOG_TIMER (1) /* if set to 1, every 100 ms the I2C communication is checked */ + #endif