Root/fw/antorcha.c

Source at commit 0e9b093d2ee3f8195e9848f5a931c2068d37e59f created 11 years 1 day ago.
By Werner Almesberger, tornado/cpu/cpu.brd: improve 3V3 routing
1/*
2 * fw/antorcha.c - Initialization of Antorcha application firmware
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 <stddef.h>
15#include <stdint.h>
16
17#include "proto.h"
18#include "param.h"
19#include "rf.h"
20#include "dispatch.h"
21#include "sweep.h"
22#include "accel.h"
23#include "image.h"
24
25#include <avr/interrupt.h>
26#include "io.h"
27
28
29static volatile enum sync_state {
30    IDLE, /* undecided */
31    BWD, /* backward sweep */
32    LEFT, /* reversing from backward to forward */
33    FWD, /* forward sweep */
34    RIGHT, /* reversing from forward to backward */
35} state = IDLE;
36
37static volatile uint32_t tR0, tR1, tL0, tL1;
38static volatile uint32_t tL, tR;
39static volatile bool wake = 0;
40
41
42static void sync_sweep(bool x, uint16_t v)
43{
44    uint32_t t;
45
46    if (!x)
47        return;
48    t = uptime_irq();
49    switch (state) {
50    case IDLE:
51        if (v < xa_low) {
52            tR0 = t;
53            state = RIGHT;
54        } else if (v > xa_high) {
55            tL0 = t;
56            state = LEFT;
57        }
58        break;
59    case RIGHT:
60        if (v < xa_low)
61            break;
62        tR1 = t;
63        tR = t-tR0;
64        state = BWD;
65        /* fall through */
66    case BWD:
67        if (v < xa_high)
68            break;
69        tL0 = t;
70        state = LEFT;
71        wake = 1;
72        break;
73    case LEFT:
74        if (v > xa_high)
75            break;
76        tL1 = t;
77        tL = t-tL0;
78        state = FWD;
79        /* fall through */
80    case FWD:
81        if (v > xa_low)
82            break;
83        tR0 = t;
84        state = RIGHT;
85        wake = 1;
86        break;
87    }
88}
89
90
91static void submit_fwd_sweep(void)
92{
93#if 0
94    uint32_t tIMG;
95
96    tIMG = (fwd_sweep.right-fwd_sweep.left+1)*(fwd_sweep.pixel_ticks)/2;
97    fwd_sweep.start_ticks = tL0+110000-tIMG/2;
98#endif
99    fwd_sweep.start_ticks = tL0+fwd_start;
100    sweep_image(&fwd_sweep);
101}
102
103
104static void submit_bwd_sweep(void)
105{
106    bwd_sweep.start_ticks = tR0+bwd_start;
107    sweep_image(&bwd_sweep);
108}
109
110
111static const struct handler *protos[] = {
112    &image_handler,
113    &reset_handler,
114    &sample_handler,
115    &param_handler,
116    &diag_handler,
117    NULL
118};
119
120
121int main(void)
122{
123    uint8_t buf[PAYLOAD+5]; /* 3 bytes header, 2 bytes CRC */
124    uint8_t got;
125    uint32_t idle = 0;
126
127    /*
128     * The boot loader has already initialized PORTx, DDRx, and MCUCR.PUD.
129     * It has also brought up RF and the underlying SPI.
130     */
131
132    sweep_init();
133    sample = sync_sweep;
134    accel_start();
135    sei();
136
137    while (1) {
138        got = rf_recv(buf, sizeof(buf));
139        if (got > 2) {
140            dispatch(buf, got-2, protos);
141            idle = 0;
142        }
143        if (wake && !sweeping) {
144            wake = 0;
145            if (state == LEFT)
146                submit_fwd_sweep();
147            else if (state == RIGHT)
148                submit_bwd_sweep();
149        }
150        /* about 1 Hz */
151        if (++idle == 100000) {
152            rf_init();
153            idle = 0;
154        }
155    }
156}
157

Archive Download this file

Branches:
master
tornado-v1



interactive