Root/atusb/fw/mac.c

Source at commit 367c1abb0ee5fd16c8ac5be0a22052596f447f1a created 7 years 7 months ago.
By Werner Almesberger, atusb/fw/: echo back TX ACK sequence number; don't panic if driver times out TX
1/*
2 * fw/mac.c - HardMAC functions
3 *
4 * Written 2011, 2013 by Werner Almesberger
5 * Copyright 2011, 2013 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#include <stddef.h>
14#include <stdbool.h>
15#include <stdint.h>
16
17#include "usb.h"
18
19#include "at86rf230.h"
20#include "spi.h"
21#include "board.h"
22#include "mac.h"
23
24
25bool (*mac_irq)(void) = NULL;
26
27
28static uint8_t rx_buf[MAX_PSDU+2]; /* PHDR+payload+LQ */
29static uint8_t tx_buf[MAX_PSDU];
30static uint8_t tx_size = 0;
31static bool txing = 0;
32static bool queued_tx_ack = 0;
33static bool queued_rx = 0;
34static uint8_t next_seq, this_seq, queued_seq;
35
36
37static uint8_t reg_read(uint8_t reg)
38{
39    uint8_t value;
40
41    spi_begin();
42    spi_send(AT86RF230_REG_READ | reg);
43    value = spi_recv();
44    spi_end();
45
46    return value;
47}
48
49
50static void reg_write(uint8_t reg, uint8_t value)
51{
52    spi_begin();
53    spi_send(AT86RF230_REG_WRITE | reg);
54    spi_send(value);
55    spi_end();
56}
57
58
59static void receive_frame(void);
60
61
62static void rx_done(void *user)
63{
64    led(0);
65    if (queued_rx) {
66        receive_frame();
67        queued_rx = 0;
68        return;
69    }
70    if (queued_tx_ack) {
71        usb_send(&eps[1], &queued_seq, 1, rx_done, NULL);
72        queued_tx_ack = 0;
73    }
74}
75
76
77static void receive_frame(void)
78{
79    uint8_t size, i;
80
81    spi_begin();
82    if (!(spi_io(AT86RF230_BUF_READ) & RX_CRC_VALID)) {
83        spi_end();
84        return;
85    }
86    size = spi_recv();
87    if (!size || (size & 0x80)) {
88        spi_end();
89        return;
90    }
91
92    rx_buf[0] = size;
93    for (i = 0; i != size+1; i++)
94        rx_buf[i+1] = spi_recv();
95    spi_end();
96    led(1);
97    usb_send(&eps[1], rx_buf, size+2, rx_done, NULL);
98}
99
100
101static void flush_queued_rx(void)
102{
103    if (queued_rx)
104        receive_frame();
105    queued_rx = 0;
106}
107
108
109static bool handle_irq(void)
110{
111    uint8_t irq;
112
113    irq = reg_read(REG_IRQ_STATUS);
114    if (!(irq & IRQ_TRX_END))
115        return 1;
116
117    if (txing) {
118        if (eps[1].state == EP_IDLE) {
119            usb_send(&eps[1], &this_seq, 1, rx_done, NULL);
120        } else {
121            queued_tx_ack = 1;
122            queued_seq = this_seq;
123        }
124        txing = 0;
125        return 1;
126    }
127
128    /* unlikely */
129    if (eps[1].state != EP_IDLE) {
130        queued_rx = 1;
131        return 1;
132    }
133
134    receive_frame();
135
136    return 1;
137}
138
139
140static void change_state(uint8_t new)
141{
142    while ((reg_read(REG_TRX_STATUS) & TRX_STATUS_MASK) ==
143        TRX_STATUS_TRANSITION);
144    reg_write(REG_TRX_STATE, new);
145}
146
147
148bool mac_rx(int on)
149{
150    if (on) {
151        mac_irq = handle_irq;
152        reg_read(REG_IRQ_STATUS);
153        change_state(TRX_CMD_RX_ON);
154    } else {
155        mac_irq = NULL;
156        change_state(TRX_CMD_FORCE_TRX_OFF);
157        txing = 0;
158    }
159    return 1;
160}
161
162
163static void do_tx(void *user)
164{
165    uint16_t timeout = 0xffff;
166    uint8_t status;
167    uint8_t i;
168
169    /*
170     * If we time out here, the host driver will time out waiting for the
171     * TRX_END acknowledgement.
172     */
173    do {
174        if (!--timeout)
175            return;
176        status = reg_read(REG_TRX_STATUS) & TRX_STATUS_MASK;
177    }
178    while (status != TRX_STATUS_RX_ON && status != TRX_STATUS_RX_AACK_ON);
179
180    /*
181     * We use TRX_CMD_FORCE_PLL_ON instead of TRX_CMD_PLL_ON because a new
182     * reception may have begun while we were still working on the previous
183     * one.
184     */
185    reg_write(REG_TRX_STATE, TRX_CMD_FORCE_PLL_ON);
186
187    flush_queued_rx();
188    handle_irq();
189    queued_rx = 0;
190
191    spi_begin();
192    spi_send(AT86RF230_BUF_WRITE);
193    spi_send(tx_size+2); /* CRC */
194    for (i = 0; i != tx_size; i++)
195        spi_send(tx_buf[i]);
196    spi_end();
197
198    slp_tr();
199
200    txing = 1;
201    this_seq = next_seq;
202
203    /*
204     * Wait until we reach BUSY_TX, so that we command the transition to
205     * RX_ON which will be executed upon TX completion.
206     */
207    change_state(TRX_CMD_RX_ON);
208}
209
210
211bool mac_tx(uint16_t flags, uint8_t seq, uint16_t len)
212{
213    if (len > MAX_PSDU)
214        return 0;
215    tx_size = len;
216    next_seq = seq;
217    usb_recv(&eps[0], tx_buf, len, do_tx, NULL);
218    return 1;
219}
220
221
222void mac_reset(void)
223{
224    mac_irq = NULL;
225    txing = 0;
226    queued_tx_ack = 0;
227    queued_rx = 0;
228    next_seq = this_seq = queued_seq = 0;
229
230    /* enable CRC and PHY_RSSI (with RX_CRC_VALID) in SPI status return */
231    reg_write(REG_TRX_CTRL_1,
232        TX_AUTO_CRC_ON | SPI_CMD_MODE_PHY_RSSI << SPI_CMD_MODE_SHIFT);
233}
234

Archive Download this file



interactive