mBed RFM12B module library

Dependents:   _EXAMPLE_RFM12B

Fork of RF12B by Sukkin Pang

RFM12B Library

The main purpose of this library was to implement the RFM12B module in order to be able to establish communication with the Moteino (arduino clone that uses the RFM12B).

In order to achieve my goal I was highly inspired by RF12B library from pangsk https://mbed.org/users/pangsk/ and by RFM12B arduino library made by Felix Rusu (http://lowpowerlab.com/blog/2012/12/28/rfm12b-arduino-library/)

Who/What is Moteino? (http://lowpowerlab.com/moteino/)

RF12B.h

Committer:
pangsk
Date:
2012-01-01
Revision:
0:66fdbf2cc578
Child:
3:e926e54424cb

File content as of revision 0:66fdbf2cc578:

#ifndef _RF12B_H
#define _RF12B_H

#include "mbed.h"


#define rf12_grp        rf12_buf[0]
#define rf12_hdr        rf12_buf[1]
#define rf12_len        rf12_buf[2]
#define rf12_data       (rf12_buf + 3)

#define RF12_HDR_CTL    0x80
#define RF12_HDR_DST    0x40
#define RF12_HDR_ACK    0x20
#define RF12_HDR_MASK   0x1F

#define RF12_MAXDATA    66
// maximum transmit / receive buffer: 3 header + data + 2 crc bytes
#define RF_MAX   (RF12_MAXDATA + 5)
#define PACKET_LEN 16

// shorthand to simplify sending out the proper ACK when requested
#define RF12_WANTS_ACK ((rf12_hdr & RF12_HDR_ACK) && !(rf12_hdr & RF12_HDR_CTL))
#define RF12_ACK_REPLY (rf12_hdr & RF12_HDR_DST ? RF12_HDR_CTL : \
            RF12_HDR_CTL | RF12_HDR_DST | (rf12_hdr & RF12_HDR_MASK))
            
            
//enum rfmode_t{RX, TX};

class RF12B {
public:
    /* Constructor */
    RF12B(PinName SDI,
          PinName SDO,
          PinName SCK,
          PinName NCS,
          PinName NIRQ);
   
     
    /* Initialises the RF12B module */
    void init(uint8_t id, uint8_t band, uint8_t g);
        
 
    /* Returns the packet length if data is available in the receive buffer, 0 otherwise*/
   unsigned int available();
   void rf12_sendStart (uint8_t hdr, const void* ptr, uint8_t len);
   void rf12_sendStart2 (uint8_t hdr); 
   uint8_t  rf12_recvDone (void);
   void rf12_recvStart (void);
   uint16_t check_crc(void);
   uint8_t length(void);
   uint8_t* get_data(void);
   
protected:

    /* SPI module */
    SPI spi;
    
    /* Other digital pins */
    DigitalOut NCS;
    InterruptIn NIRQ;
    DigitalIn NIRQ_in;
    DigitalOut rfled;
    DigitalOut t1;
    DigitalOut t2;
    volatile uint16_t rf12_crc; // running crc value
    volatile unsigned char rf12_buf[RF_MAX];  // recv/xmit buf, including hdr & crc bytes  
    volatile  uint8_t nodeid;              // address of this node
    volatile  uint8_t group;               // network grou
    volatile uint8_t rxfill;     // number of data bytes in rf12_buf
    volatile int8_t rxstate;     // current transceiver state
 

    /* Write a command to the RF Module */
    unsigned int writeCmd(unsigned int cmd);
    
    /* Sends a byte of data across RF */
    void send(unsigned char data);
    
    /* Interrupt routine for data reception */
    void rxISR();
    

    uint16_t _crc16_update(uint16_t crc, uint8_t data);
    
    uint16_t rf12_xfer (uint16_t cmd);    
    uint8_t rf12_byte(uint8_t out);
};

#endif /* _RF12B_H */