lpcware

watchdog timing

Discussion created by lpcware Employee on Jun 15, 2016
Latest reply on Jun 15, 2016 by lpcware
Content originally posted in LPCWare by borik2003 on Wed Nov 24 04:40:09 MST 2010
Hi

I am trying to write watchdog function with programmable time out

[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055][LEFT]void[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [B]WDTInit[/B]([/SIZE][SIZE=2][COLOR=#005032][SIZE=2][COLOR=#005032]uint32_t[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] timeout_us )
{
[/SIZE][SIZE=2][COLOR=#005032][SIZE=2][COLOR=#005032]uint32_t[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] pclk_wdt = __WDT_OSC_CLK;
[/SIZE][SIZE=2][COLOR=#005032][SIZE=2][COLOR=#005032]uint32_t[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] tempval = 0;

[/SIZE][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]//[U]Selectable[/U] time period from (TWDCLK × 256 × 4) to (TWDCLK × 2^24 × 4) in multiples of TWDCLK × 4.[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f] [/LEFT]
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][LEFT]
tempval = timeout_us;
[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]if[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2](!((tempval >= 256) && (tempval <= 16777215)))
{
[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]return[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];
}

[/SIZE][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]//wdt_osc_clk = [U]Fclkana[/U]&#8260;(2 × (1 + DIVSEL)) = 7.8 kHz to 1.7 MHz (nominal values).[/LEFT]
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][LEFT]
LPC_SYSCON->[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]WDTOSCCTRL[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = 0 | (SYSCON_WDT_FREQ_3_4 << 5); [/SIZE][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]//[U]Fclkana[/U]=3.4Mhz,DIVSEL = 0 => wdt_osc_clk=1.7 MHz[/LEFT]
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][LEFT]LPC_SYSCON->[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]WDTCLKSEL[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = SYSCON_WDT_CLK_WDTOSC; [/SIZE][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]// Select clock source[/LEFT]
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][LEFT]LPC_SYSCON->[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]WDTCLKUEN[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = 0x01; [/SIZE][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]// Update clock[/LEFT]
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][LEFT]LPC_SYSCON->[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]WDTCLKUEN[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = 0x00; [/SIZE][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]// Toggle update register once[/LEFT]
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][LEFT]LPC_SYSCON->[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]WDTCLKUEN[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = 0x01;
[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]while[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] ( !(LPC_SYSCON->[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]WDTCLKUEN[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] & 0x01) ); [/SIZE][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]// Wait until updated[/LEFT]
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][LEFT]LPC_SYSCON->[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]WDTCLKDIV[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = 1; [/SIZE][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]// Divided by 1[/LEFT]
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][LEFT]LPC_SYSCON->[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]PDRUNCFG[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] &= ~(0x1<<6); [/SIZE][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]// Let WDT clock run[/LEFT]
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][LEFT]
&#12288;
[/SIZE][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]// Enable clock to WDT[/LEFT]
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][LEFT]LPC_SYSCON->[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]SYSAHBCLKCTRL[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] |= (1<<15);

wdt_counter = 0;

[/SIZE][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]//Enable the WDT Interrupt[/LEFT]
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][LEFT]NVIC_EnableIRQ([/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]WDT_IRQn[/I][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);

LPC_WDT->[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]TC[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = tempval;

LPC_WDT->[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]MOD[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = WDEN | WDRESET;[/LEFT]
[/SIZE][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f][LEFT]//LPC_WDT->MOD = WDEN;[/LEFT]
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][LEFT]
LPC_WDT->[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]FEED[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = 0xAA; [/SIZE][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]// Feeding sequence[/LEFT]
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][LEFT]LPC_WDT->[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]FEED[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = 0x55;

[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]return[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/LEFT]
}


[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055][LEFT]int[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [B]main[/B] ([/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]void[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2])
{
InitPLLIRC(48);

[/SIZE][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]// Set port for LED to output[/LEFT]
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][LEFT]SetupGPIO(LED_PORT,LED_BIT,GPIO_DIR_OUTPUT,GPIO_FUNC_MASK,GPIO_FUNC_0);

WDTInit(100000);[/SIZE][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]//WDT_TIMEOUT_US(50000));[/LEFT]
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][LEFT]
&#12288;
SetGPIOBit(LED_PORT, LED_BIT);
delay32_Us(0, 100);
ClrGPIOBit(LED_PORT, LED_BIT);

[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]while[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (1)
{[/LEFT]
[/SIZE][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f][LEFT]    //no feeed to get wdt reset[/LEFT]
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]}

i send to [B]WDTInit() value =100,000 that means (1/1.7Mhz)*100,000*4 = 235.3ms timeout[/B]

[B]but what i get is time out of 174ms ( i see it between led toggles)[/B]

[B]what i am missing ?[/B]
[/SIZE][/SIZE]

Outcomes