Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: FreescaleIAP mbed-rtos mbed
Fork of RAJANGAM_REVIEW_BAE_CODE by
Diff: BCN.cpp
- Revision:
- 12:af1d7e18b868
- Parent:
- 11:1fdb94ae6563
- Child:
- 13:fb7facaf308b
--- a/BCN.cpp Wed Apr 13 17:00:25 2016 +0000
+++ b/BCN.cpp Wed Apr 13 18:34:28 2016 +0000
@@ -1,43 +1,47 @@
+
#include "BCN.h"
#include <stdio.h>
#include "pin_config.h"
+//Check the pin names
+//Takes max 4.3 sec in void FCTN_BCN_TX_MAIN() (temp.calc. + long_beacon + short_beacon)
Serial pc_bcn(USBTX, USBRX); //tx,rx
-SPI spi(PIN16, PIN17, PIN15); // mosi, miso, sclk
-DigitalOut cs(PIN6); //slave select or chip select
-Timer t;
+SPI spi(PIN16, PIN17, PIN15); // mosi, miso, sclk
+DigitalOut cs(PIN87); //slave select or chip select
Timer t_i;
+Timeout rf_sl_timeout;
Ticker loop;
//GLOBAL VARIABLES
-uint8_t BCN_INIT_STATUS;
-uint8_t BCN_TX_MAIN_STATUS;
-uint8_t BCN_TX_STATUS;
+uint8_t BCN_INIT_STATUS = 0;
+uint8_t BCN_TX_MAIN_STATUS = 0;
+uint8_t BCN_TX_STATUS = 0;
uint8_t BCN_TX_EN = 1; //hardcoding for now //check where is this variable toggled??
-uint8_t BCN_FEN = 1; //write this to non-erasable memory.
+uint8_t BCN_FEN = 0; //hardcoding for now //write this value to flash
uint8_t BCN_STANDBY = 0; //hardcoding for now //check where is this variable toggled??
-uint8_t BCN_TS_BUFFER; // For Temperature
+uint8_t BCN_TS_BUFFER = 0; // For Temperature
+uint8_t BCN_FAIL_COUNT=0;
-uint8_t BCN_FAIL_COUNT=0; //bcn fail count added
-
-void P_BCN_INIT()
+void FCTN_BCN_INIT()
{
- pc_bcn.printf("P_BCN_INIT\n");
+ pc_bcn.printf("FCTN_BCN_INIT\n");
BCN_INIT_STATUS = 1;
+ if(BCN_FEN == 0)
+ rf_sl_timeout.attach(&FCTN_BCN_FEN, 30);
Init_BEACON_HW();
- if (BCN_FEN == 0)
- t.start();//Start the timer for RF_Silence
BCN_INIT_STATUS = 0;
}
-void P_BCN_FEN()
+void FCTN_BCN_FEN()
{
- pc_bcn.printf("P_FEN\n");
+ pc_bcn.printf("FCTN_FEN\n");
BCN_FEN = 1;//write this value to flash
}
-void P_BCN_TX_MAIN()
+void FCTN_BCN_TX_MAIN()
{
- pc_bcn.printf("P_BCN_TX_MAIN\n");
+ pc_bcn.printf("FCTN_BCN_TX_MAIN\n");
+ t_i.start();
+ int begin = t_i.read_us();
BCN_TX_MAIN_STATUS = 1;
if(BCN_FEN == 1)
@@ -58,13 +62,8 @@
else
{
//transmit short beacon and long beacon
- t_i.start();
- int begin = t_i.read_us();
- SHORT_BCN_TX();
+ //SHORT_BCN_TX();
LONG_BCN_TX();
- t_i.stop();
- int end = t_i.read_us();
- pc_bcn.printf("The time required for short and long is %d seconds\r\n", end-begin);
if(Check_ACK_RECEIVED() == 1)
{
@@ -91,6 +90,10 @@
Set_BCN_TX_STATUS(BCN_RF_SILENCE); //Window of RF Silence: None of the Txs should be on.
BCN_TX_MAIN_STATUS = 0;
}
+ t_i.stop();
+ int end = t_i.read_us();
+ pc_bcn.printf("The time required for FCTN_BCN_TX_MAIN is %d useconds\r\n", end-begin);
+
}
void Set_BCN_TX_STATUS(uint8_t STATUS)
@@ -101,9 +104,10 @@
uint8_t check_Temperature()
{
uint8_t temperature;
- writereg(RF22_REG_0F_ADC_CONFIGURATION,0x80);
+ writereg(RF22_REG_0F_ADC_CONFIGURATION,0x00);
writereg(RF22_REG_12_Temperature_Sensor_Calibration,0x20);
- wait(1);
+ writereg(RF22_REG_0F_ADC_CONFIGURATION,0x80);
+ wait(0.1);
temperature = readreg(RF22_REG_11_ADC_Value);
temperature = (float)temperature*0.5 - 64;
return temperature;
@@ -112,18 +116,13 @@
void SHORT_BCN_TX()
{
writereg(RF22_REG_6E_TX_DATA_RATE,0x01);
- writereg(RF22_REG_6F_TX_DATA_RATE,0x4F);//160bps
- writereg(RF22_REG_3E_PACKET_LENGTH,SHORT_TX_DATA); //short packet length
- /*
- init();
- //init complete
- pc_bcn.printf("init complete.....press t to send\n");
- while(pc_bcn.getc()=='t')
- { */
- //button.rise(&interrupt_func); //interrupt enabled ( rising edge of pin 9)
- wait(0.02); // pl. update this value or even avoid it!!!
- int i=0;
+ writereg(RF22_REG_6F_TX_DATA_RATE,0x50);//160bps
+ writereg(RF22_REG_3E_PACKET_LENGTH,SHORT_TX_DATA); //short packet length
+ wait(0.02);
+
+ uint32_t timeout_count = 10e5;
//extract values from short_beacon[]
+
struct Short_beacon
{
uint8_t Voltage[1];
@@ -136,20 +135,13 @@
//filling hk data
//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]};
uint8_t short_beacon[] = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,Shortbeacon.Voltage[0],Shortbeacon.AngularSpeed[0], Shortbeacon.AngularSpeed[1],Shortbeacon.SubsystemStatus[0],Shortbeacon.Temp[0],Shortbeacon.Temp[1],Shortbeacon.Temp[2],Shortbeacon.ErrorFlag[0]};
-
- //uint8_t short_beacon[15];
-// for(int i = 0;i<15;i++)
-// {
-// short_beacon[i] = 0xAA;
-// }
-
- //writereg(RF22_REG_07_OPERATING_MODE1,0x01); //ready mode ??
+
clearTxBuf();
//writing data first time
int byte = 0;
cs = 0;
spi.write(0xFF);
- pc_bcn.printf("line 151");
+
for (int byte_counter = 0; byte_counter <15 ; byte_counter++)
{
for(int j = 3; j >= 0 ; j--)
@@ -175,43 +167,42 @@
}
cs = 1;
- pc_bcn.printf("line 177\n");
+
//Set to Tx mode
- writereg(RF22_REG_07_OPERATING_MODE1,0x09);
-
+ writereg(RF22_REG_07_OPERATING_MODE1,0x08);//*
+ wait(0.1);
//Check for fifoThresh
- while((readreg(RF22_REG_03_INTERRUPT_STATUS1) & 0x20) != 0x20);
- //pc_bcn.printf("fifothresh1?\n");
- pc_bcn.printf("line 184\n");
- //rf22.waitPacketSent();
- while((readreg(RF22_REG_03_INTERRUPT_STATUS1) & 0x04) != 0x04);
- //pc_bcn.printf(" chk pkt sent!\r\n");
- pc_bcn.printf("Short packet sent\r\n");
+ while(timeout_count--)if((readreg(RF22_REG_03_INTERRUPT_STATUS1) & 0x20) == 0x20)break;
+ timeout_count=10e5;
+
+ //Check for packet_sent
+ while(timeout_count--)if((readreg(RF22_REG_03_INTERRUPT_STATUS1) & 0x04) == 0x04)break;
+
+ //pc_bcn.printf("Short packet sent\r\n");
writereg(RF22_REG_07_OPERATING_MODE1,0x00); //standby mode
}
void LONG_BCN_TX()
{
- writereg(RF22_REG_6E_TX_DATA_RATE,0x04);
- writereg(RF22_REG_6F_TX_DATA_RATE,0xEA);//600 bps
+ writereg(RF22_REG_6E_TX_DATA_RATE,0x08);
+ writereg(RF22_REG_6F_TX_DATA_RATE,0x31);//1000 bps
writereg(RF22_REG_3E_PACKET_LENGTH,LONG_TX_DATA); //long packet length
wait(0.02);
-
+ uint32_t timeout_count=10e5;
//get long_beacon array
- uint8_t Long_beacon[75];
- for(int i = 0;i<75;i++)
+ uint8_t Long_beacon[125];
+ for(int i = 0;i<125;)
{
- Long_beacon[i] = 0xA0;
+ Long_beacon[i++] = 0xAA;
}
//setModeIdle();
- writereg(RF22_REG_07_OPERATING_MODE1,0x01); //ready mode
clearTxBuf();
//writing data first time
cs = 0;
@@ -223,32 +214,35 @@
cs = 1;
//Set to Tx mode
- writereg(RF22_REG_07_OPERATING_MODE1,0x09);
-
+ writereg(RF22_REG_07_OPERATING_MODE1,0x08);//*
+ wait(0.1);
+
//Check for fifoThresh
- while((readreg(RF22_REG_03_INTERRUPT_STATUS1) & 0x20) != 0x20);
+ while(timeout_count--)if((readreg(RF22_REG_03_INTERRUPT_STATUS1) & 0x20) == 0x20)break;
+ timeout_count=10e5;
cs = 0;
spi.write(0xFF);
- for(int i=60; i<75;i++)
+ for(int i=60; i<125;i++)
{
spi.write(Long_beacon[i]);
}
cs = 1;
- wait(0.01);
+ wait(0.1);
//Check for fifoThresh
- while((readreg(RF22_REG_03_INTERRUPT_STATUS1) & 0x20) != 0x20);
+ while(timeout_count--)if((readreg(RF22_REG_03_INTERRUPT_STATUS1) & 0x20) == 0x20)break;
+ timeout_count=10e5;
//Check for packetsent interrupt
- while((readreg(RF22_REG_03_INTERRUPT_STATUS1) & 0x04) != 0x04);
+ while(timeout_count--)if((readreg(RF22_REG_03_INTERRUPT_STATUS1) & 0x04) == 0x04)break;
- pc_bcn.printf("Long packet sent\r\n");
+ //pc_bcn.printf("Long packet sent\r\n");
writereg(RF22_REG_07_OPERATING_MODE1,0x00); //standby mode
}
-void reset_uC()
+void reset_rfm()
{
- P_BCN_INIT();
+ FCTN_BCN_INIT();
}
void writereg(uint8_t reg,uint8_t val)
{
@@ -265,7 +259,7 @@
}
else if(count == 5)
{
- reset_uC(), printf("reg = 0x%X\n",reg);break;
+ reset_rfm(), printf("reg = 0x%X\n",reg);break;
}
}
else
@@ -281,19 +275,13 @@
writereg(RF22_REG_08_OPERATING_MODE2,0x01);
writereg(RF22_REG_08_OPERATING_MODE2,0x00);
}
-uint8_t setFrequency(double centre,float afcPullInRange)
+uint8_t setFrequency(double centre)
{
uint8_t fbsel = 0x40;
- uint8_t afclimiter;
if (centre >= 480.0) {
centre /= 2;
fbsel |= 0x20;
- afclimiter = afcPullInRange * 1000000.0 / 1250.0;
- } else {
- if (afcPullInRange < 0.0 || afcPullInRange > 0.159375)
- return false;
- afclimiter = afcPullInRange * 1000000.0 / 625.0;
- }
+ }
centre /= 10.0;
double integerPart = floor(centre);
double fractionalPart = centre - integerPart;
@@ -306,14 +294,15 @@
writereg(RF22_REG_75_FREQUENCY_BAND_SELECT, fbsel);
writereg(RF22_REG_76_NOMINAL_CARRIER_FREQUENCY1, fc >> 8);
writereg(RF22_REG_77_NOMINAL_CARRIER_FREQUENCY0, fc & 0xff);
- writereg(RF22_REG_2A_AFC_LIMITER, afclimiter);
return 0;
}
void Init_BEACON_HW()
{
+ wait(0.1);
cs=1; // chip must be deselected
+ wait(0.1);
spi.format(8,0);
spi.frequency(10000000); //10MHz SCLK
@@ -322,11 +311,11 @@
pc_bcn.printf("spi connection valid\r\n");
else
{pc_bcn.printf("error in spi connection\r\n");
- reset_uC();
+ reset_rfm();
}
writereg(RF22_REG_07_OPERATING_MODE1,0x80); //sw_reset
- wait(1); //takes time to reset
+ wait(0.1); //takes time to reset
clearTxBuf();
@@ -344,28 +333,28 @@
writereg(RF22_REG_0B_GPIO_CONFIGURATION0,0x15); // TX state
writereg(RF22_REG_0C_GPIO_CONFIGURATION1,0x12); // RX state
- setFrequency(435.0,05);
+ setFrequency(435.0);
if((readreg(RF22_REG_02_DEVICE_STATUS)& 0x08)!= 0x00)
{
- //pc_bcn.printf("frequency not set properly\r\n");
- reset_uC();
+ pc_bcn.printf("frequency not set properly\r\n");
+ reset_rfm();
}
//set Modem Configuration
- writereg(RF22_REG_1C_IF_FILTER_BANDWIDTH,0xdf);
- writereg(RF22_REG_1F_CLOCK_RECOVERY_GEARSHIFT_OVERRIDE,0x03);
- writereg(RF22_REG_20_CLOCK_RECOVERY_OVERSAMPLING_RATE,0x39);
- writereg(RF22_REG_21_CLOCK_RECOVERY_OFFSET2,0x20);
- writereg(RF22_REG_22_CLOCK_RECOVERY_OFFSET1,0x68); //updated 20 to 25 reg values from excel sheet for 1.2 Khz freq. deviation,fsk
- writereg(RF22_REG_23_CLOCK_RECOVERY_OFFSET0,0xdc);
- writereg(RF22_REG_24_CLOCK_RECOVERY_TIMING_LOOP_GAIN1,0x00);
- writereg(RF22_REG_25_CLOCK_RECOVERY_TIMING_LOOP_GAIN0,0x6B);
- writereg(RF22_REG_2C_OOK_COUNTER_VALUE_1,0x2C);
- writereg(RF22_REG_2D_OOK_COUNTER_VALUE_2,0x11); //not required for fsk (OOK counter value)
- writereg(RF22_REG_2E_SLICER_PEAK_HOLD,0x2A); //??
+ //writereg(RF22_REG_1C_IF_FILTER_BANDWIDTH,0xdf);
+ //writereg(RF22_REG_1F_CLOCK_RECOVERY_GEARSHIFT_OVERRIDE,0x03);
+ //writereg(RF22_REG_20_CLOCK_RECOVERY_OVERSAMPLING_RATE,0x39);
+ //writereg(RF22_REG_21_CLOCK_RECOVERY_OFFSET2,0x20);
+ //writereg(RF22_REG_22_CLOCK_RECOVERY_OFFSET1,0x68); //updated 20 to 25 reg values from excel sheet for 1.2 Khz freq. deviation,fsk
+ //writereg(RF22_REG_23_CLOCK_RECOVERY_OFFSET0,0xdc);
+ //writereg(RF22_REG_24_CLOCK_RECOVERY_TIMING_LOOP_GAIN1,0x00);
+ //writereg(RF22_REG_25_CLOCK_RECOVERY_TIMING_LOOP_GAIN0,0x6B);
+ //writereg(RF22_REG_2C_OOK_COUNTER_VALUE_1,0x2C);
+ //writereg(RF22_REG_2D_OOK_COUNTER_VALUE_2,0x11); //not required for fsk (OOK counter value)
+ //writereg(RF22_REG_2E_SLICER_PEAK_HOLD,0x2A); //??
writereg(RF22_REG_58,0x80);
- writereg(RF22_REG_69_AGC_OVERRIDE1,0x60);
+ //writereg(RF22_REG_69_AGC_OVERRIDE1,0x60);
//Data rate set later
@@ -390,16 +379,3 @@
return 0;
}
}
-/*int main()
-{
- P_BCN_INIT();
-
- loop.attach(&P_BCN_TX_MAIN, 10.0);
-
- while(t.read() < RF_SILENCE_TIME);
-
- P_BCN_FEN();
-
- while(1);
-
-}*/
\ No newline at end of file
