Project

General

Profile

rfm12btest.c

Stephen99, 2013-01-02 10:09

 
1

    
2
/*
3
 * rfm12btest.c:
4
 *        Simple shell program to test receiving data from RFM12B via SPI (in Jeenode format)
5
 *  Build with gcc -o rfm12btest  -l rt rfm12btest.c -l bcm2835
6
 */
7

    
8
#include <stdio.h>
9
#include <stdlib.h>
10

    
11
#include <bcm2835.h> 
12

    
13
static uint8_t group = 212;         // network group
14
volatile uint16_t rf12_crc;         // running crc value
15

    
16
#define RFM_IRQ 22                        //IRQ GPIO pin.
17
#define RFM_CE BCM2835_SPI_CS1  //SPI chip select
18

    
19
void spi_init() 
20
{
21
  if (!bcm2835_init())
22
         exit (1);     
23
  bcm2835_spi_begin();     
24
  bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST);      
25
  bcm2835_spi_setDataMode(BCM2835_SPI_MODE0);                   
26
  bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_128);   // 2MHz
27
  bcm2835_spi_chipSelect(RFM_CE);                      
28
  bcm2835_spi_setChipSelectPolarity(RFM_CE, LOW);      
29

    
30
  //Set IRQ pin details
31
  bcm2835_gpio_fsel(RFM_IRQ, BCM2835_GPIO_FSEL_INPT);
32
  bcm2835_gpio_set_pud(RFM_IRQ, BCM2835_GPIO_PUD_UP);
33
}
34

    
35
static uint16_t _crc16_update(uint16_t crc, uint8_t a)
36
{
37
        int i;
38

    
39
        crc ^= a;
40
        for (i = 0; i < 8; ++i)
41
        {
42
                if (crc & 1)
43
                        crc = (crc >> 1) ^ 0xA001;
44
                else
45
                        crc = (crc >> 1);
46
        }
47

    
48
        return crc;
49
}
50

    
51
uint16_t rf12_xfer(uint16_t cmd) 
52
{
53
  unsigned char buffer[2];
54
  uint16_t reply;
55
  buffer[0] = cmd >> 8;
56
  buffer[1] = cmd;
57
  bcm2835_spi_transfern(buffer,2);
58
  reply = buffer[0] << 8;
59
  reply |= buffer[1];
60
  return reply;
61
}
62

    
63
void rf12_init() {
64
    rf12_xfer(0x80E7); // EL,EF,868band,12.0pF 
65
    rf12_xfer(0xA640); // 868MHz 
66
    rf12_xfer(0xC606); // approx 49.2kbps 
67
    rf12_xfer(0x94A2); // VDI,FAST,134kHz,0dBm,-103dBm 
68
    rf12_xfer(0xC2AC); // AL,!ml,DIG,DQD4 
69
    rf12_xfer(0xCA83); // FIFO8,SYNC,!ff,DR 
70
    rf12_xfer(0xCED4); // SYNC=2DD4?. 
71
    rf12_xfer(0xC483); // @PWR,NO RSTRIC,!st,!fi,OE,EN 
72
    rf12_xfer(0x9850); // !mp,90kHz,MAX OUT 
73
    rf12_xfer(0xCC77); // OB1?.OB0, LPX,!ddy?.DDIT?.BW0 
74
    rf12_xfer(0xE000); // NOT USE 
75
    rf12_xfer(0xC800); // NOT USE 
76
    rf12_xfer(0xC049); // 1.66MHz,2.2V 
77
}
78

    
79
void loop() 
80
{
81
  uint16_t ret ;
82
  unsigned char buffer[128];
83
  int i;
84
  int j;
85

    
86
  rf12_xfer(0x82dd);
87
  
88
  for(j = 0;j<20;j++)
89
  {
90
        rf12_xfer(0xCA80); //reset the sync cicuit to look for a new packet
91
    rf12_xfer(0xCA83);
92
        rf12_xfer(0);
93

    
94
        buffer[2] = 0;
95
    for(i = 1;i<buffer[2]+5;i++)
96
    {
97
                while(bcm2835_gpio_lev(RFM_IRQ));
98
          
99
                rf12_xfer(0);
100
                buffer[i] = rf12_xfer(0xB000);
101
                
102
                if(buffer[2] > 66) //If length is over the max, reset
103
                {
104
                        buffer[2] = 0;
105
                        break;                        
106
                }
107
    }
108

    
109
        printf("buffer");
110
        if(group != 0) buffer[0] = group;
111
        rf12_crc = ~0;
112
        for(i = 0;i<buffer[2]+5;i++)
113
        {
114
          rf12_crc = _crc16_update(rf12_crc, buffer[i]);
115
      printf(" %i",buffer[i]);
116
        }
117
        printf(" (crc = %i)",rf12_crc);
118
        printf("\n");
119
  }
120

    
121
  return;
122
}
123

    
124
int main (void)
125
{
126
  printf ("Raspberry Pi RFM12B test program\n") ;
127
  
128
  spi_init();
129
  rf12_init();
130
  
131
  loop();
132

    
133
  bcm2835_spi_end();
134

    
135
  printf("Done\n");
136
  return 0 ;
137
}