From: ian Date: Sat, 17 May 2008 14:21:52 +0000 (+0000) Subject: handle watchdog X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ijackson/git?a=commitdiff_plain;h=13c71057c5989a36a90bda0b5b8d35f18f53d8e7;p=trains.git handle watchdog --- diff --git a/hostside/parse-proto-spec b/hostside/parse-proto-spec index 8e94b51..e739902 100755 --- a/hostside/parse-proto-spec +++ b/hostside/parse-proto-spec @@ -98,6 +98,7 @@ sub process_line () { $v{cname}= $cname; $v{noiselevel}= ($cname =~ m/nmradone/ ? 3 : + $cname =~ m/watchdog/ ? 3 : $cname =~ m/p[io]ng/ ? 2 : $cname =~ m/detect/ ? 2 : 0); diff --git a/hostside/safety.h b/hostside/safety.h index 7c07b4d..735145c 100644 --- a/hostside/safety.h +++ b/hostside/safety.h @@ -360,9 +360,14 @@ int trackloc_set_exactinto(TrackLocation *t, TrackAdvanceContext *c, #define MARGIN_NOSE 6 /*mm*/ #define MARGIN_TAIL 6 /*mm*/ #define MARGIN_SPEED 1.2 /*ratio*/ -#define MARGIN_STOPTIME 50 /*ms*/ #define MARGIN_AUTOPOINTTIME 500 /*ms*/ -#define MARGIN_POLARISETIME 50 /*ms*/ #define UNMARGIN_ROUNDING 1e-4 /* mm/ms; for 1s, leads to max err of 100um */ +#define MARGIN_STOPTIME 60 /*ms*/ +#define MARGIN_POLARISETIME 60 /*ms*/ + +#define UNMARGIN_WATCHDOG_16 3 /* 16ms units */ /* should be < MARGIN_*TIME */ +#define UNMARGIN_WATCHDOG (UNMARGIN_WATCHDOG_16*16) /*ms*/ +#define MARGIN_WATCHDOG 20 + #endif /*SAFETY_H*/ diff --git a/hostside/startup.c b/hostside/startup.c index fc97508..fb7c137 100644 --- a/hostside/startup.c +++ b/hostside/startup.c @@ -9,20 +9,14 @@ const char *const stastatelist[]= DEFINE_STASTATE_DATA; StartupState sta_state; static TimeoutEvent sta_toev= { .pclass="startup", .pinst="sta" }; -static TimeoutEvent ping_toev= { .pclass="startup", .pinst="ping" }; -static int ping_seq; static PicInsn piob; static void sta_goto(StartupState new_state); -static void timedout_onward(TimeoutEvent *toev) { - assert(sta_state != Sta_Run); - if (sta_state == Sta_Settling) { - enco_pic_off(&piob); - serial_transmit(&piob); - } - sta_goto(sta_state == Sta_Flush ? Sta_Ping : sta_state + 1); -} +/*---------- ping ----------*/ + +static int ping_seq; +static TimeoutEvent ping_toev= { .pclass="startup", .pinst="ping" }; static void timedout_ping(TimeoutEvent *toev) { assert(sta_state >= Sta_Ping); @@ -40,13 +34,47 @@ static void initial_ping(void) { struct timeval now; mgettimeofday(&now); -oprintf(DUPO("ip") " %ld.%06ld\n", now.tv_sec, now.tv_usec); ping_seq= (now.tv_sec & 0x1fU) << 5; /* bottom 5bi of secs: period 32s */ ping_seq |= (now.tv_usec >> 15); /* top 5bi of 20bi us: res.~2^15us */ ping_toev.duration= 300; timefor_ping(0); } +/*---------- watchdog ----------*/ + +static TimeoutEvent watchdog_toev= { .pclass="startup", .pinst="watchdog" }; +static PicInsn watchdog_piob; + +static void watchdog_transmit(TimeoutEvent *toev) { + toev_start(&watchdog_toev); + serial_transmit(&watchdog_piob); +} + +static void watchdog_start(void) { + watchdog_toev.duration= UNMARGIN_WATCHDOG - MARGIN_WATCHDOG; + watchdog_toev.callback= watchdog_transmit; + enco_pic_watchdog(&watchdog_piob, UNMARGIN_WATCHDOG_16); + watchdog_transmit(0); +} + +static void watchdog_stop(void) { + if (!watchdog_toev.running) return; + toev_stop(&watchdog_toev); + enco_pic_watchdog(&watchdog_piob, 0); + serial_transmit(&watchdog_piob); +} + +/*---------- main startup algorithm ----------*/ + +static void timedout_onward(TimeoutEvent *toev) { + assert(sta_state != Sta_Run); + if (sta_state == Sta_Settling) { + enco_pic_off(&piob); + serial_transmit(&piob); + } + sta_goto(sta_state == Sta_Flush ? Sta_Ping : sta_state + 1); +} + static void sta_startup_manual(void) { waggle_startup_manual(); retransmit_start(); @@ -119,6 +147,9 @@ static void sta_goto(StartupState new_state) { } if (piob.l) serial_transmit(&piob); + if (new_state >= Sta_Run) watchdog_start(); + else watchdog_stop(); + toev_start(&sta_toev); sta_state= new_state; @@ -228,9 +259,7 @@ void on_pic_fault(const PicInsnInfo *pii, const PicInsn *pi, int objnum) { void on_pic_wtimeout(const PicInsnInfo *pii, const PicInsn *pi, int objnum) { if (sta_state <= Sta_Settling) return; - if (sta_state == Sta_Resolving || sta_state == Sta_Finalising) - die("PIC sent WTIMEOUT in Resolving or Finalising"); - oprintf(UPO, "warning watchdog : PIC watchdog timer triggered\n"); + die("microcontrollers' watchdog timer triggered\n"); } void on_pic_hello(const PicInsnInfo *pii, const PicInsn *pi, int objnum)