JeeLib
An easy interface to the port headers, the RF12 driver library, timers, low-power code, and more.
 All Classes Files Functions Variables Enumerations Macros Pages
RF12.h
Go to the documentation of this file.
1 // 2009-02-09 <jc@wippler.nl> http://opensource.org/licenses/mit-license.php
2 
3 #ifndef RF12_h
4 #define RF12_h
5 
6 /// @file
7 /// RFM12B driver definitions
8 
9 #include <stdint.h>
10 
11 /// RFM12B Protocol version.
12 /// Version 1 did not include the group code in the crc.
13 /// Version 2 does include the group code in the crc.
14 #define RF12_VERSION 2
15 
16 /// Shorthand for RFM12B group byte in rf12_buf.
17 #define rf12_grp rf12_buf[0]
18 /// Shorthand for RFM12B header byte in rf12_buf.
19 #define rf12_hdr rf12_buf[1]
20 /// Shorthand for RFM12B length byte in rf12_buf.
21 #define rf12_len rf12_buf[2]
22 /// Shorthand for first RFM12B data byte in rf12_buf.
23 #define rf12_data (rf12_buf + 3)
24 
25 /// RFM12B CTL bit mask.
26 #define RF12_HDR_CTL 0x80
27 /// RFM12B DST bit mask.
28 #define RF12_HDR_DST 0x40
29 /// RFM12B ACK bit mask.
30 #define RF12_HDR_ACK 0x20
31 /// RFM12B HDR bit mask.
32 #define RF12_HDR_MASK 0x1F
33 
34 /// RFM12B Maximum message size in bytes.
35 #define RF12_MAXDATA 66
36 
37 #define RF12_433MHZ 1 ///< RFM12B 433 MHz frequency band.
38 #define RF12_868MHZ 2 ///< RFM12B 868 MHz frequency band.
39 #define RF12_915MHZ 3 ///< RFM12B 915 MHz frequency band.
40 
41 // EEPROM address range used by the rf12_config() code
42 #define RF12_EEPROM_ADDR ((uint8_t*) 0x20) ///< Starting offset.
43 #define RF12_EEPROM_SIZE 16 ///< Number of bytes.
44 #define RF12_EEPROM_EKEY ((uint8_t*) 0x40) ///< EE start, same as before.
45 #define RF12_EEPROM_ELEN 16 ///< EE number of bytes.
46 #define RF12_EEPROM_VERSION 1 ///< Only this version is valid.
47 
48 /// Shorthand to simplify detecting a request for an ACK.
49 #define RF12_WANTS_ACK ((rf12_hdr & RF12_HDR_ACK) && !(rf12_hdr & RF12_HDR_CTL))
50 /// Shorthand to simplify sending out the proper ACK reply.
51 #define RF12_ACK_REPLY (rf12_hdr & RF12_HDR_DST ? RF12_HDR_CTL : \
52  RF12_HDR_CTL | RF12_HDR_DST | (rf12_hdr & RF12_HDR_MASK))
53 
54 // options for RF12_sleep()
55 #define RF12_SLEEP 0 ///< Enter sleep mode.
56 #define RF12_WAKEUP -1 ///< Wake up from sleep mode.
57 
58 /// Running crc value, should be zero at end.
59 extern volatile uint16_t rf12_crc;
60 /// Recv/xmit buf including hdr & crc bytes.
61 extern volatile uint8_t rf12_buf[];
62 /// Seq number of encrypted packet (or -1).
63 extern long rf12_seq;
64 
65 /// Option to set RFM12 CS (or SS) pin for use on different hardware setups.
66 /// Set to Dig10 by default for JeeNode. Can be Dig10, Dig9 or Dig8
67 void rf12_set_cs(uint8_t pin);
68 
69 /// Only needed if you want to init the SPI bus before rf12_initialize() does.
70 void rf12_spiInit(void);
71 
72 /// Call this once with the node ID, frequency band, and optional group.
73 uint8_t rf12_initialize(uint8_t id, uint8_t band, uint8_t group=0xD4, uint16_t frequency=1600);
74 
75 /// Initialize the RFM12B module from settings stored in EEPROM by "RF12demo"
76 /// don't call rf12_initialize() if you init the hardware with rf12_config().
77 /// @return the node ID as 1..31, or 0 if there is no config on EEPROM.
78 uint8_t rf12_configSilent();
79 /// Call this to send a description of the EEPROM settings to the serial port.
80 void rf12_configDump();
81 
82 /// @deprecated Please switch over to rf12_configSilent() and rf12_configDump().
83 uint8_t rf12_config(uint8_t show =1);
84 
85 /// Call this frequently, returns true if a packet has been received.
86 uint8_t rf12_recvDone(void);
87 
88 /// Call this to check whether a new transmission can be started.
89 /// @return true when a new transmission may be started with rf12_sendStart().
90 uint8_t rf12_canSend(void);
91 
92 /// Call this only when rf12_recvDone() or rf12_canSend() return true.
93 void rf12_sendStart(uint8_t hdr);
94 /// Call this only when rf12_recvDone() or rf12_canSend() return true.
95 void rf12_sendStart(uint8_t hdr, const void* ptr, uint8_t len);
96 /// This variant loops on rf12_canSend() and then calls rf12_sendStart() asap.
97 void rf12_sendNow(uint8_t hdr, const void* ptr, uint8_t len);
98 
99 /// Wait for send to finish.
100 /// @param mode sleep mode 0=none, 1=idle, 2=standby, 3=powerdown.
101 void rf12_sendWait(uint8_t mode);
102 
103 /// This simulates OOK by turning the transmitter on and off via SPI commands.
104 /// Use this only when the radio was initialized with a fake zero node ID.
105 void rf12_onOff(uint8_t value);
106 
107 /// Power off the RFM12B, ms > 0 sets watchdog to wake up again after N * 32 ms.
108 /// @note if off, calling this with -1 can be used to bring the RFM12B back up.
109 void rf12_sleep(char n);
110 
111 /// Return true if the supply voltage is below 3.1V.
112 char rf12_lowbat(void);
113 
114 /// Set up the easy tranmission mode, arg is number of seconds between packets.
115 void rf12_easyInit(uint8_t secs);
116 
117 /// Call this often to keep the easy transmission mode going.
118 char rf12_easyPoll(void);
119 
120 /// Send new data using easy transmission mode, buffer gets copied to driver.
121 char rf12_easySend(const void* data, uint8_t size);
122 
123 /// Enable encryption (null arg disables it again).
124 void rf12_encrypt(const uint8_t*);
125 
126 /// Low-level control of the RFM12B via direct register access.
127 /// http://jeelabs.org/tools/rfm12b is useful for calculating these.
128 uint16_t rf12_control(uint16_t cmd);
129 
130 /// See http://blog.strobotics.com.au/2009/07/27/rfm12-tutorial-part-3a/
131 /// Transmissions are packetized, don't assume you can sustain these speeds!
132 ///
133 /// @note Data rates are approximate. For higher data rates you may need to
134 /// alter receiver radio bandwidth and transmitter modulator bandwidth.
135 /// Note that bit 7 is a prescaler - don't just interpolate rates between
136 /// RF12_DATA_RATE_3 and RF12_DATA_RATE_2.
138  RF12_DATA_RATE_CMD = 0xC600,
139  RF12_DATA_RATE_9 = RF12_DATA_RATE_CMD | 0x02, // Approx 115200 bps
140  RF12_DATA_RATE_8 = RF12_DATA_RATE_CMD | 0x05, // Approx 57600 bps
141  RF12_DATA_RATE_7 = RF12_DATA_RATE_CMD | 0x06, // Approx 49200 bps
142  RF12_DATA_RATE_6 = RF12_DATA_RATE_CMD | 0x08, // Approx 38400 bps
143  RF12_DATA_RATE_5 = RF12_DATA_RATE_CMD | 0x11, // Approx 19200 bps
144  RF12_DATA_RATE_4 = RF12_DATA_RATE_CMD | 0x23, // Approx 9600 bps
145  RF12_DATA_RATE_3 = RF12_DATA_RATE_CMD | 0x47, // Approx 4800 bps
146  RF12_DATA_RATE_2 = RF12_DATA_RATE_CMD | 0x91, // Approx 2400 bps
147  RF12_DATA_RATE_1 = RF12_DATA_RATE_CMD | 0x9E, // Approx 1200 bps
148  RF12_DATA_RATE_DEFAULT = RF12_DATA_RATE_7,
149 };
150 
151 #endif
uint8_t rf12_configSilent()
Initialize the RFM12B module from settings stored in EEPROM by "RF12demo" don't call rf12_initializ...
Definition: RF12.cpp:645
long rf12_seq
Seq number of encrypted packet (or -1).
Definition: RF12.cpp:143
uint8_t rf12_canSend(void)
Call this to check whether a new transmission can be started.
Definition: RF12.cpp:416
void rf12_easyInit(uint8_t secs)
Set up the easy tranmission mode, arg is number of seconds between packets.
Definition: RF12.cpp:769
void rf12_sendNow(uint8_t hdr, const void *ptr, uint8_t len)
This variant loops on rf12_canSend() and then calls rf12_sendStart() asap.
Definition: RF12.cpp:484
char rf12_easyPoll(void)
Call this often to keep the easy transmission mode going.
Definition: RF12.cpp:782
uint8_t rf12_recvDone(void)
Call this frequently, returns true if a packet has been received.
Definition: RF12.cpp:383
uint8_t rf12_initialize(uint8_t id, uint8_t band, uint8_t group=0xD4, uint16_t frequency=1600)
Call this once with the node ID, frequency band, and optional group.
Definition: RF12.cpp:547
void rf12_configDump()
Call this to send a description of the EEPROM settings to the serial port.
Definition: RF12.cpp:668
char rf12_easySend(const void *data, uint8_t size)
Send new data using easy transmission mode, buffer gets copied to driver.
Definition: RF12.cpp:837
void rf12_sendWait(uint8_t mode)
Wait for send to finish.
Definition: RF12.cpp:499
void rf12_sleep(char n)
Power off the RFM12B, ms > 0 sets watchdog to wake up again after N * 32 ms.
Definition: RF12.cpp:726
volatile uint16_t rf12_crc
Running crc value, should be zero at end.
Definition: RF12.cpp:141
void rf12_sendStart(uint8_t hdr)
Call this only when rf12_recvDone() or rf12_canSend() return true.
Definition: RF12.cpp:428
rf12DataRates
See http://blog.strobotics.com.au/2009/07/27/rfm12-tutorial-part-3a/ Transmissions are packetized...
Definition: RF12.h:137
void rf12_onOff(uint8_t value)
This simulates OOK by turning the transmitter on and off via SPI commands.
Definition: RF12.cpp:632
void rf12_encrypt(const uint8_t *)
Enable encryption (null arg disables it again).
Definition: RF12.cpp:930
volatile uint8_t rf12_buf[]
Recv/xmit buf including hdr & crc bytes.
Definition: RF12.cpp:142
uint16_t rf12_control(uint16_t cmd)
Low-level control of the RFM12B via direct register access.
Definition: RF12.cpp:252
uint8_t rf12_config(uint8_t show=1)
Definition: RF12.cpp:706
char rf12_lowbat(void)
Return true if the supply voltage is below 3.1V.
Definition: RF12.cpp:743
void rf12_set_cs(uint8_t pin)
Option to set RFM12 CS (or SS) pin for use on different hardware setups.
Definition: RF12.cpp:150
void rf12_spiInit(void)
Only needed if you want to init the SPI bus before rf12_initialize() does.
Definition: RF12.cpp:160