Moved the cc2420 driver into a separate dev/cc2420 module
This commit is contained in:
parent
b78ab6753c
commit
0b805e4763
28 changed files with 30 additions and 30 deletions
118
dev/cc2420/cc2420-aes.c
Normal file
118
dev/cc2420/cc2420-aes.c
Normal file
|
@ -0,0 +1,118 @@
|
|||
/*
|
||||
* Copyright (c) 2008, Swedish Institute of Computer Science.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the Institute nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* This file is part of the Contiki operating system.
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* \file
|
||||
* AES encryption functions.
|
||||
* \author
|
||||
* Adam Dunkels <adam@sics.se>
|
||||
*/
|
||||
|
||||
#include "contiki.h"
|
||||
#include "cc2420.h"
|
||||
#include "cc2420-aes.h"
|
||||
#include "dev/spi.h"
|
||||
|
||||
#define KEYLEN 16
|
||||
#define MAX_DATALEN 16
|
||||
|
||||
#define CC2420_WRITE_RAM_REV(buffer,adr,count) \
|
||||
do { \
|
||||
uint8_t i; \
|
||||
CC2420_SPI_ENABLE(); \
|
||||
SPI_WRITE_FAST(0x80 | (adr & 0x7f)); \
|
||||
SPI_WRITE_FAST((adr >> 1) & 0xc0); \
|
||||
for(i = (count); i > 0; i--) { \
|
||||
SPI_WRITE_FAST(((uint8_t*)(buffer))[i - 1]); \
|
||||
} \
|
||||
SPI_WAITFORTx_ENDED(); \
|
||||
CC2420_SPI_DISABLE(); \
|
||||
} while(0)
|
||||
|
||||
#define MIN(a,b) ((a) < (b)? (a): (b))
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
void
|
||||
cc2420_aes_set_key(const uint8_t *key, int index)
|
||||
{
|
||||
switch(index) {
|
||||
case 0:
|
||||
CC2420_WRITE_RAM_REV(key, CC2420RAM_KEY0, KEYLEN);
|
||||
break;
|
||||
case 1:
|
||||
CC2420_WRITE_RAM_REV(key, CC2420RAM_KEY1, KEYLEN);
|
||||
break;
|
||||
}
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/* Encrypt at most 16 bytes of data. */
|
||||
static void
|
||||
cipher16(uint8_t *data, int len)
|
||||
{
|
||||
uint8_t status;
|
||||
|
||||
len = MIN(len, MAX_DATALEN);
|
||||
|
||||
CC2420_WRITE_RAM(data, CC2420RAM_SABUF, len);
|
||||
CC2420_STROBE(CC2420_SAES);
|
||||
/* Wait for the encryption to finish */
|
||||
do {
|
||||
CC2420_GET_STATUS(status);
|
||||
} while(status & BV(CC2420_ENC_BUSY));
|
||||
CC2420_READ_RAM(data, CC2420RAM_SABUF, len);
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
void
|
||||
cc2420_aes_cipher(uint8_t *data, int len, int key_index)
|
||||
{
|
||||
int i;
|
||||
uint16_t secctrl0;
|
||||
|
||||
CC2420_READ_REG(CC2420_SECCTRL0, secctrl0);
|
||||
|
||||
secctrl0 &= ~(CC2420_SECCTRL0_SAKEYSEL0 | CC2420_SECCTRL0_SAKEYSEL1);
|
||||
|
||||
switch(key_index) {
|
||||
case 0:
|
||||
secctrl0 |= CC2420_SECCTRL0_SAKEYSEL0;
|
||||
break;
|
||||
case 1:
|
||||
secctrl0 |= CC2420_SECCTRL0_SAKEYSEL1;
|
||||
break;
|
||||
}
|
||||
CC2420_WRITE_REG(CC2420_SECCTRL0, secctrl0);
|
||||
|
||||
for(i = 0; i < len; i = i + MAX_DATALEN) {
|
||||
cipher16(data + i, MIN(len - i, MAX_DATALEN));
|
||||
}
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
75
dev/cc2420/cc2420-aes.h
Normal file
75
dev/cc2420/cc2420-aes.h
Normal file
|
@ -0,0 +1,75 @@
|
|||
/*
|
||||
* Copyright (c) 2008, Swedish Institute of Computer Science.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the Institute nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* This file is part of the Contiki operating system.
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* \file
|
||||
* Interface to the CC2420 AES encryption/decryption functions
|
||||
* \author
|
||||
* Adam Dunkels <adam@sics.se>
|
||||
*/
|
||||
|
||||
#ifndef CC2420_AES_H_
|
||||
#define CC2420_AES_H_
|
||||
|
||||
/**
|
||||
* \brief Setup an AES key
|
||||
* \param key A pointer to a 16-byte AES key
|
||||
* \param index The key index: either 0 or 1.
|
||||
*
|
||||
* This function sets up an AES key with the CC2420
|
||||
* chip. The AES key can later be used with the
|
||||
* cc2420_aes_cipher() function to encrypt or decrypt
|
||||
* data.
|
||||
*
|
||||
* The CC2420 can store two separate keys in its
|
||||
* memory. The keys are indexed as 0 or 1 and the key
|
||||
* index is given by the 'index' parameter.
|
||||
*
|
||||
*/
|
||||
void cc2420_aes_set_key(const uint8_t *key, int index);
|
||||
|
||||
|
||||
/**
|
||||
* \brief Encrypt/decrypt data with AES
|
||||
* \param data A pointer to the data to be encrypted/decrypted
|
||||
* \param len The length of the data to be encrypted/decrypted
|
||||
* \param key_index The key to use. The key must have previously been set up with cc2420_aes_set_key().
|
||||
*
|
||||
* This function encrypts/decrypts data with AES. A
|
||||
* pointer to the data is passed as a parameter, and the
|
||||
* function overwrites the data with the encrypted data.
|
||||
*
|
||||
*/
|
||||
void cc2420_aes_cipher(uint8_t *data, int len, int key_index);
|
||||
|
||||
|
||||
#endif /* CC2420_AES_H_ */
|
876
dev/cc2420/cc2420.c
Normal file
876
dev/cc2420/cc2420.c
Normal file
|
@ -0,0 +1,876 @@
|
|||
/*
|
||||
* Copyright (c) 2007, Swedish Institute of Computer Science
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the Institute nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* This file is part of the Contiki operating system.
|
||||
*
|
||||
*/
|
||||
/*
|
||||
* This code is almost device independent and should be easy to port.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "contiki.h"
|
||||
|
||||
#if defined(__AVR__)
|
||||
#include <avr/io.h>
|
||||
#endif
|
||||
|
||||
#include "dev/leds.h"
|
||||
#include "dev/spi.h"
|
||||
#include "cc2420.h"
|
||||
#include "cc2420_const.h"
|
||||
|
||||
#include "net/packetbuf.h"
|
||||
#include "net/rime/rimestats.h"
|
||||
#include "net/netstack.h"
|
||||
|
||||
#define WITH_SEND_CCA 1
|
||||
|
||||
#define FOOTER_LEN 2
|
||||
|
||||
#ifndef CC2420_CONF_CHECKSUM
|
||||
#define CC2420_CONF_CHECKSUM 0
|
||||
#endif /* CC2420_CONF_CHECKSUM */
|
||||
|
||||
#ifndef CC2420_CONF_CHANNEL
|
||||
#define CC2420_CONF_CHANNEL 26
|
||||
#endif /* CC2420_CONF_CHANNEL */
|
||||
|
||||
#ifndef CC2420_CONF_CCA_THRESH
|
||||
#define CC2420_CONF_CCA_THRESH -45
|
||||
#endif /* CC2420_CONF_CCA_THRESH */
|
||||
|
||||
|
||||
#ifndef CC2420_CONF_AUTOACK
|
||||
#define CC2420_CONF_AUTOACK 0
|
||||
#endif /* CC2420_CONF_AUTOACK */
|
||||
|
||||
#if CC2420_CONF_CHECKSUM
|
||||
#include "lib/crc16.h"
|
||||
#define CHECKSUM_LEN 2
|
||||
#else
|
||||
#define CHECKSUM_LEN 0
|
||||
#endif /* CC2420_CONF_CHECKSUM */
|
||||
|
||||
#define AUX_LEN (CHECKSUM_LEN + FOOTER_LEN)
|
||||
|
||||
|
||||
#define FOOTER1_CRC_OK 0x80
|
||||
#define FOOTER1_CORRELATION 0x7f
|
||||
|
||||
#define DEBUG 0
|
||||
#if DEBUG
|
||||
#include <stdio.h>
|
||||
#define PRINTF(...) printf(__VA_ARGS__)
|
||||
#else
|
||||
#define PRINTF(...) do {} while (0)
|
||||
#endif
|
||||
|
||||
#define DEBUG_LEDS DEBUG
|
||||
#undef LEDS_ON
|
||||
#undef LEDS_OFF
|
||||
#if DEBUG_LEDS
|
||||
#define LEDS_ON(x) leds_on(x)
|
||||
#define LEDS_OFF(x) leds_off(x)
|
||||
#else
|
||||
#define LEDS_ON(x)
|
||||
#define LEDS_OFF(x)
|
||||
#endif
|
||||
|
||||
void cc2420_arch_init(void);
|
||||
|
||||
/* XXX hack: these will be made as Chameleon packet attributes */
|
||||
rtimer_clock_t cc2420_time_of_arrival, cc2420_time_of_departure;
|
||||
|
||||
int cc2420_authority_level_of_sender;
|
||||
|
||||
int cc2420_packets_seen, cc2420_packets_read;
|
||||
|
||||
static uint8_t volatile pending;
|
||||
|
||||
#define BUSYWAIT_UNTIL(cond, max_time) \
|
||||
do { \
|
||||
rtimer_clock_t t0; \
|
||||
t0 = RTIMER_NOW(); \
|
||||
while(!(cond) && RTIMER_CLOCK_LT(RTIMER_NOW(), t0 + (max_time))); \
|
||||
} while(0)
|
||||
|
||||
volatile uint8_t cc2420_sfd_counter;
|
||||
volatile uint16_t cc2420_sfd_start_time;
|
||||
volatile uint16_t cc2420_sfd_end_time;
|
||||
|
||||
static volatile uint16_t last_packet_timestamp;
|
||||
/*---------------------------------------------------------------------------*/
|
||||
PROCESS(cc2420_process, "CC2420 driver");
|
||||
/*---------------------------------------------------------------------------*/
|
||||
|
||||
|
||||
int cc2420_on(void);
|
||||
int cc2420_off(void);
|
||||
|
||||
static int cc2420_read(void *buf, unsigned short bufsize);
|
||||
|
||||
static int cc2420_prepare(const void *data, unsigned short len);
|
||||
static int cc2420_transmit(unsigned short len);
|
||||
static int cc2420_send(const void *data, unsigned short len);
|
||||
|
||||
static int cc2420_receiving_packet(void);
|
||||
static int pending_packet(void);
|
||||
static int cc2420_cca(void);
|
||||
/*static int detected_energy(void);*/
|
||||
|
||||
signed char cc2420_last_rssi;
|
||||
uint8_t cc2420_last_correlation;
|
||||
|
||||
const struct radio_driver cc2420_driver =
|
||||
{
|
||||
cc2420_init,
|
||||
cc2420_prepare,
|
||||
cc2420_transmit,
|
||||
cc2420_send,
|
||||
cc2420_read,
|
||||
/* cc2420_set_channel, */
|
||||
/* detected_energy, */
|
||||
cc2420_cca,
|
||||
cc2420_receiving_packet,
|
||||
pending_packet,
|
||||
cc2420_on,
|
||||
cc2420_off,
|
||||
};
|
||||
|
||||
static uint8_t receive_on;
|
||||
|
||||
static int channel;
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
|
||||
static void
|
||||
getrxdata(void *buf, int len)
|
||||
{
|
||||
CC2420_READ_FIFO_BUF(buf, len);
|
||||
}
|
||||
static void
|
||||
getrxbyte(uint8_t *byte)
|
||||
{
|
||||
CC2420_READ_FIFO_BYTE(*byte);
|
||||
}
|
||||
static void
|
||||
flushrx(void)
|
||||
{
|
||||
uint8_t dummy;
|
||||
|
||||
CC2420_READ_FIFO_BYTE(dummy);
|
||||
CC2420_STROBE(CC2420_SFLUSHRX);
|
||||
CC2420_STROBE(CC2420_SFLUSHRX);
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static void
|
||||
strobe(enum cc2420_register regname)
|
||||
{
|
||||
CC2420_STROBE(regname);
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static unsigned int
|
||||
status(void)
|
||||
{
|
||||
uint8_t status;
|
||||
CC2420_GET_STATUS(status);
|
||||
return status;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static uint8_t locked, lock_on, lock_off;
|
||||
|
||||
static void
|
||||
on(void)
|
||||
{
|
||||
CC2420_ENABLE_FIFOP_INT();
|
||||
strobe(CC2420_SRXON);
|
||||
|
||||
BUSYWAIT_UNTIL(status() & (BV(CC2420_XOSC16M_STABLE)), RTIMER_SECOND / 100);
|
||||
|
||||
ENERGEST_ON(ENERGEST_TYPE_LISTEN);
|
||||
receive_on = 1;
|
||||
}
|
||||
static void
|
||||
off(void)
|
||||
{
|
||||
/* PRINTF("off\n");*/
|
||||
receive_on = 0;
|
||||
|
||||
/* Wait for transmission to end before turning radio off. */
|
||||
BUSYWAIT_UNTIL(!(status() & BV(CC2420_TX_ACTIVE)), RTIMER_SECOND / 10);
|
||||
|
||||
ENERGEST_OFF(ENERGEST_TYPE_LISTEN);
|
||||
strobe(CC2420_SRFOFF);
|
||||
CC2420_DISABLE_FIFOP_INT();
|
||||
|
||||
if(!CC2420_FIFOP_IS_1) {
|
||||
flushrx();
|
||||
}
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
#define GET_LOCK() locked++
|
||||
static void RELEASE_LOCK(void) {
|
||||
if(locked == 1) {
|
||||
if(lock_on) {
|
||||
on();
|
||||
lock_on = 0;
|
||||
}
|
||||
if(lock_off) {
|
||||
off();
|
||||
lock_off = 0;
|
||||
}
|
||||
}
|
||||
locked--;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static unsigned
|
||||
getreg(enum cc2420_register regname)
|
||||
{
|
||||
unsigned reg;
|
||||
CC2420_READ_REG(regname, reg);
|
||||
return reg;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static void
|
||||
setreg(enum cc2420_register regname, unsigned value)
|
||||
{
|
||||
CC2420_WRITE_REG(regname, value);
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static void
|
||||
set_txpower(uint8_t power)
|
||||
{
|
||||
uint16_t reg;
|
||||
|
||||
reg = getreg(CC2420_TXCTRL);
|
||||
reg = (reg & 0xffe0) | (power & 0x1f);
|
||||
setreg(CC2420_TXCTRL, reg);
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
#define AUTOACK (1 << 4)
|
||||
#define ADR_DECODE (1 << 11)
|
||||
#define RXFIFO_PROTECTION (1 << 9)
|
||||
#define CORR_THR(n) (((n) & 0x1f) << 6)
|
||||
#define FIFOP_THR(n) ((n) & 0x7f)
|
||||
#define RXBPF_LOCUR (1 << 13);
|
||||
/*---------------------------------------------------------------------------*/
|
||||
int
|
||||
cc2420_init(void)
|
||||
{
|
||||
uint16_t reg;
|
||||
{
|
||||
int s = splhigh();
|
||||
cc2420_arch_init(); /* Initalize ports and SPI. */
|
||||
CC2420_DISABLE_FIFOP_INT();
|
||||
CC2420_FIFOP_INT_INIT();
|
||||
splx(s);
|
||||
}
|
||||
|
||||
/* Turn on voltage regulator and reset. */
|
||||
SET_VREG_ACTIVE();
|
||||
clock_delay(250);
|
||||
SET_RESET_ACTIVE();
|
||||
clock_delay(127);
|
||||
SET_RESET_INACTIVE();
|
||||
clock_delay(125);
|
||||
|
||||
|
||||
/* Turn on the crystal oscillator. */
|
||||
strobe(CC2420_SXOSCON);
|
||||
|
||||
/* Turn on/off automatic packet acknowledgment and address decoding. */
|
||||
reg = getreg(CC2420_MDMCTRL0);
|
||||
|
||||
#if CC2420_CONF_AUTOACK
|
||||
reg |= AUTOACK | ADR_DECODE;
|
||||
#else
|
||||
reg &= ~(AUTOACK | ADR_DECODE);
|
||||
#endif /* CC2420_CONF_AUTOACK */
|
||||
setreg(CC2420_MDMCTRL0, reg);
|
||||
|
||||
/* Set transmission turnaround time to the lower setting (8 symbols
|
||||
= 0.128 ms) instead of the default (12 symbols = 0.192 ms). */
|
||||
/* reg = getreg(CC2420_TXCTRL);
|
||||
reg &= ~(1 << 13);
|
||||
setreg(CC2420_TXCTRL, reg);*/
|
||||
|
||||
|
||||
/* Change default values as recomended in the data sheet, */
|
||||
/* correlation threshold = 20, RX bandpass filter = 1.3uA. */
|
||||
setreg(CC2420_MDMCTRL1, CORR_THR(20));
|
||||
reg = getreg(CC2420_RXCTRL1);
|
||||
reg |= RXBPF_LOCUR;
|
||||
setreg(CC2420_RXCTRL1, reg);
|
||||
|
||||
/* Set the FIFOP threshold to maximum. */
|
||||
setreg(CC2420_IOCFG0, FIFOP_THR(127));
|
||||
|
||||
/* Turn off "Security enable" (page 32). */
|
||||
reg = getreg(CC2420_SECCTRL0);
|
||||
reg &= ~RXFIFO_PROTECTION;
|
||||
setreg(CC2420_SECCTRL0, reg);
|
||||
|
||||
cc2420_set_pan_addr(0xffff, 0x0000, NULL);
|
||||
cc2420_set_channel(CC2420_CONF_CHANNEL);
|
||||
cc2420_set_cca_threshold(CC2420_CONF_CCA_THRESH);
|
||||
|
||||
flushrx();
|
||||
|
||||
process_start(&cc2420_process, NULL);
|
||||
return 1;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static int
|
||||
cc2420_transmit(unsigned short payload_len)
|
||||
{
|
||||
int i, txpower;
|
||||
uint8_t total_len;
|
||||
#if CC2420_CONF_CHECKSUM
|
||||
uint16_t checksum;
|
||||
#endif /* CC2420_CONF_CHECKSUM */
|
||||
|
||||
GET_LOCK();
|
||||
|
||||
txpower = 0;
|
||||
if(packetbuf_attr(PACKETBUF_ATTR_RADIO_TXPOWER) > 0) {
|
||||
/* Remember the current transmission power */
|
||||
txpower = cc2420_get_txpower();
|
||||
/* Set the specified transmission power */
|
||||
set_txpower(packetbuf_attr(PACKETBUF_ATTR_RADIO_TXPOWER) - 1);
|
||||
}
|
||||
|
||||
total_len = payload_len + AUX_LEN;
|
||||
|
||||
/* The TX FIFO can only hold one packet. Make sure to not overrun
|
||||
* FIFO by waiting for transmission to start here and synchronizing
|
||||
* with the CC2420_TX_ACTIVE check in cc2420_send.
|
||||
*
|
||||
* Note that we may have to wait up to 320 us (20 symbols) before
|
||||
* transmission starts.
|
||||
*/
|
||||
#ifndef CC2420_CONF_SYMBOL_LOOP_COUNT
|
||||
#error CC2420_CONF_SYMBOL_LOOP_COUNT needs to be set!!!
|
||||
#else
|
||||
#define LOOP_20_SYMBOLS CC2420_CONF_SYMBOL_LOOP_COUNT
|
||||
#endif
|
||||
|
||||
#if WITH_SEND_CCA
|
||||
strobe(CC2420_SRXON);
|
||||
BUSYWAIT_UNTIL(status() & BV(CC2420_RSSI_VALID), RTIMER_SECOND / 10);
|
||||
strobe(CC2420_STXONCCA);
|
||||
#else /* WITH_SEND_CCA */
|
||||
strobe(CC2420_STXON);
|
||||
#endif /* WITH_SEND_CCA */
|
||||
for(i = LOOP_20_SYMBOLS; i > 0; i--) {
|
||||
if(CC2420_SFD_IS_1) {
|
||||
{
|
||||
rtimer_clock_t sfd_timestamp;
|
||||
sfd_timestamp = cc2420_sfd_start_time;
|
||||
if(packetbuf_attr(PACKETBUF_ATTR_PACKET_TYPE) ==
|
||||
PACKETBUF_ATTR_PACKET_TYPE_TIMESTAMP) {
|
||||
/* Write timestamp to last two bytes of packet in TXFIFO. */
|
||||
CC2420_WRITE_RAM(&sfd_timestamp, CC2420RAM_TXFIFO + payload_len - 1, 2);
|
||||
}
|
||||
}
|
||||
|
||||
if(!(status() & BV(CC2420_TX_ACTIVE))) {
|
||||
/* SFD went high but we are not transmitting. This means that
|
||||
we just started receiving a packet, so we drop the
|
||||
transmission. */
|
||||
RELEASE_LOCK();
|
||||
return RADIO_TX_COLLISION;
|
||||
}
|
||||
if(receive_on) {
|
||||
ENERGEST_OFF(ENERGEST_TYPE_LISTEN);
|
||||
}
|
||||
ENERGEST_ON(ENERGEST_TYPE_TRANSMIT);
|
||||
/* We wait until transmission has ended so that we get an
|
||||
accurate measurement of the transmission time.*/
|
||||
BUSYWAIT_UNTIL(!(status() & BV(CC2420_TX_ACTIVE)), RTIMER_SECOND / 10);
|
||||
|
||||
#ifdef ENERGEST_CONF_LEVELDEVICE_LEVELS
|
||||
ENERGEST_OFF_LEVEL(ENERGEST_TYPE_TRANSMIT,cc2420_get_txpower());
|
||||
#endif
|
||||
ENERGEST_OFF(ENERGEST_TYPE_TRANSMIT);
|
||||
if(receive_on) {
|
||||
ENERGEST_ON(ENERGEST_TYPE_LISTEN);
|
||||
} else {
|
||||
/* We need to explicitly turn off the radio,
|
||||
* since STXON[CCA] -> TX_ACTIVE -> RX_ACTIVE */
|
||||
off();
|
||||
}
|
||||
|
||||
if(packetbuf_attr(PACKETBUF_ATTR_RADIO_TXPOWER) > 0) {
|
||||
/* Restore the transmission power */
|
||||
set_txpower(txpower & 0xff);
|
||||
}
|
||||
|
||||
RELEASE_LOCK();
|
||||
return RADIO_TX_OK;
|
||||
}
|
||||
}
|
||||
|
||||
/* If we are using WITH_SEND_CCA, we get here if the packet wasn't
|
||||
transmitted because of other channel activity. */
|
||||
RIMESTATS_ADD(contentiondrop);
|
||||
PRINTF("cc2420: do_send() transmission never started\n");
|
||||
|
||||
if(packetbuf_attr(PACKETBUF_ATTR_RADIO_TXPOWER) > 0) {
|
||||
/* Restore the transmission power */
|
||||
set_txpower(txpower & 0xff);
|
||||
}
|
||||
|
||||
RELEASE_LOCK();
|
||||
return RADIO_TX_COLLISION;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static int
|
||||
cc2420_prepare(const void *payload, unsigned short payload_len)
|
||||
{
|
||||
uint8_t total_len;
|
||||
#if CC2420_CONF_CHECKSUM
|
||||
uint16_t checksum;
|
||||
#endif /* CC2420_CONF_CHECKSUM */
|
||||
GET_LOCK();
|
||||
|
||||
PRINTF("cc2420: sending %d bytes\n", payload_len);
|
||||
|
||||
RIMESTATS_ADD(lltx);
|
||||
|
||||
/* Wait for any previous transmission to finish. */
|
||||
/* while(status() & BV(CC2420_TX_ACTIVE));*/
|
||||
|
||||
/* Write packet to TX FIFO. */
|
||||
strobe(CC2420_SFLUSHTX);
|
||||
|
||||
#if CC2420_CONF_CHECKSUM
|
||||
checksum = crc16_data(payload, payload_len, 0);
|
||||
#endif /* CC2420_CONF_CHECKSUM */
|
||||
total_len = payload_len + AUX_LEN;
|
||||
CC2420_WRITE_FIFO_BUF(&total_len, 1);
|
||||
CC2420_WRITE_FIFO_BUF(payload, payload_len);
|
||||
#if CC2420_CONF_CHECKSUM
|
||||
CC2420_WRITE_FIFO_BUF(&checksum, CHECKSUM_LEN);
|
||||
#endif /* CC2420_CONF_CHECKSUM */
|
||||
|
||||
RELEASE_LOCK();
|
||||
return 0;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static int
|
||||
cc2420_send(const void *payload, unsigned short payload_len)
|
||||
{
|
||||
cc2420_prepare(payload, payload_len);
|
||||
return cc2420_transmit(payload_len);
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
int
|
||||
cc2420_off(void)
|
||||
{
|
||||
/* Don't do anything if we are already turned off. */
|
||||
if(receive_on == 0) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* If we are called when the driver is locked, we indicate that the
|
||||
radio should be turned off when the lock is unlocked. */
|
||||
if(locked) {
|
||||
/* printf("Off when locked (%d)\n", locked);*/
|
||||
lock_off = 1;
|
||||
return 1;
|
||||
}
|
||||
|
||||
GET_LOCK();
|
||||
/* If we are currently receiving a packet (indicated by SFD == 1),
|
||||
we don't actually switch the radio off now, but signal that the
|
||||
driver should switch off the radio once the packet has been
|
||||
received and processed, by setting the 'lock_off' variable. */
|
||||
if(status() & BV(CC2420_TX_ACTIVE)) {
|
||||
lock_off = 1;
|
||||
} else {
|
||||
off();
|
||||
}
|
||||
RELEASE_LOCK();
|
||||
return 1;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
int
|
||||
cc2420_on(void)
|
||||
{
|
||||
if(receive_on) {
|
||||
return 1;
|
||||
}
|
||||
if(locked) {
|
||||
lock_on = 1;
|
||||
return 1;
|
||||
}
|
||||
|
||||
GET_LOCK();
|
||||
on();
|
||||
RELEASE_LOCK();
|
||||
return 1;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
int
|
||||
cc2420_get_channel(void)
|
||||
{
|
||||
return channel;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
int
|
||||
cc2420_set_channel(int c)
|
||||
{
|
||||
uint16_t f;
|
||||
|
||||
GET_LOCK();
|
||||
/*
|
||||
* Subtract the base channel (11), multiply by 5, which is the
|
||||
* channel spacing. 357 is 2405-2048 and 0x4000 is LOCK_THR = 1.
|
||||
*/
|
||||
channel = c;
|
||||
|
||||
f = 5 * (c - 11) + 357 + 0x4000;
|
||||
/*
|
||||
* Writing RAM requires crystal oscillator to be stable.
|
||||
*/
|
||||
BUSYWAIT_UNTIL((status() & (BV(CC2420_XOSC16M_STABLE))), RTIMER_SECOND / 10);
|
||||
|
||||
/* Wait for any transmission to end. */
|
||||
BUSYWAIT_UNTIL(!(status() & BV(CC2420_TX_ACTIVE)), RTIMER_SECOND / 10);
|
||||
|
||||
setreg(CC2420_FSCTRL, f);
|
||||
|
||||
/* If we are in receive mode, we issue an SRXON command to ensure
|
||||
that the VCO is calibrated. */
|
||||
if(receive_on) {
|
||||
strobe(CC2420_SRXON);
|
||||
}
|
||||
|
||||
RELEASE_LOCK();
|
||||
return 1;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
void
|
||||
cc2420_set_pan_addr(unsigned pan,
|
||||
unsigned addr,
|
||||
const uint8_t *ieee_addr)
|
||||
{
|
||||
uint16_t f = 0;
|
||||
uint8_t tmp[2];
|
||||
|
||||
GET_LOCK();
|
||||
|
||||
/*
|
||||
* Writing RAM requires crystal oscillator to be stable.
|
||||
*/
|
||||
BUSYWAIT_UNTIL(status() & (BV(CC2420_XOSC16M_STABLE)), RTIMER_SECOND / 10);
|
||||
|
||||
tmp[0] = pan & 0xff;
|
||||
tmp[1] = pan >> 8;
|
||||
CC2420_WRITE_RAM(&tmp, CC2420RAM_PANID, 2);
|
||||
|
||||
tmp[0] = addr & 0xff;
|
||||
tmp[1] = addr >> 8;
|
||||
CC2420_WRITE_RAM(&tmp, CC2420RAM_SHORTADDR, 2);
|
||||
if(ieee_addr != NULL) {
|
||||
uint8_t tmp_addr[8];
|
||||
/* LSB first, MSB last for 802.15.4 addresses in CC2420 */
|
||||
for (f = 0; f < 8; f++) {
|
||||
tmp_addr[7 - f] = ieee_addr[f];
|
||||
}
|
||||
CC2420_WRITE_RAM(tmp_addr, CC2420RAM_IEEEADDR, 8);
|
||||
}
|
||||
RELEASE_LOCK();
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/*
|
||||
* Interrupt leaves frame intact in FIFO.
|
||||
*/
|
||||
int
|
||||
cc2420_interrupt(void)
|
||||
{
|
||||
CC2420_CLEAR_FIFOP_INT();
|
||||
process_poll(&cc2420_process);
|
||||
|
||||
last_packet_timestamp = cc2420_sfd_start_time;
|
||||
pending++;
|
||||
cc2420_packets_seen++;
|
||||
return 1;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
PROCESS_THREAD(cc2420_process, ev, data)
|
||||
{
|
||||
int len;
|
||||
PROCESS_BEGIN();
|
||||
|
||||
PRINTF("cc2420_process: started\n");
|
||||
|
||||
while(1) {
|
||||
PROCESS_YIELD_UNTIL(ev == PROCESS_EVENT_POLL);
|
||||
|
||||
PRINTF("cc2420_process: calling receiver callback\n");
|
||||
|
||||
packetbuf_clear();
|
||||
packetbuf_set_attr(PACKETBUF_ATTR_TIMESTAMP, last_packet_timestamp);
|
||||
len = cc2420_read(packetbuf_dataptr(), PACKETBUF_SIZE);
|
||||
|
||||
packetbuf_set_datalen(len);
|
||||
|
||||
NETSTACK_RDC.input();
|
||||
}
|
||||
|
||||
PROCESS_END();
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static int
|
||||
cc2420_read(void *buf, unsigned short bufsize)
|
||||
{
|
||||
uint8_t footer[2];
|
||||
uint8_t len;
|
||||
#if CC2420_CONF_CHECKSUM
|
||||
uint16_t checksum;
|
||||
#endif /* CC2420_CONF_CHECKSUM */
|
||||
|
||||
if(!CC2420_FIFOP_IS_1) {
|
||||
return 0;
|
||||
}
|
||||
/* if(!pending) {
|
||||
return 0;
|
||||
}*/
|
||||
|
||||
pending = 0;
|
||||
|
||||
GET_LOCK();
|
||||
|
||||
cc2420_packets_read++;
|
||||
|
||||
getrxbyte(&len);
|
||||
|
||||
if(len > CC2420_MAX_PACKET_LEN) {
|
||||
/* Oops, we must be out of sync. */
|
||||
flushrx();
|
||||
RIMESTATS_ADD(badsynch);
|
||||
RELEASE_LOCK();
|
||||
return 0;
|
||||
}
|
||||
|
||||
if(len <= AUX_LEN) {
|
||||
flushrx();
|
||||
RIMESTATS_ADD(tooshort);
|
||||
RELEASE_LOCK();
|
||||
return 0;
|
||||
}
|
||||
|
||||
if(len - AUX_LEN > bufsize) {
|
||||
flushrx();
|
||||
RIMESTATS_ADD(toolong);
|
||||
RELEASE_LOCK();
|
||||
return 0;
|
||||
}
|
||||
|
||||
getrxdata(buf, len - AUX_LEN);
|
||||
#if CC2420_CONF_CHECKSUM
|
||||
getrxdata(&checksum, CHECKSUM_LEN);
|
||||
#endif /* CC2420_CONF_CHECKSUM */
|
||||
getrxdata(footer, FOOTER_LEN);
|
||||
|
||||
#if CC2420_CONF_CHECKSUM
|
||||
if(checksum != crc16_data(buf, len - AUX_LEN, 0)) {
|
||||
PRINTF("checksum failed 0x%04x != 0x%04x\n",
|
||||
checksum, crc16_data(buf, len - AUX_LEN, 0));
|
||||
}
|
||||
|
||||
if(footer[1] & FOOTER1_CRC_OK &&
|
||||
checksum == crc16_data(buf, len - AUX_LEN, 0)) {
|
||||
#else
|
||||
if(footer[1] & FOOTER1_CRC_OK) {
|
||||
#endif /* CC2420_CONF_CHECKSUM */
|
||||
cc2420_last_rssi = footer[0];
|
||||
cc2420_last_correlation = footer[1] & FOOTER1_CORRELATION;
|
||||
|
||||
|
||||
packetbuf_set_attr(PACKETBUF_ATTR_RSSI, cc2420_last_rssi);
|
||||
packetbuf_set_attr(PACKETBUF_ATTR_LINK_QUALITY, cc2420_last_correlation);
|
||||
|
||||
RIMESTATS_ADD(llrx);
|
||||
|
||||
} else {
|
||||
RIMESTATS_ADD(badcrc);
|
||||
len = AUX_LEN;
|
||||
}
|
||||
|
||||
if(CC2420_FIFOP_IS_1) {
|
||||
if(!CC2420_FIFO_IS_1) {
|
||||
/* Clean up in case of FIFO overflow! This happens for every
|
||||
* full length frame and is signaled by FIFOP = 1 and FIFO =
|
||||
* 0. */
|
||||
flushrx();
|
||||
} else {
|
||||
/* Another packet has been received and needs attention. */
|
||||
process_poll(&cc2420_process);
|
||||
}
|
||||
}
|
||||
|
||||
RELEASE_LOCK();
|
||||
|
||||
if(len < AUX_LEN) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return len - AUX_LEN;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
void
|
||||
cc2420_set_txpower(uint8_t power)
|
||||
{
|
||||
GET_LOCK();
|
||||
set_txpower(power);
|
||||
RELEASE_LOCK();
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
int
|
||||
cc2420_get_txpower(void)
|
||||
{
|
||||
int power;
|
||||
GET_LOCK();
|
||||
power = (int)(getreg(CC2420_TXCTRL) & 0x001f);
|
||||
RELEASE_LOCK();
|
||||
return power;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
int
|
||||
cc2420_rssi(void)
|
||||
{
|
||||
int rssi;
|
||||
int radio_was_off = 0;
|
||||
|
||||
if(locked) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
GET_LOCK();
|
||||
|
||||
if(!receive_on) {
|
||||
radio_was_off = 1;
|
||||
cc2420_on();
|
||||
}
|
||||
BUSYWAIT_UNTIL(status() & BV(CC2420_RSSI_VALID), RTIMER_SECOND / 100);
|
||||
|
||||
rssi = (int)((signed char)getreg(CC2420_RSSI));
|
||||
|
||||
if(radio_was_off) {
|
||||
cc2420_off();
|
||||
}
|
||||
RELEASE_LOCK();
|
||||
return rssi;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/*
|
||||
static int
|
||||
detected_energy(void)
|
||||
{
|
||||
return cc2420_rssi();
|
||||
}
|
||||
*/
|
||||
/*---------------------------------------------------------------------------*/
|
||||
int
|
||||
cc2420_cca_valid(void)
|
||||
{
|
||||
int valid;
|
||||
if(locked) {
|
||||
return 1;
|
||||
}
|
||||
GET_LOCK();
|
||||
valid = !!(status() & BV(CC2420_RSSI_VALID));
|
||||
RELEASE_LOCK();
|
||||
return valid;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static int
|
||||
cc2420_cca(void)
|
||||
{
|
||||
int cca;
|
||||
int radio_was_off = 0;
|
||||
|
||||
/* If the radio is locked by an underlying thread (because we are
|
||||
being invoked through an interrupt), we preted that the coast is
|
||||
clear (i.e., no packet is currently being transmitted by a
|
||||
neighbor). */
|
||||
if(locked) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
GET_LOCK();
|
||||
if(!receive_on) {
|
||||
radio_was_off = 1;
|
||||
cc2420_on();
|
||||
}
|
||||
|
||||
/* Make sure that the radio really got turned on. */
|
||||
if(!receive_on) {
|
||||
RELEASE_LOCK();
|
||||
if(radio_was_off) {
|
||||
cc2420_off();
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
BUSYWAIT_UNTIL(status() & BV(CC2420_RSSI_VALID), RTIMER_SECOND / 100);
|
||||
|
||||
cca = CC2420_CCA_IS_1;
|
||||
|
||||
if(radio_was_off) {
|
||||
cc2420_off();
|
||||
}
|
||||
RELEASE_LOCK();
|
||||
return cca;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
int
|
||||
cc2420_receiving_packet(void)
|
||||
{
|
||||
return CC2420_SFD_IS_1;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static int
|
||||
pending_packet(void)
|
||||
{
|
||||
return CC2420_FIFOP_IS_1;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
void
|
||||
cc2420_set_cca_threshold(int value)
|
||||
{
|
||||
uint16_t shifted = value << 8;
|
||||
GET_LOCK();
|
||||
setreg(CC2420_RSSI, shifted);
|
||||
RELEASE_LOCK();
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
203
dev/cc2420/cc2420.h
Normal file
203
dev/cc2420/cc2420.h
Normal file
|
@ -0,0 +1,203 @@
|
|||
/*
|
||||
* Copyright (c) 2007, Swedish Institute of Computer Science.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the Institute nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* This file is part of the Contiki operating system.
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* \file
|
||||
* CC2420 driver header file
|
||||
* \author
|
||||
* Adam Dunkels <adam@sics.se>
|
||||
* Joakim Eriksson <joakime@sics.se>
|
||||
*/
|
||||
|
||||
#ifndef CC2420_H_
|
||||
#define CC2420_H_
|
||||
|
||||
#include "contiki.h"
|
||||
#include "dev/spi.h"
|
||||
#include "dev/radio.h"
|
||||
#include "cc2420_const.h"
|
||||
|
||||
int cc2420_init(void);
|
||||
|
||||
#define CC2420_MAX_PACKET_LEN 127
|
||||
|
||||
int cc2420_set_channel(int channel);
|
||||
int cc2420_get_channel(void);
|
||||
|
||||
void cc2420_set_pan_addr(unsigned pan,
|
||||
unsigned addr,
|
||||
const uint8_t *ieee_addr);
|
||||
|
||||
extern signed char cc2420_last_rssi;
|
||||
extern uint8_t cc2420_last_correlation;
|
||||
|
||||
int cc2420_rssi(void);
|
||||
|
||||
extern const struct radio_driver cc2420_driver;
|
||||
|
||||
/**
|
||||
* \param power Between 1 and 31.
|
||||
*/
|
||||
void cc2420_set_txpower(uint8_t power);
|
||||
int cc2420_get_txpower(void);
|
||||
#define CC2420_TXPOWER_MAX 31
|
||||
#define CC2420_TXPOWER_MIN 0
|
||||
|
||||
/**
|
||||
* Interrupt function, called from the simple-cc2420-arch driver.
|
||||
*
|
||||
*/
|
||||
int cc2420_interrupt(void);
|
||||
|
||||
/* XXX hack: these will be made as Chameleon packet attributes */
|
||||
extern rtimer_clock_t cc2420_time_of_arrival,
|
||||
cc2420_time_of_departure;
|
||||
extern int cc2420_authority_level_of_sender;
|
||||
|
||||
int cc2420_on(void);
|
||||
int cc2420_off(void);
|
||||
|
||||
void cc2420_set_cca_threshold(int value);
|
||||
|
||||
/************************************************************************/
|
||||
/* Additional SPI Macros for the CC2420 */
|
||||
/************************************************************************/
|
||||
/* Send a strobe to the CC2420 */
|
||||
#define CC2420_STROBE(s) \
|
||||
do { \
|
||||
CC2420_SPI_ENABLE(); \
|
||||
SPI_WRITE(s); \
|
||||
CC2420_SPI_DISABLE(); \
|
||||
} while (0)
|
||||
|
||||
/* Write to a register in the CC2420 */
|
||||
/* Note: the SPI_WRITE(0) seems to be needed for getting the */
|
||||
/* write reg working on the Z1 / MSP430X platform */
|
||||
#define CC2420_WRITE_REG(adr,data) \
|
||||
do { \
|
||||
CC2420_SPI_ENABLE(); \
|
||||
SPI_WRITE_FAST(adr); \
|
||||
SPI_WRITE_FAST((uint8_t)((data) >> 8)); \
|
||||
SPI_WRITE_FAST((uint8_t)(data & 0xff)); \
|
||||
SPI_WAITFORTx_ENDED(); \
|
||||
SPI_WRITE(0); \
|
||||
CC2420_SPI_DISABLE(); \
|
||||
} while(0)
|
||||
|
||||
/* Read a register in the CC2420 */
|
||||
#define CC2420_READ_REG(adr,data) \
|
||||
do { \
|
||||
CC2420_SPI_ENABLE(); \
|
||||
SPI_WRITE(adr | 0x40); \
|
||||
data = (uint8_t)SPI_RXBUF; \
|
||||
SPI_TXBUF = 0; \
|
||||
SPI_WAITFOREORx(); \
|
||||
data = SPI_RXBUF << 8; \
|
||||
SPI_TXBUF = 0; \
|
||||
SPI_WAITFOREORx(); \
|
||||
data |= SPI_RXBUF; \
|
||||
CC2420_SPI_DISABLE(); \
|
||||
} while(0)
|
||||
|
||||
#define CC2420_READ_FIFO_BYTE(data) \
|
||||
do { \
|
||||
CC2420_SPI_ENABLE(); \
|
||||
SPI_WRITE(CC2420_RXFIFO | 0x40); \
|
||||
(void)SPI_RXBUF; \
|
||||
SPI_READ(data); \
|
||||
clock_delay(1); \
|
||||
CC2420_SPI_DISABLE(); \
|
||||
} while(0)
|
||||
|
||||
#define CC2420_READ_FIFO_BUF(buffer,count) \
|
||||
do { \
|
||||
uint8_t i; \
|
||||
CC2420_SPI_ENABLE(); \
|
||||
SPI_WRITE(CC2420_RXFIFO | 0x40); \
|
||||
(void)SPI_RXBUF; \
|
||||
for(i = 0; i < (count); i++) { \
|
||||
SPI_READ(((uint8_t *)(buffer))[i]); \
|
||||
} \
|
||||
clock_delay(1); \
|
||||
CC2420_SPI_DISABLE(); \
|
||||
} while(0)
|
||||
|
||||
#define CC2420_WRITE_FIFO_BUF(buffer,count) \
|
||||
do { \
|
||||
uint8_t i; \
|
||||
CC2420_SPI_ENABLE(); \
|
||||
SPI_WRITE_FAST(CC2420_TXFIFO); \
|
||||
for(i = 0; i < (count); i++) { \
|
||||
SPI_WRITE_FAST(((uint8_t *)(buffer))[i]); \
|
||||
} \
|
||||
SPI_WAITFORTx_ENDED(); \
|
||||
CC2420_SPI_DISABLE(); \
|
||||
} while(0)
|
||||
|
||||
/* Write to RAM in the CC2420 */
|
||||
#define CC2420_WRITE_RAM(buffer,adr,count) \
|
||||
do { \
|
||||
uint8_t i; \
|
||||
CC2420_SPI_ENABLE(); \
|
||||
SPI_WRITE_FAST(0x80 | ((adr) & 0x7f)); \
|
||||
SPI_WRITE_FAST(((adr) >> 1) & 0xc0); \
|
||||
for(i = 0; i < (count); i++) { \
|
||||
SPI_WRITE_FAST(((uint8_t*)(buffer))[i]); \
|
||||
} \
|
||||
SPI_WAITFORTx_ENDED(); \
|
||||
CC2420_SPI_DISABLE(); \
|
||||
} while(0)
|
||||
|
||||
/* Read from RAM in the CC2420 */
|
||||
#define CC2420_READ_RAM(buffer,adr,count) \
|
||||
do { \
|
||||
uint8_t i; \
|
||||
CC2420_SPI_ENABLE(); \
|
||||
SPI_WRITE(0x80 | ((adr) & 0x7f)); \
|
||||
SPI_WRITE((((adr) >> 1) & 0xc0) | 0x20); \
|
||||
SPI_RXBUF; \
|
||||
for(i = 0; i < (count); i++) { \
|
||||
SPI_READ(((uint8_t*)(buffer))[i]); \
|
||||
} \
|
||||
CC2420_SPI_DISABLE(); \
|
||||
} while(0)
|
||||
|
||||
/* Read status of the CC2420 */
|
||||
#define CC2420_GET_STATUS(s) \
|
||||
do { \
|
||||
CC2420_SPI_ENABLE(); \
|
||||
SPI_WRITE(CC2420_SNOP); \
|
||||
s = SPI_RXBUF; \
|
||||
CC2420_SPI_DISABLE(); \
|
||||
} while (0)
|
||||
|
||||
#endif /* CC2420_H_ */
|
152
dev/cc2420/cc2420_const.h
Normal file
152
dev/cc2420/cc2420_const.h
Normal file
|
@ -0,0 +1,152 @@
|
|||
/*
|
||||
* Copyright (c) 2006, Swedish Institute of Computer Science
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the Institute nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#ifndef CC2420_CONST_H
|
||||
#define CC2420_CONST_H
|
||||
|
||||
/*
|
||||
* All constants are from the Chipcon CC2420 Data Sheet that at one
|
||||
* point in time could be found at
|
||||
* http://www.chipcon.com/files/CC2420_Data_Sheet_1_4.pdf
|
||||
*
|
||||
* The page numbers below refer to pages in this document.
|
||||
*/
|
||||
|
||||
/* Page 27. */
|
||||
enum cc2420_status_byte {
|
||||
CC2420_XOSC16M_STABLE = 6,
|
||||
CC2420_TX_UNDERFLOW = 5,
|
||||
CC2420_ENC_BUSY = 4,
|
||||
CC2420_TX_ACTIVE = 3,
|
||||
CC2420_LOCK = 2,
|
||||
CC2420_RSSI_VALID = 1,
|
||||
};
|
||||
|
||||
/* Page 27. */
|
||||
enum cc2420_memory_size {
|
||||
CC2420_RAM_SIZE = 368,
|
||||
CC2420_FIFO_SIZE = 128,
|
||||
};
|
||||
|
||||
/* Page 29. */
|
||||
enum cc2420_address {
|
||||
CC2420RAM_TXFIFO = 0x000,
|
||||
CC2420RAM_RXFIFO = 0x080,
|
||||
CC2420RAM_KEY0 = 0x100,
|
||||
CC2420RAM_RXNONCE = 0x110,
|
||||
CC2420RAM_SABUF = 0x120,
|
||||
CC2420RAM_KEY1 = 0x130,
|
||||
CC2420RAM_TXNONCE = 0x140,
|
||||
CC2420RAM_CBCSTATE = 0x150,
|
||||
CC2420RAM_IEEEADDR = 0x160,
|
||||
CC2420RAM_PANID = 0x168,
|
||||
CC2420RAM_SHORTADDR = 0x16A,
|
||||
};
|
||||
|
||||
/* Page 60. */
|
||||
enum cc2420_register {
|
||||
CC2420_SNOP = 0x00,
|
||||
CC2420_SXOSCON = 0x01,
|
||||
CC2420_STXCAL = 0x02,
|
||||
CC2420_SRXON = 0x03,
|
||||
CC2420_STXON = 0x04,
|
||||
CC2420_STXONCCA = 0x05,
|
||||
CC2420_SRFOFF = 0x06,
|
||||
CC2420_SXOSCOFF = 0x07,
|
||||
CC2420_SFLUSHRX = 0x08,
|
||||
CC2420_SFLUSHTX = 0x09,
|
||||
CC2420_SACK = 0x0A,
|
||||
CC2420_SACKPEND = 0x0B,
|
||||
CC2420_SRXDEC = 0x0C,
|
||||
CC2420_STXENC = 0x0D,
|
||||
CC2420_SAES = 0x0E,
|
||||
CC2420_foo = 0x0F,
|
||||
CC2420_MAIN = 0x10,
|
||||
CC2420_MDMCTRL0 = 0x11,
|
||||
CC2420_MDMCTRL1 = 0x12,
|
||||
CC2420_RSSI = 0x13,
|
||||
CC2420_SYNCWORD = 0x14,
|
||||
CC2420_TXCTRL = 0x15,
|
||||
CC2420_RXCTRL0 = 0x16,
|
||||
CC2420_RXCTRL1 = 0x17,
|
||||
CC2420_FSCTRL = 0x18,
|
||||
CC2420_SECCTRL0 = 0x19,
|
||||
CC2420_SECCTRL1 = 0x1A,
|
||||
CC2420_BATTMON = 0x1B,
|
||||
CC2420_IOCFG0 = 0x1C,
|
||||
CC2420_IOCFG1 = 0x1D,
|
||||
CC2420_MANFIDL = 0x1E,
|
||||
CC2420_MANFIDH = 0x1F,
|
||||
CC2420_FSMTC = 0x20,
|
||||
CC2420_MANAND = 0x21,
|
||||
CC2420_MANOR = 0x22,
|
||||
CC2420_AGCCTRL = 0x23,
|
||||
CC2420_AGCTST0 = 0x24,
|
||||
CC2420_AGCTST1 = 0x25,
|
||||
CC2420_AGCTST2 = 0x26,
|
||||
CC2420_FSTST0 = 0x27,
|
||||
CC2420_FSTST1 = 0x28,
|
||||
CC2420_FSTST2 = 0x29,
|
||||
CC2420_FSTST3 = 0x2A,
|
||||
CC2420_RXBPFTST = 0x2B,
|
||||
CC2420_FSMSTATE = 0x2C,
|
||||
CC2420_ADCTST = 0x2D,
|
||||
CC2420_DACTST = 0x2E,
|
||||
CC2420_TOPTST = 0x2F,
|
||||
CC2420_RESERVED = 0x30,
|
||||
/* 0x31 - 0x3D not used */
|
||||
CC2420_TXFIFO = 0x3E,
|
||||
CC2420_RXFIFO = 0x3F,
|
||||
};
|
||||
|
||||
/* Page 69. */
|
||||
enum cc2420_secctrl0 {
|
||||
CC2420_SECCTRL0_NO_SECURITY = 0x0000,
|
||||
CC2420_SECCTRL0_CBC_MAC = 0x0001,
|
||||
CC2420_SECCTRL0_CTR = 0x0002,
|
||||
CC2420_SECCTRL0_CCM = 0x0003,
|
||||
|
||||
CC2420_SECCTRL0_SEC_M_IDX = 2,
|
||||
|
||||
CC2420_SECCTRL0_RXKEYSEL0 = 0x0000,
|
||||
CC2420_SECCTRL0_RXKEYSEL1 = 0x0020,
|
||||
|
||||
CC2420_SECCTRL0_TXKEYSEL0 = 0x0000,
|
||||
CC2420_SECCTRL0_TXKEYSEL1 = 0x0040,
|
||||
|
||||
CC2420_SECCTRL0_SAKEYSEL0 = 0x0000,
|
||||
CC2420_SECCTRL0_SAKEYSEL1 = 0x0080,
|
||||
|
||||
CC2420_SECCTRL0_SEC_CBC_HEAD = 0x0100,
|
||||
CC2420_SECCTRL0_RXFIFO_PROTECTION = 0x0200,
|
||||
};
|
||||
|
||||
#endif /* CC2420_CONST_H */
|
Loading…
Add table
Add a link
Reference in a new issue