2011-07-12 17:23:21 +03:00
|
|
|
/*
|
|
|
|
* fw/mac.c - HardMAC functions
|
|
|
|
*
|
2013-02-13 06:35:23 +02:00
|
|
|
* Written 2011, 2013 by Werner Almesberger
|
|
|
|
* Copyright 2011, 2013 Werner Almesberger
|
2011-07-12 17:23:21 +03:00
|
|
|
*
|
|
|
|
* 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 <stddef.h>
|
2013-02-20 23:48:53 +02:00
|
|
|
#include <stdbool.h>
|
2011-07-12 17:23:21 +03:00
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
#include "usb.h"
|
|
|
|
|
|
|
|
#include "at86rf230.h"
|
|
|
|
#include "spi.h"
|
2011-07-12 23:45:00 +03:00
|
|
|
#include "board.h"
|
2011-07-12 17:23:21 +03:00
|
|
|
#include "mac.h"
|
|
|
|
|
2013-07-31 02:38:13 +03:00
|
|
|
#define RX_BUFS 3
|
2013-07-24 13:57:35 +03:00
|
|
|
|
|
|
|
|
2013-03-30 00:19:49 +02:00
|
|
|
bool (*mac_irq)(void) = NULL;
|
2011-07-12 17:23:21 +03:00
|
|
|
|
|
|
|
|
2013-07-24 13:57:35 +03:00
|
|
|
static uint8_t rx_buf[RX_BUFS][MAX_PSDU+2]; /* PHDR+payload+LQ */
|
2011-07-12 17:23:21 +03:00
|
|
|
static uint8_t tx_buf[MAX_PSDU];
|
|
|
|
static uint8_t tx_size = 0;
|
2013-02-20 23:48:53 +02:00
|
|
|
static bool txing = 0;
|
|
|
|
static bool queued_tx_ack = 0;
|
2013-04-02 04:04:05 +03:00
|
|
|
static uint8_t next_seq, this_seq, queued_seq;
|
2011-07-12 17:23:21 +03:00
|
|
|
|
|
|
|
|
2013-07-24 22:21:38 +03:00
|
|
|
/* ----- Receive buffer management ----------------------------------------- */
|
|
|
|
|
|
|
|
|
|
|
|
static uint8_t rx_in = 0, rx_out = 0;
|
|
|
|
|
|
|
|
|
|
|
|
static inline void next_buf(uint8_t *index)
|
|
|
|
{
|
|
|
|
*index = (*index+1) % RX_BUFS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-23 18:50:42 +03:00
|
|
|
/* ----- Register access --------------------------------------------------- */
|
|
|
|
|
|
|
|
|
2011-07-12 17:23:21 +03:00
|
|
|
static uint8_t reg_read(uint8_t reg)
|
|
|
|
{
|
|
|
|
uint8_t value;
|
|
|
|
|
|
|
|
spi_begin();
|
|
|
|
spi_send(AT86RF230_REG_READ | reg);
|
2013-02-13 14:30:21 +02:00
|
|
|
value = spi_recv();
|
2011-07-12 17:23:21 +03:00
|
|
|
spi_end();
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void reg_write(uint8_t reg, uint8_t value)
|
|
|
|
{
|
|
|
|
spi_begin();
|
|
|
|
spi_send(AT86RF230_REG_WRITE | reg);
|
|
|
|
spi_send(value);
|
|
|
|
spi_end();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-23 18:50:42 +03:00
|
|
|
/* ----- Interrupt handling ------------------------------------------------ */
|
|
|
|
|
|
|
|
|
2013-07-24 13:57:35 +03:00
|
|
|
static void rx_done(void *user);
|
|
|
|
static void tx_ack_done(void *user);
|
2013-04-01 21:17:37 +03:00
|
|
|
|
|
|
|
|
2013-07-24 13:57:35 +03:00
|
|
|
static void usb_next(void)
|
2011-07-12 23:45:00 +03:00
|
|
|
{
|
2013-07-24 13:57:35 +03:00
|
|
|
const uint8_t *buf;
|
|
|
|
|
|
|
|
if (rx_in != rx_out) {
|
|
|
|
buf = rx_buf[rx_out];
|
|
|
|
led(1);
|
|
|
|
usb_send(&eps[1], buf, buf[0]+2, rx_done, NULL);
|
2013-04-01 21:17:37 +03:00
|
|
|
}
|
2013-07-24 13:57:35 +03:00
|
|
|
|
2011-07-13 20:22:49 +03:00
|
|
|
if (queued_tx_ack) {
|
2013-07-24 13:57:35 +03:00
|
|
|
usb_send(&eps[1], &queued_seq, 1, tx_ack_done, NULL);
|
2011-07-13 20:22:49 +03:00
|
|
|
queued_tx_ack = 0;
|
|
|
|
}
|
2011-07-12 23:45:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-24 13:57:35 +03:00
|
|
|
static void tx_ack_done(void *user)
|
|
|
|
{
|
|
|
|
usb_next();
|
|
|
|
}
|
|
|
|
|
2015-05-24 15:37:38 +03:00
|
|
|
static void change_state(uint8_t new)
|
|
|
|
{
|
|
|
|
while ((reg_read(REG_TRX_STATUS) & TRX_STATUS_MASK) ==
|
|
|
|
TRX_STATUS_TRANSITION);
|
|
|
|
reg_write(REG_TRX_STATE, new);
|
|
|
|
}
|
2013-07-24 13:57:35 +03:00
|
|
|
|
|
|
|
static void rx_done(void *user)
|
|
|
|
{
|
|
|
|
led(0);
|
2013-07-24 22:21:38 +03:00
|
|
|
next_buf(&rx_out);
|
2013-07-24 13:57:35 +03:00
|
|
|
usb_next();
|
2015-05-24 15:37:38 +03:00
|
|
|
#ifdef RZUSB
|
|
|
|
/* slap at86rf230 - reduce fragmentation issue */
|
|
|
|
change_state(TRX_STATUS_RX_AACK_ON);
|
|
|
|
#endif
|
2013-07-24 13:57:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-04-01 20:29:57 +03:00
|
|
|
static void receive_frame(void)
|
|
|
|
{
|
2013-07-31 01:46:55 +03:00
|
|
|
uint8_t size;
|
2013-07-24 13:57:35 +03:00
|
|
|
uint8_t *buf;
|
2013-04-01 20:29:57 +03:00
|
|
|
|
|
|
|
spi_begin();
|
2015-05-24 15:37:38 +03:00
|
|
|
#ifdef ATUSB
|
2013-04-01 20:29:57 +03:00
|
|
|
if (!(spi_io(AT86RF230_BUF_READ) & RX_CRC_VALID)) {
|
|
|
|
spi_end();
|
|
|
|
return;
|
|
|
|
}
|
2015-05-24 15:37:38 +03:00
|
|
|
#endif
|
|
|
|
#ifdef RZUSB
|
|
|
|
spi_io(AT86RF230_BUF_READ);
|
|
|
|
#endif
|
|
|
|
|
2013-04-01 20:29:57 +03:00
|
|
|
size = spi_recv();
|
|
|
|
if (!size || (size & 0x80)) {
|
|
|
|
spi_end();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-07-24 13:57:35 +03:00
|
|
|
buf = rx_buf[rx_in];
|
2013-07-31 01:46:55 +03:00
|
|
|
spi_recv_block(buf+1, size+1);
|
2013-04-01 20:29:57 +03:00
|
|
|
spi_end();
|
|
|
|
|
2013-07-24 13:57:35 +03:00
|
|
|
buf[0] = size;
|
2013-07-24 22:21:38 +03:00
|
|
|
next_buf(&rx_in);
|
2013-04-01 20:29:57 +03:00
|
|
|
|
2013-07-24 13:57:35 +03:00
|
|
|
if (eps[1].state == EP_IDLE)
|
|
|
|
usb_next();
|
2013-04-01 21:17:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-03-30 00:19:49 +02:00
|
|
|
static bool handle_irq(void)
|
2011-07-12 17:23:21 +03:00
|
|
|
{
|
|
|
|
uint8_t irq;
|
|
|
|
|
2013-02-13 06:35:23 +02:00
|
|
|
irq = reg_read(REG_IRQ_STATUS);
|
2013-04-01 18:48:30 +03:00
|
|
|
if (!(irq & IRQ_TRX_END))
|
|
|
|
return 1;
|
|
|
|
|
2011-07-12 17:23:21 +03:00
|
|
|
if (txing) {
|
2013-04-02 04:04:05 +03:00
|
|
|
if (eps[1].state == EP_IDLE) {
|
2013-07-24 13:57:35 +03:00
|
|
|
usb_send(&eps[1], &this_seq, 1, tx_ack_done, NULL);
|
2013-04-02 04:04:05 +03:00
|
|
|
} else {
|
2011-07-13 20:22:49 +03:00
|
|
|
queued_tx_ack = 1;
|
2013-04-02 04:04:05 +03:00
|
|
|
queued_seq = this_seq;
|
2011-07-13 20:22:49 +03:00
|
|
|
}
|
2011-07-12 17:23:21 +03:00
|
|
|
txing = 0;
|
2011-07-12 18:03:12 +03:00
|
|
|
return 1;
|
2013-04-01 18:48:30 +03:00
|
|
|
}
|
2011-07-12 18:03:12 +03:00
|
|
|
|
2013-07-24 13:57:35 +03:00
|
|
|
/* likely */
|
|
|
|
if (eps[1].state == EP_IDLE || rx_in != rx_out)
|
|
|
|
receive_frame();
|
2011-07-12 17:23:21 +03:00
|
|
|
|
2011-07-12 18:03:12 +03:00
|
|
|
return 1;
|
2011-07-12 17:23:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-23 18:50:42 +03:00
|
|
|
/* ----- TX/RX ------------------------------------------------------------- */
|
|
|
|
|
|
|
|
|
2013-03-30 00:14:11 +02:00
|
|
|
bool mac_rx(int on)
|
2011-07-12 17:23:21 +03:00
|
|
|
{
|
|
|
|
if (on) {
|
|
|
|
mac_irq = handle_irq;
|
|
|
|
reg_read(REG_IRQ_STATUS);
|
2015-05-20 16:00:57 +03:00
|
|
|
change_state(TRX_CMD_RX_AACK_ON);
|
2011-07-12 17:23:21 +03:00
|
|
|
} else {
|
|
|
|
mac_irq = NULL;
|
2013-02-20 18:05:33 +02:00
|
|
|
change_state(TRX_CMD_FORCE_TRX_OFF);
|
2011-07-12 17:23:21 +03:00
|
|
|
txing = 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void do_tx(void *user)
|
|
|
|
{
|
2013-03-29 23:50:54 +02:00
|
|
|
uint16_t timeout = 0xffff;
|
2011-07-12 17:23:21 +03:00
|
|
|
uint8_t status;
|
|
|
|
uint8_t i;
|
|
|
|
|
2013-03-29 23:50:54 +02:00
|
|
|
/*
|
|
|
|
* If we time out here, the host driver will time out waiting for the
|
|
|
|
* TRX_END acknowledgement.
|
|
|
|
*/
|
|
|
|
do {
|
|
|
|
if (!--timeout)
|
|
|
|
return;
|
|
|
|
status = reg_read(REG_TRX_STATUS) & TRX_STATUS_MASK;
|
|
|
|
}
|
2011-07-12 17:23:21 +03:00
|
|
|
while (status != TRX_STATUS_RX_ON && status != TRX_STATUS_RX_AACK_ON);
|
|
|
|
|
2015-05-24 15:37:38 +03:00
|
|
|
#ifdef ATUSB
|
2011-07-12 17:23:21 +03:00
|
|
|
/*
|
|
|
|
* We use TRX_CMD_FORCE_PLL_ON instead of TRX_CMD_PLL_ON because a new
|
|
|
|
* reception may have begun while we were still working on the previous
|
|
|
|
* one.
|
|
|
|
*/
|
|
|
|
reg_write(REG_TRX_STATE, TRX_CMD_FORCE_PLL_ON);
|
2015-05-24 15:37:38 +03:00
|
|
|
#endif
|
|
|
|
#ifdef RZUSB
|
|
|
|
/*
|
|
|
|
* at86rf230 doesn't support force change, nevetherless this works
|
|
|
|
* somehow
|
|
|
|
*/
|
|
|
|
reg_write(REG_TRX_STATE, TRX_CMD_PLL_ON);
|
|
|
|
#endif
|
2011-07-12 17:23:21 +03:00
|
|
|
|
|
|
|
handle_irq();
|
|
|
|
|
|
|
|
spi_begin();
|
|
|
|
spi_send(AT86RF230_BUF_WRITE);
|
2011-07-12 23:45:00 +03:00
|
|
|
spi_send(tx_size+2); /* CRC */
|
2011-07-12 17:23:21 +03:00
|
|
|
for (i = 0; i != tx_size; i++)
|
|
|
|
spi_send(tx_buf[i]);
|
|
|
|
spi_end();
|
|
|
|
|
2013-03-29 20:03:03 +02:00
|
|
|
slp_tr();
|
2011-07-12 17:23:21 +03:00
|
|
|
|
|
|
|
txing = 1;
|
2013-04-02 04:04:05 +03:00
|
|
|
this_seq = next_seq;
|
2011-07-12 17:23:21 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Wait until we reach BUSY_TX, so that we command the transition to
|
2015-05-20 16:00:57 +03:00
|
|
|
* RX_AACK_ON which will be executed upon TX completion.
|
2011-07-12 17:23:21 +03:00
|
|
|
*/
|
2015-05-20 16:00:57 +03:00
|
|
|
change_state(TRX_CMD_RX_AACK_ON);
|
2011-07-12 17:23:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-04-02 04:04:05 +03:00
|
|
|
bool mac_tx(uint16_t flags, uint8_t seq, uint16_t len)
|
2011-07-12 17:23:21 +03:00
|
|
|
{
|
|
|
|
if (len > MAX_PSDU)
|
|
|
|
return 0;
|
|
|
|
tx_size = len;
|
2013-04-02 04:04:05 +03:00
|
|
|
next_seq = seq;
|
2011-07-12 17:23:21 +03:00
|
|
|
usb_recv(&eps[0], tx_buf, len, do_tx, NULL);
|
|
|
|
return 1;
|
|
|
|
}
|
2013-02-13 14:30:21 +02:00
|
|
|
|
|
|
|
|
|
|
|
void mac_reset(void)
|
|
|
|
{
|
|
|
|
mac_irq = NULL;
|
|
|
|
txing = 0;
|
|
|
|
queued_tx_ack = 0;
|
2013-07-24 13:57:35 +03:00
|
|
|
rx_in = rx_out = 0;
|
2013-04-02 04:04:05 +03:00
|
|
|
next_seq = this_seq = queued_seq = 0;
|
2013-03-29 22:02:24 +02:00
|
|
|
|
|
|
|
/* enable CRC and PHY_RSSI (with RX_CRC_VALID) in SPI status return */
|
|
|
|
reg_write(REG_TRX_CTRL_1,
|
|
|
|
TX_AUTO_CRC_ON | SPI_CMD_MODE_PHY_RSSI << SPI_CMD_MODE_SHIFT);
|
2013-02-13 14:30:21 +02:00
|
|
|
}
|