Root/atrf/tools/atspi-txrx/atspi-txrx.c

Source at commit 73043a52ea70185b5dcf037ffa07abaf94a36134 created 10 years 30 days ago.
By Werner Almesberger, Finished and tested TX/RX. (LQ doesn't work yet, the rest does.)
1/*
2 * atspi-txrx/atspi-txrx.c - ben-wpan AF86RF230 TX/RX
3 *
4 * Written 2010 by Werner Almesberger
5 * Copyright 2010 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 <stdlib.h>
15#include <stdio.h>
16#include <string.h>
17#include <usb.h>
18
19#include "at86rf230.h"
20#include "atspi/ep0.h"
21#include "atspi.h"
22
23
24/*
25 * According to IEEE 802.15.4-2003 section E.2.6, channel 15 is the only
26 * channel that falls into the 802.11 guard bands in North America an Europe.
27 */
28
29#define DEFAULT_CHANNEL 15 /* channel 15, 2425 MHz */
30
31/*
32 * Transmit power, dBm. IEEE 802.15.4-2003 section E.3.1.3 specifies a transmit
33 * power of 0 dBm for IEEE 802.15.4. We assume an antenna gain of 3 dB or
34 * better.
35 */
36
37#define DEFAULT_POWER -3.2 /* transmit power, dBm */
38
39
40static double tx_pwr[] = {
41     3.0, 2.6, 2.1, 1.6,
42     1.1, 0.5, -0.2, -1.2,
43    -2.2, -3.2, -4.2, -5.2,
44    -7.2, -9.2, -12.2, -17.2
45};
46
47
48static usb_dev_handle *init_txrx(int trim)
49{
50    usb_dev_handle *dev;
51
52    dev = atspi_open();
53    if (!dev)
54        exit(1);
55    
56    atspi_reset_rf(dev);
57    atspi_reg_write(dev, REG_TRX_STATE, TRX_CMD_TRX_OFF);
58    atspi_reg_write(dev, REG_XOSC_CTRL,
59        (XTAL_MODE_INT << XTAL_MODE_SHIFT) | trim);
60
61    return dev;
62}
63
64
65static void set_channel(usb_dev_handle *dev, int channel)
66{
67    atspi_reg_write(dev, REG_PHY_CC_CCA, (1 << CCA_MODE_SHIFT) | channel);
68}
69
70
71static void set_power(usb_dev_handle *dev, double power)
72{
73    int n;
74
75    for (n = 0; n != sizeof(tx_pwr)/sizeof(*tx_pwr); n++)
76        if (tx_pwr[n] <= power)
77            break;
78    atspi_reg_write(dev, REG_PHY_TX_PWR, TX_AUTO_CRC_ON | n);
79}
80
81
82static void receive(usb_dev_handle *dev)
83{
84    uint8_t irq;
85    uint8_t buf[MAX_PSDU+1]; /* PSDU+LQ */
86    int n, ok, i;
87    uint8_t lq;
88
89    atspi_reg_write(dev, REG_TRX_STATE, TRX_CMD_RX_ON);
90
91    (void) atspi_reg_read(dev, REG_IRQ_STATUS);
92
93    fprintf(stderr, "Ready.\n");
94    while (1) {
95        irq = atspi_reg_read(dev, REG_IRQ_STATUS);
96        if (atspi_error())
97            exit(1);
98        if (!irq)
99            continue;
100        if (irq == IRQ_TRX_END)
101            break;
102        fprintf(stderr, "IRQ (0x%02x):", irq);
103        if (irq & IRQ_PLL_LOCK)
104            fprintf(stderr, " PLL_LOCK");
105        if (irq & IRQ_PLL_UNLOCK)
106            fprintf(stderr, " PLL_UNLOCK");
107        if (irq & IRQ_RX_START)
108            fprintf(stderr, " RX_START");
109        if (irq & IRQ_TRX_UR)
110            fprintf(stderr, " TRX_UR");
111        if (irq & IRQ_BAT_LOW)
112            fprintf(stderr, " BAT_LOW");
113        fprintf(stderr, "\n");
114        if (irq & IRQ_TRX_END)
115            break;
116    }
117
118    n = atspi_buf_read(dev, buf, sizeof(buf));
119    if (n < 0)
120        exit(1);
121    if (n < 3) {
122        fprintf(stderr, "%d bytes received\n", n);
123        exit(1);
124    }
125    ok = !!(atspi_reg_read(dev, REG_PHY_RSSI) & RX_CRC_VALID);
126    lq = buf[n-1];
127    fprintf(stderr, "%d bytes payload, CRC %s, LQ %u\n",
128        n-3, ok ? "OK" : "BAD", lq);
129    for (i = 0; i != n-3; i++)
130        putchar(buf[i] < ' ' || buf[i] > '~' ? '?' : buf[i]);
131    putchar('\n');
132}
133
134
135static void transmit(usb_dev_handle *dev, const char *msg)
136{
137    uint8_t buf[MAX_PSDU];
138
139    atspi_reg_write(dev, REG_TRX_STATE, TRX_CMD_PLL_ON);
140
141    /*
142     * We need to copy the message to append the CRC placeholders.
143     */
144    strcpy((void *) buf, msg);
145    atspi_buf_write(dev, buf, strlen(msg)+2);
146
147    /* @@@ should wait for clear channel */
148    atspi_reg_write(dev, REG_TRX_STATE, TRX_CMD_TX_START);
149    /* @@@ should wait for TX done */
150}
151
152
153static void usage(const char *name)
154{
155    fprintf(stderr,
156"usage: %s [-c channel] [-p power] [-t trim] [message]\n"
157" -c channel channel number, 11 to 26 (default %d)\n"
158" -p power transmit power, -17.2 to 3.0 dBm (default %.1f)\n"
159" -t trim trim capacitor, 0 to 15 (default 0)\n"
160        , name , DEFAULT_CHANNEL, DEFAULT_POWER);
161    exit(1);
162}
163
164
165int main(int argc, char *const *argv)
166{
167    int channel = DEFAULT_CHANNEL;
168    double power = DEFAULT_POWER;
169    int trim = 0;
170    char *end;
171    int c;
172    usb_dev_handle *dev;
173
174    while ((c = getopt(argc, argv, "c:p:t:")) != EOF)
175        switch (c) {
176        case 'c':
177            channel = strtoul(optarg, &end, 0);
178            if (*end)
179                usage(*argv);
180            if (channel < 11 || channel > 26)
181                usage(*argv);
182            break;
183        case 'p':
184            power = strtod(optarg, &end);
185            if (*end)
186                usage(*argv);
187            break;
188        case 't':
189            trim = strtoul(optarg, &end, 0);
190            if (*end)
191                usage(*argv);
192            if (trim > 15)
193                usage(*argv);
194            break;
195        default:
196            usage(*argv);
197        }
198
199    switch (argc-optind) {
200    case 0:
201        dev = init_txrx(trim);
202        set_channel(dev, channel);
203        receive(dev);
204        break;
205    case 1:
206        dev = init_txrx(trim);
207        set_channel(dev, channel);
208        set_power(dev, power);
209        transmit(dev, argv[optind]);
210        break;
211    default:
212        usage(*argv);
213    }
214
215    return 0;
216}
217

Archive Download this file



interactive