tom dunigan
/
driftrtc
LPC1768 RTC frequency check with WDT. Compare RTC against MCU's microsecond Timer.
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 // use WDT to measure drift of RTC and IRC 00002 // seems WDT ISR will only fire once 00003 #include "mbed.h" 00004 00005 DigitalOut led1(LED1); 00006 uint32_t ticks; 00007 00008 void kick() { 00009 __disable_irq(); 00010 LPC_WDT->WDFEED = 0xAA; 00011 LPC_WDT->WDFEED = 0x55; 00012 __enable_irq(); 00013 } 00014 00015 void isr_wdt(void) { 00016 ticks++; 00017 led1 = 1; 00018 LPC_WDT->WDMOD |= (1<<3); 00019 // LPC_WDT->WDMOD &= ~(1<<2); 00020 // kick(); 00021 } 00022 00023 void wdt_init() { 00024 LPC_WDT->WDCLKSEL = 0x2; // Set CLK src 0 IRC 1 PCLK 2 RTC 00025 // uint32_t clk = SystemCoreClock / 16; // WD has a fixed /4 prescaler, PCLK default is /4 00026 LPC_WDT->WDTC = 0xfffffff; // max countdown 00027 LPC_WDT->WDMOD = 0x01; // enable but no reset 00028 NVIC_SetVector(WDT_IRQn, (uint32_t)&isr_wdt); 00029 NVIC_ClearPendingIRQ(WDT_IRQn); 00030 NVIC_EnableIRQ(WDT_IRQn); 00031 kick(); 00032 } 00033 00034 int main() { 00035 uint32_t t,t0=0,us,us0,prev; 00036 int ppm; 00037 Timer tmr; 00038 tmr.start(); 00039 printf("hello %x\n",LPC_WDT->WDMOD); 00040 wait(1.0); 00041 wdt_init(); 00042 prev=LPC_WDT->WDTV; 00043 while(1) { 00044 t=LPC_WDT->WDTV; 00045 us = tmr.read_us(); 00046 if (t0==0 || t0 == 255) { 00047 t0 = t; 00048 us0 = us; 00049 } 00050 double tf = (t0-t)/8192.; // /8192 for rtc 1000000 for IRC 00051 double secs = 1.e-6*(us-us0); 00052 ppm = 1.e6*(tf-secs)/secs; 00053 printf("ticks %d %d %u %d us %x %d ppm \n",ticks,prev-t,t0-t,us-us0,t,ppm); 00054 prev=t; 00055 wait(2.0); 00056 } 00057 }
Generated on Tue Jul 12 2022 20:24:43 by 1.7.2