-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrfBeeCore.pde
163 lines (137 loc) · 4.83 KB
/
rfBeeCore.pde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// rfBeeCore.cpp core routines for the rfBee
// see www.seeedstudio.com for details and ordering rfBee hardware.
// Copyright (c) 2010 Hans Klunder <hans.klunder (at) bigfoot.com>
// Author: Hans Klunder, based on the original Rfbee v1.0 firmware by Seeedstudio
// Version: July 14, 2010
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#include "rfBeeCore.h"
// send data via RF
void transmitData(byte *txData,byte len, byte srcAddress, byte destAddress){
DEBUGPRINT()
byte stat;
//Serial.println(len,DEC);
CCx.Write(CCx_TXFIFO,len+2);
CCx.Write(CCx_TXFIFO,destAddress);
CCx.Write(CCx_TXFIFO,srcAddress);
CCx.WriteBurst(CCx_TXFIFO,txData, len); // write len bytes of the serialData buffer into the CCx txfifo
CCx.Strobe(CCx_STX);
while(1){
byte size;
stat=CCx.Read(CCx_TXBYTES, &size);
if( 0 == size ){
break;
}
else{
CCx.Strobe(CCx_STX);
}
}
#ifdef DEBUG
txData[len]='\0';
Serial.println((char *)txData);
#endif
}
// read available txFifo size and handle underflow (which should not have occured anyway)
byte txFifoFree(){
DEBUGPRINT()
byte stat;
byte size;
stat=CCx.Read(CCx_TXBYTES, &size);
// handle a potential TX underflow by flushing the TX FIFO as described in section 10.1 of the CC 1100 datasheet
if (size >= 64){//state got here seems not right, so using size to make sure it no more than 64
CCx.Strobe(CCx_SFTX);
stat=CCx.Read(CCx_TXBYTES,&size);
#ifdef DEBUG
Serial.println(stat,HEX);
#endif
}
#ifdef DEBUG
//Serial.println(stat,HEX);
Serial.println(CCx_FIFO_SIZE - size,DEC);
#endif
return (CCx_FIFO_SIZE - size);
}
// receive data via RF, rxData must be at least CCx_PACKT_LEN bytes long
int receiveData(byte *rxData, byte *len, byte *srcAddress, byte *destAddress, byte *rssi , byte *lqi){
DEBUGPRINT()
byte stat;
stat=CCx.Read(CCx_RXFIFO,len);
#ifdef DEBUG
Serial.print("length:");
Serial.println(*len,DEC);
#endif
CCx.Read(CCx_RXFIFO,destAddress);
CCx.Read(CCx_RXFIFO,srcAddress);
*len -= 2; // discard address bytes from payloadLen
CCx.ReadBurst(CCx_RXFIFO, rxData,*len);
CCx.Read(CCx_RXFIFO,rssi);
*rssi=CCx.RSSIdecode(*rssi);
stat=CCx.Read(CCx_RXFIFO,lqi);
// check checksum ok
if ((*lqi & 0x80)==0){
return NOTHING;
}
*lqi=*lqi & 0x7F; // strip off the CRC bit
// handle potential RX overflows by flushing the RF FIFO as described in section 10.1 of the CC 1100 datasheet
if ((stat & 0xF0) == 0x60){ //Modified by Icing. When overflows, STATE[2:0] = 110
errNo=3; //Error RX overflow
CCx.Strobe(CCx_SFRX); // flush the RX buffer
return ERR;
}
return OK;
}
// power saving
void sleepNow(byte mode)
{
/* Now is the time to set the sleep mode. In the Atmega8 datasheet
* http://www.atmel.com/dyn/resources/prod_documents/doc2486.pdf on page 35
* there is a list of sleep modes which explains which clocks and
* wake up sources are available in which sleep modus.
*
* In the avr/sleep.h file, the call names of these sleep modus are to be found:
*
* The 5 different modes are:
* SLEEP_MODE_IDLE -the least power savings
* SLEEP_MODE_ADC
* SLEEP_MODE_PWR_SAVE
* SLEEP_MODE_STANDBY
* SLEEP_MODE_PWR_DOWN -the most power savings
*
* the power reduction management <avr/power.h> is described in
* http://www.nongnu.org/avr-libc/user-manual/group__avr__power.html
*/
DEBUGPRINT()
set_sleep_mode(mode); // sleep mode is set here
sleep_enable(); // enables the sleep bit in the mcucr register
// so sleep is possible. just a safety pin
power_adc_disable();
power_spi_disable();
power_timer0_disable();
power_timer1_disable();
power_timer2_disable();
power_twi_disable();
sleep_mode(); // here the device is actually put to sleep!!
// THE PROGRAM CONTINUES FROM HERE AFTER WAKING UP
sleep_disable(); // first thing after waking from sleep:
// disable sleep...
power_all_enable();
}
void lowPowerOn(){
DEBUGPRINT()
CCx.Write(CCx_WORCTRL,0x78); // set WORCRTL.RC_PD to 0 to enable the wakeup oscillator
CCx.Strobe(CCx_SWOR);
sleepNow(SLEEP_MODE_IDLE);
//CCx.Strobe(CCx_SIDLE);
}