Hi,
I was trying to find something in the documents provided by freescale and also sample BSP provided by freescale. I wanted to use watchdog timer for my project. Can someone please help me with a sample code of watchdog timer initialisation, setting the timer and using it.
Please post me some example to learn watchdog functioning on freedom kl26 board.
Thanks.
Thanks Jorge,
It was a great help. I Tried it and it worked fine. :smileyhappy:
Hello Mridul Pandey:
The watchdog or COP (Computer Operating Properly) in KL26 is very simple and not hard to configure. You only need to configure the SIM_COPC register according to Table 3-23 COP configuration options in the KL26 Reference Manual:
In the example code below for FRDM-KL26 the COP is configured for a 1 second timeout. If you compile with the macro RESET_WDOG = 0 the blue LED will toggle each second because the MCU is resetting. Otherwise if you compile with RESET_WDOG = 1, the counter is refreshed in the while loop and the MCU does not reset.
:smileyalert: Make sure that the SIM_COPC is not written in the startup code of the project. Typically the watchdog is disabled at startup and this register can only be written once after reset.
#include "MKL26Z4.h"
#define RESET_WDOG 0
int counter = 0;
int main(void)
{
/* Initialize blue LED */
SIM_SCGC5 |= SIM_SCGC5_PORTD_MASK; // Enable clock gate for PORTD
PORTD_PCR5 = PORT_PCR_MUX(1); // PTD5 as GPIO
GPIOD_PDDR |= (1 << 5); // PTD5 as output
GPIOD_PDOR &= !(1 << 5); // Output 0 [Blue LED off]
for (counter = 0; counter <= 50000; counter++){} // Delay
GPIOD_PDOR |= (1 << 5); // Output 1 [Blue LED on
/* Configure watchdog (COP) */
SIM_COPC = 0xC; // COPT = 11 [Timeout after 2^10 LPO cycles] OR [Timeout after 2^18 bus clock cycles]
// COPCLKS = 0 [LPO (1kHz) clock selected]
// COPW = 0 [Normal mode]
// Timeout = (1/LPO clock) x 2^10 = (1/1 kHz) x 2^18 = 1 s
while(1)
{
#if RESET_WDOG
for (counter = 0; counter <= 50000; counter++){} // Delay
SIM_SRVCOP = 0x55;
SIM_SRVCOP = 0xAA;
#endif
}
return 0;
}
The COP is also supported by the KSDK platform library, which you can download from the platform page: Software Development Kit for Kinetis MCUs|Freescale
Once installed you can find an example for COP in the next installation path:
C:\Freescale\KSDK_1.2.0\examples\frdmkl26z\driver_examples\cop
Regards!,
Jorge Gonzalez
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------