54 #include "contiki-esb.h"
56 #include "dev/tr1001.h"
57 #include "dev/radio-sensor.h"
65 #ifdef TR1001_CONF_BEEP_ON_BAD_CRC
66 #define BEEP_ON_BAD_CRC TR1001_CONF_BEEP_ON_BAD_CRC
68 #define BEEP_ON_BAD_CRC 1
73 #define BEEP_BEEP(t) beep_beep(t)
78 #define RXSTATE_READY 0
79 #define RXSTATE_RECEIVING 1
80 #define RXSTATE_FULL 2
85 #ifdef TR1001_CONF_BUFFER_SIZE
86 #define RXBUFSIZE TR1001_CONF_BUFFER_SIZE
88 #define RXBUFSIZE PACKETBUF_SIZE
94 static const void *pending_data;
99 unsigned char tr1001_rxbuf[RXBUFSIZE];
104 static unsigned short tr1001_rxlen = 0;
109 volatile unsigned char tr1001_rxstate = RXSTATE_READY;
111 static uint16_t rxcrc, rxcrctmp;
124 #define TR1001_HDRLEN sizeof(struct tr1001_hdr)
128 static uint8_t onoroff = OFF;
130 #define NUM_SYNCHBYTES 4
132 void tr1001_default_rxhandler(
unsigned char c);
133 PT_THREAD(tr1001_default_rxhandler_pt(
unsigned char c));
134 static struct pt rxhandler_pt;
142 static struct timer rxtimer;
144 static unsigned short tmp_sstrength, sstrength;
145 static unsigned short tmp_count;
150 #define LOG(...) printf(__VA_ARGS__)
156 PROCESS(tr1001_process,
"TR1001 driver");
159 static int prepare_packet(
const void *data,
unsigned short len);
160 static int transmit_packet(
unsigned short len);
161 static int receiving_packet(
void);
162 static int pending_packet(
void);
163 static int channel_clear(
void);
164 static int tr1001_on(
void);
165 static int tr1001_off(
void);
230 tr1001_rxstate = RXSTATE_READY;
247 ENERGEST_OFF(ENERGEST_TYPE_LISTEN);
262 ENERGEST_ON(ENERGEST_TYPE_LISTEN);
276 send(
unsigned char b)
283 while((IFG1 & UTXIFG0) == 0) {
301 send2(
unsigned char b)
309 send2_crc16(
unsigned char b, uint16_t crcacc)
332 for(i = 0; i < 102; ++i) {
341 for(i = 0; i < p; ++i) {
360 ME1 |= UTXE0 + URXE0;
379 interrupt (UART0RX_VECTOR)
380 tr1001_rxhandler(
void)
382 ENERGEST_ON(ENERGEST_TYPE_IRQ);
383 tr1001_default_rxhandler_pt(RXBUF0);
384 if(tr1001_rxstate == RXSTATE_FULL) {
387 ENERGEST_OFF(ENERGEST_TYPE_IRQ);
395 for(i = 0; i < len; ++i) {
396 LOG(
"%d: 0x%02x\n", i, tr1001_rxbuf[i]);
401 PT_THREAD(tr1001_default_rxhandler_pt(
unsigned char incoming_byte))
403 static unsigned char rxtmp, tmppos;
405 if(
timer_expired(&rxtimer) && tr1001_rxstate != RXSTATE_FULL) {
411 if(tr1001_rxstate == RXSTATE_RECEIVING) {
412 unsigned short signal = radio_sensor.value(0);
413 tmp_sstrength += (signal >> 2);
427 tr1001_rxstate = RXSTATE_RECEIVING;
434 if(incoming_byte != SYNCH2) {
446 for(tmppos = 0; tmppos < TR1001_HDRLEN; ++tmppos) {
455 LOG(
"Incorrect manchester in header at byte %d/1\n", tmppos);
456 RIMESTATS_ADD(badsynch);
467 LOG(
"Incorrect manchester in header at byte %d/2\n", tmppos);
468 RIMESTATS_ADD(badsynch);
475 tr1001_rxbuf[tmppos] = (rxtmp << 4) |
me_decode8(incoming_byte);
478 rxcrc =
crc16_add(tr1001_rxbuf[tmppos], rxcrc);
484 tr1001_rxlen = ((((
struct tr1001_hdr *)tr1001_rxbuf)->len[0] << 8) +
485 ((
struct tr1001_hdr *)tr1001_rxbuf)->len[1]);
489 if(tmppos + tr1001_rxlen >
sizeof(tr1001_rxbuf)) {
490 RIMESTATS_ADD(toolong);
495 for(; tmppos < tr1001_rxlen + TR1001_HDRLEN; ++tmppos) {
499 LOG(
"Incorrect manchester 0x%02x at byte %d/1\n", incoming_byte,
500 tmppos - TR1001_HDRLEN);
502 RIMESTATS_ADD(badsynch);
511 LOG(
"Incorrect manchester at byte %d/2\n", tmppos - TR1001_HDRLEN);
513 RIMESTATS_ADD(badsynch);
517 tr1001_rxbuf[tmppos] = (rxtmp << 4) |
me_decode8(incoming_byte);
518 rxcrc =
crc16_add(tr1001_rxbuf[tmppos], rxcrc);
523 for(tmppos = 0; tmppos < 4; ++tmppos) {
529 RIMESTATS_ADD(badsynch);
533 rxcrctmp = (rxcrctmp << 4) |
me_decode8(incoming_byte);
536 if(rxcrctmp == rxcrc) {
546 tr1001_rxstate = RXSTATE_FULL;
547 PT_WAIT_UNTIL(&rxhandler_pt, tr1001_rxstate != RXSTATE_FULL);
550 LOG(
"Incorrect CRC\n");
552 RIMESTATS_ADD(badcrc);
559 prepare_packet(
const void *data,
unsigned short len)
566 transmit_packet(
unsigned short len)
568 int ret = RADIO_TX_ERR;
569 if(pending_data !=
NULL) {
577 tr1001_send(
const void *packet,
unsigned short len)
582 LOG(
"tr1001_send: sending %d bytes\n", len);
585 ENERGEST_OFF(ENERGEST_TYPE_LISTEN);
587 ENERGEST_ON(ENERGEST_TYPE_TRANSMIT);
599 while(tr1001_rxstate == RXSTATE_RECEIVING &&
616 for(i = 0; i < 20; ++i) {
623 for(i = 0; i < NUM_SYNCHBYTES; ++i) {
631 crc16 = send2_crc16(len >> 8, crc16);
632 crc16 = send2_crc16(len & 0xff, crc16);
635 for(i = 0; i < len; ++i) {
636 crc16 = send2_crc16(((uint8_t *)packet)[i], crc16);
651 ENERGEST_ON(ENERGEST_TYPE_LISTEN);
659 ENERGEST_OFF(ENERGEST_TYPE_TRANSMIT);
668 unsigned short tmplen;
670 if(tr1001_rxstate == RXSTATE_FULL) {
673 dump_packet(tr1001_rxlen + 2);
676 tmplen = tr1001_rxlen;
678 if(tmplen > bufsize) {
679 LOG(
"tr1001_read: too large packet: %d/%d bytes\n", tmplen, bufsize);
681 RIMESTATS_ADD(toolong);
685 memcpy(buf, &tr1001_rxbuf[TR1001_HDRLEN], tmplen);
688 sstrength = (tmp_count ? ((tmp_sstrength / tmp_count) << 2) : 0);
692 LOG(
"tr1001_read: got %d bytes\n", tmplen);
700 receiving_packet(
void)
702 return tr1001_rxstate == RXSTATE_RECEIVING &&
709 return tr1001_rxstate == RXSTATE_FULL;
733 NETSTACK_RDC.input();
744 if(speed == TR1001_19200) {
749 }
else if(speed == TR1001_38400) {
754 }
else if(speed == TR1001_57600) {
758 }
else if(speed == TR1001_115200) {