2007-06-28 14:52:41 +02:00
|
|
|
/*
|
2009-03-24 16:32:26 +01:00
|
|
|
* Copyright 2006, Freie Universitaet Berlin. All rights reserved.
|
|
|
|
*
|
|
|
|
* These sources were developed at the Freie Universitaet Berlin, Computer
|
|
|
|
* Systems and Telematics group.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* - Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* - 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.
|
|
|
|
*
|
|
|
|
* - Neither the name of Freie Universitaet Berlin (FUB) 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 FUB and the contributors on an "as is"
|
|
|
|
* basis, without any representations or warranties of any kind, express
|
|
|
|
* or implied including, but not limited to, representations or
|
|
|
|
* warranties of non-infringement, merchantability or fitness for a
|
|
|
|
* particular purpose. In no event shall FUB 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 implementation was originally developed by the CST group at the FUB.
|
|
|
|
*/
|
2007-06-28 14:52:41 +02:00
|
|
|
|
|
|
|
/**
|
2008-11-17 17:18:52 +01:00
|
|
|
* \file cc1020.c
|
|
|
|
* \author FUB ScatterWeb Developers, Michael Baar, Nicolas Tsiftes
|
2007-06-28 14:52:41 +02:00
|
|
|
**/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <signal.h>
|
|
|
|
|
2007-12-13 13:51:38 +01:00
|
|
|
#include "contiki.h"
|
2007-06-28 14:52:41 +02:00
|
|
|
#include "contiki-msb430.h"
|
|
|
|
#include "cc1020-internal.h"
|
|
|
|
#include "cc1020.h"
|
|
|
|
#include "lib/random.h"
|
2008-11-11 13:24:43 +01:00
|
|
|
#include "lib/crc16.h"
|
2007-09-14 21:14:54 +02:00
|
|
|
#include "net/rime/rimestats.h"
|
2007-06-28 14:52:41 +02:00
|
|
|
#include "dev/irq.h"
|
2007-08-16 15:52:17 +02:00
|
|
|
#include "dev/dma.h"
|
2007-12-13 13:51:38 +01:00
|
|
|
#include "energest.h"
|
2007-06-28 14:52:41 +02:00
|
|
|
|
2009-03-24 16:32:26 +01:00
|
|
|
#define DEBUG 0
|
|
|
|
#if DEBUG
|
|
|
|
#include <stdio.h>
|
|
|
|
#define PRINTF(...) printf(__VA_ARGS__)
|
|
|
|
#else
|
|
|
|
#define PRINTF(...)
|
|
|
|
#endif
|
2008-11-11 13:24:43 +01:00
|
|
|
|
2009-03-30 16:07:34 +02:00
|
|
|
#define SEND_TIMEOUT 10
|
|
|
|
|
2007-06-28 14:52:41 +02:00
|
|
|
static int cc1020_calibrate(void);
|
|
|
|
static int cc1020_setupTX(int);
|
|
|
|
static int cc1020_setupRX(int);
|
|
|
|
static void cc1020_setupPD(void);
|
|
|
|
static void cc1020_wakeupTX(int);
|
|
|
|
static void cc1020_wakeupRX(int);
|
2007-08-16 22:38:40 +02:00
|
|
|
static uint8_t cc1020_read_reg(uint8_t addr);
|
|
|
|
static void cc1020_write_reg(uint8_t addr, uint8_t adata);
|
|
|
|
static void cc1020_load_config(const uint8_t *);
|
2007-06-28 14:52:41 +02:00
|
|
|
static void cc1020_reset(void);
|
|
|
|
|
2009-03-24 16:32:26 +01:00
|
|
|
/* current mode of cc1020 chip */
|
2007-12-19 14:09:01 +01:00
|
|
|
static volatile enum cc1020_state cc1020_state = CC1020_OFF;
|
2009-05-14 14:18:49 +02:00
|
|
|
static volatile uint8_t cc1020_rxbuf[HDR_SIZE + CC1020_BUFFERSIZE];
|
|
|
|
static uint8_t cc1020_txbuf[PREAMBLE_SIZE + SYNCWORD_SIZE + HDR_SIZE +
|
|
|
|
CC1020_BUFFERSIZE + TAIL_SIZE];
|
2007-06-28 14:52:41 +02:00
|
|
|
|
2009-03-24 16:32:26 +01:00
|
|
|
/* number of bytes in receive and transmit buffers respectively. */
|
2009-03-25 16:25:05 +01:00
|
|
|
static uint8_t cc1020_rxlen;
|
|
|
|
static uint8_t cc1020_txlen;
|
2007-06-28 14:52:41 +02:00
|
|
|
|
2009-03-24 16:32:26 +01:00
|
|
|
/* received signal strength indicator reading for last received packet */
|
2007-08-16 22:38:40 +02:00
|
|
|
static volatile uint8_t rssi;
|
2007-06-28 14:52:41 +02:00
|
|
|
|
2009-03-24 16:32:26 +01:00
|
|
|
/* callback when a packet has been received */
|
2007-08-16 22:38:40 +02:00
|
|
|
static uint8_t cc1020_pa_power = PA_POWER;
|
2007-06-28 14:52:41 +02:00
|
|
|
|
2009-03-25 16:25:05 +01:00
|
|
|
static volatile char dma_done;
|
2007-12-17 16:26:47 +01:00
|
|
|
|
2007-09-14 20:51:51 +02:00
|
|
|
static void (*receiver_callback)(const struct radio_driver *);
|
2007-06-28 20:27:45 +02:00
|
|
|
|
2007-06-28 14:52:41 +02:00
|
|
|
const struct radio_driver cc1020_driver =
|
|
|
|
{
|
|
|
|
cc1020_send,
|
|
|
|
cc1020_read,
|
|
|
|
cc1020_set_receiver,
|
|
|
|
cc1020_on,
|
|
|
|
cc1020_off
|
|
|
|
};
|
|
|
|
|
2007-12-20 11:48:01 +01:00
|
|
|
#define MS_DELAY(x) clock_delay(354 * (x))
|
|
|
|
|
2007-12-17 17:23:32 +01:00
|
|
|
PROCESS(cc1020_receiver_process, "CC1020 receiver");
|
|
|
|
|
2007-12-17 16:26:47 +01:00
|
|
|
static void
|
|
|
|
dma_callback(void)
|
|
|
|
{
|
|
|
|
dma_done = 1;
|
|
|
|
}
|
2007-06-28 14:52:41 +02:00
|
|
|
|
2008-11-25 17:05:22 +01:00
|
|
|
static void
|
2008-11-11 13:24:43 +01:00
|
|
|
reset_receiver(void)
|
|
|
|
{
|
2009-03-24 16:32:26 +01:00
|
|
|
/* reset receiver */
|
2008-11-11 13:24:43 +01:00
|
|
|
cc1020_rxlen = 0;
|
|
|
|
|
2009-03-19 15:43:31 +01:00
|
|
|
if((cc1020_state & CC1020_TURN_OFF) && (cc1020_txlen == 0)) {
|
2008-11-11 13:24:43 +01:00
|
|
|
cc1020_off();
|
|
|
|
} else {
|
|
|
|
CC1020_SET_OPSTATE(CC1020_RX | CC1020_RX_SEARCHING);
|
2009-03-30 16:07:34 +02:00
|
|
|
cc1020_set_rx();
|
2008-11-11 13:24:43 +01:00
|
|
|
ENABLE_RX_IRQ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-28 14:52:41 +02:00
|
|
|
void
|
2007-08-16 22:38:40 +02:00
|
|
|
cc1020_init(const uint8_t *config)
|
2007-06-28 14:52:41 +02:00
|
|
|
{
|
|
|
|
cc1020_setupPD();
|
|
|
|
cc1020_reset();
|
|
|
|
cc1020_load_config(config);
|
|
|
|
|
2009-03-24 16:32:26 +01:00
|
|
|
/* init tx buffer with preamble + syncword */
|
2009-05-14 14:18:49 +02:00
|
|
|
memset(cc1020_txbuf, PREAMBLE, PREAMBLE_SIZE);
|
|
|
|
memcpy((char *)cc1020_txbuf + PREAMBLE_SIZE, &syncword, SYNCWORD_SIZE);
|
2007-06-28 14:52:41 +02:00
|
|
|
|
2009-03-24 16:32:26 +01:00
|
|
|
/* calibrate receiver */
|
2007-06-28 14:52:41 +02:00
|
|
|
cc1020_wakeupRX(RX_CURRENT);
|
2009-03-19 15:43:31 +01:00
|
|
|
if(!cc1020_calibrate()) {
|
2009-03-24 16:32:26 +01:00
|
|
|
PRINTF("cc1020: rx calibration failed\n");
|
2009-03-19 15:43:31 +01:00
|
|
|
}
|
2007-06-28 14:52:41 +02:00
|
|
|
|
2009-03-24 16:32:26 +01:00
|
|
|
/* calibrate transmitter */
|
2007-06-28 14:52:41 +02:00
|
|
|
cc1020_wakeupTX(TX_CURRENT);
|
2009-03-19 15:43:31 +01:00
|
|
|
if(!cc1020_calibrate()) {
|
2009-03-24 16:32:26 +01:00
|
|
|
PRINTF("cc1020: tx calibration failed\n");
|
2009-03-19 15:43:31 +01:00
|
|
|
}
|
2007-06-28 14:52:41 +02:00
|
|
|
|
2009-03-24 16:32:26 +01:00
|
|
|
/* power down */
|
2007-06-28 14:52:41 +02:00
|
|
|
cc1020_setupPD();
|
2007-08-16 15:52:17 +02:00
|
|
|
|
2007-12-17 16:53:57 +01:00
|
|
|
process_start(&cc1020_receiver_process, NULL);
|
2007-12-17 16:26:47 +01:00
|
|
|
dma_subscribe(0, dma_callback);
|
2007-06-28 14:52:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cc1020_set_rx(void)
|
|
|
|
{
|
2007-09-14 20:51:51 +02:00
|
|
|
int s;
|
|
|
|
|
|
|
|
s = splhigh();
|
2007-06-28 14:52:41 +02:00
|
|
|
|
2009-03-24 16:32:26 +01:00
|
|
|
/* Reset SEL for P3[1-3] (CC DIO, DIO, DCLK) and P3[4-5] (Camera Rx+Tx) */
|
2007-06-28 14:52:41 +02:00
|
|
|
P3SEL &= ~0x3E;
|
2009-03-24 16:32:26 +01:00
|
|
|
IFG1 &= ~(UTXIE0 | URXIE0); /* Clear interrupt flags */
|
|
|
|
ME1 &= ~(UTXE0 | URXE0); /* Disable Uart0 Tx + Rx */
|
|
|
|
UCTL0 = SWRST; /* U0 into reset state. */
|
|
|
|
UCTL0 |= CHAR | SYNC; /* 8-bit character, SPI, Slave mode */
|
2007-06-28 14:52:41 +02:00
|
|
|
|
2009-03-24 16:32:26 +01:00
|
|
|
/* CKPH works also, but not CKPH+CKPL or none of them!! */
|
2007-07-05 10:35:13 +02:00
|
|
|
UTCTL0 = CKPL | STC;
|
2007-06-28 14:52:41 +02:00
|
|
|
URCTL0 = 0x00;
|
2009-03-24 16:32:26 +01:00
|
|
|
UBR00 = 0x00; /* No baudrate divider */
|
|
|
|
UBR10 = 0x00; /* settings for a spi */
|
|
|
|
UMCTL0 = 0x00; /* slave. */
|
|
|
|
ME1 |= URXE0; /* Enable USART0 RXD, disabling does not yield any powersavings */
|
|
|
|
P3SEL |= 0x0A; /* Select rx line and clk */
|
|
|
|
UCTL0 &= ~SWRST; /* Clear reset bit */
|
2007-09-14 20:51:51 +02:00
|
|
|
splx(s);
|
2007-06-28 14:52:41 +02:00
|
|
|
|
2009-03-24 16:32:26 +01:00
|
|
|
/* configure driver */
|
|
|
|
cc1020_rxlen = 0; /* receive buffer position to start */
|
|
|
|
CC1020_SET_OPSTATE(CC1020_RX | CC1020_RX_SEARCHING); /* driver state to receive mode */
|
2007-06-28 14:52:41 +02:00
|
|
|
|
2009-03-24 16:32:26 +01:00
|
|
|
/* configure radio */
|
2007-12-13 13:51:38 +01:00
|
|
|
ENERGEST_ON(ENERGEST_TYPE_LISTEN);
|
2007-06-28 14:52:41 +02:00
|
|
|
cc1020_wakeupRX(RX_CURRENT);
|
|
|
|
cc1020_setupRX(RX_CURRENT);
|
2009-03-24 16:32:26 +01:00
|
|
|
LNA_POWER_ON(); /* enable amplifier */
|
2007-06-28 14:52:41 +02:00
|
|
|
|
2009-03-24 16:32:26 +01:00
|
|
|
/* activate */
|
|
|
|
IE1 |= URXIE0; /* enable interrupt */
|
2007-06-28 14:52:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cc1020_set_tx(void)
|
|
|
|
{
|
2007-09-14 20:51:51 +02:00
|
|
|
int s;
|
|
|
|
|
2009-03-24 16:32:26 +01:00
|
|
|
/* configure radio rx */
|
2007-12-13 13:51:38 +01:00
|
|
|
ENERGEST_OFF(ENERGEST_TYPE_LISTEN);
|
2009-03-24 16:32:26 +01:00
|
|
|
LNA_POWER_OFF(); /* power down LNA */
|
2007-09-14 20:51:51 +02:00
|
|
|
s = splhigh();
|
2007-06-28 14:52:41 +02:00
|
|
|
DISABLE_RX_IRQ();
|
2009-03-24 16:32:26 +01:00
|
|
|
P3SEL &= ~0x02; /* Ensure Rx line is off */
|
2007-09-14 20:51:51 +02:00
|
|
|
splx(s);
|
2007-06-28 14:52:41 +02:00
|
|
|
|
2009-03-24 16:32:26 +01:00
|
|
|
/* configure radio tx */
|
2007-12-13 13:51:38 +01:00
|
|
|
ENERGEST_ON(ENERGEST_TYPE_TRANSMIT);
|
2007-06-28 14:52:41 +02:00
|
|
|
cc1020_wakeupTX(TX_CURRENT);
|
|
|
|
cc1020_setupTX(TX_CURRENT);
|
2009-03-24 16:32:26 +01:00
|
|
|
P3SEL |= 0x0C; /* select Tx line and clk */
|
|
|
|
U0CTL |= SWRST; /* UART to reset mode */
|
|
|
|
IFG1 &= ~UTXIFG0; /* Reset IFG. */
|
2007-06-28 14:52:41 +02:00
|
|
|
|
2009-03-24 16:32:26 +01:00
|
|
|
/* configure driver */
|
2007-12-13 13:51:38 +01:00
|
|
|
CC1020_SET_OPSTATE(CC1020_TX);
|
2007-06-28 14:52:41 +02:00
|
|
|
}
|
|
|
|
|
2007-07-05 10:35:13 +02:00
|
|
|
void
|
2007-08-16 22:38:40 +02:00
|
|
|
cc1020_set_power(uint8_t pa_power)
|
2007-06-28 14:52:41 +02:00
|
|
|
{
|
|
|
|
cc1020_pa_power = pa_power;
|
|
|
|
}
|
|
|
|
|
2007-09-14 20:51:51 +02:00
|
|
|
int
|
2007-11-18 13:25:22 +01:00
|
|
|
cc1020_send(const void *buf, unsigned short len)
|
2007-06-28 14:52:41 +02:00
|
|
|
{
|
2008-11-11 13:24:43 +01:00
|
|
|
int try;
|
2009-05-14 14:18:49 +02:00
|
|
|
int normal_header = HDR_SIZE + len;
|
|
|
|
uint16_t rxcrc = 0xffff; /* For checksum purposes */
|
2009-03-30 16:07:34 +02:00
|
|
|
rtimer_clock_t timeout_time;
|
|
|
|
|
|
|
|
timeout_time = RTIMER_NOW() + RTIMER_SECOND / 1000 * SEND_TIMEOUT;
|
|
|
|
while(cc1020_state & CC1020_RX_RECEIVING) {
|
|
|
|
if(RTIMER_CLOCK_LT(timeout_time, RTIMER_NOW())) {
|
|
|
|
PRINTF("cc1020: transmission blocked by reception in progress\n");
|
|
|
|
return -3;
|
|
|
|
}
|
|
|
|
}
|
2008-11-11 13:24:43 +01:00
|
|
|
|
2009-03-19 15:43:31 +01:00
|
|
|
if(cc1020_state == CC1020_OFF) {
|
2007-10-02 16:05:45 +02:00
|
|
|
return -2;
|
2009-03-19 15:43:31 +01:00
|
|
|
}
|
2007-10-02 16:05:45 +02:00
|
|
|
|
2009-03-19 15:43:31 +01:00
|
|
|
if(len > CC1020_BUFFERSIZE) {
|
2007-09-14 20:51:51 +02:00
|
|
|
return -1;
|
2009-03-19 15:43:31 +01:00
|
|
|
}
|
2007-06-28 20:27:45 +02:00
|
|
|
|
2007-07-05 10:35:13 +02:00
|
|
|
/* The preamble and the sync word are already in buffer. */
|
2009-05-14 14:18:49 +02:00
|
|
|
cc1020_txlen = PREAMBLE_SIZE + SYNCWORD_SIZE;
|
2007-06-28 14:52:41 +02:00
|
|
|
|
2009-03-24 16:32:26 +01:00
|
|
|
/* header */
|
2008-11-11 13:24:43 +01:00
|
|
|
cc1020_txbuf[cc1020_txlen++] = 0x00;
|
2009-05-14 14:18:49 +02:00
|
|
|
cc1020_txbuf[cc1020_txlen++] = normal_header + CRC_SIZE;
|
2008-11-11 13:24:43 +01:00
|
|
|
|
2009-03-24 16:32:26 +01:00
|
|
|
/* Adding the checksum on header and data */
|
2009-05-14 14:18:49 +02:00
|
|
|
rxcrc = crc16_add(normal_header & 0xff, rxcrc);
|
|
|
|
rxcrc = crc16_add((normal_header >> 8) & 0xff, rxcrc);
|
|
|
|
|
|
|
|
rxcrc = crc16_data(buf, len, rxcrc);
|
2008-11-11 13:24:43 +01:00
|
|
|
|
2009-03-24 16:32:26 +01:00
|
|
|
/* data to send */
|
2007-10-02 16:05:45 +02:00
|
|
|
memcpy((char *)cc1020_txbuf + cc1020_txlen, buf, len);
|
2007-06-28 14:52:41 +02:00
|
|
|
cc1020_txlen += len;
|
|
|
|
|
2009-03-24 16:32:26 +01:00
|
|
|
/* Send checksum */
|
2009-05-14 14:18:49 +02:00
|
|
|
cc1020_txbuf[cc1020_txlen++] = rxcrc >> 8;
|
|
|
|
cc1020_txbuf[cc1020_txlen++] = rxcrc & 0xff;
|
2008-11-25 17:05:22 +01:00
|
|
|
|
2009-03-24 16:32:26 +01:00
|
|
|
/* suffix */
|
2007-07-05 10:35:13 +02:00
|
|
|
cc1020_txbuf[cc1020_txlen++] = TAIL;
|
2008-11-11 13:24:43 +01:00
|
|
|
cc1020_txbuf[cc1020_txlen++] = TAIL;
|
2007-06-28 14:52:41 +02:00
|
|
|
|
2009-03-24 16:32:26 +01:00
|
|
|
/* Wait for the medium to become idle. */
|
2009-03-19 15:43:31 +01:00
|
|
|
if(cc1020_carrier_sense()) {
|
|
|
|
for(try = 0; try < CC1020_CONF_CCA_TIMEOUT; try++) {
|
2007-12-20 15:44:31 +01:00
|
|
|
MS_DELAY(1);
|
2009-03-19 15:43:31 +01:00
|
|
|
if(!cc1020_carrier_sense()) {
|
2009-03-24 16:13:11 +01:00
|
|
|
break;
|
2007-12-20 15:44:31 +01:00
|
|
|
}
|
|
|
|
}
|
2009-03-19 15:43:31 +01:00
|
|
|
if(try == CC1020_CONF_CCA_TIMEOUT) {
|
2009-03-24 16:32:26 +01:00
|
|
|
PRINTF("cc1020: CCA failed (RSSI %d)\n", cc1020_get_rssi());
|
2007-12-20 15:44:31 +01:00
|
|
|
return -3;
|
|
|
|
}
|
2007-12-17 16:26:47 +01:00
|
|
|
|
2009-03-24 16:32:26 +01:00
|
|
|
/* Then wait for a short pseudo-random time before sending. */
|
2007-12-20 11:48:01 +01:00
|
|
|
clock_delay(100 * ((random_rand() + 1) & 0xf));
|
2007-12-17 16:26:47 +01:00
|
|
|
}
|
|
|
|
|
2009-03-24 16:32:26 +01:00
|
|
|
/* Switch to transceive mode. */
|
2007-12-17 16:26:47 +01:00
|
|
|
cc1020_set_tx();
|
|
|
|
|
2009-03-24 16:32:26 +01:00
|
|
|
/* Initiate radio transfer. */
|
2007-12-17 16:26:47 +01:00
|
|
|
dma_done = 0;
|
2009-03-19 15:43:31 +01:00
|
|
|
dma_transfer((unsigned char *)&TXBUF0, cc1020_txbuf, cc1020_txlen);
|
|
|
|
while(!dma_done);
|
2007-12-17 16:26:47 +01:00
|
|
|
|
|
|
|
ENERGEST_OFF(ENERGEST_TYPE_TRANSMIT);
|
|
|
|
RIMESTATS_ADD(lltx);
|
|
|
|
|
2009-03-24 16:32:26 +01:00
|
|
|
/* clean up */
|
2007-12-17 16:26:47 +01:00
|
|
|
cc1020_txlen = 0;
|
2009-03-19 15:43:31 +01:00
|
|
|
if(cc1020_state & CC1020_TURN_OFF) {
|
2007-12-17 16:26:47 +01:00
|
|
|
cc1020_off();
|
|
|
|
} else {
|
|
|
|
cc1020_set_rx();
|
|
|
|
}
|
2007-06-28 20:27:45 +02:00
|
|
|
|
2007-06-28 14:52:41 +02:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2007-09-14 20:51:51 +02:00
|
|
|
int
|
2007-11-18 13:25:22 +01:00
|
|
|
cc1020_read(void *buf, unsigned short size)
|
2007-09-14 20:51:51 +02:00
|
|
|
{
|
2007-11-07 16:34:41 +01:00
|
|
|
unsigned len;
|
2007-09-14 20:51:51 +02:00
|
|
|
|
2009-05-14 14:18:49 +02:00
|
|
|
if(cc1020_rxlen <= HDR_SIZE) {
|
2007-09-14 20:51:51 +02:00
|
|
|
return 0;
|
2009-03-19 15:43:31 +01:00
|
|
|
}
|
2007-09-14 20:51:51 +02:00
|
|
|
|
2009-05-14 14:18:49 +02:00
|
|
|
len = cc1020_rxlen - HDR_SIZE;
|
2009-03-19 15:43:31 +01:00
|
|
|
if(len > size) {
|
2007-09-14 21:14:54 +02:00
|
|
|
RIMESTATS_ADD(toolong);
|
2007-09-14 20:51:51 +02:00
|
|
|
return -1;
|
2007-09-14 21:14:54 +02:00
|
|
|
}
|
2007-09-14 20:51:51 +02:00
|
|
|
|
2009-05-14 14:18:49 +02:00
|
|
|
memcpy(buf, (char *)cc1020_rxbuf + HDR_SIZE, len);
|
2007-09-14 21:14:54 +02:00
|
|
|
RIMESTATS_ADD(llrx);
|
|
|
|
|
2008-11-11 13:24:43 +01:00
|
|
|
reset_receiver();
|
2007-12-17 16:53:57 +01:00
|
|
|
|
2007-09-14 20:51:51 +02:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cc1020_set_receiver(void (*recv)(const struct radio_driver *))
|
|
|
|
{
|
|
|
|
receiver_callback = recv;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
cc1020_on(void)
|
|
|
|
{
|
2009-03-19 15:43:31 +01:00
|
|
|
if(cc1020_state == CC1020_OFF) {
|
2009-03-25 16:25:05 +01:00
|
|
|
cc1020_set_rx();
|
|
|
|
} else if(cc1020_state & CC1020_TURN_OFF) {
|
2009-03-19 15:43:31 +01:00
|
|
|
cc1020_state &= ~CC1020_TURN_OFF;
|
|
|
|
cc1020_set_rx();
|
|
|
|
}
|
2007-09-14 20:51:51 +02:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
cc1020_off(void)
|
|
|
|
{
|
|
|
|
int s;
|
|
|
|
|
2009-03-19 15:43:31 +01:00
|
|
|
if(cc1020_state & CC1020_RX_SEARCHING) {
|
2009-03-24 16:32:26 +01:00
|
|
|
/* Discard the current read buffer when the radio is shutting down. */
|
2007-12-13 13:51:38 +01:00
|
|
|
cc1020_rxlen = 0;
|
|
|
|
|
2009-03-24 16:32:26 +01:00
|
|
|
LNA_POWER_OFF(); /* power down lna */
|
2007-12-13 13:51:38 +01:00
|
|
|
s = splhigh();
|
|
|
|
DISABLE_RX_IRQ();
|
|
|
|
cc1020_state = CC1020_OFF;
|
|
|
|
splx(s);
|
2009-03-24 16:32:26 +01:00
|
|
|
cc1020_setupPD(); /* power down radio */
|
2007-12-13 13:51:38 +01:00
|
|
|
ENERGEST_OFF(ENERGEST_TYPE_LISTEN);
|
|
|
|
cc1020_state = CC1020_OFF;
|
|
|
|
} else {
|
|
|
|
cc1020_state |= CC1020_TURN_OFF;
|
|
|
|
}
|
2007-09-14 20:51:51 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t
|
|
|
|
cc1020_get_rssi(void)
|
|
|
|
{
|
2009-03-24 16:13:11 +01:00
|
|
|
return (cc1020_read_reg(CC1020_RSS) & 0x7F);
|
2008-10-29 10:22:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t
|
|
|
|
cc1020_get_packet_rssi(void)
|
|
|
|
{
|
2007-09-14 20:51:51 +02:00
|
|
|
return rssi;
|
|
|
|
}
|
|
|
|
|
2007-11-07 16:34:41 +01:00
|
|
|
int
|
|
|
|
cc1020_carrier_sense(void)
|
|
|
|
{
|
2009-03-24 16:13:11 +01:00
|
|
|
return (cc1020_read_reg(CC1020_STATUS) & CARRIER_SENSE);
|
2007-11-07 16:34:41 +01:00
|
|
|
}
|
|
|
|
|
2007-12-17 16:53:57 +01:00
|
|
|
PROCESS_THREAD(cc1020_receiver_process, ev, data)
|
|
|
|
{
|
|
|
|
PROCESS_BEGIN();
|
|
|
|
|
|
|
|
while(1) {
|
2007-12-17 19:48:50 +01:00
|
|
|
ev = PROCESS_EVENT_NONE;
|
2008-11-11 13:24:43 +01:00
|
|
|
PROCESS_YIELD_UNTIL(ev == PROCESS_EVENT_POLL);
|
|
|
|
|
2007-12-17 16:53:57 +01:00
|
|
|
if(receiver_callback != NULL) {
|
2009-03-25 16:25:05 +01:00
|
|
|
/* Verify the checksum. */
|
2008-11-11 13:24:43 +01:00
|
|
|
uint16_t expected_crc = 0xffff;
|
2009-05-14 14:18:49 +02:00
|
|
|
uint16_t actual_crc;
|
|
|
|
|
|
|
|
actual_crc = (cc1020_rxbuf[cc1020_rxlen - CRC_SIZE] << 8) |
|
|
|
|
cc1020_rxbuf[cc1020_rxlen - CRC_SIZE + 1];
|
|
|
|
cc1020_rxlen -= CRC_SIZE;
|
2008-11-25 17:05:22 +01:00
|
|
|
|
2009-05-14 14:18:49 +02:00
|
|
|
expected_crc = crc16_add(cc1020_rxlen & 0xff, expected_crc);
|
|
|
|
expected_crc = crc16_add((cc1020_rxlen >> 8) & 0xff, expected_crc);
|
|
|
|
expected_crc = crc16_data((char *)&cc1020_rxbuf[HDR_SIZE],
|
|
|
|
cc1020_rxlen - HDR_SIZE, expected_crc);
|
2008-11-11 13:24:43 +01:00
|
|
|
|
2009-03-19 15:43:31 +01:00
|
|
|
if(expected_crc == actual_crc) {
|
2009-03-24 16:13:11 +01:00
|
|
|
receiver_callback(&cc1020_driver);
|
2008-11-11 13:24:43 +01:00
|
|
|
} else {
|
2009-03-24 16:13:11 +01:00
|
|
|
RIMESTATS_ADD(badcrc);
|
|
|
|
reset_receiver();
|
2008-11-11 13:24:43 +01:00
|
|
|
}
|
2007-12-17 16:53:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PROCESS_END();
|
|
|
|
}
|
|
|
|
|
2007-06-28 14:52:41 +02:00
|
|
|
interrupt(UART0RX_VECTOR) cc1020_rxhandler(void)
|
|
|
|
{
|
|
|
|
static signed char syncbs;
|
|
|
|
static union {
|
|
|
|
struct {
|
2007-08-16 22:38:40 +02:00
|
|
|
uint8_t b2;
|
|
|
|
uint8_t b1;
|
|
|
|
uint8_t b4;
|
|
|
|
uint8_t b3;
|
2007-06-28 14:52:41 +02:00
|
|
|
};
|
|
|
|
struct {
|
2007-08-16 15:24:57 +02:00
|
|
|
uint16_t i1;
|
|
|
|
uint16_t i2;
|
2007-06-28 14:52:41 +02:00
|
|
|
};
|
|
|
|
} shiftbuf;
|
2007-10-04 12:02:08 +02:00
|
|
|
static unsigned char pktlen;
|
2007-06-28 14:52:41 +02:00
|
|
|
|
2009-03-19 15:43:31 +01:00
|
|
|
if(cc1020_state & CC1020_RX_SEARCHING) {
|
2007-06-28 14:52:41 +02:00
|
|
|
shiftbuf.b1 = shiftbuf.b2;
|
|
|
|
shiftbuf.b2 = shiftbuf.b3;
|
|
|
|
shiftbuf.b3 = shiftbuf.b4;
|
|
|
|
shiftbuf.b4 = RXBUF0;
|
2009-03-19 15:43:31 +01:00
|
|
|
if(shiftbuf.i1 == 0xAAD3 && shiftbuf.b3 == 0x91) {
|
2009-03-24 16:32:26 +01:00
|
|
|
/* 0 AA D3 91 00 | FF 00 | */
|
2007-06-28 14:52:41 +02:00
|
|
|
syncbs = 0;
|
|
|
|
cc1020_rxbuf[cc1020_rxlen++] = shiftbuf.b4;
|
2009-03-19 15:43:31 +01:00
|
|
|
} else if(shiftbuf.i1 == 0x5569 && shiftbuf.i2 == 0xC880) {
|
2009-03-24 16:32:26 +01:00
|
|
|
/* 1 55 69 C8 80 | 7F 80 | */
|
2007-06-28 14:52:41 +02:00
|
|
|
syncbs = -1;
|
2009-03-19 15:43:31 +01:00
|
|
|
} else if(shiftbuf.i1 == 0xAAB4 && shiftbuf.i2 == 0xE440) {
|
2009-03-24 16:32:26 +01:00
|
|
|
/* 2 AA B4 E4 40 | 3F C0 | */
|
2007-06-28 14:52:41 +02:00
|
|
|
syncbs = -2;
|
2009-03-19 15:43:31 +01:00
|
|
|
} else if(shiftbuf.i1 == 0x555A && shiftbuf.i2 == 0x7220) {
|
2009-03-24 16:32:26 +01:00
|
|
|
/* 3 55 5A 72 20 | 1F E0 | */
|
2007-06-28 14:52:41 +02:00
|
|
|
syncbs = -3;
|
2009-03-19 15:43:31 +01:00
|
|
|
} else if(shiftbuf.i1 == 0xAAAD && shiftbuf.i2 == 0x3910) {
|
2009-03-24 16:32:26 +01:00
|
|
|
/* 4 AA AD 39 10 | 0F F0 | */
|
2007-06-28 14:52:41 +02:00
|
|
|
syncbs = -4;
|
2009-03-19 15:43:31 +01:00
|
|
|
} else if(shiftbuf.i1 == 0x5556 && shiftbuf.i2 == 0x9C88) {
|
2009-03-24 16:32:26 +01:00
|
|
|
/* 5 55 56 9C 88 | 07 F8 | */
|
2007-06-28 14:52:41 +02:00
|
|
|
syncbs = +3;
|
2009-03-19 15:43:31 +01:00
|
|
|
} else if(shiftbuf.i1 == 0xAAAB && shiftbuf.i2 == 0x4E44) {
|
2009-03-24 16:32:26 +01:00
|
|
|
/* 6 AA AB 4E 44 | 03 FC | */
|
2007-06-28 14:52:41 +02:00
|
|
|
syncbs = +2;
|
2009-03-19 15:43:31 +01:00
|
|
|
} else if(shiftbuf.i1 == 0x5555 && shiftbuf.i2 == 0xA722) {
|
2009-03-24 16:32:26 +01:00
|
|
|
/* 7 55 55 A7 22 | 01 FE | */
|
2007-06-28 14:52:41 +02:00
|
|
|
syncbs = +1;
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
2009-03-24 16:13:11 +01:00
|
|
|
rssi = cc1020_get_rssi();
|
2007-12-19 14:09:01 +01:00
|
|
|
CC1020_SET_OPSTATE(CC1020_RX | CC1020_RX_RECEIVING);
|
2009-03-19 15:43:31 +01:00
|
|
|
} else if(cc1020_state & CC1020_RX_RECEIVING) {
|
|
|
|
if(syncbs == 0) {
|
2007-06-28 14:52:41 +02:00
|
|
|
cc1020_rxbuf[cc1020_rxlen] = RXBUF0;
|
|
|
|
} else {
|
|
|
|
shiftbuf.b3 = shiftbuf.b4;
|
|
|
|
shiftbuf.b4 = RXBUF0;
|
2009-03-19 15:43:31 +01:00
|
|
|
if(syncbs < 0) {
|
2007-07-05 10:35:13 +02:00
|
|
|
shiftbuf.i1 = shiftbuf.i2 << -syncbs;
|
|
|
|
cc1020_rxbuf[cc1020_rxlen] = shiftbuf.b1;
|
2007-06-28 14:52:41 +02:00
|
|
|
} else {
|
2007-07-05 10:35:13 +02:00
|
|
|
shiftbuf.i1 = shiftbuf.i2 >> syncbs;
|
|
|
|
cc1020_rxbuf[cc1020_rxlen] = shiftbuf.b2;
|
2007-06-28 14:52:41 +02:00
|
|
|
}
|
|
|
|
}
|
2007-06-28 20:27:45 +02:00
|
|
|
|
2007-06-28 14:52:41 +02:00
|
|
|
cc1020_rxlen++;
|
2008-11-11 13:24:43 +01:00
|
|
|
|
2009-05-14 14:18:49 +02:00
|
|
|
if(cc1020_rxlen == HDR_SIZE) {
|
2007-10-04 12:02:08 +02:00
|
|
|
pktlen = ((struct cc1020_header *)cc1020_rxbuf)->length;
|
2009-03-19 15:43:31 +01:00
|
|
|
if(pktlen == 0 || pktlen > sizeof (cc1020_rxbuf)) {
|
2009-03-24 16:13:11 +01:00
|
|
|
cc1020_rxlen = 0;
|
|
|
|
CC1020_SET_OPSTATE(CC1020_RX | CC1020_RX_SEARCHING);
|
2007-10-04 12:02:08 +02:00
|
|
|
}
|
2009-05-14 14:18:49 +02:00
|
|
|
} else if(cc1020_rxlen > HDR_SIZE) {
|
2009-03-19 15:43:31 +01:00
|
|
|
if(cc1020_rxlen == pktlen) {
|
2007-12-20 11:48:01 +01:00
|
|
|
/* Disable interrupts while processing the packet. */
|
2007-07-05 10:35:13 +02:00
|
|
|
DISABLE_RX_IRQ();
|
2009-03-24 16:13:11 +01:00
|
|
|
CC1020_SET_OPSTATE(CC1020_RX | CC1020_RX_PROCESSING);
|
|
|
|
_BIC_SR_IRQ(LPM3_bits);
|
2007-12-17 16:53:57 +01:00
|
|
|
process_poll(&cc1020_receiver_process);
|
2007-06-28 14:52:41 +02:00
|
|
|
}
|
|
|
|
}
|
2007-12-13 13:51:38 +01:00
|
|
|
}
|
2007-06-28 14:52:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2007-08-16 22:38:40 +02:00
|
|
|
cc1020_write_reg(uint8_t addr, uint8_t adata)
|
2007-06-28 14:52:41 +02:00
|
|
|
{
|
2009-03-24 16:13:11 +01:00
|
|
|
unsigned int i;
|
|
|
|
uint8_t data;
|
2007-06-28 14:52:41 +02:00
|
|
|
|
|
|
|
data = addr << 1;
|
|
|
|
PSEL_ON;
|
|
|
|
|
2009-03-24 16:32:26 +01:00
|
|
|
/* Send address bits */
|
2009-03-19 15:43:31 +01:00
|
|
|
for(i = 0; i < 7; i++) {
|
2007-06-28 14:52:41 +02:00
|
|
|
PCLK_LOW;
|
2009-03-19 15:43:31 +01:00
|
|
|
if(data & 0x80) {
|
2007-06-28 14:52:41 +02:00
|
|
|
PDI_HIGH;
|
2009-03-19 15:43:31 +01:00
|
|
|
} else {
|
2007-06-28 14:52:41 +02:00
|
|
|
PDI_LOW;
|
2009-03-19 15:43:31 +01:00
|
|
|
}
|
2007-06-28 14:52:41 +02:00
|
|
|
data = data << 1;
|
|
|
|
PCLK_HIGH;
|
2009-03-24 16:13:11 +01:00
|
|
|
nop();
|
2007-06-28 14:52:41 +02:00
|
|
|
}
|
|
|
|
|
2009-03-24 16:32:26 +01:00
|
|
|
/* Send read/write bit */
|
|
|
|
/* Ignore bit in data, always use 1 */
|
2007-06-28 14:52:41 +02:00
|
|
|
PCLK_LOW;
|
|
|
|
PDI_HIGH;
|
|
|
|
PCLK_HIGH;
|
2007-08-02 10:58:38 +02:00
|
|
|
nop();
|
2007-06-28 14:52:41 +02:00
|
|
|
data = adata;
|
|
|
|
|
2009-03-24 16:32:26 +01:00
|
|
|
/* Send data bits */
|
2009-03-19 15:43:31 +01:00
|
|
|
for(i = 0; i < 8; i++) {
|
2007-06-28 14:52:41 +02:00
|
|
|
PCLK_LOW;
|
2009-03-19 15:43:31 +01:00
|
|
|
if(data & 0x80) {
|
2007-06-28 14:52:41 +02:00
|
|
|
PDI_HIGH;
|
2009-03-19 15:43:31 +01:00
|
|
|
} else {
|
2007-06-28 14:52:41 +02:00
|
|
|
PDI_LOW;
|
2009-03-19 15:43:31 +01:00
|
|
|
}
|
2007-06-28 14:52:41 +02:00
|
|
|
data = data << 1;
|
|
|
|
PCLK_HIGH;
|
2009-03-24 16:13:11 +01:00
|
|
|
nop();
|
2007-06-28 14:52:41 +02:00
|
|
|
}
|
2007-06-28 20:27:45 +02:00
|
|
|
|
2007-06-28 14:52:41 +02:00
|
|
|
PCLK_LOW;
|
|
|
|
PSEL_OFF;
|
|
|
|
}
|
|
|
|
|
2007-08-16 22:38:40 +02:00
|
|
|
static uint8_t
|
|
|
|
cc1020_read_reg(uint8_t addr)
|
2007-06-28 14:52:41 +02:00
|
|
|
{
|
2009-03-24 16:13:11 +01:00
|
|
|
unsigned int i;
|
|
|
|
uint8_t data;
|
2007-06-28 14:52:41 +02:00
|
|
|
|
|
|
|
data = addr << 1;
|
|
|
|
PSEL_ON;
|
2007-08-02 10:58:38 +02:00
|
|
|
|
2009-03-24 16:32:26 +01:00
|
|
|
/* Send address bits */
|
2009-03-19 15:43:31 +01:00
|
|
|
for(i = 0; i < 7; i++) {
|
2007-06-28 14:52:41 +02:00
|
|
|
PCLK_LOW;
|
2009-03-19 15:43:31 +01:00
|
|
|
if(data & 0x80) {
|
2007-06-28 14:52:41 +02:00
|
|
|
PDI_HIGH;
|
2009-03-19 15:43:31 +01:00
|
|
|
} else {
|
2007-06-28 14:52:41 +02:00
|
|
|
PDI_LOW;
|
2009-03-19 15:43:31 +01:00
|
|
|
}
|
2007-06-28 14:52:41 +02:00
|
|
|
data = data << 1;
|
|
|
|
PCLK_HIGH;
|
2009-03-24 16:13:11 +01:00
|
|
|
nop();
|
2007-06-28 14:52:41 +02:00
|
|
|
}
|
|
|
|
|
2009-03-24 16:32:26 +01:00
|
|
|
/* Send read/write bit */
|
|
|
|
/* Ignore bit in data, always use 0 */
|
2007-06-28 14:52:41 +02:00
|
|
|
PCLK_LOW;
|
|
|
|
PDI_LOW;
|
|
|
|
PCLK_HIGH;
|
2007-08-02 10:58:38 +02:00
|
|
|
nop();
|
2007-06-28 14:52:41 +02:00
|
|
|
PCLK_LOW;
|
|
|
|
|
2009-03-24 16:32:26 +01:00
|
|
|
/* Receive data bits */
|
2009-03-19 15:43:31 +01:00
|
|
|
for(i = 0; i < 8; i++) {
|
2007-08-02 10:58:38 +02:00
|
|
|
nop();
|
2007-06-28 14:52:41 +02:00
|
|
|
PCLK_HIGH;
|
2007-08-02 10:58:38 +02:00
|
|
|
nop();
|
2007-06-28 14:52:41 +02:00
|
|
|
data = data << 1;
|
2009-03-19 15:43:31 +01:00
|
|
|
if(PDO) {
|
2007-06-28 14:52:41 +02:00
|
|
|
data++;
|
2009-03-19 15:43:31 +01:00
|
|
|
}
|
2007-06-28 14:52:41 +02:00
|
|
|
PCLK_LOW;
|
|
|
|
}
|
2007-06-28 20:27:45 +02:00
|
|
|
|
2007-06-28 14:52:41 +02:00
|
|
|
PSEL_OFF;
|
2007-08-02 10:58:38 +02:00
|
|
|
|
2007-06-28 14:52:41 +02:00
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2007-08-16 22:38:40 +02:00
|
|
|
cc1020_load_config(const uint8_t * config)
|
2007-06-28 14:52:41 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2009-03-19 15:43:31 +01:00
|
|
|
for(i = 0; i < 0x28; i++)
|
2007-06-28 14:52:41 +02:00
|
|
|
cc1020_write_reg(i, config[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cc1020_reset(void)
|
|
|
|
{
|
2009-03-24 16:32:26 +01:00
|
|
|
/* Reset CC1020 */
|
2007-06-28 14:52:41 +02:00
|
|
|
cc1020_write_reg(CC1020_MAIN, 0x0FU & ~0x01U);
|
|
|
|
|
2009-03-24 16:32:26 +01:00
|
|
|
/* Bring CC1020 out of reset */
|
2007-06-28 14:52:41 +02:00
|
|
|
cc1020_write_reg(CC1020_MAIN, 0x1F);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
cc1020_calibrate(void)
|
|
|
|
{
|
|
|
|
unsigned int timeout_cnt;
|
|
|
|
|
2009-03-24 16:32:26 +01:00
|
|
|
/* Turn off PA to avoid spurs during calibration in TX mode */
|
2007-06-28 14:52:41 +02:00
|
|
|
cc1020_write_reg(CC1020_PA_POWER, 0x00);
|
|
|
|
|
2009-03-24 16:32:26 +01:00
|
|
|
/* Start calibration */
|
2007-06-28 14:52:41 +02:00
|
|
|
cc1020_write_reg(CC1020_CALIBRATE, 0xB5);
|
2009-03-19 15:43:31 +01:00
|
|
|
while((cc1020_read_reg(CC1020_STATUS) & CAL_COMPLETE) == 0);
|
2007-06-28 14:52:41 +02:00
|
|
|
|
2009-03-24 16:32:26 +01:00
|
|
|
/* Monitor lock */
|
2009-03-19 15:43:31 +01:00
|
|
|
for(timeout_cnt = LOCK_TIMEOUT; timeout_cnt > 0; timeout_cnt--) {
|
|
|
|
if(cc1020_read_reg(CC1020_STATUS) & LOCK_CONTINUOUS) {
|
2007-07-31 09:54:44 +02:00
|
|
|
break;
|
2009-03-19 15:43:31 +01:00
|
|
|
}
|
2007-07-31 09:54:44 +02:00
|
|
|
}
|
2007-06-28 14:52:41 +02:00
|
|
|
|
2009-03-24 16:32:26 +01:00
|
|
|
/* Restore PA_POWER */
|
2007-06-28 14:52:41 +02:00
|
|
|
cc1020_write_reg(CC1020_PA_POWER, cc1020_pa_power);
|
|
|
|
|
2009-03-24 16:32:26 +01:00
|
|
|
/* Return state of LOCK_CONTINUOUS bit */
|
2007-07-31 09:54:44 +02:00
|
|
|
return (cc1020_read_reg(CC1020_STATUS) & LOCK_CONTINUOUS) == LOCK_CONTINUOUS;
|
2007-06-28 14:52:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
cc1020_lock(void)
|
|
|
|
{
|
|
|
|
char lock_status;
|
|
|
|
int i;
|
|
|
|
|
2009-03-24 16:32:26 +01:00
|
|
|
/* Monitor LOCK, lasts 420 - 510 cycles @ 4505600 = 93 us - 113 us */
|
2009-03-19 15:43:31 +01:00
|
|
|
for(i = LOCK_TIMEOUT; i > 0; i--) {
|
2007-06-28 14:52:41 +02:00
|
|
|
lock_status = cc1020_read_reg(CC1020_STATUS) & LOCK_CONTINUOUS;
|
2009-03-19 15:43:31 +01:00
|
|
|
if(lock_status) {
|
2007-06-28 14:52:41 +02:00
|
|
|
break;
|
2009-03-19 15:43:31 +01:00
|
|
|
}
|
2007-06-28 14:52:41 +02:00
|
|
|
}
|
2007-06-28 20:27:45 +02:00
|
|
|
|
2009-03-19 15:43:31 +01:00
|
|
|
if(lock_status == LOCK_CONTINUOUS) {
|
2007-06-28 14:52:41 +02:00
|
|
|
return LOCK_OK;
|
|
|
|
}
|
2007-12-20 11:48:01 +01:00
|
|
|
|
|
|
|
return cc1020_calibrate() ? LOCK_RECAL_OK : LOCK_NOK;
|
2007-06-28 14:52:41 +02:00
|
|
|
}
|
2007-12-20 11:48:01 +01:00
|
|
|
|
2007-06-28 14:52:41 +02:00
|
|
|
static int
|
|
|
|
cc1020_setupRX(int analog)
|
|
|
|
{
|
|
|
|
char lock_status;
|
|
|
|
|
2009-03-24 16:32:26 +01:00
|
|
|
/* Switch into RX, switch to freq. reg A */
|
2009-03-24 16:13:11 +01:00
|
|
|
cc1020_write_reg(CC1020_MAIN, 0x01);
|
2007-06-28 14:52:41 +02:00
|
|
|
lock_status = cc1020_lock();
|
|
|
|
|
2009-03-24 16:32:26 +01:00
|
|
|
/* Switch RX part of CC1020 on */
|
2007-06-28 14:52:41 +02:00
|
|
|
cc1020_write_reg(CC1020_INTERFACE, 0x02);
|
|
|
|
|
2009-03-24 16:32:26 +01:00
|
|
|
/* Return LOCK status to application */
|
2007-06-28 14:52:41 +02:00
|
|
|
return lock_status;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
cc1020_setupTX(int analog)
|
|
|
|
{
|
|
|
|
char lock_status;
|
|
|
|
|
2009-03-24 16:32:26 +01:00
|
|
|
/* Switch into TX, switch to freq. reg B */
|
2007-06-28 14:52:41 +02:00
|
|
|
cc1020_write_reg(CC1020_MAIN, 0xC1);
|
|
|
|
lock_status = cc1020_lock();
|
|
|
|
|
2009-03-24 16:32:26 +01:00
|
|
|
/* Restore PA_POWER */
|
2007-06-28 14:52:41 +02:00
|
|
|
cc1020_write_reg(CC1020_PA_POWER, cc1020_pa_power);
|
|
|
|
|
2009-03-24 16:32:26 +01:00
|
|
|
/* Turn OFF DCLK squelch in TX */
|
2007-06-28 14:52:41 +02:00
|
|
|
cc1020_write_reg(CC1020_INTERFACE, 0x01);
|
|
|
|
|
2009-03-24 16:32:26 +01:00
|
|
|
/* Return LOCK status to application */
|
2007-06-28 14:52:41 +02:00
|
|
|
return lock_status;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cc1020_setupPD(void)
|
|
|
|
{
|
2007-10-02 16:05:45 +02:00
|
|
|
/*
|
|
|
|
* Power down components an reset all registers except MAIN
|
|
|
|
* to their default values.
|
|
|
|
*/
|
|
|
|
cc1020_write_reg(CC1020_MAIN,
|
|
|
|
RESET_N | BIAS_PD | FS_PD | XOSC_PD | PD_MODE_1);
|
|
|
|
|
|
|
|
/* Turn off the power amplifier. */
|
2007-06-28 14:52:41 +02:00
|
|
|
cc1020_write_reg(CC1020_PA_POWER, 0x00);
|
2007-10-02 16:05:45 +02:00
|
|
|
|
|
|
|
cc1020_write_reg(CC1020_POWERDOWN, 0x1F);
|
2007-06-28 14:52:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cc1020_wakeupRX(int analog)
|
|
|
|
{
|
2009-03-24 16:32:26 +01:00
|
|
|
/* Turn on crystal oscillator core. */
|
2007-06-28 14:52:41 +02:00
|
|
|
cc1020_write_reg(CC1020_MAIN, 0x1B);
|
|
|
|
|
2009-03-24 16:32:26 +01:00
|
|
|
/* Setup bias current adjustment. */
|
2007-06-28 14:52:41 +02:00
|
|
|
cc1020_write_reg(CC1020_ANALOG, analog);
|
|
|
|
|
2007-12-20 11:48:01 +01:00
|
|
|
/*
|
|
|
|
* Wait for the crystal oscillator to stabilize.
|
|
|
|
* This typically takes 2-5 ms.
|
|
|
|
*/
|
|
|
|
MS_DELAY(5);
|
2007-06-28 14:52:41 +02:00
|
|
|
|
2009-03-24 16:32:26 +01:00
|
|
|
/* Turn on bias generator. */
|
2007-06-28 14:52:41 +02:00
|
|
|
cc1020_write_reg(CC1020_MAIN, 0x19);
|
|
|
|
|
2009-03-24 16:32:26 +01:00
|
|
|
/* Turn on frequency synthesizer. */
|
2007-06-28 14:52:41 +02:00
|
|
|
cc1020_write_reg(CC1020_MAIN, 0x11);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cc1020_wakeupTX(int analog)
|
|
|
|
{
|
2009-03-24 16:32:26 +01:00
|
|
|
/* Turn on crystal oscillator core. */
|
2007-06-28 14:52:41 +02:00
|
|
|
cc1020_write_reg(CC1020_MAIN, 0xDB);
|
|
|
|
|
2009-03-24 16:32:26 +01:00
|
|
|
/* Setup bias current adjustment. */
|
2007-06-28 14:52:41 +02:00
|
|
|
cc1020_write_reg(CC1020_ANALOG, analog);
|
|
|
|
|
2007-12-20 11:48:01 +01:00
|
|
|
/*
|
|
|
|
* Wait for the crystal oscillator to stabilize.
|
|
|
|
* This typically takes 2-5 ms.
|
|
|
|
*/
|
|
|
|
MS_DELAY(5);
|
2007-06-28 14:52:41 +02:00
|
|
|
|
2009-03-24 16:32:26 +01:00
|
|
|
/* Turn on bias generator. */
|
2007-06-28 14:52:41 +02:00
|
|
|
cc1020_write_reg(CC1020_MAIN, 0xD9);
|
|
|
|
|
2009-03-24 16:32:26 +01:00
|
|
|
/* Turn on frequency synthesizer. */
|
2007-12-20 11:48:01 +01:00
|
|
|
MS_DELAY(1);
|
2007-06-28 14:52:41 +02:00
|
|
|
cc1020_write_reg(CC1020_MAIN, 0xD1);
|
|
|
|
}
|