2012-06-18 17:00:10 +03:00
|
|
|
/*
|
|
|
|
* tools/antorcha.c - Antorcha control utility
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
2012-06-23 20:35:44 +03:00
|
|
|
#include <signal.h>
|
2012-06-18 17:00:10 +03:00
|
|
|
#include <assert.h>
|
|
|
|
|
2012-06-19 00:26:18 +03:00
|
|
|
#include <at86rf230.h>
|
2012-06-18 17:00:10 +03:00
|
|
|
#include <atrf.h>
|
|
|
|
#include <misctxrx.h>
|
|
|
|
|
|
|
|
#include <proto.h>
|
|
|
|
|
2012-06-21 22:21:56 +03:00
|
|
|
#include "hash.h"
|
|
|
|
#include "plot.h"
|
|
|
|
|
2012-06-18 17:00:10 +03:00
|
|
|
|
|
|
|
static int verbose = 1;
|
2012-06-20 23:15:24 +03:00
|
|
|
static int debug = 0;
|
2012-06-18 17:00:10 +03:00
|
|
|
|
|
|
|
|
|
|
|
static void rf_init(struct atrf_dsc *dsc, int trim, int channel)
|
|
|
|
{
|
|
|
|
atrf_reg_write(dsc, REG_TRX_STATE, TRX_CMD_TRX_OFF);
|
|
|
|
atrf_reg_write(dsc, REG_XOSC_CTRL,
|
|
|
|
(XTAL_MODE_INT << XTAL_MODE_SHIFT) | trim);
|
|
|
|
atrf_reg_write(dsc, REG_PHY_CC_CCA, (1 << CCA_MODE_SHIFT) | channel);
|
|
|
|
atrf_reg_write(dsc, REG_IRQ_MASK, 0xff);
|
|
|
|
flush_interrupts(dsc);
|
|
|
|
atrf_reg_write(dsc, REG_TRX_STATE, TRX_CMD_PLL_ON);
|
|
|
|
wait_for_interrupt(dsc, IRQ_PLL_LOCK, IRQ_PLL_LOCK, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void rf_send(struct atrf_dsc *dsc, void *buf, int len)
|
|
|
|
{
|
2012-06-19 00:22:56 +03:00
|
|
|
uint8_t tmp[MAX_PSDU];
|
|
|
|
|
|
|
|
/* Copy the message to append the CRC placeholders */
|
|
|
|
memcpy(tmp, buf, len);
|
2012-06-18 17:00:10 +03:00
|
|
|
atrf_reg_write(dsc, REG_TRX_STATE, TRX_CMD_PLL_ON);
|
2012-06-19 02:31:20 +03:00
|
|
|
flush_interrupts(dsc);
|
2012-06-19 00:22:56 +03:00
|
|
|
atrf_buf_write(dsc, tmp, len+2);
|
2012-06-18 17:00:10 +03:00
|
|
|
atrf_reg_write(dsc, REG_TRX_STATE, TRX_CMD_TX_START);
|
|
|
|
wait_for_interrupt(dsc, IRQ_TRX_END,
|
|
|
|
IRQ_TRX_END | IRQ_PLL_LOCK, 10);
|
|
|
|
atrf_reg_write(dsc, REG_TRX_STATE, TRX_CMD_RX_ON);
|
2012-06-20 23:15:24 +03:00
|
|
|
|
|
|
|
if (debug) {
|
|
|
|
int i;
|
|
|
|
fprintf(stderr, "\r%d:", len);
|
|
|
|
for (i = 0; i != len; i++)
|
|
|
|
fprintf(stderr, " %02x", ((uint8_t *) buf)[i]);;
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
}
|
2012-06-18 17:00:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int rf_recv(struct atrf_dsc *dsc, void *buf, int size)
|
|
|
|
{
|
|
|
|
uint8_t irq;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
/* Wait up to 100 ms */
|
|
|
|
irq = wait_for_interrupt(dsc, IRQ_TRX_END,
|
|
|
|
IRQ_TRX_END | IRQ_RX_START | IRQ_AMI, 100);
|
|
|
|
if (!irq)
|
|
|
|
return 0;
|
|
|
|
len = atrf_buf_read(dsc, buf, size);
|
|
|
|
if (len < 0)
|
|
|
|
exit(1);
|
|
|
|
if (len < 3) /* CRC and LQI */
|
|
|
|
return 0;
|
|
|
|
return atrf_reg_read(dsc, REG_PHY_RSSI) & RX_CRC_VALID ? len-3 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-19 02:31:20 +03:00
|
|
|
static void packet_noack(struct atrf_dsc *dsc,
|
2012-06-19 00:22:56 +03:00
|
|
|
uint8_t type, uint8_t seq, uint8_t last, const void *payload, int len)
|
2012-06-18 17:00:10 +03:00
|
|
|
{
|
|
|
|
uint8_t tx_buf[PAYLOAD+3] = { type, seq, last };
|
|
|
|
|
|
|
|
assert(len == PAYLOAD);
|
|
|
|
memcpy(tx_buf+3, payload, len);
|
2012-06-19 02:31:20 +03:00
|
|
|
rf_send(dsc, tx_buf, sizeof(tx_buf));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void packet(struct atrf_dsc *dsc,
|
|
|
|
uint8_t type, uint8_t seq, uint8_t last, const void *payload, int len)
|
|
|
|
{
|
|
|
|
uint8_t rx_buf[10];
|
|
|
|
int got;
|
|
|
|
|
2012-06-18 17:00:10 +03:00
|
|
|
while (1) {
|
2012-06-19 02:31:20 +03:00
|
|
|
packet_noack(dsc, type, seq, last, payload, len);
|
2012-06-18 17:00:10 +03:00
|
|
|
if (verbose)
|
|
|
|
write(2, ">", 1);
|
|
|
|
got = rf_recv(dsc, rx_buf, sizeof(rx_buf));
|
2012-06-19 00:22:56 +03:00
|
|
|
if (got <= 0) {
|
|
|
|
if (!seq && verbose)
|
|
|
|
write(2, "\b", 1);
|
2012-06-18 17:00:10 +03:00
|
|
|
continue;
|
2012-06-19 00:22:56 +03:00
|
|
|
}
|
2012-06-18 17:00:10 +03:00
|
|
|
if (verbose)
|
|
|
|
write(2, "\b?", 2);
|
|
|
|
if (got < 3)
|
|
|
|
continue;
|
|
|
|
if (verbose)
|
|
|
|
write(2, "\bA", 2);
|
|
|
|
if (rx_buf[0] != type+1 || rx_buf[1] != seq)
|
|
|
|
continue;
|
|
|
|
if (verbose)
|
|
|
|
write(2, "\b.", 2);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-20 17:31:56 +03:00
|
|
|
/* ----- Ping -------------------------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
|
|
static void ping(struct atrf_dsc *dsc)
|
|
|
|
{
|
|
|
|
uint8_t ping[] = { PING, 0, 0 };
|
|
|
|
uint8_t buf[100];
|
|
|
|
int got, i;
|
|
|
|
|
|
|
|
rf_send(dsc, ping, sizeof(ping));
|
|
|
|
got = rf_recv(dsc, buf, sizeof(buf));
|
|
|
|
if (!got)
|
|
|
|
return;
|
|
|
|
printf("%d: ", got);
|
|
|
|
for (i = 3; i != got; i++)
|
|
|
|
printf("%c", buf[i] >= ' ' && buf[i] <= '~' ? buf[i] : '?');
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ----- Firmware upload --------------------------------------------------- */
|
|
|
|
|
|
|
|
|
2012-06-19 00:22:56 +03:00
|
|
|
static const uint8_t unlock_secret[PAYLOAD] = {
|
2012-06-18 17:00:10 +03:00
|
|
|
#include "unlock-secret.inc"
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static void send_firmware(struct atrf_dsc *dsc, void *buf, int len)
|
|
|
|
{
|
|
|
|
uint8_t payload[PAYLOAD];
|
|
|
|
uint8_t last, seq;
|
|
|
|
|
|
|
|
if (verbose)
|
|
|
|
write(2, "firmware ", 9);
|
|
|
|
last = (len+63)/64;
|
|
|
|
seq = 0;
|
2012-06-19 02:31:20 +03:00
|
|
|
packet_noack(dsc, RESET, 0, 0, unlock_secret, PAYLOAD);
|
2012-06-19 00:22:56 +03:00
|
|
|
packet(dsc, FIRMWARE, seq++, last, unlock_secret, PAYLOAD);
|
2012-06-18 17:00:10 +03:00
|
|
|
while (len >= PAYLOAD) {
|
|
|
|
packet(dsc, FIRMWARE, seq++, last, buf, PAYLOAD);
|
|
|
|
buf += PAYLOAD;
|
|
|
|
len -= PAYLOAD;
|
|
|
|
}
|
|
|
|
if (len) {
|
|
|
|
memcpy(payload, buf, len);
|
|
|
|
memset(payload+len, 0, PAYLOAD-len);
|
|
|
|
packet(dsc, FIRMWARE, seq++, last, payload, PAYLOAD);
|
|
|
|
}
|
|
|
|
if (verbose)
|
|
|
|
write(2, "\n", 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void firmware(struct atrf_dsc *dsc, const char *name)
|
|
|
|
{
|
|
|
|
FILE *file;
|
|
|
|
uint8_t fw[12*1024]; /* we have 8-12kB */
|
|
|
|
ssize_t len;
|
|
|
|
|
|
|
|
file = fopen(name, "r");
|
|
|
|
if (!file) {
|
|
|
|
perror(name);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
len = fread(fw, 1, sizeof(fw), file);
|
|
|
|
if (len < 0) {
|
|
|
|
perror(name);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
fclose(file);
|
|
|
|
|
|
|
|
send_firmware(dsc, fw, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-20 22:50:23 +03:00
|
|
|
/* ----- Image upload ------------------------------------------------------ */
|
|
|
|
|
|
|
|
|
|
|
|
static const uint8_t image_secret[PAYLOAD*2] = {
|
|
|
|
#include "image-secret.inc"
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static void send_image(struct atrf_dsc *dsc, void *buf, int len)
|
|
|
|
{
|
|
|
|
uint8_t payload[PAYLOAD];
|
|
|
|
uint8_t last, seq;
|
|
|
|
|
|
|
|
hash_init();
|
|
|
|
hash_merge(image_secret, sizeof(image_secret));
|
|
|
|
if (verbose)
|
|
|
|
write(2, "image ", 6);
|
|
|
|
|
|
|
|
last = (len+63)/64+3;
|
|
|
|
seq = 0;
|
|
|
|
while (len >= PAYLOAD) {
|
|
|
|
packet(dsc, IMAGE, seq++, last, buf, PAYLOAD);
|
|
|
|
hash_merge(buf, PAYLOAD);
|
|
|
|
buf += PAYLOAD;
|
|
|
|
len -= PAYLOAD;
|
|
|
|
}
|
|
|
|
if (len) {
|
|
|
|
memcpy(payload, buf, len);
|
|
|
|
memset(payload+len, 0, PAYLOAD-len);
|
2012-06-20 23:17:23 +03:00
|
|
|
packet(dsc, IMAGE, seq++, last, payload, PAYLOAD);
|
2012-06-20 22:50:23 +03:00
|
|
|
hash_merge(payload, PAYLOAD);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* @@@ salt */
|
|
|
|
packet(dsc, IMAGE, seq++, last, payload, PAYLOAD);
|
|
|
|
packet(dsc, IMAGE, seq++, last, payload, PAYLOAD);
|
|
|
|
hash_end();
|
|
|
|
|
|
|
|
/* hash */
|
|
|
|
hash_cp(payload, PAYLOAD, 0);
|
|
|
|
packet(dsc, IMAGE, seq++, last, payload, PAYLOAD);
|
|
|
|
hash_cp(payload, PAYLOAD, PAYLOAD);
|
|
|
|
packet(dsc, IMAGE, seq++, last, payload, PAYLOAD);
|
|
|
|
|
|
|
|
if (verbose)
|
|
|
|
write(2, "\n", 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void image(struct atrf_dsc *dsc, const char *name)
|
|
|
|
{
|
|
|
|
FILE *file;
|
|
|
|
uint8_t img[200] = { 0 };
|
|
|
|
ssize_t len;
|
|
|
|
|
|
|
|
file = fopen(name, "r");
|
|
|
|
if (!file) {
|
|
|
|
perror(name);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
len = fread(img, 1, sizeof(img), file);
|
|
|
|
if (len < 0) {
|
|
|
|
perror(name);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
fclose(file);
|
|
|
|
|
|
|
|
send_image(dsc, img, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-21 22:21:56 +03:00
|
|
|
/* ----- Samples ----------------------------------------------------------- */
|
|
|
|
|
|
|
|
|
2012-06-23 20:35:44 +03:00
|
|
|
static volatile int run = 1;
|
|
|
|
|
|
|
|
|
|
|
|
static void sigint(int sig)
|
|
|
|
{
|
|
|
|
run = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-23 20:25:08 +03:00
|
|
|
static int read_sample(uint8_t **s, uint16_t *t_high, uint16_t *t_low,
|
|
|
|
uint16_t *last)
|
|
|
|
{
|
|
|
|
int v;
|
|
|
|
|
|
|
|
*t_low = *(*s)++;
|
|
|
|
*t_low |= *(*s)++ << 8;
|
|
|
|
if (*t_low < *last)
|
|
|
|
(*t_high)++;
|
|
|
|
*last = *t_low;
|
|
|
|
v = *(*s)++;
|
|
|
|
v |= *(*s)++ << 8;
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-23 20:35:44 +03:00
|
|
|
static void samples(struct atrf_dsc *dsc, int gui)
|
2012-06-21 22:21:56 +03:00
|
|
|
{
|
|
|
|
uint8_t buf[MAX_PSDU] = { 0, };
|
|
|
|
int got;
|
|
|
|
uint8_t *s;
|
2012-06-22 01:18:07 +03:00
|
|
|
uint16_t t_high, t_low, last;
|
2012-06-21 22:21:56 +03:00
|
|
|
int x, y;
|
2012-06-23 20:35:44 +03:00
|
|
|
double t;
|
2012-06-21 22:21:56 +03:00
|
|
|
|
|
|
|
buf[0] = 1;
|
|
|
|
packet(dsc, SAMPLE, 0, 0, buf, PAYLOAD);
|
2012-06-23 20:35:44 +03:00
|
|
|
if (gui)
|
|
|
|
plot_init();
|
|
|
|
else
|
|
|
|
signal(SIGINT, sigint);
|
|
|
|
while (run) {
|
2012-06-21 22:21:56 +03:00
|
|
|
got = rf_recv(dsc, buf, sizeof(buf));
|
|
|
|
if (got <= 3)
|
|
|
|
continue;
|
|
|
|
if (buf[0] != SAMPLES)
|
|
|
|
continue;
|
|
|
|
if (debug > 1) {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i != got; i++)
|
|
|
|
fprintf(stderr, " %02x", buf[i]);
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
}
|
|
|
|
if (debug)
|
|
|
|
fprintf(stderr, "%d:", got);
|
2012-06-22 01:18:07 +03:00
|
|
|
s = buf+3;
|
|
|
|
t_high = *s++;
|
|
|
|
t_high |= *s++ << 8;
|
|
|
|
last = 0;
|
2012-06-21 22:21:56 +03:00
|
|
|
while (s < buf+got-2) {
|
2012-06-23 20:25:08 +03:00
|
|
|
x = read_sample(&s, &t_high, &t_low, &last);
|
2012-06-23 20:35:44 +03:00
|
|
|
t = (t_high << 16 | t_low)/1000000.0;
|
2012-06-22 01:18:07 +03:00
|
|
|
if (debug)
|
2012-06-23 20:35:44 +03:00
|
|
|
fprintf(stderr, "\t%11.6f %d", t, x);
|
|
|
|
if (!gui)
|
|
|
|
printf("%11.6f\t%d\t", t, x);
|
2012-06-22 01:18:07 +03:00
|
|
|
|
2012-06-23 20:25:08 +03:00
|
|
|
y = read_sample(&s, &t_high, &t_low, &last);
|
2012-06-23 20:35:44 +03:00
|
|
|
t = (t_high << 16 | t_low)/1000000.0;
|
2012-06-21 22:21:56 +03:00
|
|
|
if (debug)
|
2012-06-23 20:35:44 +03:00
|
|
|
fprintf(stderr, "\t%11.6f %d\n", t, y);
|
|
|
|
if (!gui)
|
|
|
|
printf("%11.6f\t%d\n", t, y);
|
2012-06-22 01:18:07 +03:00
|
|
|
|
2012-06-23 20:35:44 +03:00
|
|
|
if (gui && !plot(x, y))
|
2012-06-22 00:48:54 +03:00
|
|
|
goto quit;
|
2012-06-21 22:21:56 +03:00
|
|
|
}
|
|
|
|
}
|
2012-06-22 00:48:54 +03:00
|
|
|
quit:
|
2012-06-21 22:21:56 +03:00
|
|
|
buf[0] = 0;
|
|
|
|
packet(dsc, SAMPLE, 0, 0, buf, PAYLOAD);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-20 17:31:56 +03:00
|
|
|
/* ----- Command-line processing ------------------------------------------- */
|
|
|
|
|
|
|
|
|
2012-06-20 16:56:11 +03:00
|
|
|
static void usage(const char *name)
|
|
|
|
{
|
2012-06-20 17:31:56 +03:00
|
|
|
fprintf(stderr,
|
2012-06-20 23:15:24 +03:00
|
|
|
"usage: %s [-d] image_file\n"
|
|
|
|
"%6s %s [-d] -F firmware_file\n"
|
|
|
|
"%6s %s [-d] -P\n"
|
2012-06-23 20:35:44 +03:00
|
|
|
"%6s %s [-d] -S [-S]\n"
|
2012-06-21 22:21:56 +03:00
|
|
|
, name, "", name, "", name, "", name);
|
2012-06-20 16:56:11 +03:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-18 17:00:10 +03:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2012-06-20 16:56:11 +03:00
|
|
|
const char *fw = NULL;
|
2012-06-21 22:21:56 +03:00
|
|
|
int do_ping = 0, do_sample = 0;
|
2012-06-18 17:00:10 +03:00
|
|
|
struct atrf_dsc *dsc;
|
2012-06-20 16:56:11 +03:00
|
|
|
int c;
|
|
|
|
|
2012-06-21 22:21:56 +03:00
|
|
|
while ((c = getopt(argc, argv, "dF:PS")) != EOF)
|
2012-06-20 16:56:11 +03:00
|
|
|
switch (c) {
|
2012-06-20 23:15:24 +03:00
|
|
|
case 'd':
|
2012-06-21 22:21:56 +03:00
|
|
|
debug++;
|
2012-06-20 23:15:24 +03:00
|
|
|
break;
|
2012-06-20 16:56:11 +03:00
|
|
|
case 'F':
|
|
|
|
fw = optarg;
|
|
|
|
break;
|
2012-06-20 17:31:56 +03:00
|
|
|
case 'P':
|
|
|
|
do_ping = 1;
|
|
|
|
break;
|
2012-06-21 22:21:56 +03:00
|
|
|
case 'S':
|
2012-06-23 20:35:44 +03:00
|
|
|
do_sample++;
|
2012-06-21 22:21:56 +03:00
|
|
|
break;
|
2012-06-20 16:56:11 +03:00
|
|
|
default:
|
|
|
|
usage(*argv);
|
|
|
|
}
|
|
|
|
|
2012-06-23 20:35:44 +03:00
|
|
|
if (do_ping+!!do_sample+!!fw > 1)
|
2012-06-20 17:31:56 +03:00
|
|
|
usage(*argv);
|
2012-06-21 22:21:56 +03:00
|
|
|
if (do_ping || do_sample || fw) {
|
2012-06-20 22:50:23 +03:00
|
|
|
if (argc != optind)
|
|
|
|
usage(*argv);
|
|
|
|
} else {
|
|
|
|
if (argc != optind+1)
|
|
|
|
usage(*argv);
|
|
|
|
}
|
2012-06-18 17:00:10 +03:00
|
|
|
|
|
|
|
dsc = atrf_open(NULL);
|
|
|
|
if (!dsc)
|
|
|
|
return 1;
|
2012-06-20 16:56:11 +03:00
|
|
|
|
2012-06-18 17:00:10 +03:00
|
|
|
rf_init(dsc, 8, 15);
|
2012-06-20 17:31:56 +03:00
|
|
|
if (do_ping)
|
|
|
|
ping(dsc);
|
2012-06-21 22:21:56 +03:00
|
|
|
else if (do_sample)
|
2012-06-23 20:35:44 +03:00
|
|
|
samples(dsc, do_sample > 1);
|
2012-06-20 22:50:23 +03:00
|
|
|
else if (fw)
|
2012-06-20 16:56:11 +03:00
|
|
|
firmware(dsc, fw);
|
2012-06-20 22:50:23 +03:00
|
|
|
else
|
|
|
|
image(dsc, argv[optind]);
|
2012-06-20 16:56:11 +03:00
|
|
|
|
2012-06-18 17:00:10 +03:00
|
|
|
return 0;
|
|
|
|
}
|