Root/fw/diag.c

Source at commit 30c683effd7ab362d1517950700c96cfa09e45e8 created 11 years 17 days ago.
By Werner Almesberger, tornado/fw/sim/p: pass arguments to "alg"; label graphs; better separate "up"
1/*
2 * fw/diag.c - Diagnostic request
3 *
4 * Written 2012 by Werner Almesberger
5 * Copyright 2012 Werner Almesberger
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 */
12
13
14#include <stdbool.h>
15#include <stdint.h>
16#include <string.h>
17
18#include <avr/interrupt.h>
19#define F_CPU 8000000UL
20#include <util/delay.h>
21
22#include "hash.h"
23#include "proto.h"
24#include "rf.h"
25#include "dispatch.h"
26#include "secret.h"
27#include "image.h"
28#include "sweep.h"
29#include "accel.h"
30
31
32static uint8_t tmp[2];
33static bool failed;
34
35
36static void do_diag(void)
37{
38    uint8_t pkg[3+2*DIAG_SAMPLES] = { DIAG_ACK, 4, 0, };
39    uint8_t *p = pkg+3;
40    uint16_t v;
41    uint8_t i;
42
43    cli();
44    set_line(localize_line(tmp[0], tmp[1]));
45    _delay_ms(50);
46    for (i = 0; i != DIAG_SAMPLES; i++) {
47        v = measure_ref();
48        *p++ = v;
49        *p++ = v >> 8;
50    }
51    set_line(localize_line(0, 0));
52    /*
53     * @@@ for some reason we get a difference of 100 mV between passes
54     * if we enable interrupts here
55     *
56     * sei();
57     */
58    rf_send(pkg, sizeof(pkg));
59}
60
61
62static bool diag_more(uint8_t seq, uint8_t limit, const uint8_t *payload)
63{
64    switch (limit-seq) {
65    default:
66        hash_merge(payload, PAYLOAD);
67        break;
68    case 1:
69        hash_end();
70        failed = !hash_eq(payload, PAYLOAD, 0);
71        break;
72    case 0:
73        if (!hash_eq(payload, PAYLOAD, PAYLOAD))
74            failed = 1;
75        if (!failed)
76            do_diag();
77        /* do_diag sends the ACK, not the dispatcher */
78        return 0;
79    }
80    return 1;
81}
82
83
84static bool diag_first(uint8_t limit, const uint8_t *payload)
85{
86    hash_init();
87    hash_merge_progmem(image_secret, sizeof(image_secret));
88    hash_merge(payload, PAYLOAD);
89    memcpy(&tmp, payload, sizeof(tmp));
90    failed = 0;
91    return 1;
92}
93
94
95struct handler diag_handler = {
96        .type = DIAG,
97        .first = diag_first,
98        .more = diag_more,
99};
100

Archive Download this file

Branches:
master
tornado-v1



interactive