From f3527386d486bf3c3578f4df292d7dc1bc86023a Mon Sep 17 00:00:00 2001 From: Krzysztof Mazur Date: Sat, 7 Oct 2017 11:31:02 +0200 Subject: [PATCH 2/2] add support for event active/led time configuration The event active/led time must be run-time configurable. Currently we need only one global event active/led time, so the grader-rt can keep sending it to all I/O modules, using broadcast. Add support for that. Signed-off-by: Krzysztof Mazur --- include/gcp.h | 11 ++++++++++ include/timer.h | 4 +++- src/main.c | 61 +++++++++++++++++++++++++++++++++++++++++-------------- src/posix-timer.c | 2 +- src/timer.c | 6 +++--- 5 files changed, 64 insertions(+), 20 deletions(-) diff --git a/include/gcp.h b/include/gcp.h index 4279821..ee7ce04 100644 --- a/include/gcp.h +++ b/include/gcp.h @@ -125,6 +125,17 @@ #define GCP_EVENT_OUTPUT_BITS 8 #define GCP_EVENT_OUTPUT_MASK ((1 << GCP_EVENT_OUTPUT_BITS) - 1) +/* + * 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * 0 | time | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * + * time - in microseconds + */ +#define GCP_CEVENT_ACTIVE_TIME 0x008 +#define GCP_CEVENT_LED_TIME 0x009 + /* * Grader periodic scheduling interface * diff --git a/include/timer.h b/include/timer.h index 9e93588..def9524 100644 --- a/include/timer.h +++ b/include/timer.h @@ -20,6 +20,8 @@ #define TICKS_PER_US (GETTIME_FREQUENCY / 1000000UL) +#define time_after(a, b) ((int)((int)(b) - (int)(a)) < 0) + static inline unsigned long us_to_ticks(unsigned long t) { return t * TICKS_PER_US; @@ -30,7 +32,7 @@ static inline unsigned long ticks_to_us(unsigned long t) return (t + TICKS_PER_US/2) / TICKS_PER_US; } -unsigned long gettime(void); +unsigned int gettime(void); void timer_init(void); #endif diff --git a/src/main.c b/src/main.c index b40e31b..ddd8309 100644 --- a/src/main.c +++ b/src/main.c @@ -64,6 +64,8 @@ /* assumed event delay, used for prediction */ static unsigned long event_delay; +static unsigned long event_active_time; +static unsigned long event_led_time; #if 1 #ifdef HAVE_USLEEP @@ -359,6 +361,16 @@ static void gcp_handle_command(struct tty_struct *tty, ret = gcp_psched(cmd, arg, gcp_resp_frame, t, pos); } else if (cmd == GCP_CADD_EVENT) { ret = gcp_add_event(cmd, arg, gcp_resp_frame); + } else if (cmd == GCP_CEVENT_ACTIVE_TIME) { + if (arg) + event_active_time = arg; + gcp_resp_frame[0] = cpu_to_le32(event_active_time); + ret = 0; + } else if (cmd == GCP_CEVENT_LED_TIME) { + if (arg) + event_led_time = arg; + gcp_resp_frame[0] = cpu_to_le32(event_led_time); + ret = 0; } else if (cmd == GCP_CECHO) { gcp_resp_frame[0] = cpu_to_le32(arg); ret = 0; @@ -385,18 +397,39 @@ out: int encoder_init(void); unsigned long encoder_get_pos(void); -#if 0 -static void diagnostic(struct tty_struct *tty) +static void diagnostics(struct tty_struct *tty) { + static unsigned int i; u32 frame[2]; u32 hdr; + u32 value; + + i = !i; + switch (i) { + case 0: + if (!event_active_time) + return; + hdr = (GCP_NODE_BROADCAST << GCP_NODE_SHIFT); + hdr |= (GCP_PROTO_SEQCMD << GCP_PROTO_SHIFT); + hdr |= (GCP_CEVENT_ACTIVE_TIME << GCP_SEQCMD_CMD_SHIFT); + hdr |= GCP_SEQCMD_RST; + value = event_active_time; + break; + case 1: + if (!event_led_time) + return; + hdr = (GCP_NODE_BROADCAST << GCP_NODE_SHIFT); + hdr |= (GCP_PROTO_SEQCMD << GCP_PROTO_SHIFT); + hdr |= (GCP_CEVENT_LED_TIME << GCP_SEQCMD_CMD_SHIFT); + hdr |= GCP_SEQCMD_RST; + value = event_led_time; + break; + } - frame[GCP_PAYLOAD] = cpu_to_le32(0); - hdr = GCP_SEQCMD_RST | (GCP_CGET << GCP_SEQCMD_CMD_SHIFT); + frame[GCP_PAYLOAD] = cpu_to_le32(value); frame[GCP_HEADER] = cpu_to_le32(hdr); grtp_xmit(tty, (void *) frame); } -#endif struct vest { unsigned long prev_pos; @@ -449,14 +482,14 @@ unsigned long v_estimate(struct vest *p, unsigned long pos, unsigned long t) int main(void) { -// struct tty_struct *control; + struct tty_struct *control; struct tty_struct *pipe_tty; struct tty_struct *tty; struct tty_struct *tty0; struct tty_struct *tty2; struct tty_struct *tty0_raw; -// unsigned long diag_next; - unsigned long t; + unsigned int diag_next; + unsigned int t; unsigned int pev_sync_old = 0; struct vest vest_state = {}; @@ -484,7 +517,7 @@ int main(void) local_irq_disable(); pipe_tty = tty_pipe(); -// control = grtp_init(pipe_tty, NULL); + control = grtp_init(pipe_tty, NULL); tty = serial_init(3); tty2 = grtp_init(tty, data_frame_handler); @@ -498,7 +531,7 @@ int main(void) local_irq_enable(); t = gettime(); -// diag_next = t; + diag_next = t; vest_state.prev_t = t; for (;;) { @@ -530,12 +563,10 @@ int main(void) ev = event_schedule(predicted_pos); -#if 0 - if ((long) diag_next - (long) t >= 0) { - diagnostic(control); - diag_next += 0x40000000; + if (time_after(t, diag_next)) { + diagnostics(control); + diag_next += us_to_ticks(500000); } -#endif c = -1; if (!ev) { diff --git a/src/posix-timer.c b/src/posix-timer.c index b0600a3..bd89b24 100644 --- a/src/posix-timer.c +++ b/src/posix-timer.c @@ -22,7 +22,7 @@ void timer_init(void) { } -unsigned long gettime(void) +unsigned int gettime(void) { static unsigned long long prev_t; static unsigned long jiffies; diff --git a/src/timer.c b/src/timer.c index d3d6933..7228253 100644 --- a/src/timer.c +++ b/src/timer.c @@ -38,10 +38,10 @@ static void systick_write(u32 x, unsigned int reg) writel(x, (u8 *) SYSTICK_BASE + reg); } -unsigned long gettime(void) +unsigned int gettime(void) { - static unsigned long et; - static unsigned long oldt; + static unsigned int et; + static unsigned int oldt; unsigned long t; t = -(systick_read(SYST_CVR) << 8); -- 2.14.1