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__)
158 PROCESS(tr1001_process,
"TR1001 driver");
161 static int prepare_packet(
const void *data,
unsigned short len);
162 static int transmit_packet(
unsigned short len);
163 static int receiving_packet(
void);
164 static int pending_packet(
void);
165 static int channel_clear(
void);
166 static int tr1001_on(
void);
167 static int tr1001_off(
void);
232 tr1001_rxstate = RXSTATE_READY;
249 ENERGEST_OFF(ENERGEST_TYPE_LISTEN);
264 ENERGEST_ON(ENERGEST_TYPE_LISTEN);
278 send(
unsigned char b)
285 while((IFG1 & UTXIFG0) == 0) {
303 sendx(
unsigned char b)
308 while(gcr_get_encoded(&b)) {
315 sendx_crc16(
unsigned char b, uint16_t crcacc)
320 while(gcr_get_encoded(&b)) {
341 for(i = 0; i < 102; ++i) {
350 for(i = 0; i < p; ++i) {
369 ME1 |= UTXE0 + URXE0;
388 interrupt (UART0RX_VECTOR)
389 tr1001_rxhandler(
void)
391 ENERGEST_ON(ENERGEST_TYPE_IRQ);
392 tr1001_default_rxhandler_pt(RXBUF0);
393 if(tr1001_rxstate == RXSTATE_FULL) {
396 ENERGEST_OFF(ENERGEST_TYPE_IRQ);
404 for(i = 0; i < len; ++i) {
405 LOG(
"%d: 0x%02x\n", i, tr1001_rxbuf[i]);
410 PT_THREAD(tr1001_default_rxhandler_pt(
unsigned char incoming_byte))
412 static unsigned char rxtmp, tmppos;
414 if(
timer_expired(&rxtimer) && tr1001_rxstate != RXSTATE_FULL) {
420 if(tr1001_rxstate == RXSTATE_RECEIVING) {
421 unsigned short signal = radio_sensor.value(0);
422 tmp_sstrength += (signal >> 2);
436 tr1001_rxstate = RXSTATE_RECEIVING;
443 if(incoming_byte != SYNCH2) {
458 for(tmppos = 0; tmppos < TR1001_HDRLEN; ++tmppos) {
463 GCRLOG(
"(%02x) ", incoming_byte);
465 gcr_decode(incoming_byte);
470 LOG(
"Incorrect GCR in header at byte %d/1 %x\n", tmppos, incoming_byte);
471 RIMESTATS_ADD(badsynch);
474 }
while(!gcr_get_decoded(&rxtmp));
475 GCRLOG(
"%02x ", rxtmp);
477 tr1001_rxbuf[tmppos] = rxtmp;
483 tr1001_rxlen = ((((
struct tr1001_hdr *)tr1001_rxbuf)->len[0] << 8) +
484 ((
struct tr1001_hdr *)tr1001_rxbuf)->len[1]);
488 if(tmppos + tr1001_rxlen >
sizeof(tr1001_rxbuf)) {
489 RIMESTATS_ADD(toolong);
494 for(; tmppos < tr1001_rxlen + TR1001_HDRLEN; ++tmppos) {
499 GCRLOG(
"(%02x)", incoming_byte);
501 gcr_decode(incoming_byte);
506 LOG(
"Incorrect GCR 0x%02x at byte %d/1\n", incoming_byte,
507 tmppos - TR1001_HDRLEN);
508 RIMESTATS_ADD(badsynch);
511 }
while(!gcr_get_decoded(&rxtmp));
513 GCRLOG(
"%02x ", rxtmp);
515 tr1001_rxbuf[tmppos] = rxtmp;
521 for(tmppos = 0; tmppos < 2; ++tmppos) {
524 GCRLOG(
"(%02x)", incoming_byte);
526 gcr_decode(incoming_byte);
529 RIMESTATS_ADD(badsynch);
532 }
while(!gcr_get_decoded(&rxtmp));
533 GCRLOG(
"%02x ", rxtmp);
535 rxcrctmp = (rxcrctmp << 8) | rxtmp;
539 if(rxcrctmp == rxcrc) {
549 tr1001_rxstate = RXSTATE_FULL;
550 PT_WAIT_UNTIL(&rxhandler_pt, tr1001_rxstate != RXSTATE_FULL);
553 LOG(
"Incorrect CRC\n");
555 RIMESTATS_ADD(badcrc);
562 prepare_packet(
const void *data,
unsigned short len)
569 transmit_packet(
unsigned short len)
571 int ret = RADIO_TX_ERR;
572 if(pending_data !=
NULL) {
585 LOG(
"tr1001_send: sending %d bytes\n", len);
588 ENERGEST_OFF(ENERGEST_TYPE_LISTEN);
590 ENERGEST_ON(ENERGEST_TYPE_TRANSMIT);
602 while(tr1001_rxstate == RXSTATE_RECEIVING &&
619 for(i = 0; i < 20; ++i) {
626 for(i = 0; i < NUM_SYNCHBYTES; ++i) {
638 crc16 = sendx_crc16(len >> 8, crc16);
639 crc16 = sendx_crc16(len & 0xff, crc16);
642 for(i = 0; i < len; ++i) {
643 crc16 = sendx_crc16(((uint8_t *)packet)[i], crc16);
651 if (!gcr_finished()) {
665 ENERGEST_ON(ENERGEST_TYPE_LISTEN);
673 ENERGEST_OFF(ENERGEST_TYPE_TRANSMIT);
682 unsigned short tmplen;
684 if(tr1001_rxstate == RXSTATE_FULL) {
687 dump_packet(tr1001_rxlen + 2);
690 tmplen = tr1001_rxlen;
692 if(tmplen > bufsize) {
693 LOG(
"tr1001_read: too large packet: %d/%d bytes\n", tmplen, bufsize);
695 RIMESTATS_ADD(toolong);
699 memcpy(buf, &tr1001_rxbuf[TR1001_HDRLEN], tmplen);
702 sstrength = (tmp_count ? ((tmp_sstrength / tmp_count) << 2) : 0);
706 LOG(
"tr1001_read: got %d bytes\n", tmplen);
714 receiving_packet(
void)
716 return tr1001_rxstate == RXSTATE_RECEIVING &&
723 return tr1001_rxstate == RXSTATE_FULL;
747 NETSTACK_RDC.input();
758 if(speed == TR1001_19200) {
763 }
else if(speed == TR1001_38400) {
768 }
else if(speed == TR1001_57600) {
772 }
else if(speed == TR1001_115200) {