- avoid costly rx state initialization if the radio is already on.

- code formatting.
This commit is contained in:
nvt-se 2009-03-19 14:43:31 +00:00
parent 8f85355f02
commit 6b22fa60e8
1 changed files with 65 additions and 56 deletions

View File

@ -120,7 +120,7 @@ reset_receiver(void)
// reset receiver // reset receiver
cc1020_rxlen = 0; cc1020_rxlen = 0;
if ((cc1020_state & CC1020_TURN_OFF) && (cc1020_txlen == 0)) { if((cc1020_state & CC1020_TURN_OFF) && (cc1020_txlen == 0)) {
cc1020_off(); cc1020_off();
} else { } else {
CC1020_SET_OPSTATE(CC1020_RX | CC1020_RX_SEARCHING); CC1020_SET_OPSTATE(CC1020_RX | CC1020_RX_SEARCHING);
@ -141,13 +141,15 @@ cc1020_init(const uint8_t *config)
// calibrate receiver // calibrate receiver
cc1020_wakeupRX(RX_CURRENT); cc1020_wakeupRX(RX_CURRENT);
if (!cc1020_calibrate()) if(!cc1020_calibrate()) {
printf("rx calibration failed\n"); printf("rx calibration failed\n");
}
// calibrate transmitter // calibrate transmitter
cc1020_wakeupTX(TX_CURRENT); cc1020_wakeupTX(TX_CURRENT);
if (!cc1020_calibrate()) if(!cc1020_calibrate()) {
printf("tx calibration failed\n"); printf("tx calibration failed\n");
}
// power down // power down
cc1020_setupPD(); cc1020_setupPD();
@ -233,11 +235,13 @@ cc1020_send(const void *buf, unsigned short len)
int normal_header = HDRSIZE + len; int normal_header = HDRSIZE + len;
uint16_t rxcrc = 0xFFFF; // For checksum purposes uint16_t rxcrc = 0xFFFF; // For checksum purposes
if (cc1020_state == CC1020_OFF) if(cc1020_state == CC1020_OFF) {
return -2; return -2;
}
if (len > CC1020_BUFFERSIZE) if(len > CC1020_BUFFERSIZE) {
return -1; return -1;
}
/* The preamble and the sync word are already in buffer. */ /* The preamble and the sync word are already in buffer. */
cc1020_txlen = PREAMBLESIZE + SYNCWDSIZE; cc1020_txlen = PREAMBLESIZE + SYNCWDSIZE;
@ -260,7 +264,6 @@ cc1020_send(const void *buf, unsigned short len)
cc1020_txlen += len; cc1020_txlen += len;
// Send checksum // Send checksum
/* printf("send checksum %04hx\n", rxcrc); */
cc1020_txbuf[cc1020_txlen++] = (uint8_t)(rxcrc >> 8); cc1020_txbuf[cc1020_txlen++] = (uint8_t)(rxcrc >> 8);
cc1020_txbuf[cc1020_txlen++] = (uint8_t)(rxcrc & 0xFF); cc1020_txbuf[cc1020_txlen++] = (uint8_t)(rxcrc & 0xFF);
@ -269,14 +272,14 @@ cc1020_send(const void *buf, unsigned short len)
cc1020_txbuf[cc1020_txlen++] = TAIL; cc1020_txbuf[cc1020_txlen++] = TAIL;
// Wait for the medium to become idle. // Wait for the medium to become idle.
if (cc1020_carrier_sense()) { if(cc1020_carrier_sense()) {
for (try = 0; try < CC1020_CONF_CCA_TIMEOUT; try++) { for(try = 0; try < CC1020_CONF_CCA_TIMEOUT; try++) {
MS_DELAY(1); MS_DELAY(1);
if (!cc1020_carrier_sense()) { if(!cc1020_carrier_sense()) {
break; break;
} }
} }
if (try == CC1020_CONF_CCA_TIMEOUT) { if(try == CC1020_CONF_CCA_TIMEOUT) {
return -3; return -3;
} }
@ -289,15 +292,15 @@ cc1020_send(const void *buf, unsigned short len)
// Initiate radio transfer. // Initiate radio transfer.
dma_done = 0; dma_done = 0;
dma_transfer(&TXBUF0, cc1020_txbuf, cc1020_txlen); dma_transfer((unsigned char *)&TXBUF0, cc1020_txbuf, cc1020_txlen);
while (!dma_done); while(!dma_done);
ENERGEST_OFF(ENERGEST_TYPE_TRANSMIT); ENERGEST_OFF(ENERGEST_TYPE_TRANSMIT);
RIMESTATS_ADD(lltx); RIMESTATS_ADD(lltx);
// clean up // clean up
cc1020_txlen = 0; cc1020_txlen = 0;
if (cc1020_state & CC1020_TURN_OFF) { if(cc1020_state & CC1020_TURN_OFF) {
cc1020_off(); cc1020_off();
} else { } else {
cc1020_set_rx(); cc1020_set_rx();
@ -311,11 +314,12 @@ cc1020_read(void *buf, unsigned short size)
{ {
unsigned len; unsigned len;
if (cc1020_rxlen <= HDRSIZE) if(cc1020_rxlen <= HDRSIZE) {
return 0; return 0;
}
len = cc1020_rxlen - HDRSIZE; len = cc1020_rxlen - HDRSIZE;
if (len > size) { if(len > size) {
RIMESTATS_ADD(toolong); RIMESTATS_ADD(toolong);
return -1; return -1;
} }
@ -337,9 +341,10 @@ cc1020_set_receiver(void (*recv)(const struct radio_driver *))
int int
cc1020_on(void) cc1020_on(void)
{ {
cc1020_state &= ~CC1020_TURN_OFF; if(cc1020_state == CC1020_OFF) {
// Switch to receive mode cc1020_state &= ~CC1020_TURN_OFF;
cc1020_set_rx(); cc1020_set_rx();
}
return 1; return 1;
} }
@ -349,7 +354,7 @@ cc1020_off(void)
{ {
int s; int s;
if (cc1020_state & CC1020_RX_SEARCHING) { if(cc1020_state & CC1020_RX_SEARCHING) {
// Discard the current read buffer when the radio is shutting down. // Discard the current read buffer when the radio is shutting down.
cc1020_rxlen = 0; cc1020_rxlen = 0;
@ -410,13 +415,11 @@ PROCESS_THREAD(cc1020_receiver_process, ev, data)
expected_crc = crc16_add(cc1020_rxbuf[i], expected_crc); expected_crc = crc16_add(cc1020_rxbuf[i], expected_crc);
} }
if(expected_crc == actual_crc){ if(expected_crc == actual_crc) {
receiver_callback(&cc1020_driver); receiver_callback(&cc1020_driver);
} else { } else {
RIMESTATS_ADD(badcrc); RIMESTATS_ADD(badcrc);
reset_receiver(); reset_receiver();
/* printf("bad crc. expected: %04hx received: %04hx\n", */
/* expected_crc, actual_crc, cc1020_rxlen); */
} }
} }
} }
@ -441,34 +444,34 @@ interrupt(UART0RX_VECTOR) cc1020_rxhandler(void)
} shiftbuf; } shiftbuf;
static unsigned char pktlen; static unsigned char pktlen;
if (cc1020_state & CC1020_RX_SEARCHING) { if(cc1020_state & CC1020_RX_SEARCHING) {
shiftbuf.b1 = shiftbuf.b2; shiftbuf.b1 = shiftbuf.b2;
shiftbuf.b2 = shiftbuf.b3; shiftbuf.b2 = shiftbuf.b3;
shiftbuf.b3 = shiftbuf.b4; shiftbuf.b3 = shiftbuf.b4;
shiftbuf.b4 = RXBUF0; shiftbuf.b4 = RXBUF0;
if (shiftbuf.i1 == 0xAAD3 && shiftbuf.b3 == 0x91) { if(shiftbuf.i1 == 0xAAD3 && shiftbuf.b3 == 0x91) {
// 0 AA D3 91 00 | FF 00 | // 0 AA D3 91 00 | FF 00 |
syncbs = 0; syncbs = 0;
cc1020_rxbuf[cc1020_rxlen++] = shiftbuf.b4; cc1020_rxbuf[cc1020_rxlen++] = shiftbuf.b4;
} else if (shiftbuf.i1 == 0x5569 && shiftbuf.i2 == 0xC880) { } else if(shiftbuf.i1 == 0x5569 && shiftbuf.i2 == 0xC880) {
// 1 55 69 C8 80 | 7F 80 | // 1 55 69 C8 80 | 7F 80 |
syncbs = -1; syncbs = -1;
} else if (shiftbuf.i1 == 0xAAB4 && shiftbuf.i2 == 0xE440) { } else if(shiftbuf.i1 == 0xAAB4 && shiftbuf.i2 == 0xE440) {
// 2 AA B4 E4 40 | 3F C0 | // 2 AA B4 E4 40 | 3F C0 |
syncbs = -2; syncbs = -2;
} else if (shiftbuf.i1 == 0x555A && shiftbuf.i2 == 0x7220) { } else if(shiftbuf.i1 == 0x555A && shiftbuf.i2 == 0x7220) {
// 3 55 5A 72 20 | 1F E0 | // 3 55 5A 72 20 | 1F E0 |
syncbs = -3; syncbs = -3;
} else if (shiftbuf.i1 == 0xAAAD && shiftbuf.i2 == 0x3910) { } else if(shiftbuf.i1 == 0xAAAD && shiftbuf.i2 == 0x3910) {
// 4 AA AD 39 10 | 0F F0 | // 4 AA AD 39 10 | 0F F0 |
syncbs = -4; syncbs = -4;
} else if (shiftbuf.i1 == 0x5556 && shiftbuf.i2 == 0x9C88) { } else if(shiftbuf.i1 == 0x5556 && shiftbuf.i2 == 0x9C88) {
// 5 55 56 9C 88 | 07 F8 | // 5 55 56 9C 88 | 07 F8 |
syncbs = +3; syncbs = +3;
} else if (shiftbuf.i1 == 0xAAAB && shiftbuf.i2 == 0x4E44) { } else if(shiftbuf.i1 == 0xAAAB && shiftbuf.i2 == 0x4E44) {
// 6 AA AB 4E 44 | 03 FC | // 6 AA AB 4E 44 | 03 FC |
syncbs = +2; syncbs = +2;
} else if (shiftbuf.i1 == 0x5555 && shiftbuf.i2 == 0xA722) { } else if(shiftbuf.i1 == 0x5555 && shiftbuf.i2 == 0xA722) {
// 7 55 55 A7 22 | 01 FE | // 7 55 55 A7 22 | 01 FE |
syncbs = +1; syncbs = +1;
} else { } else {
@ -476,13 +479,13 @@ interrupt(UART0RX_VECTOR) cc1020_rxhandler(void)
} }
rssi = cc1020_read_reg(CC1020_RSS); rssi = cc1020_read_reg(CC1020_RSS);
CC1020_SET_OPSTATE(CC1020_RX | CC1020_RX_RECEIVING); CC1020_SET_OPSTATE(CC1020_RX | CC1020_RX_RECEIVING);
} else if (cc1020_state & CC1020_RX_RECEIVING) { } else if(cc1020_state & CC1020_RX_RECEIVING) {
if (syncbs == 0) { if(syncbs == 0) {
cc1020_rxbuf[cc1020_rxlen] = RXBUF0; cc1020_rxbuf[cc1020_rxlen] = RXBUF0;
} else { } else {
shiftbuf.b3 = shiftbuf.b4; shiftbuf.b3 = shiftbuf.b4;
shiftbuf.b4 = RXBUF0; shiftbuf.b4 = RXBUF0;
if (syncbs < 0) { if(syncbs < 0) {
shiftbuf.i1 = shiftbuf.i2 << -syncbs; shiftbuf.i1 = shiftbuf.i2 << -syncbs;
cc1020_rxbuf[cc1020_rxlen] = shiftbuf.b1; cc1020_rxbuf[cc1020_rxlen] = shiftbuf.b1;
} else { } else {
@ -493,14 +496,14 @@ interrupt(UART0RX_VECTOR) cc1020_rxhandler(void)
cc1020_rxlen++; cc1020_rxlen++;
if (cc1020_rxlen == HDRSIZE) { if(cc1020_rxlen == HDRSIZE) {
pktlen = ((struct cc1020_header *)cc1020_rxbuf)->length; pktlen = ((struct cc1020_header *)cc1020_rxbuf)->length;
if (pktlen == 0 || pktlen > sizeof (cc1020_rxbuf)) { if(pktlen == 0 || pktlen > sizeof (cc1020_rxbuf)) {
cc1020_rxlen = 0; cc1020_rxlen = 0;
CC1020_SET_OPSTATE(CC1020_RX | CC1020_RX_SEARCHING); CC1020_SET_OPSTATE(CC1020_RX | CC1020_RX_SEARCHING);
} }
} else if (cc1020_rxlen > HDRSIZE) { } else if(cc1020_rxlen > HDRSIZE) {
if (cc1020_rxlen == pktlen) { if(cc1020_rxlen == pktlen) {
/* Disable interrupts while processing the packet. */ /* Disable interrupts while processing the packet. */
DISABLE_RX_IRQ(); DISABLE_RX_IRQ();
CC1020_SET_OPSTATE(CC1020_RX | CC1020_RX_PROCESSING); CC1020_SET_OPSTATE(CC1020_RX | CC1020_RX_PROCESSING);
@ -522,14 +525,15 @@ cc1020_write_reg(uint8_t addr, uint8_t adata)
PSEL_ON; PSEL_ON;
// Send address bits // Send address bits
for (i = 0; i < 7; i++) { for(i = 0; i < 7; i++) {
nop(); nop();
PCLK_LOW; PCLK_LOW;
nop(); nop();
if (data & 0x80) if(data & 0x80) {
PDI_HIGH; PDI_HIGH;
else } else {
PDI_LOW; PDI_LOW;
}
data = data << 1; data = data << 1;
PCLK_HIGH; PCLK_HIGH;
} }
@ -546,14 +550,15 @@ cc1020_write_reg(uint8_t addr, uint8_t adata)
data = adata; data = adata;
// Send data bits // Send data bits
for (i = 0; i < 8; i++) { for(i = 0; i < 8; i++) {
nop(); nop();
PCLK_LOW; PCLK_LOW;
nop(); nop();
if (data & 0x80) if(data & 0x80) {
PDI_HIGH; PDI_HIGH;
else } else {
PDI_LOW; PDI_LOW;
}
data = data << 1; data = data << 1;
PCLK_HIGH; PCLK_HIGH;
} }
@ -576,14 +581,15 @@ cc1020_read_reg(uint8_t addr)
nop(); nop();
// Send address bits // Send address bits
for (i = 0; i < 7; i++) { for(i = 0; i < 7; i++) {
nop(); nop();
PCLK_LOW; PCLK_LOW;
nop(); nop();
if (data & 0x80) if(data & 0x80) {
PDI_HIGH; PDI_HIGH;
else } else {
PDI_LOW; PDI_LOW;
}
data = data << 1; data = data << 1;
PCLK_HIGH; PCLK_HIGH;
} }
@ -599,13 +605,14 @@ cc1020_read_reg(uint8_t addr)
PCLK_LOW; PCLK_LOW;
// Receive data bits // Receive data bits
for (i = 0; i < 8; i++) { for(i = 0; i < 8; i++) {
nop(); nop();
PCLK_HIGH; PCLK_HIGH;
nop(); nop();
data = data << 1; data = data << 1;
if (PDO) if(PDO) {
data++; data++;
}
nop(); nop();
PCLK_LOW; PCLK_LOW;
} }
@ -621,7 +628,7 @@ cc1020_load_config(const uint8_t * config)
{ {
int i; int i;
for (i = 0; i < 0x28; i++) for(i = 0; i < 0x28; i++)
cc1020_write_reg(i, config[i]); cc1020_write_reg(i, config[i]);
} }
@ -646,13 +653,14 @@ cc1020_calibrate(void)
// Start calibration // Start calibration
cc1020_write_reg(CC1020_CALIBRATE, 0xB5); cc1020_write_reg(CC1020_CALIBRATE, 0xB5);
MS_DELAY(3); MS_DELAY(3);
while ((cc1020_read_reg(CC1020_STATUS) & CAL_COMPLETE) == 0); while((cc1020_read_reg(CC1020_STATUS) & CAL_COMPLETE) == 0);
MS_DELAY(2); MS_DELAY(2);
// Monitor lock // Monitor lock
for (timeout_cnt = LOCK_TIMEOUT; timeout_cnt > 0; timeout_cnt--) { for(timeout_cnt = LOCK_TIMEOUT; timeout_cnt > 0; timeout_cnt--) {
if (cc1020_read_reg(CC1020_STATUS) & LOCK_CONTINUOUS) if(cc1020_read_reg(CC1020_STATUS) & LOCK_CONTINUOUS) {
break; break;
}
} }
// Restore PA_POWER // Restore PA_POWER
@ -669,13 +677,14 @@ cc1020_lock(void)
int i; int i;
// Monitor LOCK, lasts 420 - 510 cycles @ 4505600 = 93 us - 113 us // Monitor LOCK, lasts 420 - 510 cycles @ 4505600 = 93 us - 113 us
for (i = LOCK_TIMEOUT; i > 0; i--) { for(i = LOCK_TIMEOUT; i > 0; i--) {
lock_status = cc1020_read_reg(CC1020_STATUS) & LOCK_CONTINUOUS; lock_status = cc1020_read_reg(CC1020_STATUS) & LOCK_CONTINUOUS;
if (lock_status) if(lock_status) {
break; break;
}
} }
if (lock_status == LOCK_CONTINUOUS) { if(lock_status == LOCK_CONTINUOUS) {
return LOCK_OK; return LOCK_OK;
} }