Date:2010-08-24 11:16:48 (9 years 26 days ago)
Author:Werner Almesberger
Commit:73043a52ea70185b5dcf037ffa07abaf94a36134
Message:Finished and tested TX/RX. (LQ doesn't work yet, the rest does.)

- Makefile.recurse (all): reorder DIRS and TARGET_ONLY_DIRS, so that
tools/lib/ gets built before the tools that depend on it
- atrf/fw/atspi/ep0.c (do_buf_write): removed duplicate sending of
AT86RF230_BUF_WRITE
- atrf/fw/atspi/ep0.c (my_setup): generate the PHR in ATSPI_BUF_WRITE
instead of expecting the host to send it
- atrf/fw/atspi/ep0.c (my_setup): don't send the PHR to the host in
AT86RF230_BUF_READ
- atrf/fw/include/at86rf230.h (REG_TR_PWR): should be REG_PHY_TX_PWR
- atrf/tools/Makefile: added atspi-txrx and "lib"
- atrf/tools/atspi-txrx/Makefile: corrected path in F32XBASE
- atrf/tools/atspi-txrx/atspi-txrx.c: major rewrite
- atrf/tools/include/atspi.h, atrf/tools/lib/atspi.c: functions
atspi_buf_write and atspi_buf_read to access the frame buffer
Files: Makefile.recurse (2 diffs)
atrf/fw/atspi/ep0.c (3 diffs)
atrf/fw/include/at86rf230.h (1 diff)
atrf/tools/Makefile (1 diff)
atrf/tools/atspi-txrx/Makefile (1 diff)
atrf/tools/atspi-txrx/atspi-txrx.c (2 diffs)
atrf/tools/include/atspi.h (1 diff)
atrf/tools/lib/atspi.c (1 diff)

Change Details

Makefile.recurse
11#
22# Makefile.recurse - Recursively build a few standard targets
33#
4# Written 2008, 2009 by Werner Almesberger
5# Copyright 2008, 2009 Werner Almesberger
4# Written 2008-2010 by Werner Almesberger
5# Copyright 2008-2010 Werner Almesberger
66#
77# This program is free software; you can redistribute it and/or modify
88# it under the terms of the GNU General Public License as published by
...... 
1414.PHONY: all install uninstall clean spotless
1515
1616all:
17        for d in $(DIRS) $(TARGET_ONLY_DIRS); \
17        for d in $(TARGET_ONLY_DIRS) $(DIRS); \
1818          do make -C $$d all || exit 1; done
1919
2020install:
atrf/fw/atspi/ep0.c
9494
9595    user; /* suppress warning */
9696    nSS = 0;
97    spi_send(AT86RF230_BUF_WRITE);
9897    for (i = 0; i != size; i++)
9998        spi_send(buf[i]);
10099    nSS = 1;
...... 
164163        debug("ATSPI_BUF_WRITE\n");
165164        if (setup->wLength < 1)
166165            return 0;
167        if (setup->wLength > MAX_PSDU+1) /* PHR+PSDU */
166        if (setup->wLength > MAX_PSDU)
168167            return 0;
169168        buf[0] = AT86RF230_BUF_WRITE;
170        size = setup->wLength+1;
171        usb_recv(&ep0, buf+1, setup->wLength, do_buf_write, NULL);
169        buf[1] = setup->wLength;
170        size = setup->wLength+2;
171        usb_recv(&ep0, buf+2, setup->wLength, do_buf_write, NULL);
172172        return 1;
173    case ATSPI_TO_DEV(ATSPI_BUF_READ):
173    case ATSPI_FROM_DEV(ATSPI_BUF_READ):
174174        debug("ATSPI_BUF_READ\n");
175175        if (setup->wLength < 2) /* PHR+LQ */
176176            return 0;
...... 
178178            return 0;
179179        nSS = 0;
180180        spi_send(AT86RF230_BUF_READ);
181        size = *buf = spi_recv();
182        if (size+2 > setup->wLength)
183            size = setup->wLength-2;
181        size = spi_recv();
182        if (size >= setup->wLength)
183            size = setup->wLength-1;
184184        for (i = 0; i != size+1; i++)
185            buf[i+1] = spi_recv();
185            buf[i] = spi_recv();
186186        nSS = 1;
187187        usb_send(&ep0, buf, size+1, NULL, NULL);
188188        return 1;
atrf/fw/include/at86rf230.h
3434    REG_TRX_STATE = 0x02,
3535    REG_TRX_CTRL_0 = 0x03,
3636
37    REG_TR_PWR = 0x05,
37    REG_PHY_TX_PWR = 0x05,
3838    REG_PHY_RSSI = 0x06,
3939    REG_PHY_ED_LEVEL = 0x07,
4040    REG_PHY_CC_CCA = 0x08,
atrf/tools/Makefile
1111#
1212
1313
14DIRS=atspi-id atspi-reset atspi-rssi atspi-trim
14DIRS=atspi-id atspi-reset atspi-rssi atspi-trim atspi-txrx
15TARGET_ONLY_DIRS=lib
1516
1617include ../../Makefile.recurse
atrf/tools/atspi-txrx/Makefile
1111#
1212
1313
14F32XBASE = ../../../f32xbase
14F32XBASE = ../../../../f32xbase
1515
1616MAIN = atspi-txrx
1717
atrf/tools/atspi-txrx/atspi-txrx.c
1313
1414#include <stdlib.h>
1515#include <stdio.h>
16#include <string.h>
1617#include <usb.h>
1718
1819#include "at86rf230.h"
...... 
2021#include "atspi.h"
2122
2223
23#define FROM_DEV ATSPI_FROM_DEV(0)
24#define TO_DEV ATSPI_TO_DEV(0)
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}
2569
26#define BUF_SIZE 256
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}
27151
28152
29153static void usage(const char *name)
30154{
31    fprintf(stderr, "%s\n", name);
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);
32161    exit(1);
33162}
34163
35164
36int main(int argc, const char **argv)
165int main(int argc, char *const *argv)
37166{
167    int channel = DEFAULT_CHANNEL;
168    double power = DEFAULT_POWER;
169    int trim = 0;
170    char *end;
171    int c;
38172    usb_dev_handle *dev;
39173
40    if (argc != 1)
41        usage(*argv);
42    dev = atspi_open();
43    if (!dev)
44        return 1;
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        }
45198
46    atspi_reg_write(dev, REG_TRX_STATE, TRX_CMD_TRX_OFF);
47    sleep(1000);
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    }
48214
49215    return 0;
50216}
atrf/tools/include/atspi.h
2929void atspi_reg_write(usb_dev_handle *dev, uint8_t reg, uint8_t value);
3030uint8_t atspi_reg_read(usb_dev_handle *dev, uint8_t reg);
3131
32void atspi_buf_write(usb_dev_handle *dev, const void *buf, int size);
33int atspi_buf_read(usb_dev_handle *dev, void *buf, int size);
34
3235#endif /* !ATSPI_H */
atrf/tools/lib/atspi.c
141141    }
142142    return value;
143143}
144
145
146/* ----- frame buffer access ----------------------------------------------- */
147
148
149void atspi_buf_write(usb_dev_handle *dev, const void *buf, int size)
150{
151    int res;
152
153    if (error)
154        return;
155
156    res = usb_control_msg(dev, TO_DEV, ATSPI_BUF_WRITE, 0, 0,
157        (void *) buf, size, 1000);
158    if (res < 0) {
159        fprintf(stderr, "ATSPI_BUF_WRITE: %d\n", res);
160        error = 1;
161    }
162
163}
164
165
166int atspi_buf_read(usb_dev_handle *dev, void *buf, int size)
167{
168    int res;
169
170    if (error)
171        return -1;
172
173    res = usb_control_msg(dev, FROM_DEV, ATSPI_BUF_READ, 0, 0,
174        buf, size, 1000);
175    if (res < 0) {
176        fprintf(stderr, "ATSPI_BUF_READ: %d\n", res);
177        error = 1;
178    }
179
180    return res;
181}

Archive Download the corresponding diff file



interactive