diff --git a/left/src/config.h b/left/src/config.h index 83e512b..1e7da00 100644 --- a/left/src/config.h +++ b/left/src/config.h @@ -3,6 +3,11 @@ // Macros: + #define I2C_WATCHDOG_VALUE_REINIT 1 + #define I2C_WATCHDOG_VALUE_REBOOT 2 + // #define DEBUG_OVER_SPI +// #define I2C_WATCHDOG I2C_WATCHDOG_VALUE_REINIT +// #define I2C_WATCHDOG I2C_WATCHDOG_VALUE_REBOOT #endif diff --git a/left/src/i2c_watchdog.c b/left/src/i2c_watchdog.c index fdd6fc5..c46c679 100644 --- a/left/src/i2c_watchdog.c +++ b/left/src/i2c_watchdog.c @@ -3,22 +3,16 @@ #include "i2c_watchdog.h" #include "test_led.h" #include "init_peripherals.h" -#include "main.h" +#include "config.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. */ -#if KEY_USE_I2C_WATCHDOG_TIMER +#ifdef I2C_WATCHDOG static uint32_t prevWatchdogCounter = 0; static uint32_t I2cWatchdog_RecoveryCounter; /* counter for how many times we had to recover and restart */ -#endif -void InitI2cWatchdog(void) -{ -} - -#if KEY_USE_I2C_WATCHDOG_TIMER void RunWatchdog(void) { static volatile uint32_t I2cWatchdog_WatchCounter = 0; /* counter for timer */ @@ -32,10 +26,14 @@ void RunWatchdog(void) 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++; +#if I2C_WATCHDOG == I2C_WATCHDOG_VALUE_REBOOT + NVIC_SystemReset(); +#endif +#if I2C_WATCHDOG == I2C_WATCHDOG_VALUE_REINIT I2C_SlaveDeinit(I2C_BUS_BASEADDR); InitI2c(); +#endif } } prevWatchdogCounter = I2C_Watchdog; /* remember previous counter */ diff --git a/left/src/i2c_watchdog.h b/left/src/i2c_watchdog.h index 5a751e8..3817793 100644 --- a/left/src/i2c_watchdog.h +++ b/left/src/i2c_watchdog.h @@ -13,7 +13,6 @@ // Functions: - void InitI2cWatchdog(void); void RunWatchdog(void); #endif diff --git a/left/src/init_peripherals.c b/left/src/init_peripherals.c index 4f2356d..cb1012a 100644 --- a/left/src/init_peripherals.c +++ b/left/src/init_peripherals.c @@ -88,7 +88,4 @@ 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 96499ef..9940693 100644 --- a/left/src/key_scanner.c +++ b/left/src/key_scanner.c @@ -1,14 +1,12 @@ #include "fsl_lptmr.h" #include "key_scanner.h" -#include "main.h" -#if KEY_USE_I2C_WATCHDOG_TIMER - #include "i2c_watchdog.h" -#endif +#include "config.h" +#include "i2c_watchdog.h" void KEY_SCANNER_HANDLER(void) { KeyMatrix_ScanRow(&keyMatrix); -#if KEY_USE_I2C_WATCHDOG_TIMER +#ifdef I2C_WATCHDOG RunWatchdog(); #endif LPTMR_ClearStatusFlags(KEY_SCANNER_LPTMR_BASEADDR, kLPTMR_TimerCompareFlag); diff --git a/left/src/main.h b/left/src/main.h index 0397f7a..7df0c8c 100644 --- a/left/src/main.h +++ b/left/src/main.h @@ -14,6 +14,4 @@ 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