![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
working code of bae with i2c
Fork of all_combined_week6 by
beacon.cpp@7:47e07755d9a4, 2014-09-18 (annotated)
- Committer:
- viswachaitanya
- Date:
- Thu Sep 18 10:29:33 2014 +0000
- Revision:
- 7:47e07755d9a4
- Parent:
- 6:82153349cc9b
working bae code with i2c
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
greenroshks | 6:82153349cc9b | 1 | //switch off the sync!!!!!!! |
greenroshks | 6:82153349cc9b | 2 | //switch off the preamble!!!!!!! |
greenroshks | 6:82153349cc9b | 3 | /*for crc in tx: |
greenroshks | 6:82153349cc9b | 4 | regIrq2(0x28) : |
greenroshks | 6:82153349cc9b | 5 | |
greenroshks | 6:82153349cc9b | 6 | regpacketconfig 1(0x37) : |
greenroshks | 6:82153349cc9b | 7 | set crc detection/calc. on : | 0x10 |
greenroshks | 6:82153349cc9b | 8 | crcautoclearoff : | 0x08 |
greenroshks | 6:82153349cc9b | 9 | |
greenroshks | 6:82153349cc9b | 10 | for data whitening : regpacketconfig 1(0x37) :| 0x40 |
greenroshks | 6:82153349cc9b | 11 | for |
greenroshks | 6:82153349cc9b | 12 | |
greenroshks | 6:82153349cc9b | 13 | |
greenroshks | 6:82153349cc9b | 14 | |
greenroshks | 6:82153349cc9b | 15 | */ |
greenroshks | 6:82153349cc9b | 16 | // 6CC000 for 435 MHz |
greenroshks | 6:82153349cc9b | 17 | //set all values as FF for checking on spectrum analyzer |
greenroshks | 6:82153349cc9b | 18 | #include "beacon.h" |
greenroshks | 6:82153349cc9b | 19 | #include "HK.h" |
greenroshks | 6:82153349cc9b | 20 | Serial chavan(USBTX, USBRX); // tx, rx |
greenroshks | 6:82153349cc9b | 21 | SPI spi(p11, p12, p13); // mosi, miso, sclk |
greenroshks | 6:82153349cc9b | 22 | DigitalOut cs_bar(p8); //slave select or chip select |
greenroshks | 6:82153349cc9b | 23 | //InterruptIn button(p9); |
greenroshks | 6:82153349cc9b | 24 | //#define TIMES 16 |
greenroshks | 6:82153349cc9b | 25 | //Timer t; |
greenroshks | 6:82153349cc9b | 26 | |
greenroshks | 6:82153349cc9b | 27 | /*void interrupt_func() |
greenroshks | 6:82153349cc9b | 28 | { |
greenroshks | 6:82153349cc9b | 29 | chavan.printf("INTERRUPT_FUNC TRIGGERED\n wait for 3 secs\n"); |
greenroshks | 6:82153349cc9b | 30 | wait(3); |
greenroshks | 6:82153349cc9b | 31 | |
greenroshks | 6:82153349cc9b | 32 | }*/ |
greenroshks | 6:82153349cc9b | 33 | |
greenroshks | 6:82153349cc9b | 34 | extern ShortBeacy Shortbeacon; |
greenroshks | 6:82153349cc9b | 35 | void writereg(uint8_t reg,uint8_t val) |
greenroshks | 6:82153349cc9b | 36 | { |
greenroshks | 6:82153349cc9b | 37 | cs_bar = 0; |
greenroshks | 6:82153349cc9b | 38 | spi.write(reg | 0x80); |
greenroshks | 6:82153349cc9b | 39 | spi.write(val); |
greenroshks | 6:82153349cc9b | 40 | cs_bar = 1; |
greenroshks | 6:82153349cc9b | 41 | } |
greenroshks | 6:82153349cc9b | 42 | uint8_t readreg(uint8_t reg) |
greenroshks | 6:82153349cc9b | 43 | { |
greenroshks | 6:82153349cc9b | 44 | uint8_t val; |
greenroshks | 6:82153349cc9b | 45 | cs_bar = 0; |
greenroshks | 6:82153349cc9b | 46 | spi.write(reg & ~0x80); |
greenroshks | 6:82153349cc9b | 47 | val = spi.write(0); |
greenroshks | 6:82153349cc9b | 48 | cs_bar = 1; |
greenroshks | 6:82153349cc9b | 49 | return val; |
greenroshks | 6:82153349cc9b | 50 | } |
greenroshks | 6:82153349cc9b | 51 | |
greenroshks | 6:82153349cc9b | 52 | void beacon_func() { |
greenroshks | 6:82153349cc9b | 53 | |
greenroshks | 6:82153349cc9b | 54 | //button.rise(&interrupt_func); //interrupt enabled ( rising edge of pin 9) |
greenroshks | 6:82153349cc9b | 55 | printf("\nBeacon function entered\n"); |
greenroshks | 6:82153349cc9b | 56 | wait(0.02); //takes 10 ms for POR event + 10ms for safety |
greenroshks | 6:82153349cc9b | 57 | |
greenroshks | 6:82153349cc9b | 58 | uint8_t byte_counter = 0; |
greenroshks | 6:82153349cc9b | 59 | |
greenroshks | 6:82153349cc9b | 60 | /*struct Short_beacon{ |
greenroshks | 6:82153349cc9b | 61 | uint8_t Voltage[1]; |
greenroshks | 6:82153349cc9b | 62 | uint8_t AngularSpeed[2]; |
greenroshks | 6:82153349cc9b | 63 | uint8_t SubsystemStatus[1]; |
greenroshks | 6:82153349cc9b | 64 | uint8_t Temp[3]; |
greenroshks | 6:82153349cc9b | 65 | uint8_t ErrorFlag[1]; |
greenroshks | 6:82153349cc9b | 66 | }Shortbeacon = { {0x22}, {0x22, 0x33} , {0x00},{0x00,0x00,0x00}, {0xFE} }; |
greenroshks | 6:82153349cc9b | 67 | */ |
greenroshks | 6:82153349cc9b | 68 | //filling hk data |
greenroshks | 6:82153349cc9b | 69 | //ShortBeacon Shortbeacon; |
greenroshks | 6:82153349cc9b | 70 | uint8_t short_beacon[] = { 0xAB, 0x8A, 0xE2, 0xBB, 0xB8, 0xA2, 0x8E,Shortbeacon.Voltage[0],Shortbeacon.AngularSpeed[0], Shortbeacon.AngularSpeed[1],Shortbeacon.SubsystemStatus[0],Shortbeacon.Temp[0],Shortbeacon.Temp[1],Shortbeacon.Temp[2],Shortbeacon.ErrorFlag[0]}; |
greenroshks | 6:82153349cc9b | 71 | |
greenroshks | 6:82153349cc9b | 72 | //mask |
greenroshks | 6:82153349cc9b | 73 | //uint8_t mask[] = {0x80, 0x40, 0x20,0x10,0x8,0x4,0x2,0x1}; |
greenroshks | 6:82153349cc9b | 74 | |
greenroshks | 6:82153349cc9b | 75 | /* for(int i = 0; i < 15 ; i++) |
greenroshks | 6:82153349cc9b | 76 | { |
greenroshks | 6:82153349cc9b | 77 | chavan.printf("0x%X\n",(short_beacon[i])); |
greenroshks | 6:82153349cc9b | 78 | }*/ |
greenroshks | 6:82153349cc9b | 79 | |
greenroshks | 6:82153349cc9b | 80 | spi.format(8,0); |
greenroshks | 6:82153349cc9b | 81 | spi.frequency(10000000); //10MHz SCLK frequency(its max for rfm69hcw) |
greenroshks | 6:82153349cc9b | 82 | cs_bar = 1; // Chip must be deselected |
greenroshks | 6:82153349cc9b | 83 | |
greenroshks | 6:82153349cc9b | 84 | //initialization |
greenroshks | 6:82153349cc9b | 85 | //Common configuration registers |
greenroshks | 6:82153349cc9b | 86 | writereg(0x01,0x04); //sequencer on,standby mode |
greenroshks | 6:82153349cc9b | 87 | writereg(0x02,0x08); //packet-mode used , ook modultion , no dc-shaping |
greenroshks | 6:82153349cc9b | 88 | writereg(0x03,0x68); //1200bps datarate |
greenroshks | 6:82153349cc9b | 89 | writereg(0x04,0x2B); //1200bps datarate |
greenroshks | 6:82153349cc9b | 90 | writereg(0x07,0x6C); //Frequency MSB |
greenroshks | 6:82153349cc9b | 91 | writereg(0x08,0xC0); //Frequency MID |
greenroshks | 6:82153349cc9b | 92 | writereg(0x09,0x00); //Frequency LSB ....6C C0 00 for 435 MHZ |
greenroshks | 6:82153349cc9b | 93 | |
greenroshks | 6:82153349cc9b | 94 | //Transmitter registers |
greenroshks | 6:82153349cc9b | 95 | // RegPaLevel(default +13 dBm) |
greenroshks | 6:82153349cc9b | 96 | |
greenroshks | 6:82153349cc9b | 97 | //IRQ and Pin Mapping Registers |
greenroshks | 6:82153349cc9b | 98 | //no DIO mapped yet |
greenroshks | 6:82153349cc9b | 99 | //regirq1(0x27): modeready (8th bit) will be checked for interrupts |
greenroshks | 6:82153349cc9b | 100 | //regIrq2(0x28): fifothresh (5th bit) ,packetsent(3rd bit) will be checked for interrupts |
greenroshks | 6:82153349cc9b | 101 | |
greenroshks | 6:82153349cc9b | 102 | //Packet Engine Registers |
greenroshks | 6:82153349cc9b | 103 | writereg(0x2C,0x00); //set preamble |
greenroshks | 6:82153349cc9b | 104 | writereg(0x2D,0x0A); //set preamble |
greenroshks | 6:82153349cc9b | 105 | writereg(0x2E,0x80); //sync off |
greenroshks | 6:82153349cc9b | 106 | writereg(0x2F,0x5E); //sync word 1 |
greenroshks | 6:82153349cc9b | 107 | writereg(0x37,0x08 | 0x40);// | 0x10); //packetconfig1, 0x40 for data whitening (only for testing) |
greenroshks | 6:82153349cc9b | 108 | writereg(0x38,0x00); //payload length = 0 ... unlimited payload mode |
greenroshks | 6:82153349cc9b | 109 | writereg(0x3C,0xB0); //fifothresh = 48 because we want it cleared once its 40!!!! |
greenroshks | 6:82153349cc9b | 110 | //Initialization complete |
greenroshks | 6:82153349cc9b | 111 | |
greenroshks | 6:82153349cc9b | 112 | //while(chavan.getc() == 't'){ |
greenroshks | 6:82153349cc9b | 113 | //t.start(); |
greenroshks | 6:82153349cc9b | 114 | //Filling Data into FIFO 64 BYTES : eff.32 bits = 4bytes //fread |
greenroshks | 6:82153349cc9b | 115 | cs_bar = 0; |
greenroshks | 6:82153349cc9b | 116 | spi.write(0x80);//fifo write access |
greenroshks | 6:82153349cc9b | 117 | for(byte_counter=0 ; byte_counter<4; byte_counter++) |
greenroshks | 6:82153349cc9b | 118 | { |
greenroshks | 6:82153349cc9b | 119 | for(int i=7; i>=0 ; i--) |
greenroshks | 6:82153349cc9b | 120 | { |
greenroshks | 6:82153349cc9b | 121 | if((short_beacon[byte_counter] & (uint8_t) pow(2.0,i))!=0) |
greenroshks | 6:82153349cc9b | 122 | //if((short_beacon[byte_counter] & mask[i]) != 0) |
greenroshks | 6:82153349cc9b | 123 | { |
greenroshks | 6:82153349cc9b | 124 | spi.write(0xFF); |
greenroshks | 6:82153349cc9b | 125 | spi.write(0xFF); |
greenroshks | 6:82153349cc9b | 126 | } |
greenroshks | 6:82153349cc9b | 127 | else |
greenroshks | 6:82153349cc9b | 128 | { |
greenroshks | 6:82153349cc9b | 129 | spi.write(0x00); |
greenroshks | 6:82153349cc9b | 130 | spi.write(0x00); |
greenroshks | 6:82153349cc9b | 131 | } |
greenroshks | 6:82153349cc9b | 132 | } |
greenroshks | 6:82153349cc9b | 133 | } |
greenroshks | 6:82153349cc9b | 134 | cs_bar = 1; //cs_bar |
greenroshks | 6:82153349cc9b | 135 | |
greenroshks | 6:82153349cc9b | 136 | //Check for fifoThresh |
greenroshks | 6:82153349cc9b | 137 | printf("\nfor loop executed\n"); |
greenroshks | 6:82153349cc9b | 138 | while((readreg(0x28) & 0x20) != 0x20); |
greenroshks | 6:82153349cc9b | 139 | printf("\nwhile loop executed\n"); |
greenroshks | 6:82153349cc9b | 140 | //Highpower settings |
greenroshks | 6:82153349cc9b | 141 | writereg(0x11,0x7F); //RegPalevel (20db) //~ |
greenroshks | 6:82153349cc9b | 142 | writereg(0x13,0x0F); //RegOCP |
greenroshks | 6:82153349cc9b | 143 | writereg(0x5A,0x5D); //RegTestPa1 |
greenroshks | 6:82153349cc9b | 144 | writereg(0x5C,0x7C); //RegTestPa2 |
greenroshks | 6:82153349cc9b | 145 | |
greenroshks | 6:82153349cc9b | 146 | //Set to Tx mode |
greenroshks | 6:82153349cc9b | 147 | writereg(0x01,0x0C); |
greenroshks | 6:82153349cc9b | 148 | |
greenroshks | 6:82153349cc9b | 149 | printf("\npre 2nd while loop\n"); |
greenroshks | 6:82153349cc9b | 150 | //Check for fifoThresh |
greenroshks | 6:82153349cc9b | 151 | while((readreg(0x28) & 0x20) != 0x00); |
greenroshks | 6:82153349cc9b | 152 | printf("\n2nd while loop executed\n"); |
greenroshks | 6:82153349cc9b | 153 | while(byte_counter!=15){ |
greenroshks | 6:82153349cc9b | 154 | |
greenroshks | 6:82153349cc9b | 155 | //writing again |
greenroshks | 6:82153349cc9b | 156 | cs_bar = 0; |
greenroshks | 6:82153349cc9b | 157 | spi.write(0x80); |
greenroshks | 6:82153349cc9b | 158 | for(int i=7; i>=0 ;i--) |
greenroshks | 6:82153349cc9b | 159 | { |
greenroshks | 6:82153349cc9b | 160 | if((short_beacon[byte_counter] & (uint8_t) pow(2.0,i))!=0) |
greenroshks | 6:82153349cc9b | 161 | //if((short_beacon[byte_counter] & mask[i]) != 0) |
greenroshks | 6:82153349cc9b | 162 | { |
greenroshks | 6:82153349cc9b | 163 | spi.write(0xFF); |
greenroshks | 6:82153349cc9b | 164 | spi.write(0xFF); |
greenroshks | 6:82153349cc9b | 165 | } |
greenroshks | 6:82153349cc9b | 166 | else |
greenroshks | 6:82153349cc9b | 167 | { |
greenroshks | 6:82153349cc9b | 168 | spi.write(0x00); |
greenroshks | 6:82153349cc9b | 169 | spi.write(0x00); |
greenroshks | 6:82153349cc9b | 170 | } |
greenroshks | 6:82153349cc9b | 171 | } |
greenroshks | 6:82153349cc9b | 172 | cs_bar = 1; |
greenroshks | 6:82153349cc9b | 173 | byte_counter++; |
greenroshks | 6:82153349cc9b | 174 | |
greenroshks | 6:82153349cc9b | 175 | //Check for fifoThresh |
greenroshks | 6:82153349cc9b | 176 | while((readreg(0x28) & 0x20) != 0x00); |
greenroshks | 6:82153349cc9b | 177 | } |
greenroshks | 6:82153349cc9b | 178 | printf("\n3rd big while loop executed\n"); |
greenroshks | 6:82153349cc9b | 179 | //wait for packet sent bit to fire |
greenroshks | 6:82153349cc9b | 180 | while((readreg(0x28) & 0x08) != 0x08); |
greenroshks | 6:82153349cc9b | 181 | printf("\n4th while loop executed\n"); |
greenroshks | 6:82153349cc9b | 182 | //chavan.printf("packet sent!!! \n"); |
greenroshks | 6:82153349cc9b | 183 | |
greenroshks | 6:82153349cc9b | 184 | //Switch back to Standby Mode |
greenroshks | 6:82153349cc9b | 185 | writereg(0x01,0x04); |
greenroshks | 6:82153349cc9b | 186 | |
greenroshks | 6:82153349cc9b | 187 | //Lowpowermode |
greenroshks | 6:82153349cc9b | 188 | writereg(0x11,0x9F); //RegPalevel (13db) |
greenroshks | 6:82153349cc9b | 189 | writereg(0x13,0x1A); //RegOCP |
greenroshks | 6:82153349cc9b | 190 | writereg(0x5A,0x55); //RegTestPa1(setting PA_BOOST on RFIO) |
greenroshks | 6:82153349cc9b | 191 | writereg(0x5C,0x70); //RegTestPa2(setting PA_BOOST on RFIO) |
greenroshks | 6:82153349cc9b | 192 | |
greenroshks | 6:82153349cc9b | 193 | //wait for modeready |
greenroshks | 6:82153349cc9b | 194 | while((readreg(0x27)&0x80)!=0x80); |
greenroshks | 6:82153349cc9b | 195 | |
greenroshks | 6:82153349cc9b | 196 | //t.stop(); |
greenroshks | 6:82153349cc9b | 197 | //chavan.printf(" time taken to init + transmit = %f \n", t.read()) ; |
greenroshks | 6:82153349cc9b | 198 | //} |
greenroshks | 6:82153349cc9b | 199 | printf("\nBeacon function exiting\n"); |
greenroshks | 6:82153349cc9b | 200 | } |