2012-06-17 04:16:37 +03:00
|
|
|
/*
|
2012-06-19 00:22:56 +03:00
|
|
|
* fw/antorcha.c - Initialization of Antorcha application firmware
|
2012-06-17 04:16:37 +03:00
|
|
|
*
|
|
|
|
* Written 2012 by Werner Almesberger
|
|
|
|
* Copyright 2012 Werner Almesberger
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2012-06-19 00:22:56 +03:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
|
2012-06-17 04:16:37 +03:00
|
|
|
#include "rf.h"
|
2012-06-19 00:22:56 +03:00
|
|
|
#include "dispatch.h"
|
2012-06-20 13:26:52 +03:00
|
|
|
#include "sweep.h"
|
2012-06-21 14:56:56 +03:00
|
|
|
#include "accel.h"
|
2012-06-20 16:41:27 +03:00
|
|
|
#include "image.h"
|
|
|
|
|
2012-06-24 03:53:26 +03:00
|
|
|
#include <avr/interrupt.h>
|
|
|
|
#include "io.h"
|
|
|
|
|
2012-06-20 16:41:27 +03:00
|
|
|
|
2012-06-25 17:37:16 +03:00
|
|
|
static struct sweep fwd_sweep = {
|
2012-06-25 16:48:19 +03:00
|
|
|
.pixel_ticks = 1100, /* 1.1 ms */
|
2012-06-20 16:41:27 +03:00
|
|
|
.left = 0,
|
2012-06-24 03:57:18 +03:00
|
|
|
.right = MAX_LINES-1,
|
2012-06-20 16:41:27 +03:00
|
|
|
.forward = 1,
|
|
|
|
};
|
2012-06-17 04:16:37 +03:00
|
|
|
|
|
|
|
|
2012-06-25 17:37:16 +03:00
|
|
|
static struct sweep bwd_sweep = {
|
|
|
|
.pixel_ticks = 1100, /* 1.1 ms */
|
|
|
|
.left = 0,
|
|
|
|
.right = MAX_LINES-1,
|
|
|
|
.forward = 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-06-24 03:53:26 +03:00
|
|
|
static volatile enum sync_state {
|
2012-06-25 16:48:19 +03:00
|
|
|
IDLE, /* undecided */
|
|
|
|
BWD, /* backward sweep */
|
|
|
|
LEFT, /* reversing from backward to forward */
|
|
|
|
FWD, /* forward sweep */
|
|
|
|
RIGHT, /* reversing from forward to backward */
|
2012-06-24 03:53:26 +03:00
|
|
|
} state = IDLE;
|
|
|
|
|
2012-06-25 16:48:19 +03:00
|
|
|
static volatile uint32_t tR0, tR1, tL0, tL1;
|
|
|
|
static volatile uint32_t tL, tR;
|
|
|
|
static volatile bool wake = 0;
|
2012-06-24 03:53:26 +03:00
|
|
|
|
2012-06-25 16:48:19 +03:00
|
|
|
|
|
|
|
//#define THRESH_HIGH 900
|
|
|
|
//#define THRESH_LOW 120
|
|
|
|
#define THRESH_HIGH 850
|
|
|
|
#define THRESH_LOW 170
|
2012-06-24 03:53:26 +03:00
|
|
|
|
|
|
|
|
|
|
|
static void sync_sweep(bool x, uint16_t v)
|
|
|
|
{
|
2012-06-25 16:48:19 +03:00
|
|
|
uint32_t t;
|
2012-06-24 03:53:26 +03:00
|
|
|
|
|
|
|
if (!x)
|
|
|
|
return;
|
2012-06-25 16:48:19 +03:00
|
|
|
t = uptime_irq();
|
2012-06-24 03:53:26 +03:00
|
|
|
switch (state) {
|
|
|
|
case IDLE:
|
2012-06-25 16:48:19 +03:00
|
|
|
if (v < THRESH_LOW) {
|
|
|
|
tR0 = t;
|
|
|
|
state = RIGHT;
|
|
|
|
} else if (v > THRESH_HIGH) {
|
|
|
|
tL0 = t;
|
|
|
|
state = LEFT;
|
2012-06-24 03:53:26 +03:00
|
|
|
}
|
|
|
|
break;
|
2012-06-25 16:48:19 +03:00
|
|
|
case RIGHT:
|
|
|
|
if (v < THRESH_LOW)
|
2012-06-24 03:53:26 +03:00
|
|
|
break;
|
2012-06-25 16:48:19 +03:00
|
|
|
tR1 = t;
|
|
|
|
tR = t-tR0;
|
|
|
|
state = BWD;
|
|
|
|
/* fall through */
|
|
|
|
case BWD:
|
|
|
|
if (v < THRESH_HIGH)
|
2012-06-24 03:53:26 +03:00
|
|
|
break;
|
2012-06-25 16:48:19 +03:00
|
|
|
tL0 = t;
|
|
|
|
state = LEFT;
|
|
|
|
wake = 1;
|
2012-06-24 03:53:26 +03:00
|
|
|
break;
|
2012-06-25 16:48:19 +03:00
|
|
|
case LEFT:
|
|
|
|
if (v > THRESH_HIGH)
|
|
|
|
break;
|
|
|
|
tL1 = t;
|
|
|
|
tL = t-tL0;
|
|
|
|
state = FWD;
|
|
|
|
/* fall through */
|
|
|
|
case FWD:
|
|
|
|
if (v > THRESH_LOW)
|
2012-06-24 03:53:26 +03:00
|
|
|
break;
|
2012-06-25 16:48:19 +03:00
|
|
|
tR0 = t;
|
|
|
|
state = RIGHT;
|
2012-06-25 17:37:16 +03:00
|
|
|
wake = 1;
|
2012-06-24 03:53:26 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-25 16:48:19 +03:00
|
|
|
static void submit_fwd_sweep(void)
|
|
|
|
{
|
|
|
|
#if 0
|
|
|
|
uint32_t tIMG;
|
|
|
|
|
2012-06-25 17:37:16 +03:00
|
|
|
tIMG = (fwd_sweep.right-fwd_sweep.left+1)*(fwd_sweep.pixel_ticks)/2;
|
|
|
|
fwd_sweep.start_ticks = tL0+110000-tIMG/2;
|
2012-06-25 16:48:19 +03:00
|
|
|
#endif
|
2012-06-25 17:37:16 +03:00
|
|
|
fwd_sweep.start_ticks = tL0+70000;
|
|
|
|
sweep_image(&fwd_sweep);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void submit_bwd_sweep(void)
|
|
|
|
{
|
|
|
|
bwd_sweep.start_ticks = tR0+50000;
|
|
|
|
sweep_image(&bwd_sweep);
|
2012-06-25 16:48:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-19 00:22:56 +03:00
|
|
|
static const struct handler *protos[] = {
|
2012-06-20 16:30:40 +03:00
|
|
|
&image_handler,
|
2012-06-19 02:31:20 +03:00
|
|
|
&reset_handler,
|
2012-06-21 18:12:10 +03:00
|
|
|
&sample_handler,
|
2012-06-19 00:22:56 +03:00
|
|
|
NULL
|
|
|
|
};
|
2012-06-17 04:16:37 +03:00
|
|
|
|
|
|
|
|
2012-06-19 00:22:56 +03:00
|
|
|
int main(void)
|
|
|
|
{
|
|
|
|
uint8_t buf[PAYLOAD+5]; /* 3 bytes header, 2 bytes CRC */
|
|
|
|
uint8_t got;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The boot loader has already initialized PORTx, DDRx, and MCUCR.PUD.
|
|
|
|
* It has also brought up RF and the underlying SPI.
|
|
|
|
*/
|
|
|
|
|
2012-06-20 13:26:52 +03:00
|
|
|
sweep_init();
|
2012-06-24 03:53:26 +03:00
|
|
|
sample = sync_sweep;
|
2012-06-21 14:56:56 +03:00
|
|
|
accel_start();
|
2012-06-24 03:53:26 +03:00
|
|
|
sei();
|
2012-06-20 13:26:52 +03:00
|
|
|
|
2012-06-19 00:22:56 +03:00
|
|
|
while (1) {
|
|
|
|
got = rf_recv(buf, sizeof(buf));
|
|
|
|
if (got > 2)
|
|
|
|
dispatch(buf, got-2, protos);
|
2012-06-25 16:48:19 +03:00
|
|
|
if (wake && !sweeping) {
|
|
|
|
wake = 0;
|
|
|
|
if (state == LEFT)
|
|
|
|
submit_fwd_sweep();
|
2012-06-25 17:37:16 +03:00
|
|
|
else if (state == RIGHT)
|
|
|
|
submit_bwd_sweep();
|
2012-06-24 03:53:26 +03:00
|
|
|
}
|
2012-06-17 04:16:37 +03:00
|
|
|
}
|
|
|
|
}
|