changed I2C watchdog timer implementation, as LPTMR was already used by key scanner

This commit is contained in:
Erich Styger
2017-12-25 17:15:41 +01:00
parent 5581dd26b5
commit 7eb83173ed
4 changed files with 33 additions and 27 deletions

View File

@@ -3,38 +3,34 @@
#include "i2c_watchdog.h" #include "i2c_watchdog.h"
#include "test_led.h" #include "test_led.h"
#include "init_peripherals.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. /* 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 * 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. * Therefore the hardware watchdog timer cannot be used without an extra way to enter bootloader or application mode.
*/ */
#if KEY_USE_I2C_WATCHDOG_TIMER
#define WATCH_ENABLE_WATCHDOG (1) /* additionally, un-comment InitI2cWatchdog() in init_peripherals.c */
#if WATCH_ENABLE_WATCHDOG
static uint32_t prevWatchdogCounter = 0; static uint32_t prevWatchdogCounter = 0;
#endif
static uint32_t I2cWatchdog_RecoveryCounter; /* counter for how many times we had to recover and restart */ static uint32_t I2cWatchdog_RecoveryCounter; /* counter for how many times we had to recover and restart */
#endif
void InitI2cWatchdog(void) 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 #if KEY_USE_I2C_WATCHDOG_TIMER
void I2C_WATCHDOG_LPTMR_HANDLER(void) void RunWatchdog(void)
{ {
static volatile uint32_t I2cWatchdog_WatchCounter = 0; /* counter for timer */ static volatile uint32_t I2cWatchdog_WatchCounter = 0; /* counter for timer */
static int cntr = 0;
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(); TEST_LED_TOGGLE();
I2cWatchdog_WatchCounter++; I2cWatchdog_WatchCounter++;
if (I2cWatchdog_WatchCounter>1) { /* do not check within the first 100 ms, as I2C might not be running yet */ 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 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(); // NVIC_SystemReset();
I2cWatchdog_RecoveryCounter++; I2cWatchdog_RecoveryCounter++;
@@ -43,7 +39,6 @@ void I2C_WATCHDOG_LPTMR_HANDLER(void)
} }
} }
prevWatchdogCounter = I2C_Watchdog; /* remember previous counter */ prevWatchdogCounter = I2C_Watchdog; /* remember previous counter */
}
LPTMR_ClearStatusFlags(LPTMR0, kLPTMR_TimerCompareFlag);
} }
#endif #endif

View File

@@ -10,6 +10,7 @@
#include "slave_protocol_handler.h" #include "slave_protocol_handler.h"
#include "i2c_watchdog.h" #include "i2c_watchdog.h"
#include "debug_over_spi.h" #include "debug_over_spi.h"
#include "main.h"
i2c_slave_config_t slaveConfig; i2c_slave_config_t slaveConfig;
i2c_slave_handle_t slaveHandle; i2c_slave_handle_t slaveHandle;
@@ -87,5 +88,7 @@ void InitPeripherals(void)
LedPwm_Init(); LedPwm_Init();
DebugOverSpi_Init(); DebugOverSpi_Init();
InitI2c(); InitI2c();
#if KEY_USE_I2C_WATCHDOG_TIMER
InitI2cWatchdog(); InitI2cWatchdog();
#endif
} }

View File

@@ -1,10 +1,16 @@
#include "fsl_lptmr.h" #include "fsl_lptmr.h"
#include "key_scanner.h" #include "key_scanner.h"
#include "main.h" #include "main.h"
#if KEY_USE_I2C_WATCHDOG_TIMER
#include "i2c_watchdog.h"
#endif
void KEY_SCANNER_HANDLER(void) void KEY_SCANNER_HANDLER(void)
{ {
KeyMatrix_ScanRow(&keyMatrix); KeyMatrix_ScanRow(&keyMatrix);
#if KEY_USE_I2C_WATCHDOG_TIMER
RunWatchdog();
#endif
LPTMR_ClearStatusFlags(KEY_SCANNER_LPTMR_BASEADDR, kLPTMR_TimerCompareFlag); LPTMR_ClearStatusFlags(KEY_SCANNER_LPTMR_BASEADDR, kLPTMR_TimerCompareFlag);
} }

View File

@@ -14,4 +14,6 @@
extern key_matrix_t keyMatrix; 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 #endif