RFM70library
rfm70.h
1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 //***************************************************************************//
14 //
15 // COPYRIGHT NOTICE (zlib license)
16 //
17 // Loosely based on the example application provided by HopeRF
18 //
19 // (c) Wouter van Ooijen - wouter@voti.nl
20 //
21 // This software is provided 'as-is', without any express or implied
22 // warranty. In no event will the authors be held liable for any damages
23 // arising from the use of this software.
24 //
25 // Permission is granted to anyone to use this software for any purpose,
26 // including commercial applications, and to alter it and redistribute it
27 // freely, subject to the following restrictions:
28 //
29 // 1. The origin of this software must not be misrepresented; you must not
30 // claim that you wrote the original software. If you use this software
31 // in a product, an acknowledgment in the product documentation would be
32 // appreciated but is not required.
33 // 2. Altered source versions must be plainly marked as such, and must not be
34 // misrepresented as being the original software.
35 // 3. This notice may not be removed or altered from any source distribution.
36 //
37 //***************************************************************************//
38 
39 #ifndef _RFM70_H_
40 #define _RFM70_H_
41 
42 //***************************************************************************//
43 //
65 //
66 //***************************************************************************//
67 
68 //***************************************************************************//
69 //
82 //
83 //***************************************************************************//
84 
85 
87 //
89 #define RFM70_LIB_VERSION "V1.02 (2011-12-31)"
90 
92 //
94 #define RFM70_MAX_PACKET_LEN 32
95 
97 //
99 typedef unsigned char rfm70_buffer [ RFM70_MAX_PACKET_LEN ];
100 
101 
102 //***************************************************************************//
103 //
104 // RFM70 SPI commands
105 //
106 //***************************************************************************//
107 
110 
112 #define RFM70_CMD_R_RX_PAYLOAD 0x61
113 
115 #define RFM70_CMD_W_TX_PAYLOAD 0xA0
116 
118 #define RFM70_CMD_FLUSH_TX 0xE1
119 
121 #define RFM70_CMD_FLUSH_RX 0xE2
122 
124 #define RFM70_CMD_REUSE_TX_PL 0xE3
125 
127 #define RFM70_CMD_W_TX_PAYLOAD_NOACK 0xB0
128 
130 #define RFM70_CMD_W_ACK_PAYLOAD 0xA8
131 
133 #define RFM70_CMD_ACTIVATE 0x50
134 
136 #define RFM70_CMD_R_RX_PL_WID 0x60
137 
139 #define RFM70_CMD_NOP 0xFF
140 
141 //***************************************************************************//
142 //
143 // RFM70 register addresses
144 //
145 //***************************************************************************//
146 
148 //
158 #define RFM70_REG_CONFIG 0x00
159 
161 //
170 #define RFM70_REG_EN_AA 0x01
171 
173 //
182 #define RFM70_REG_EN_RXADDR 0x02
183 
185 //
189 #define RFM70_REG_SETUP_AW 0x03
190 
192 //
196 #define RFM70_REG_SETUP_RETR 0x04
197 
199 //
201 #define RFM70_REG_RF_CH 0x05
202 
204 //
210 #define RFM70_REG_RF_SETUP 0x06
211 
213 //
229 #define RFM70_REG_STATUS 0x07
230 
232 //
242 #define RFM70_REG_OBSERVE_TX 0x08
243 
245 //
249 #define RFM70_REG_CD 0x09
250 
252 //
256 #define RFM70_REG_RX_ADDR_P0 0x0A
257 
259 //
263 #define RFM70_REG_RX_ADDR_P1 0x0B
264 
266 //
270 #define RFM70_REG_RX_ADDR_P2 0x0C
271 
273 //
277 #define RFM70_REG_RX_ADDR_P3 0x0D
278 
280 //
284 #define RFM70_REG_RX_ADDR_P4 0x0E
285 
287 //
291 #define RFM70_REG_RX_ADDR_P5 0x0F
292 
294 //
298 #define RFM70_REG_TX_ADDR 0x10
299 
301 //
304 #define RFM70_REG_RX_PW_P0 0x11
305 
307 //
310 #define RFM70_REG_RX_PW_P1 0x12
311 
313 //
316 #define RFM70_REG_RX_PW_P2 0x13
317 
319 //
322 #define RFM70_REG_RX_PW_P3 0x14
323 
325 //
328 #define RFM70_REG_RX_PW_P4 0x15
329 
331 //
334 #define RFM70_REG_RX_PW_P5 0x16
335 
337 //
346 #define RFM70_REG_FIFO_STATUS 0x17
347 
349 //
360 #define RFM70_REG_DYNPD 0x1C
361 
363 //
369 #define RFM70_REG_FEATURE 0x1D
370 
372 
373 
374 
375 #include "pins.h"
376 
377 
378 
380 class rfm70 {
381 
382 private:
383  pins::output_pin &sclk;
384  pins::output_pin &mosi;
385  pins::input_pin &miso;
386  pins::output_pin &csn;
387  pins::output_pin &ce;
388 
389  void(*wait_ms)(unsigned int);
390  void(*wait_us)(unsigned int);
391 
392  unsigned char SPI_RW( unsigned char value );
393 
394 public:
396  //
402  pins::output_pin &sclk,
403  pins::output_pin &mosi,
404  pins::input_pin &miso,
405  pins::output_pin &csn,
406  pins::output_pin &ce
407 
408  ,void(*wait_ms)(unsigned int)
409  ,void(*wait_us)(unsigned int)
410 
411  ):
412  sclk( sclk ),
413  mosi( mosi ),
414  miso( miso ),
415  csn( csn ),
416  ce( ce )
417  ,wait_ms( wait_ms )
418  ,wait_us( wait_us )
419  {}
420 
421 private:
422  void bank( unsigned char b );
423  void init_bank1();
424 
425 public:
426 
427 
429 //
446 void init( void );
447 
449 //
453 unsigned char register_read( unsigned char reg );
454 
456 //
460 void buffer_read(
461  unsigned char reg,
462  unsigned char *buf,
463  unsigned char length
464 );
465 
467 //
471 void register_write( unsigned char reg, unsigned char val );
472 
474 //
479 void buffer_write(
480  char reg,
481  const unsigned char *buf,
482  unsigned char length
483 );
484 
485 //***************************************************************************//
486 //
487 // high-level interface
488 //
489 //***************************************************************************//
490 
492 //
498 bool is_present( void );
499 
501 //
505 void mode_transmit( void );
506 
508 //
512 void mode_receive( void );
513 
515 //
523 void mode_standby( void );
524 
526 //
531 void mode_powerdown( void );
532 
534 //
538 void lna_low( void );
539 
541 //
545 void lna_high( void );
546 
548 //
555 void channel( unsigned char ch );
556 
558 //
569 void air_data_rate( unsigned char rate );
570 
572 //
580 void crc_length( unsigned char len );
581 
583 //
590 void address_length( unsigned char len );
591 
593 //
602 void power( unsigned char level );
603 
605 //
621 void retransmit_delay_attempts( unsigned char d, unsigned char n );
622 
624 //
629 unsigned char retransmit_count( void );
630 
632 //
640 unsigned char lost_packets_count( void );
641 
643 //
647 void lost_packets_reset( void );
648 
650 //
657 void pipe_autoack( unsigned char pipe, bool enabled );
658 
660 //
666 void pipe_enable( unsigned char d, bool enabled );
667 
669 //
678 void receive_address_p0( const unsigned char address[ 5 ] );
679 
681 //
690 void receive_address_p1( const unsigned char address[ 5 ] );
691 
693 //
698 void receive_address_pn( unsigned char channel, unsigned char address );
699 
701 //
708 void channel_payload_size( unsigned char n, unsigned char size );
709 
711 //
715 void transmit_address( const unsigned char *address );
716 
718 //
721 bool transmit_fifo_full( void );
722 
724 //
727 bool receive_fifo_empty( void );
728 
730 //
745 void transmit_message(
746  const unsigned char *buf,
747  unsigned char length
748 );
749 
751 //
765  const unsigned char *buf,
766  unsigned char length
767 );
768 
770 //
777 unsigned char receive_next_pipe( void );
778 
780 //
787 unsigned char receive_next_length( void );
788 
790 //
805 bool receive(
806  unsigned char & pipe,
807  unsigned char *buf,
808  unsigned char & length
809 );
810 
811 
812 }; // class frm70
813 
814 
815 #endif
void channel_payload_size(unsigned char n, unsigned char size)
set the payload size for pipe n
Definition: rfm70.cpp:749
unsigned char receive_next_pipe(void)
get pipe number of the next message in receive FIFO
Definition: rfm70.cpp:784
unsigned char rfm70_buffer[RFM70_MAX_PACKET_LEN]
type of rfm70 (transmit or receive) buffer
Definition: rfm70.h:99
unsigned char lost_packets_count(void)
read rfm70 lost packets count
Definition: rfm70.cpp:684
void receive_address_p1(const unsigned char address[5])
set the rfm70 pipe 1 address
Definition: rfm70.cpp:668
void buffer_read(unsigned char reg, unsigned char *buf, unsigned char length)
read a multi-byte command or register
Definition: rfm70.cpp:442
void mode_standby(void)
switch the rfm70 to standby mode
Definition: rfm70.cpp:589
void transmit_message_once(const unsigned char *buf, unsigned char length)
transmit a message once
Definition: rfm70.cpp:774
bool transmit_fifo_full(void)
report whether the transmit fifo is full
Definition: rfm70.cpp:652
void retransmit_delay_attempts(unsigned char d, unsigned char n)
set the retransmission delay and number of attempts
Definition: rfm70.cpp:719
bool receive(unsigned char &pipe, unsigned char *buf, unsigned char &length)
(try to) receive a message
Definition: rfm70.cpp:793
void air_data_rate(unsigned char rate)
set the rfm70 air data rate (baudrate)
Definition: rfm70.cpp:610
void channel(unsigned char ch)
set the rfm70 channel frequency
Definition: rfm70.cpp:605
void receive_address_pn(unsigned char channel, unsigned char address)
set the rfm70 pipe n (2..5) address
Definition: rfm70.cpp:672
void mode_powerdown(void)
switch the rfm70 to power down mode
Definition: rfm70.cpp:597
bool is_present(void)
report whether the rfm70 module is present
Definition: rfm70.cpp:542
unsigned char register_read(unsigned char reg)
read a single-byte command or register
Definition: rfm70.cpp:430
void transmit_address(const unsigned char *address)
set the rfm70 transmit address
Definition: rfm70.cpp:676
void buffer_write(char reg, const unsigned char *buf, unsigned char length)
write a multi-byte command or register
Definition: rfm70.cpp:459
#define RFM70_MAX_PACKET_LEN
maximum number of data bytes in a (received or transmitted) rfm70 packet
Definition: rfm70.h:94
void transmit_message(const unsigned char *buf, unsigned char length)
transmit a message
Definition: rfm70.cpp:764
void lost_packets_reset(void)
reset rfm70 lost packets count
Definition: rfm70.cpp:714
void mode_receive(void)
switch the rfm70 to receive mode
Definition: rfm70.cpp:551
void pipe_autoack(unsigned char pipe, bool enabled)
enables or disables the autoack on a pipe
Definition: rfm70.cpp:688
void mode_transmit(void)
switch the rfm70 to transmit mode
Definition: rfm70.cpp:570
interface to an RFM70 module
Definition: rfm70.h:380
unsigned char retransmit_count(void)
read rfm70 retransmit count
Definition: rfm70.cpp:680
void receive_address_p0(const unsigned char address[5])
set the rfm70 pipe 0 address
Definition: rfm70.cpp:664
rfm70(pins::output_pin &sclk, pins::output_pin &mosi, pins::input_pin &miso, pins::output_pin &csn, pins::output_pin &ce, void(*wait_ms)(unsigned int), void(*wait_us)(unsigned int))
constructor: create and RFM70 object
Definition: rfm70.h:401
void register_write(unsigned char reg, unsigned char val)
write a single-byte command or register
Definition: rfm70.cpp:420
void pipe_enable(unsigned char d, bool enabled)
enables or disables a pipe
Definition: rfm70.cpp:701
void power(unsigned char level)
set the rfm70 transmit power
Definition: rfm70.cpp:736
unsigned char receive_next_length(void)
get payload length of the next message in receive FIFO
Definition: rfm70.cpp:789
void lna_high(void)
set the rfm70 lna gain to high
Definition: rfm70.cpp:724
void address_length(unsigned char len)
set the rfm70 address length
Definition: rfm70.cpp:642
void init(void)
initialize the library and the rfm70 module
Definition: rfm70.cpp:808
bool receive_fifo_empty(void)
report whether the receive fifo is empty
Definition: rfm70.cpp:658
void crc_length(unsigned char len)
set the rfm70 CRC length
Definition: rfm70.cpp:622
void lna_low(void)
set the rfm70 lna gain to low
Definition: rfm70.cpp:730