Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed FATFileSystem
Fork of KL46Z-USBHostMSD_HelloWorld by
RF22.h
00001 // RF22.h 00002 // Author: Mike McCauley (mikem@open.com.au) 00003 // Copyright (C) 2011 Mike McCauley 00004 // $Id: RF22.h,v 1.23 2013/02/06 21:33:56 mikem Exp mikem $ 00005 // 00006 // ported to mbed by Karl Zweimueller 00007 /// \mainpage RF22 library for Arduino 00008 /// 00009 /// This is the Arduino RF22 library. 00010 /// It provides an object-oriented interface for sending and receiving data messages with Hope-RF 00011 /// RF22B based radio modules, and compatible chips and modules, 00012 /// including the RFM22B transceiver module such as 00013 /// this bare module: http://www.sparkfun.com/products/10153 00014 /// and this shield: https://www.sparkfun.com/products/11018 00015 /// 00016 /// RF22 also supports some of the features of ZigBee and XBee, 00017 /// (such as mesh routing and automatic route discovery), 00018 /// but with a much less complicated system and less expensive radios. 00019 /// 00020 /// The Hope-RF (http://www.hoperf.com) RFM22B (http://www.hoperf.com/rf_fsk/fsk/RFM22B.htm) 00021 /// is a low-cost ISM transceiver module. It supports FSK, GFSK, OOK over a wide 00022 /// range of frequencies and programmable data rates. 00023 /// 00024 /// This library provides functions for sending and receiving messages of up to 255 octets on any 00025 /// frequency supported by the RF22B, in a range of predefined data rates and frequency deviations. 00026 /// Frequency can be set with 312Hz precision to any frequency from 240.0MHz to 960.0MHz. 00027 /// 00028 /// Up to 2 RF22B modules can be connected to an Arduino, permitting the construction of translators 00029 /// and frequency changers, etc. 00030 /// 00031 /// This library provides classes for 00032 /// - RF22: unaddressed, unreliable messages 00033 /// - RF22Datagram: addressed, unreliable messages 00034 /// - RF22ReliableDatagram: addressed, reliable, retransmitted, acknowledged messages. 00035 /// - RF22Router: multi hop delivery from source node to destination node via 0 or more intermediate nodes 00036 /// - RF22Mesh: multi hop delivery with automatic route discovery and rediscovery. 00037 /// 00038 /// The following modulation types are suppported with a range of modem configurations for 00039 /// common data rates and frequency deviations: 00040 /// - GFSK Gaussian Frequency Shift Keying 00041 /// - FSK Frequency Shift Keying 00042 /// - OOK On-Off Keying 00043 /// 00044 /// Support for other RF22B features such as on-chip temperature measurement, analog-digital 00045 /// converter, transmitter power control etc is also provided. 00046 /// 00047 /// The latest version of this documentation can be downloaded from 00048 /// http://www.open.com.au/mikem/arduino/RF22 00049 /// 00050 /// \par Packet Format 00051 /// 00052 /// All messages sent and received by this RF22 library must conform to this packet format: 00053 /// 00054 /// - 8 nibbles (4 octets) PREAMBLE 00055 /// - 2 octets SYNC 0x2d, 0xd4 00056 /// - 4 octets HEADER: (TO, FROM, ID, FLAGS) 00057 /// - 1 octet LENGTH (0 to 255), number of octets in DATA 00058 /// - 0 to 255 octets DATA 00059 /// - 2 octets CRC computed with CRC16(IBM), computed on HEADER, LENGTH and DATA 00060 /// 00061 /// \par Connecting RFM-22 to Arduino 00062 /// 00063 /// If you have the Sparkfun RFM22 Shield (https://www.sparkfun.com/products/11018) 00064 /// the connections described below are done for you on the shield, no changes required, 00065 /// just add headers and plug it in to an Arduino (but not and Arduino Mega, see below) 00066 /// 00067 /// The physical connection between the RF22B and the Arduino require 3.3V, the 3 x SPI pins (SCK, SDI, SDO), 00068 /// a Slave Select pin and an interrupt pin. 00069 /// Note also that on the RFF22B, it is required to control the TX_ANT and X_ANT pins of the RFM22 in order to enable the 00070 /// antenna connection. The RF22 library is configured so that GPIO0 and GPIO1 outputs can control TX_ANT and RX_ANT input pins 00071 /// automatically. You must connect GPIO0 to TX_ANT and GPIO1 to RX_ANT for this automatic antenna switching to occur. 00072 00073 /// \par Interrupts 00074 /// 00075 /// The RF22 library uses interrupts to react to events in the RF22 module, 00076 /// such as the reception of a new packet, or the completion of transmission of a packet. 00077 /// The RF22 library interrupt service routine reads status from and writes data 00078 /// to the the RF22 module via the SPI interface. It is very important therefore, 00079 /// that if you are using the RF22 library with another SPI based deviced, that you 00080 /// disable interrupts while you transfer data to and from that other device. 00081 /// Use cli() to disable interrupts and sei() to reenable them. 00082 /// 00083 /// \par Memory 00084 /// 00085 /// The RF22 library requires non-trivial amounts of memory. The sample programs above all compile to 00086 /// about 9 to 14kbytes each, which will fit in the flash proram memory of most Arduinos. However, 00087 /// the RAM requirements are more critical. Most sample programs above will run on Duemilanova, 00088 /// but not on Diecimila. Even on Duemilanova, the RAM requirements are very close to the 00089 /// available memory of 2kbytes. Therefore, you should be vary sparing with RAM use in programs that use 00090 /// the RF22 library on Duemilanova. 00091 /// 00092 /// The sample RF22Router and RF22Mesh programs compile to about 14kbytes, 00093 /// and require more RAM than the others. 00094 /// They will not run on Duemilanova or Diecimila, but will run on Arduino Mega. 00095 /// 00096 /// It is often hard to accurately identify when you are hitting RAM limits on Arduino. 00097 /// The symptoms can include: 00098 /// - Mysterious crashes and restarts 00099 /// - Changes in behaviour when seemingly unrelated changes are made (such as adding print() statements) 00100 /// - Hanging 00101 /// - Output from Serial.print() not appearing 00102 /// 00103 /// With an Arduino Mega, with 8 kbytes of SRAM, there is much more RAM headroom for 00104 /// your own elaborate programs. 00105 /// This library is reported to work with Arduino Pro Mini, but that has not been tested by me. 00106 /// 00107 /// The Arduino UNO is now known to work with RF22. 00108 /// 00109 /// \par Automatic Frequency Control (AFC) 00110 /// 00111 /// The RF22M modules use an inexpensive crystal to control the frequency synthesizer, and therfore you can expect 00112 /// the transmitter and receiver frequencies to be subject to the usual inaccuracies of such crystals. The RF22 00113 /// contains an AFC circuit to compensate for differences in transmitter and receiver frequencies. 00114 /// It does this by altering the receiver frequency during reception by up to the pull-in frequency range. 00115 /// This RF22 library enables the AFC and by default sets the pull-in frequency range to 00116 /// 0.05MHz, which should be sufficient to handle most situations. However, if you observe unexplained packet losses 00117 /// or failure to operate correctly all the time it may be because your modules have a wider frequency difference, and 00118 /// you may need to set the afcPullInRange to a differentvalue, using setFrequency(); 00119 /// 00120 /// \par Performance 00121 /// 00122 /// Some simple speed performance tests have been conducted. 00123 /// In general packet transmission rate will be limited by the modulation scheme. 00124 /// Also, if your code does any slow operations like Serial printing it will also limit performance. 00125 /// We disabled any printing in the tests below. 00126 /// We tested with RF22::GFSK_Rb125Fd125, which is probably the fastest scheme available. 00127 /// We tested with a 13 octet message length, over a very short distance of 10cm. 00128 /// 00129 /// Transmission (no reply) tests with modulation RF22::GFSK_Rb125Fd125 and a 00130 /// 13 octet message show about 330 messages per second transmitted. 00131 /// 00132 /// Transmit-and-wait-for-a-reply tests with modulation RF22::GFSK_Rb125Fd125 and a 00133 /// 13 octet message (send and receive) show about 160 round trips per second. 00134 /// 00135 /// \par Installation 00136 /// 00137 /// Install in the usual way: unzip the distribution zip file to the libraries 00138 /// sub-folder of your sketchbook. 00139 /// 00140 /// This software is Copyright (C) 2011 Mike McCauley. Use is subject to license 00141 /// conditions. The main licensing options available are GPL V2 or Commercial: 00142 /// 00143 /// \par Open Source Licensing GPL V2 00144 /// 00145 /// This is the appropriate option if you want to share the source code of your 00146 /// application with everyone you distribute it to, and you also want to give them 00147 /// the right to share who uses it. If you wish to use this software under Open 00148 /// Source Licensing, you must contribute all your source code to the open source 00149 /// community in accordance with the GPL Version 2 when your application is 00150 /// distributed. See http://www.gnu.org/copyleft/gpl.html 00151 /// 00152 /// \par Commercial Licensing 00153 /// 00154 /// This is the appropriate option if you are creating proprietary applications 00155 /// and you are not prepared to distribute and share the source code of your 00156 /// application. Contact info@open.com.au for details. 00157 /// 00158 /// \par Revision History 00159 /// 00160 /// \version 1.0 Initial release 00161 /// 00162 /// \version 1.1 Added rf22_snoop and rf22_specan examples 00163 /// 00164 /// \version 1.2 Changed default modulation to FSK_Rb2_4Fd36 00165 /// Some internal reorganisation. 00166 /// Added RF22Router and RF22Mesh classes plus sample programs to support multi-hop and 00167 /// automatic route discovery. 00168 /// \version 1.3 Removed some unnecessary debug messages. Added virtual doArp and isPhysicalAddress 00169 /// functions to RF22Mesh to support other physical address interpretation schemes (IPV4/IPV6?) 00170 /// \version 1.4 RF22Router and RF22Mesh were inadvertently left out of the distro. 00171 /// \version 1.5 Improvements contributed by Peter Mousley: Modem config table is now in flash rather than SRAM, 00172 /// saving 400 bytes of SRAM. Allow a user-defined buffer size. Thanks Peter. 00173 /// \version 1.6 Fixed some minor typos on doc and clarified that this code is for the RF22B. Fixed errors in the 00174 /// definition of the power output constants which were incorrectly set to the values for the RF22. 00175 /// Reported by Fred Slamen. If you were using a previous version of RF22, you probably were not getting the output 00176 /// power you thought. 00177 /// \version 1.7 Added code to initialise GPIO0 and GPIO1 so they can automatically control the TX_ANT and RX_ANT 00178 /// antenna switching inputs. You must connect GPIO0 to TX_ANT and GPIO1 to RX_ANT for this automatic 00179 /// antenna switching to occur. Updated doc to reflect this new connection requirement 00180 /// \version 1.8 Changed the name of RF22_ENLBD in RF22_REG_06_INTERRUPT_ENABLE2 to RF22_ENLBDI because it collided 00181 /// with a define of the same name in RF22_REG_07_OPERATING_MODE. RF22_REG_05_INTERRUPT_ENABLE1 enable mask 00182 /// incorrectly used RF22_IFFERROR instead of RF22_ENFFERR. Reported by Steffan Woltjer. 00183 /// \version 1.9 Fixed typos in RF22_REG_21_CLOCk*. Reported by Steffan Woltjer. 00184 /// \version 1.10 Fixed a problem where a IFFERR during transmission could cause an infinite loop and a hang. 00185 /// Reported by Raymond Gilbert. 00186 /// \version 1.11 Fixed an innocuous typo in RF22::handleInterrupt. Reported by Zhentao. 00187 /// 00188 /// \version 1.12 Improvements to RF22::init from Guy Molinari to improve compatibility with some 00189 /// Arduinos. Now reported to be working with official Mega 2560 and Uno. 00190 /// Updated so compiles on Arduino 1.0. 00191 /// 00192 /// \version 1.13 Announce google support group 00193 /// 00194 /// \version 1.14 Added definitions for bits and masks in RF22_REG_1D_AFC_LOOP_GEARSHIFT_OVERRIDE 00195 /// and RF22_REG_1E_AFC_TIMING_CONTROL 00196 /// 00197 /// \version 1.15 Small alterations to initialisation code so that SS pin is not set to output: may cause 00198 /// interference with other devices connected to the Arduino. Testing with Uno: OK. 00199 /// 00200 /// \version 1.16 Fixed a problem that prevented building with arduino 0021 00201 /// 00202 /// \version 1.17 Added optional AFC pull-in frequency range argument to setFrequency(). 00203 /// Default AFC pull-in range set to 0.05MHz 00204 /// 00205 /// \version 1.18 Changed default value for slave slect pin in constructor to be SS, ie the normal one for 00206 /// the compiled Arduino (D10 for Diecimila, Uno etc and D53 for Mega). This is because some Arduinos such as Mega 2560 00207 /// reportedly use the type of the SS pin to determine whether to run in slave or master mode. Therfore it 00208 /// is preferred that the slave select pin actually be the normal SS pin. 00209 /// 00210 /// \version 1.19 Added new mode() function. 00211 /// Fixed a potential race condition in RF22Datagram::recvfrom which might cause corrupt from, to, id or flags 00212 /// under extreme circumstances. Improvements to interrupt hygeine by adding cli()_/sei() around all 00213 /// RF22 register acceses. Found that 0 length transmit packets confuses the RF22, so they are now forbidden. 00214 /// Added IPGateway example, which routes UDP messages from an internet connection using an 00215 /// Ethernet Shield and sends them 00216 /// to a radio whose ID is based on the UDP port. Replies are sent back to the originating UDP 00217 /// address and port. 00218 /// 00219 /// \version 1.20 _mode is now volatile. 00220 /// RF22::send() now waits until any previous transmission is complete before sending. 00221 /// RF22::waitPacketSent() now waits for the RF22 to not be in _mode == RF22_MODE_TX 00222 /// _txPacketSent member is now redundant and removed. 00223 /// Improvements to interrupt handling and blocking. Now use ATOMIC_BLOCK(ATOMIC_RESTORESTATE) 00224 /// to prevent reenabling interrupts too soon. Thanks to Roland Mieslinger for this suggestion. 00225 /// Added some performance measurements to documentation. 00226 /// 00227 /// \version 1.21 Fixed a case where a receiver buffer overflow could occur. Reported by Joe Tuttle. 00228 /// 00229 /// \version 1.22 Added documentation after testing with Sparkfun RFM22 Shield DEV-11018. 00230 /// Fixed incorrect link to register calculator excel file, reported by Joey Morin. 00231 /// 00232 /// \version 1.23 Added support for alternative SPI interfaces, with default implementation for the standard 00233 /// Arduino hardware SPI interface. Contributed by Joanna Rutkowska. 00234 /// 00235 /// \version 1.24 Fixed a problem that could cause corrupted receive messages if a transmit interrupted 00236 /// a partial receive (as was common with eg ReliableDatagram with poor reception. 00237 /// Also fixed possible receive buffer overrun. 00238 /// \version 1.25 More rigorous use of const, additional register defines (RF22_CRCHDRS RF22_VARPKLEN) 00239 /// and two methods (setPreambleLength() 00240 /// and setSyncWords())made public. Patch provided by 00241 /// Matthijs Kooijman. 00242 /// \author Mike McCauley (mikem@open.com.au) 00243 00244 #ifndef RF22_h 00245 #define RF22_h 00246 #include "mbed.h" 00247 00248 #define boolean bool 00249 00250 //#include <wiring.h> 00251 // These defs cause trouble on some versions of Arduino 00252 #undef round 00253 #undef double 00254 00255 // This is the bit in the SPI address that marks it as a write 00256 #define RF22_SPI_WRITE_MASK 0x80 00257 00258 // This is the maximum message length that can be supported by this library. Limited by 00259 // the single message length octet in the header. 00260 // Yes, 255 is correct even though the FIFO size in the RF22 is only 00261 // 64 octets. We use interrupts to refill the Tx FIFO during transmission and to empty the 00262 // Rx FIFO during reception 00263 // Can be pre-defined to a smaller size (to save SRAM) prior to including this header 00264 #ifndef RF22_MAX_MESSAGE_LEN 00265 #define RF22_MAX_MESSAGE_LEN 255 00266 //#define RF22_MAX_MESSAGE_LEN 50 00267 #endif 00268 00269 // Max number of octets the RF22 Rx and Tx FIFOs can hold 00270 #define RF22_FIFO_SIZE 64 00271 00272 // Keep track of the mode the RF22 is in 00273 #define RF22_MODE_IDLE 0 00274 #define RF22_MODE_RX 1 00275 #define RF22_MODE_TX 2 00276 00277 // These values we set for FIFO thresholds are actually the same as the POR values 00278 #define RF22_TXFFAEM_THRESHOLD 4 00279 #define RF22_RXFFAFULL_THRESHOLD 55 00280 00281 // This is the default node address, 00282 #define RF22_DEFAULT_NODE_ADDRESS 0 00283 00284 // This address in the TO addreess signifies a broadcast 00285 #define RF22_BROADCAST_ADDRESS 0xff 00286 00287 // Number of registers to be passed to setModemConfig() 00288 #define RF22_NUM_MODEM_CONFIG_REGS 18 00289 00290 // Register names 00291 #define RF22_REG_00_DEVICE_TYPE 0x00 00292 #define RF22_REG_01_VERSION_CODE 0x01 00293 #define RF22_REG_02_DEVICE_STATUS 0x02 00294 #define RF22_REG_03_INTERRUPT_STATUS1 0x03 00295 #define RF22_REG_04_INTERRUPT_STATUS2 0x04 00296 #define RF22_REG_05_INTERRUPT_ENABLE1 0x05 00297 #define RF22_REG_06_INTERRUPT_ENABLE2 0x06 00298 #define RF22_REG_07_OPERATING_MODE1 0x07 00299 #define RF22_REG_08_OPERATING_MODE2 0x08 00300 #define RF22_REG_09_OSCILLATOR_LOAD_CAPACITANCE 0x09 00301 #define RF22_REG_0A_UC_OUTPUT_CLOCK 0x0a 00302 #define RF22_REG_0B_GPIO_CONFIGURATION0 0x0b 00303 #define RF22_REG_0C_GPIO_CONFIGURATION1 0x0c 00304 #define RF22_REG_0D_GPIO_CONFIGURATION2 0x0d 00305 #define RF22_REG_0E_IO_PORT_CONFIGURATION 0x0e 00306 #define RF22_REG_0F_ADC_CONFIGURATION 0x0f 00307 #define RF22_REG_10_ADC_SENSOR_AMP_OFFSET 0x10 00308 #define RF22_REG_11_ADC_VALUE 0x11 00309 #define RF22_REG_12_TEMPERATURE_SENSOR_CALIBRATION 0x12 00310 #define RF22_REG_13_TEMPERATURE_VALUE_OFFSET 0x13 00311 #define RF22_REG_14_WAKEUP_TIMER_PERIOD1 0x14 00312 #define RF22_REG_15_WAKEUP_TIMER_PERIOD2 0x15 00313 #define RF22_REG_16_WAKEUP_TIMER_PERIOD3 0x16 00314 #define RF22_REG_17_WAKEUP_TIMER_VALUE1 0x17 00315 #define RF22_REG_18_WAKEUP_TIMER_VALUE2 0x18 00316 #define RF22_REG_19_LDC_MODE_DURATION 0x19 00317 #define RF22_REG_1A_LOW_BATTERY_DETECTOR_THRESHOLD 0x1a 00318 #define RF22_REG_1B_BATTERY_VOLTAGE_LEVEL 0x1b 00319 #define RF22_REG_1C_IF_FILTER_BANDWIDTH 0x1c 00320 #define RF22_REG_1D_AFC_LOOP_GEARSHIFT_OVERRIDE 0x1d 00321 #define RF22_REG_1E_AFC_TIMING_CONTROL 0x1e 00322 #define RF22_REG_1F_CLOCK_RECOVERY_GEARSHIFT_OVERRIDE 0x1f 00323 #define RF22_REG_20_CLOCK_RECOVERY_OVERSAMPLING_RATE 0x20 00324 #define RF22_REG_21_CLOCK_RECOVERY_OFFSET2 0x21 00325 #define RF22_REG_22_CLOCK_RECOVERY_OFFSET1 0x22 00326 #define RF22_REG_23_CLOCK_RECOVERY_OFFSET0 0x23 00327 #define RF22_REG_24_CLOCK_RECOVERY_TIMING_LOOP_GAIN1 0x24 00328 #define RF22_REG_25_CLOCK_RECOVERY_TIMING_LOOP_GAIN0 0x25 00329 #define RF22_REG_26_RSSI 0x26 00330 #define RF22_REG_27_RSSI_THRESHOLD 0x27 00331 #define RF22_REG_28_ANTENNA_DIVERSITY1 0x28 00332 #define RF22_REG_29_ANTENNA_DIVERSITY2 0x29 00333 #define RF22_REG_2A_AFC_LIMITER 0x2a 00334 #define RF22_REG_2B_AFC_CORRECTION_READ 0x2b 00335 #define RF22_REG_2C_OOK_COUNTER_VALUE_1 0x2c 00336 #define RF22_REG_2D_OOK_COUNTER_VALUE_2 0x2d 00337 #define RF22_REG_2E_SLICER_PEAK_HOLD 0x2e 00338 #define RF22_REG_30_DATA_ACCESS_CONTROL 0x30 00339 #define RF22_REG_31_EZMAC_STATUS 0x31 00340 #define RF22_REG_32_HEADER_CONTROL1 0x32 00341 #define RF22_REG_33_HEADER_CONTROL2 0x33 00342 #define RF22_REG_34_PREAMBLE_LENGTH 0x34 00343 #define RF22_REG_35_PREAMBLE_DETECTION_CONTROL1 0x35 00344 #define RF22_REG_36_SYNC_WORD3 0x36 00345 #define RF22_REG_37_SYNC_WORD2 0x37 00346 #define RF22_REG_38_SYNC_WORD1 0x38 00347 #define RF22_REG_39_SYNC_WORD0 0x39 00348 #define RF22_REG_3A_TRANSMIT_HEADER3 0x3a 00349 #define RF22_REG_3B_TRANSMIT_HEADER2 0x3b 00350 #define RF22_REG_3C_TRANSMIT_HEADER1 0x3c 00351 #define RF22_REG_3D_TRANSMIT_HEADER0 0x3d 00352 #define RF22_REG_3E_PACKET_LENGTH 0x3e 00353 #define RF22_REG_3F_CHECK_HEADER3 0x3f 00354 #define RF22_REG_40_CHECK_HEADER2 0x40 00355 #define RF22_REG_41_CHECK_HEADER1 0x41 00356 #define RF22_REG_42_CHECK_HEADER0 0x42 00357 #define RF22_REG_43_HEADER_ENABLE3 0x43 00358 #define RF22_REG_44_HEADER_ENABLE2 0x44 00359 #define RF22_REG_45_HEADER_ENABLE1 0x45 00360 #define RF22_REG_46_HEADER_ENABLE0 0x46 00361 #define RF22_REG_47_RECEIVED_HEADER3 0x47 00362 #define RF22_REG_48_RECEIVED_HEADER2 0x48 00363 #define RF22_REG_49_RECEIVED_HEADER1 0x49 00364 #define RF22_REG_4A_RECEIVED_HEADER0 0x4a 00365 #define RF22_REG_4B_RECEIVED_PACKET_LENGTH 0x4b 00366 #define RF22_REG_50_ANALOG_TEST_BUS_SELECT 0x50 00367 #define RF22_REG_51_DIGITAL_TEST_BUS_SELECT 0x51 00368 #define RF22_REG_52_TX_RAMP_CONTROL 0x52 00369 #define RF22_REG_53_PLL_TUNE_TIME 0x53 00370 #define RF22_REG_55_CALIBRATION_CONTROL 0x55 00371 #define RF22_REG_56_MODEM_TEST 0x56 00372 #define RF22_REG_57_CHARGE_PUMP_TEST 0x57 00373 #define RF22_REG_58_CHARGE_PUMP_CURRENT_TRIMMING 0x58 00374 #define RF22_REG_59_DIVIDER_CURRENT_TRIMMING 0x59 00375 #define RF22_REG_5A_VCO_CURRENT_TRIMMING 0x5a 00376 #define RF22_REG_5B_VCO_CALIBRATION 0x5b 00377 #define RF22_REG_5C_SYNTHESIZER_TEST 0x5c 00378 #define RF22_REG_5D_BLOCK_ENABLE_OVERRIDE1 0x5d 00379 #define RF22_REG_5E_BLOCK_ENABLE_OVERRIDE2 0x5e 00380 #define RF22_REG_5F_BLOCK_ENABLE_OVERRIDE3 0x5f 00381 #define RF22_REG_60_CHANNEL_FILTER_COEFFICIENT_ADDRESS 0x60 00382 #define RF22_REG_61_CHANNEL_FILTER_COEFFICIENT_VALUE 0x61 00383 #define RF22_REG_62_CRYSTAL_OSCILLATOR_POR_CONTROL 0x62 00384 #define RF22_REG_63_RC_OSCILLATOR_COARSE_CALIBRATION 0x63 00385 #define RF22_REG_64_RC_OSCILLATOR_FINE_CALIBRATION 0x64 00386 #define RF22_REG_65_LDO_CONTROL_OVERRIDE 0x65 00387 #define RF22_REG_66_LDO_LEVEL_SETTINGS 0x66 00388 #define RF22_REG_67_DELTA_SIGMA_ADC_TUNING1 0x67 00389 #define RF22_REG_68_DELTA_SIGMA_ADC_TUNING2 0x68 00390 #define RF22_REG_69_AGC_OVERRIDE1 0x69 00391 #define RF22_REG_6A_AGC_OVERRIDE2 0x6a 00392 #define RF22_REG_6B_GFSK_FIR_FILTER_COEFFICIENT_ADDRESS 0x6b 00393 #define RF22_REG_6C_GFSK_FIR_FILTER_COEFFICIENT_VALUE 0x6c 00394 #define RF22_REG_6D_TX_POWER 0x6d 00395 #define RF22_REG_6E_TX_DATA_RATE1 0x6e 00396 #define RF22_REG_6F_TX_DATA_RATE0 0x6f 00397 #define RF22_REG_70_MODULATION_CONTROL1 0x70 00398 #define RF22_REG_71_MODULATION_CONTROL2 0x71 00399 #define RF22_REG_72_FREQUENCY_DEVIATION 0x72 00400 #define RF22_REG_73_FREQUENCY_OFFSET1 0x73 00401 #define RF22_REG_74_FREQUENCY_OFFSET2 0x74 00402 #define RF22_REG_75_FREQUENCY_BAND_SELECT 0x75 00403 #define RF22_REG_76_NOMINAL_CARRIER_FREQUENCY1 0x76 00404 #define RF22_REG_77_NOMINAL_CARRIER_FREQUENCY0 0x77 00405 #define RF22_REG_79_FREQUENCY_HOPPING_CHANNEL_SELECT 0x79 00406 #define RF22_REG_7A_FREQUENCY_HOPPING_STEP_SIZE 0x7a 00407 #define RF22_REG_7C_TX_FIFO_CONTROL1 0x7c 00408 #define RF22_REG_7D_TX_FIFO_CONTROL2 0x7d 00409 #define RF22_REG_7E_RX_FIFO_CONTROL 0x7e 00410 #define RF22_REG_7F_FIFO_ACCESS 0x7f 00411 00412 // These register masks etc are named wherever possible 00413 // corresponding to the bit and field names in the RF-22 Manual 00414 // RF22_REG_00_DEVICE_TYPE 0x00 00415 #define RF22_DEVICE_TYPE_RX_TRX 0x08 00416 #define RF22_DEVICE_TYPE_TX 0x07 00417 00418 // RF22_REG_02_DEVICE_STATUS 0x02 00419 #define RF22_FFOVL 0x80 00420 #define RF22_FFUNFL 0x40 00421 #define RF22_RXFFEM 0x20 00422 #define RF22_HEADERR 0x10 00423 #define RF22_FREQERR 0x08 00424 #define RF22_LOCKDET 0x04 00425 #define RF22_CPS 0x03 00426 #define RF22_CPS_IDLE 0x00 00427 #define RF22_CPS_RX 0x01 00428 #define RF22_CPS_TX 0x10 00429 00430 // RF22_REG_03_INTERRUPT_STATUS1 0x03 00431 #define RF22_IFFERROR 0x80 00432 #define RF22_ITXFFAFULL 0x40 00433 #define RF22_ITXFFAEM 0x20 00434 #define RF22_IRXFFAFULL 0x10 00435 #define RF22_IEXT 0x08 00436 #define RF22_IPKSENT 0x04 00437 #define RF22_IPKVALID 0x02 00438 #define RF22_ICRCERROR 0x01 00439 00440 // RF22_REG_04_INTERRUPT_STATUS2 0x04 00441 #define RF22_ISWDET 0x80 00442 #define RF22_IPREAVAL 0x40 00443 #define RF22_IPREAINVAL 0x20 00444 #define RF22_IRSSI 0x10 00445 #define RF22_IWUT 0x08 00446 #define RF22_ILBD 0x04 00447 #define RF22_ICHIPRDY 0x02 00448 #define RF22_IPOR 0x01 00449 00450 // RF22_REG_05_INTERRUPT_ENABLE1 0x05 00451 #define RF22_ENFFERR 0x80 00452 #define RF22_ENTXFFAFULL 0x40 00453 #define RF22_ENTXFFAEM 0x20 00454 #define RF22_ENRXFFAFULL 0x10 00455 #define RF22_ENEXT 0x08 00456 #define RF22_ENPKSENT 0x04 00457 #define RF22_ENPKVALID 0x02 00458 #define RF22_ENCRCERROR 0x01 00459 00460 // RF22_REG_06_INTERRUPT_ENABLE2 0x06 00461 #define RF22_ENSWDET 0x80 00462 #define RF22_ENPREAVAL 0x40 00463 #define RF22_ENPREAINVAL 0x20 00464 #define RF22_ENRSSI 0x10 00465 #define RF22_ENWUT 0x08 00466 #define RF22_ENLBDI 0x04 00467 #define RF22_ENCHIPRDY 0x02 00468 #define RF22_ENPOR 0x01 00469 00470 // RF22_REG_07_OPERATING_MODE 0x07 00471 #define RF22_SWRES 0x80 00472 #define RF22_ENLBD 0x40 00473 #define RF22_ENWT 0x20 00474 #define RF22_X32KSEL 0x10 00475 #define RF22_TXON 0x08 00476 #define RF22_RXON 0x04 00477 #define RF22_PLLON 0x02 00478 #define RF22_XTON 0x01 00479 00480 // RF22_REG_08_OPERATING_MODE2 0x08 00481 #define RF22_ANTDIV 0xc0 00482 #define RF22_RXMPK 0x10 00483 #define RF22_AUTOTX 0x08 00484 #define RF22_ENLDM 0x04 00485 #define RF22_FFCLRRX 0x02 00486 #define RF22_FFCLRTX 0x01 00487 00488 // RF22_REG_0F_ADC_CONFIGURATION 0x0f 00489 #define RF22_ADCSTART 0x80 00490 #define RF22_ADCDONE 0x80 00491 #define RF22_ADCSEL 0x70 00492 #define RF22_ADCSEL_INTERNAL_TEMPERATURE_SENSOR 0x00 00493 #define RF22_ADCSEL_GPIO0_SINGLE_ENDED 0x10 00494 #define RF22_ADCSEL_GPIO1_SINGLE_ENDED 0x20 00495 #define RF22_ADCSEL_GPIO2_SINGLE_ENDED 0x30 00496 #define RF22_ADCSEL_GPIO0_GPIO1_DIFFERENTIAL 0x40 00497 #define RF22_ADCSEL_GPIO1_GPIO2_DIFFERENTIAL 0x50 00498 #define RF22_ADCSEL_GPIO0_GPIO2_DIFFERENTIAL 0x60 00499 #define RF22_ADCSEL_GND 0x70 00500 #define RF22_ADCREF 0x0c 00501 #define RF22_ADCREF_BANDGAP_VOLTAGE 0x00 00502 #define RF22_ADCREF_VDD_ON_3 0x08 00503 #define RF22_ADCREF_VDD_ON_2 0x0c 00504 #define RF22_ADCGAIN 0x03 00505 00506 // RF22_REG_10_ADC_SENSOR_AMP_OFFSET 0x10 00507 #define RF22_ADCOFFS 0x0f 00508 00509 // RF22_REG_12_TEMPERATURE_SENSOR_CALIBRATION 0x12 00510 #define RF22_TSRANGE 0xc0 00511 #define RF22_TSRANGE_M64_64C 0x00 00512 #define RF22_TSRANGE_M64_192C 0x40 00513 #define RF22_TSRANGE_0_128C 0x80 00514 #define RF22_TSRANGE_M40_216F 0xc0 00515 #define RF22_ENTSOFFS 0x20 00516 #define RF22_ENTSTRIM 0x10 00517 #define RF22_TSTRIM 0x0f 00518 00519 // RF22_REG_14_WAKEUP_TIMER_PERIOD1 0x14 00520 #define RF22_WTR 0x3c 00521 #define RF22_WTD 0x03 00522 00523 // RF22_REG_1D_AFC_LOOP_GEARSHIFT_OVERRIDE 0x1d 00524 #define RF22_AFBCD 0x80 00525 #define RF22_ENAFC 0x40 00526 #define RF22_AFCGEARH 0x38 00527 #define RF22_AFCGEARL 0x07 00528 00529 // RF22_REG_1E_AFC_TIMING_CONTROL 0x1e 00530 #define RF22_SWAIT_TIMER 0xc0 00531 #define RF22_SHWAIT 0x38 00532 #define RF22_ANWAIT 0x07 00533 00534 // RF22_REG_30_DATA_ACCESS_CONTROL 0x30 00535 #define RF22_ENPACRX 0x80 00536 #define RF22_MSBFRST 0x00 00537 #define RF22_LSBFRST 0x40 00538 #define RF22_CRCHDRS 0x00 00539 #define RF22_CRCDONLY 0x20 00540 #define RF22_ENPACTX 0x08 00541 #define RF22_ENCRC 0x04 00542 #define RF22_CRC 0x03 00543 #define RF22_CRC_CCITT 0x00 00544 #define RF22_CRC_CRC_16_IBM 0x01 00545 #define RF22_CRC_IEC_16 0x02 00546 #define RF22_CRC_BIACHEVA 0x03 00547 00548 // RF22_REG_32_HEADER_CONTROL1 0x32 00549 #define RF22_BCEN 0xf0 00550 #define RF22_BCEN_NONE 0x00 00551 #define RF22_BCEN_HEADER0 0x10 00552 #define RF22_BCEN_HEADER1 0x20 00553 #define RF22_BCEN_HEADER2 0x40 00554 #define RF22_BCEN_HEADER3 0x80 00555 #define RF22_HDCH 0x0f 00556 #define RF22_HDCH_NONE 0x00 00557 #define RF22_HDCH_HEADER0 0x01 00558 #define RF22_HDCH_HEADER1 0x02 00559 #define RF22_HDCH_HEADER2 0x04 00560 #define RF22_HDCH_HEADER3 0x08 00561 00562 // RF22_REG_33_HEADER_CONTROL2 0x33 00563 #define RF22_HDLEN 0x70 00564 #define RF22_HDLEN_0 0x00 00565 #define RF22_HDLEN_1 0x10 00566 #define RF22_HDLEN_2 0x20 00567 #define RF22_HDLEN_3 0x30 00568 #define RF22_HDLEN_4 0x40 00569 #define RF22_VARPKLEN 0x00 00570 #define RF22_FIXPKLEN 0x08 00571 #define RF22_SYNCLEN 0x06 00572 #define RF22_SYNCLEN_1 0x00 00573 #define RF22_SYNCLEN_2 0x02 00574 #define RF22_SYNCLEN_3 0x04 00575 #define RF22_SYNCLEN_4 0x06 00576 #define RF22_PREALEN8 0x01 00577 00578 // RF22_REG_6D_TX_POWER 0x6d 00579 #define RF22_TXPOW 0x07 00580 #define RF22_TXPOW_4X31 0x08 // Not used in RFM22B 00581 #define RF22_TXPOW_1DBM 0x00 00582 #define RF22_TXPOW_2DBM 0x01 00583 #define RF22_TXPOW_5DBM 0x02 00584 #define RF22_TXPOW_8DBM 0x03 00585 #define RF22_TXPOW_11DBM 0x04 00586 #define RF22_TXPOW_14DBM 0x05 00587 #define RF22_TXPOW_17DBM 0x06 00588 #define RF22_TXPOW_20DBM 0x07 00589 // IN RFM23B 00590 #define RF22_TXPOW_LNA_SW 0x08 00591 00592 // RF22_REG_71_MODULATION_CONTROL2 0x71 00593 #define RF22_TRCLK 0xc0 00594 #define RF22_TRCLK_NONE 0x00 00595 #define RF22_TRCLK_GPIO 0x40 00596 #define RF22_TRCLK_SDO 0x80 00597 #define RF22_TRCLK_NIRQ 0xc0 00598 #define RF22_DTMOD 0x30 00599 #define RF22_DTMOD_DIRECT_GPIO 0x00 00600 #define RF22_DTMOD_DIRECT_SDI 0x10 00601 #define RF22_DTMOD_FIFO 0x20 00602 #define RF22_DTMOD_PN9 0x30 00603 #define RF22_ENINV 0x08 00604 #define RF22_FD8 0x04 00605 #define RF22_MODTYP 0x30 00606 #define RF22_MODTYP_UNMODULATED 0x00 00607 #define RF22_MODTYP_OOK 0x01 00608 #define RF22_MODTYP_FSK 0x02 00609 #define RF22_MODTYP_GFSK 0x03 00610 00611 // RF22_REG_75_FREQUENCY_BAND_SELECT 0x75 00612 #define RF22_SBSEL 0x40 00613 #define RF22_HBSEL 0x20 00614 #define RF22_FB 0x1f 00615 00616 // Define this to include Serial printing in diagnostic routines 00617 //#define RF22_HAVE_SERIAL 00618 00619 //#include <GenericSPI.h> 00620 //#include <HardwareSPI.h> 00621 ///////////////////////////////////////////////////////////////////// 00622 /// \class RF22 RF22.h <RF22.h> 00623 /// \brief Send and receive unaddressed, unreliable datagrams. 00624 /// 00625 /// This base class provides basic functions for sending and receiving unaddressed, 00626 /// unreliable datagrams of arbitrary length to 255 octets per packet. 00627 /// 00628 /// Subclasses may use this class to implement reliable, addressed datagrams and streams, 00629 /// mesh routers, repeaters, translators etc. 00630 /// 00631 /// On transmission, the TO and FROM addresses default to 0x00, unless changed by a subclass. 00632 /// On reception the TO addressed is checked against the node address (defaults to 0x00) or the 00633 /// broadcast address (which is 0xff). The ID and FLAGS are set to 0, and not checked by this class. 00634 /// This permits use of the this base RF22 class as an 00635 /// unaddresed, unreliable datagram service. Subclasses are expected to change this behaviour to 00636 /// add node address, ids, retransmission etc 00637 /// 00638 /// Naturally, for any 2 radios to communicate that must be configured to use the same frequence and 00639 /// modulation scheme. 00640 class RF22 00641 { 00642 public: 00643 00644 /// \brief Defines register values for a set of modem configuration registers 00645 /// 00646 /// Defines register values for a set of modem configuration registers 00647 /// that can be passed to setModemConfig() 00648 /// if none of the choices in ModemConfigChoice suit your need 00649 /// setModemConfig() writes the register values to the appropriate RF22 registers 00650 /// to set the desired modulation type, data rate and deviation/bandwidth. 00651 /// Suitable values for these registers can be computed using the register calculator at 00652 /// http://www.hoperf.com/upload/rf/RF22B%2023B%2031B%2042B%2043B%20Register%20Settings_RevB1-v5.xls 00653 00654 typedef struct 00655 { 00656 uint8_t hodnota; 00657 uint8_t paket; 00658 } zprava; 00659 00660 typedef struct 00661 { 00662 uint8_t reg_1c; ///< Value for register RF22_REG_1C_IF_FILTER_BANDWIDTH 00663 uint8_t reg_1f; ///< Value for register RF22_REG_1F_CLOCK_RECOVERY_GEARSHIFT_OVERRIDE 00664 uint8_t reg_20; ///< Value for register RF22_REG_20_CLOCK_RECOVERY_OVERSAMPLING_RATE 00665 uint8_t reg_21; ///< Value for register RF22_REG_21_CLOCK_RECOVERY_OFFSET2 00666 uint8_t reg_22; ///< Value for register RF22_REG_22_CLOCK_RECOVERY_OFFSET1 00667 uint8_t reg_23; ///< Value for register RF22_REG_23_CLOCK_RECOVERY_OFFSET0 00668 uint8_t reg_24; ///< Value for register RF22_REG_24_CLOCK_RECOVERY_TIMING_LOOP_GAIN1 00669 uint8_t reg_25; ///< Value for register RF22_REG_25_CLOCK_RECOVERY_TIMING_LOOP_GAIN0 00670 uint8_t reg_2c; ///< Value for register RF22_REG_2C_OOK_COUNTER_VALUE_1 00671 uint8_t reg_2d; ///< Value for register RF22_REG_2D_OOK_COUNTER_VALUE_2 00672 uint8_t reg_2e; ///< Value for register RF22_REG_2E_SLICER_PEAK_HOLD 00673 uint8_t reg_58; ///< Value for register RF22_REG_58_CHARGE_PUMP_CURRENT_TRIMMING 00674 uint8_t reg_69; ///< Value for register RF22_REG_69_AGC_OVERRIDE1 00675 uint8_t reg_6e; ///< Value for register RF22_REG_6E_TX_DATA_RATE1 00676 uint8_t reg_6f; ///< Value for register RF22_REG_6F_TX_DATA_RATE0 00677 uint8_t reg_70; ///< Value for register RF22_REG_70_MODULATION_CONTROL1 00678 uint8_t reg_71; ///< Value for register RF22_REG_71_MODULATION_CONTROL2 00679 uint8_t reg_72; ///< Value for register RF22_REG_72_FREQUENCY_DEVIATION 00680 } ModemConfig; 00681 00682 /// Choices for setModemConfig() for a selected subset of common modulation types, 00683 /// and data rates. If you need another configuration, use the register calculator. 00684 /// and call setModemRegisters() with your desired settings 00685 /// These are indexes into _modemConfig 00686 typedef enum 00687 { 00688 UnmodulatedCarrier = 0, ///< Unmodulated carrier for testing 00689 FSK_PN9_Rb2Fd5, ///< FSK, No Manchester, Rb = 2kbs, Fd = 5kHz, PN9 random modulation for testing 00690 00691 FSK_Rb2Fd5, ///< FSK, No Manchester, Rb = 2kbs, Fd = 5kHz 00692 FSK_Rb2_4Fd36, ///< FSK, No Manchester, Rb = 2.4kbs, Fd = 36kHz 00693 FSK_Rb4_8Fd45, ///< FSK, No Manchester, Rb = 4.8kbs, Fd = 45kHz 00694 FSK_Rb9_6Fd45, ///< FSK, No Manchester, Rb = 9.6kbs, Fd = 45kHz 00695 FSK_Rb19_2Fd9_6, ///< FSK, No Manchester, Rb = 19.2kbs, Fd = 9.6kHz 00696 FSK_Rb38_4Fd19_6, ///< FSK, No Manchester, Rb = 38.4kbs, Fd = 19.6kHz 00697 FSK_Rb57_6Fd28_8, ///< FSK, No Manchester, Rb = 57.6kbs, Fd = 28.8kHz 00698 FSK_Rb125Fd125, ///< FSK, No Manchester, Rb = 125kbs, Fd = 125kHz 00699 00700 GFSK_Rb2Fd5, ///< GFSK, No Manchester, Rb = 2kbs, Fd = 5kHz 00701 GFSK_Rb2_4Fd36, ///< GFSK, No Manchester, Rb = 2.4kbs, Fd = 36kHz 00702 GFSK_Rb4_8Fd45, ///< GFSK, No Manchester, Rb = 4.8kbs, Fd = 45kHz 00703 GFSK_Rb9_6Fd45, ///< GFSK, No Manchester, Rb = 9.6kbs, Fd = 45kHz 00704 GFSK_Rb19_2Fd9_6, ///< GFSK, No Manchester, Rb = 19.2kbs, Fd = 9.6kHz 00705 GFSK_Rb38_4Fd19_6, ///< GFSK, No Manchester, Rb = 38.4kbs, Fd = 19.6kHz 00706 GFSK_Rb57_6Fd28_8, ///< GFSK, No Manchester, Rb = 57.6kbs, Fd = 28.8kHz 00707 GFSK_Rb125Fd125, ///< GFSK, No Manchester, Rb = 125kbs, Fd = 125kHz 00708 00709 OOK_Rb1_2Bw75, ///< OOK, No Manchester, Rb = 1.2kbs, Rx Bandwidth = 75kHz 00710 OOK_Rb2_4Bw335, ///< OOK, No Manchester, Rb = 2.4kbs, Rx Bandwidth = 335kHz 00711 OOK_Rb4_8Bw335, ///< OOK, No Manchester, Rb = 4.8kbs, Rx Bandwidth = 335kHz 00712 OOK_Rb9_6Bw335, ///< OOK, No Manchester, Rb = 9.6kbs, Rx Bandwidth = 335kHz 00713 OOK_Rb19_2Bw335, ///< OOK, No Manchester, Rb = 19.2kbs, Rx Bandwidth = 335kHz 00714 OOK_Rb38_4Bw335, ///< OOK, No Manchester, Rb = 38.4kbs, Rx Bandwidth = 335kHz 00715 OOK_Rb40Bw335 ///< OOK, No Manchester, Rb = 40kbs, Rx Bandwidth = 335kHz 00716 } ModemConfigChoice; 00717 00718 /// Constructor. You can have multiple instances, but each instance must have its own 00719 /// interrupt and slave select pin. After constructing, you must call init() to initialise the intnerface 00720 /// and the radio module 00721 /// \param[in] slaveSelectPin the Arduino pin number of the output to use to select the RF22 before 00722 /// accessing it. Defaults to the normal SS pin for your Arduino (D10 for Diecimila, Uno etc, D53 for Mega) 00723 /// \param[in] interrupt The interrupt number to use. Default is interrupt 0 (Arduino input pin 2) 00724 RF22(PinName slaveSelectPin , PinName mosi, PinName miso, PinName sclk, PinName interrupt ); 00725 00726 /// Initialises this instance and the radio module connected to it. 00727 /// The following steps are taken: 00728 /// - Initialise the slave select pin and the SPI interface library 00729 /// - Software reset the RF22 module 00730 /// - Checks the connected RF22 module is either a RF22_DEVICE_TYPE_RX_TRX or a RF22_DEVICE_TYPE_TX 00731 /// - Attaches an interrupt handler 00732 /// - Configures the RF22 module 00733 /// - Sets the frequncy to 434.0 MHz 00734 /// - Sets the modem data rate to FSK_Rb2_4Fd36 00735 /// \return true if everything was successful 00736 00737 void obsluhapreruseni(); 00738 void vypisfifo(); 00739 00740 boolean init(); 00741 00742 /// Issues a software reset to the 00743 /// RF22 module. Blocks for 1ms to ensure the reset is complete. 00744 void reset(); 00745 00746 /// Reads a single register from the RF22 00747 /// \param[in] reg Register number, one of RF22_REG_* 00748 /// \return The value of the register 00749 uint8_t spiRead(uint8_t reg); 00750 00751 /// Writes a single byte to the RF22 00752 /// \param[in] reg Register number, one of RF22_REG_* 00753 /// \param[in] val The value to write 00754 void spiWrite(uint8_t reg, uint8_t val); 00755 00756 /// Reads a number of consecutive registers from the RF22 using burst read mode 00757 /// \param[in] reg Register number of the first register, one of RF22_REG_* 00758 /// \param[in] dest Array to write the register values to. Must be at least len bytes 00759 /// \param[in] len Number of bytes to read 00760 void spiBurstRead(uint8_t reg, uint8_t* dest, uint8_t len); 00761 00762 /// Write a number of consecutive registers using burst write mode 00763 /// \param[in] reg Register number of the first register, one of RF22_REG_* 00764 /// \param[in] src Array of new register values to write. Must be at least len bytes 00765 /// \param[in] len Number of bytes to write 00766 void spiBurstWrite(uint8_t reg, const uint8_t* src, uint8_t len); 00767 00768 /// Reads and returns the device status register RF22_REG_02_DEVICE_STATUS 00769 /// \return The value of the device status register 00770 uint8_t statusRead(); 00771 00772 /// Reads a value from the on-chip analog-digital converter 00773 /// \param[in] adcsel Selects the ADC input to measure. One of RF22_ADCSEL_*. Defaults to the 00774 /// internal temperature sensor 00775 /// \param[in] adcref Specifies the refernce voltage to use. One of RF22_ADCREF_*. 00776 /// Defaults to the internal bandgap voltage. 00777 /// \param[in] adcgain Amplifier gain selection. 00778 /// \param[in] adcoffs Amplifier offseet (0 to 15). 00779 /// \return The analog value. 0 to 255. 00780 uint8_t adcRead(uint8_t adcsel = RF22_ADCSEL_INTERNAL_TEMPERATURE_SENSOR, 00781 uint8_t adcref = RF22_ADCREF_BANDGAP_VOLTAGE, 00782 uint8_t adcgain = 0, 00783 uint8_t adcoffs = 0); 00784 00785 00786 /// Reads the wakeup timer value in registers RF22_REG_17_WAKEUP_TIMER_VALUE1 00787 /// and RF22_REG_18_WAKEUP_TIMER_VALUE2 00788 /// \return The wakeup timer value 00789 uint16_t wutRead(); 00790 00791 /// Sets the wakeup timer period registers RF22_REG_14_WAKEUP_TIMER_PERIOD1, 00792 /// RF22_REG_15_WAKEUP_TIMER_PERIOD2 and RF22_R<EG_16_WAKEUP_TIMER_PERIOD3 00793 /// \param[in] wtm Wakeup timer mantissa value 00794 /// \param[in] wtr Wakeup timer exponent R value 00795 /// \param[in] wtd Wakeup timer exponent D value 00796 void setWutPeriod(uint16_t wtm, uint8_t wtr = 0, uint8_t wtd = 0); 00797 00798 /// Sets the transmitter and receiver centre frequency 00799 /// \param[in] centre Frequency in MHz. 240.0 to 960.0. Caution, some versions of RF22 and derivatives 00800 /// implemented more restricted frequency ranges. 00801 /// \param[in] afcPullInRange Sets the AF Pull In Range in MHz. Defaults to 0.05MHz (50kHz). Range is 0.0 to 0.159375 00802 /// for frequencies 240.0 to 480MHz, and 0.0 to 0.318750MHz for frequencies 480.0 to 960MHz, 00803 /// \return true if the selected frquency centre + (fhch * fhs) is within range and the afcPullInRange is within range 00804 boolean setFrequency(float centre, float afcPullInRange = 0.05); 00805 00806 /// Sets the frequency hopping step size. 00807 /// \param[in] fhs Frequency Hopping step size in 10kHz increments 00808 /// \return true if centre + (fhch * fhs) is within limits 00809 boolean setFHStepSize(uint8_t fhs); 00810 00811 /// Sets the frequncy hopping channel. Adds fhch * fhs to centre frequency 00812 /// \param[in] fhch The channel number 00813 /// \return true if the selected frquency centre + (fhch * fhs) is within range 00814 boolean setFHChannel(uint8_t fhch); 00815 00816 /// Reads and returns the current RSSI value from register RF22_REG_26_RSSI. If you want to find the RSSI 00817 /// of the last received message, use lastRssi() instead. 00818 /// \return The current RSSI value 00819 uint8_t rssiRead(); 00820 00821 /// Reads and returns the current EZMAC value from register RF22_REG_31_EZMAC_STATUS 00822 /// \return The current EZMAC value 00823 uint8_t ezmacStatusRead(); 00824 00825 /// Sets the parameters for the RF22 Idle mode in register RF22_REG_07_OPERATING_MODE. 00826 /// Idle mode is the mode the RF22 will be in when not transmitting or receiving. The default idle mode 00827 /// is RF22_XTON ie READY mode. 00828 /// \param[in] mode Mask of mode bits, using RF22_SWRES, RF22_ENLBD, RF22_ENWT, 00829 /// RF22_X32KSEL, RF22_PLLON, RF22_XTON. 00830 void setMode(uint8_t mode); 00831 00832 /// If current mode is Rx or Tx changes it to Idle. If the transmitter or receiver is running, 00833 /// disables them. 00834 void setModeIdle(); 00835 00836 /// If current mode is Tx or Idle, changes it to Rx. 00837 /// Starts the receiver in the RF22. 00838 void setModeRx(); 00839 00840 /// If current mode is Rx or Idle, changes it to Rx. 00841 /// Starts the transmitter in the RF22. 00842 void setModeTx(); 00843 00844 /// Returns the operating mode of the library. 00845 /// \return the current mode, one of RF22_MODE_* 00846 uint8_t mode(); 00847 00848 /// Sets the transmitter power output level in register RF22_REG_6D_TX_POWER. 00849 /// Be a good neighbour and set the lowest power level you need. 00850 /// After init(), the power wil be set to RF22_TXPOW_8DBM. 00851 /// Caution: In some countries you may only select RF22_TXPOW_17DBM if you 00852 /// are also using frequency hopping. 00853 /// \param[in] power Transmitter power level, one of RF22_TXPOW_* 00854 void setTxPower(uint8_t power); 00855 00856 /// Sets all the registered required to configure the data modem in the RF22, including the data rate, 00857 /// bandwidths etc. You cas use this to configure the modem with custom configuraitons if none of the 00858 /// canned configurations in ModemConfigChoice suit you. 00859 /// \param[in] config A ModemConfig structure containing values for the modem configuration registers. 00860 void setModemRegisters(const ModemConfig* config); 00861 00862 /// Select one of the predefined modem configurations. If you need a modem configuration not provided 00863 /// here, use setModemRegisters() with your own ModemConfig. 00864 /// \param[in] index The configuration choice. 00865 /// \return true if index is a valid choice. 00866 boolean setModemConfig(ModemConfigChoice index); 00867 00868 /// Starts the receiver and checks whether a received message is available. 00869 /// This can be called multiple times in a timeout loop 00870 /// \return true if a complete, valid message has been received and is able to be retrieved by 00871 /// recv() 00872 boolean available(); 00873 00874 /// Starts the receiver and blocks until a valid received 00875 /// message is available. 00876 void waitAvailable(); 00877 00878 /// Starts the receiver and blocks until a received message is available or a timeout 00879 /// \param[in] timeout Maximum time to wait in milliseconds. 00880 /// \return true if a message is available 00881 bool waitAvailableTimeout(uint16_t timeout); 00882 00883 /// Turns the receiver on if it not already on. 00884 /// If there is a valid message available, copy it to buf and return true 00885 /// else return false. 00886 /// If a message is copied, *len is set to the length (Caution, 0 length messages are permitted). 00887 /// You should be sure to call this function frequently enough to not miss any messages 00888 /// It is recommended that you call it in your main loop. 00889 /// \param[in] buf Location to copy the received message 00890 /// \param[in,out] len Pointer to available space in buf. Set to the actual number of octets copied. 00891 /// \return true if a valid message was copied to buf 00892 boolean recv(uint8_t* buf, uint8_t* len); 00893 00894 /// Waits until any previous transmit packet is finished being transmitted with waitPacketSent(). 00895 /// Then loads a message into the transmitter and starts the transmitter. Note that a message length 00896 /// of 0 is NOT permitted. 00897 /// \param[in] data Array of data to be sent 00898 /// \param[in] len Number of bytes of data to send (> 0) 00899 /// \return true if the message length was valid and it was correctly queued for transmit 00900 boolean send(const uint8_t* data, uint8_t len); 00901 00902 /// Blocks until the RF22 is not in mode RF22_MODE_TX (ie until the RF22 is not transmitting). 00903 /// This effectively waits until any previous transmit packet is finished being transmitted. 00904 void waitPacketSent(); 00905 00906 /// Tells the receiver to accept messages with any TO address, not just messages 00907 /// addressed to this node or the broadcast address 00908 /// \param[in] promiscuous true if you wish to receive messages with any TO address 00909 void setPromiscuous(boolean promiscuous); 00910 00911 /// Returns the TO header of the last received message 00912 /// \return The TO header 00913 uint8_t headerTo(); 00914 00915 /// Returns the FROM header of the last received message 00916 /// \return The FROM header 00917 uint8_t headerFrom(); 00918 00919 /// Returns the ID header of the last received message 00920 /// \return The ID header 00921 uint8_t headerId(); 00922 00923 /// Returns the FLAGS header of the last received message 00924 /// \return The FLAGS header 00925 uint8_t headerFlags(); 00926 00927 /// Returns the RSSI (Receiver Signal Strength Indicator) 00928 /// of the last received message. This measurement is taken when 00929 /// the preamble has been received. It is a (non-linear) measure of the received signal strength. 00930 /// \return The RSSI 00931 uint8_t lastRssi(); 00932 00933 /// Prints a data buffer in HEX. 00934 /// For diagnostic use 00935 /// \param[in] prompt string to preface the print 00936 /// \param[in] buf Location of the buffer to print 00937 /// \param[in] len Length of the buffer in octets. 00938 static void printBuffer(const uint8_t *buf, uint8_t len); 00939 00940 /// Sets the length of the preamble 00941 /// in 4-bit nibbles. 00942 /// Caution: this should be set to the same 00943 /// value on all nodes in your network. Default is 8. 00944 /// Sets the message preamble length in RF22_REG_34_PREAMBLE_LENGTH 00945 /// \param[in] nibbles Preamble length in nibbles of 4 bits each. 00946 void setPreambleLength(uint8_t nibbles); 00947 00948 /// Sets the sync words for transmit and receive in registers RF22_REG_36_SYNC_WORD3 00949 /// to RF22_REG_39_SYNC_WORD0 00950 /// Caution: this should be set to the same 00951 /// value on all nodes in your network. Default is { 0x2d, 0xd4 } 00952 /// \param[in] syncWords Array of sync words 00953 /// \param[in] len Number of sync words to set 00954 void setSyncWords(const uint8_t* syncWords, uint8_t len); 00955 00956 protected: 00957 /// This is a low level function to handle the interrupts for one instance of RF22. 00958 /// Called automatically by isr0() and isr1() 00959 /// Should not need to be called. 00960 void handleInterrupt(); 00961 00962 /// Clears the receiver buffer. 00963 /// Internal use only 00964 void clearRxBuf(); 00965 00966 /// Clears the transmitter buffer 00967 /// Internal use only 00968 void clearTxBuf(); 00969 00970 /// Fills the transmitter buffer with the data of a mesage to be sent 00971 /// \param[in] data Array of data bytes to be sent (1 to 255) 00972 /// \param[in] len Number of data bytes in data (> 0) 00973 /// \return true if the message length is valid 00974 boolean fillTxBuf(const uint8_t* data, uint8_t len); 00975 00976 /// Appends the transmitter buffer with the data of a mesage to be sent 00977 /// \param[in] data Array of data bytes to be sent (0 to 255) 00978 /// \param[in] len Number of data bytes in data 00979 /// \return false if the resulting message would exceed RF22_MAX_MESSAGE_LEN, else true 00980 boolean appendTxBuf(const uint8_t* data, uint8_t len); 00981 00982 /// Internal function to load the next fragment of 00983 /// the current message into the transmitter FIFO 00984 /// Internal use only 00985 void sendNextFragment(); 00986 00987 /// function to copy the next fragment from 00988 /// the receiver FIFO into the receiver buffer 00989 void readNextFragment(); 00990 00991 /// Clears the RF22 Rx and Tx FIFOs 00992 /// Internal use only 00993 void resetFifos(); 00994 00995 /// Clears the RF22 Rx FIFO 00996 /// Internal use only 00997 void resetRxFifo(); 00998 00999 /// Clears the RF22 Tx FIFO 01000 /// Internal use only 01001 void resetTxFifo(); 01002 01003 /// This function will be called by handleInterrupt() if an RF22 external interrupt occurs. 01004 /// This can only happen if external interrupts are enabled in the RF22 01005 /// (which they are not by default). 01006 /// Subclasses may override this function to get control when an RF22 external interrupt occurs. 01007 virtual void handleExternalInterrupt(); 01008 01009 /// This function will be called by handleInterrupt() if an RF22 wakeup timer interrupt occurs. 01010 /// This can only happen if wakeup timer interrupts are enabled in the RF22 01011 /// (which they are not by default). 01012 /// Subclasses may override this function to get control when an RF22 wakeup timer interrupt occurs. 01013 virtual void handleWakeupTimerInterrupt(); 01014 01015 /// Sets the TO header to be sent in all subsequent messages 01016 /// \param[in] to The new TO header value 01017 void setHeaderTo(uint8_t to); 01018 01019 /// Sets the FROM header to be sent in all subsequent messages 01020 /// \param[in] from The new FROM header value 01021 void setHeaderFrom(uint8_t from); 01022 01023 /// Sets the ID header to be sent in all subsequent messages 01024 /// \param[in] id The new ID header value 01025 void setHeaderId(uint8_t id); 01026 01027 /// Sets the FLAGS header to be sent in all subsequent messages 01028 /// \param[in] flags The new FLAGS header value 01029 void setHeaderFlags(uint8_t flags); 01030 01031 /// Start the transmission of the contents 01032 /// of the Tx buffer 01033 void startTransmit(); 01034 01035 /// ReStart the transmission of the contents 01036 /// of the Tx buffer after a atransmission failure 01037 void restartTransmit(); 01038 01039 protected: 01040 //GenericSPIClass* _spi; 01041 01042 /// Low level interrupt service routine for RF22 connected to interrupt 0 01043 //static void isr0(); 01044 void isr0(); 01045 01046 /// Low level interrupt service routine for RF22 connected to interrupt 1 01047 //static void isr1(); 01048 private: 01049 /// Array of instances connected to interrupts 0 and 1 01050 //static RF22* _RF22ForInterrupt[]; 01051 01052 01053 volatile uint8_t _mode; // One of RF22_MODE_* 01054 01055 uint8_t _idleMode; 01056 DigitalOut _slaveSelectPin; 01057 SPI _spi; 01058 InterruptIn _interrupt; 01059 uint8_t _deviceType; 01060 01061 //DigitalOut led1; 01062 //DigitalOut led2; 01063 //DigitalOut led3; 01064 //DigitalOut led4; 01065 01066 // These volatile members may get changed in the interrupt service routine 01067 volatile uint8_t _bufLen; 01068 uint8_t _buf[RF22_MAX_MESSAGE_LEN]; 01069 01070 volatile boolean _rxBufValid; 01071 01072 volatile boolean _txPacketSent; 01073 volatile uint8_t _txBufSentIndex; 01074 01075 volatile uint16_t _rxBad; 01076 volatile uint16_t _rxGood; 01077 volatile uint16_t _txGood; 01078 01079 volatile uint8_t _lastRssi; 01080 }; 01081 01082 /// @example rf22_client.pde 01083 /// Client side of simple client/server pair using RF22 class 01084 01085 /// @example rf22_server.pde 01086 /// Server side of simple client/server pair using RF22 class 01087 01088 /// @example rf22_datagram_client.pde 01089 /// Client side of simple client/server pair using RF22Datagram class 01090 01091 /// @example rf22_datagram_server.pde 01092 /// Server side of simple client/server pair using RF22Datagram class 01093 01094 /// @example rf22_reliable_datagram_client.pde 01095 /// Client side of simple client/server pair using RF22ReliableDatagram class 01096 01097 /// @example rf22_reliable_datagram_server.pde 01098 /// Server side of simple client/server pair using RF22ReliableDatagram class 01099 01100 /// @example rf22_router_client.pde 01101 /// Client side of RF22Router network chain 01102 01103 /// @example rf22_router_server1.pde 01104 /// Server node for RF22Router network chain 01105 01106 /// @example rf22_router_server2.pde 01107 /// Server node for RF22Router network chain 01108 01109 /// @example rf22_router_server3.pde 01110 /// Server node for RF22Router network chain 01111 01112 /// @example rf22_mesh_client.pde 01113 /// Client side of RF22Mesh network chain 01114 01115 /// @example rf22_mesh_server1.pde 01116 /// Server node for RF22Mesh network chain 01117 01118 /// @example rf22_mesh_server2.pde 01119 /// Server node for RF22Mesh network chain 01120 01121 /// @example rf22_mesh_server3.pde 01122 /// Server node for RF22Mesh network chain 01123 01124 /// @example rf22_test.pde 01125 /// Test suite for RF22 library 01126 01127 /// @example rf22_snoop.pde 01128 /// Capture and print RF22 packet from the air 01129 01130 /// @example rf22_specan.pde 01131 /// Simple spectrum analyser using the RSSI measurements of the RF22 01132 /// (see <a href="specan1.png">Sample output</a> showing a plot from 395.0MHz to 396.0MHz of a 01133 /// signal generator at 395.5MHz amplitude modulated at 100% 1kHz) 01134 /// 01135 01136 /// @example IPGateway.pde 01137 /// Sketch to provide an IP gateway for a set of RF22 radios (Datagram, ReliableDatagram, Router or Mesh) 01138 /// Routes UDP messages from an internet connection using an Ethernet Shield and sends them 01139 /// to a radio whose ID is based on the UDP port. Replies are sent back to the originating UDP 01140 /// address and port 01141 01142 01143 #endif
Generated on Thu Jul 14 2022 18:53:30 by
1.7.2
