mag n gyro included

Dependencies:   mbed-rtos mbed

Fork of BAE_vr2_gingerbread2 by Seeker of Truth ,

Committer:
sakthipriya
Date:
Wed Dec 17 06:52:26 2014 +0000
Revision:
0:e9c32e1df869
i2c working with new hk(no battery gauge)

Who changed what in which revision?

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