Root/atusb/fw/mac.c

Source at commit eeeb5599f87a4088e3201beb90015c650dcd0767 created 9 years 2 months ago.
By Werner Almesberger, atusb/fw/mac.c: reserved code 0 and added notification of TX completion
1/*
2 * fw/mac.c - HardMAC functions
3 *
4 * Written 2011 by Werner Almesberger
5 * Copyright 2011 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 <stdint.h>
15
16#include "usb.h"
17
18#include "at86rf230.h"
19#include "spi.h"
20#include "board.h"
21#include "mac.h"
22
23
24int (*mac_irq)(void) = NULL;
25
26
27static uint8_t rx_buf[MAX_PSDU+2]; /* PHDR+payload+LQ */
28static uint8_t tx_buf[MAX_PSDU];
29static uint8_t tx_size = 0;
30static int txing = 0;
31static int queued_tx_ack = 0;
32
33
34static uint8_t reg_read(uint8_t reg)
35{
36    uint8_t value;
37
38    spi_begin();
39    spi_send(AT86RF230_REG_READ | reg);
40    value= spi_recv();
41    spi_end();
42
43    return value;
44}
45
46
47static void reg_write(uint8_t reg, uint8_t value)
48{
49    spi_begin();
50    spi_send(AT86RF230_REG_WRITE | reg);
51    spi_send(value);
52    spi_end();
53}
54
55
56static void rx_done(void *user)
57{
58    led(0);
59    if (queued_tx_ack) {
60        usb_send(&eps[1], "", 1, rx_done, NULL);
61        queued_tx_ack = 0;
62    }
63}
64
65
66static int handle_irq(void)
67{
68    uint8_t irq;
69    uint8_t size, i;
70
71    if (txing) {
72        if (eps[1].state == EP_IDLE)
73            usb_send(&eps[1], "", 1, rx_done, NULL);
74        else {
75            if (queued_tx_ack)
76                panic();
77            queued_tx_ack = 1;
78        }
79        txing = 0;
80        return 0;
81    }
82
83    irq = reg_read(REG_IRQ_STATUS);
84    if (!(irq & IRQ_TRX_END))
85        return 1;
86
87    /* unlikely */
88    if (eps[1].state != EP_IDLE)
89        return 1;
90
91    spi_begin();
92    spi_send(AT86RF230_BUF_READ);
93    size = spi_recv();
94    if (!size || (size & 0x80)) {
95        spi_end();
96        return 1;
97    }
98
99    rx_buf[0] = size;
100    for (i = 0; i != size+1; i++)
101        rx_buf[i+1] = spi_recv();
102    spi_end();
103    led(1);
104    usb_send(&eps[1], rx_buf, size+2, rx_done, NULL);
105    return 1;
106}
107
108
109int mac_rx(int on)
110{
111    if (on) {
112        mac_irq = handle_irq;
113        reg_read(REG_IRQ_STATUS);
114        reg_write(REG_TRX_STATE, TRX_CMD_RX_ON);
115    } else {
116        mac_irq = NULL;
117        reg_write(REG_TRX_STATE, TRX_CMD_FORCE_TRX_OFF);
118        txing = 0;
119    }
120    return 1;
121}
122
123
124static void do_tx(void *user)
125{
126    uint8_t status;
127    uint8_t i;
128
129    do status = reg_read(REG_TRX_STATUS) & TRX_STATUS_MASK;
130    while (status != TRX_STATUS_RX_ON && status != TRX_STATUS_RX_AACK_ON);
131
132    /*
133     * We use TRX_CMD_FORCE_PLL_ON instead of TRX_CMD_PLL_ON because a new
134     * reception may have begun while we were still working on the previous
135     * one.
136     */
137    reg_write(REG_TRX_STATE, TRX_CMD_FORCE_PLL_ON);
138
139    handle_irq();
140
141    spi_begin();
142    spi_send(AT86RF230_BUF_WRITE);
143    spi_send(tx_size+2); /* CRC */
144    for (i = 0; i != tx_size; i++)
145        spi_send(tx_buf[i]);
146    spi_end();
147
148    reg_write(REG_TRX_STATE, TRX_CMD_TX_START);
149
150    txing = 1;
151
152    /*
153     * Wait until we reach BUSY_TX, so that we command the transition to
154     * RX_ON which will be executed upon TX completion.
155     */
156    while ((reg_read(REG_TRX_STATUS) & TRX_STATUS_MASK) ==
157        TRX_STATUS_TRANSITION);
158    reg_write(REG_TRX_STATE, TRX_CMD_RX_ON);
159}
160
161
162int mac_tx(uint16_t flags, uint16_t len)
163{
164    if (len > MAX_PSDU)
165        return 0;
166    tx_size = len;
167    usb_recv(&eps[0], tx_buf, len, do_tx, NULL);
168    return 1;
169}
170

Archive Download this file



interactive