|
|
|
|
@@ -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
|
|
|
|
|
|