![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
working code of cdms with i2c
Fork of rtos_basic by
Revision 7:6b1a6941ac87, committed 2014-09-18
- Comitter:
- viswa_chaitanya
- Date:
- Thu Sep 18 10:32:43 2014 +0000
- Parent:
- 6:209f4db62daf
- Commit message:
- working code of cdms with i2c
Changed in this revision
diff -r 209f4db62daf -r 6b1a6941ac87 main.cpp --- a/main.cpp Tue Jun 04 16:01:32 2013 +0100 +++ b/main.cpp Thu Sep 18 10:32:43 2014 +0000 @@ -1,21 +1,103 @@ #include "mbed.h" #include "rtos.h" +#include "master.h" + +//#include "tmtc.h" +//#include "science.h" +//#include "hk.h" -DigitalOut led1(LED1); -DigitalOut led2(LED2); +#define intmax 11999 + +Thread* t_tmtc; +Thread* t_science_store_sd; +Thread* t_hk_store_sd; + +void t_tmtc_main(void const *args); +void t_science_main(void const *args); +void t_hk_main(void const *args); +void scheduler(void const * args); + +Timer timer_main; +int schedcount = 1; +Mutex critical; -void led2_thread(void const *args) { +int main() { + Thread thread_tmtc(t_tmtc_main, NULL, osPriorityIdle); + t_tmtc = &thread_tmtc; + Thread thread_science(t_science_main, NULL, osPriorityLow); + t_science_store_sd = &thread_science; + Thread thread_hk(t_hk_main, NULL, osPriorityBelowNormal); + t_hk_store_sd = &thread_hk; + + RtosTimer schedule(scheduler,osTimerPeriodic); + timer_main.start(); + schedule.start(5000); + Thread::wait(osWaitForever); +} + +void scheduler(void const * args) +{ + if(schedcount == intmax+1) //the value is reset at this value so as to ensure smooth flow, 65532 and 0 are divisible by 3 and 2. + { + schedcount =0; + } + + if(schedcount%4==0) + { + printf("\nHK signal at %f\n",timer_main.read()); + t_hk_store_sd->signal_set(0x01); + } + + if(schedcount%3==0) + { + printf("\nScience signal at %f\n",timer_main.read()); + t_science_store_sd->signal_set(0x01); + } + + if(schedcount==5) + { + printf("\nTMTC signalled"); + t_tmtc->signal_set(0x1); + } + + schedcount++; +} + +void t_science_main(void const *args) { while (true) { - led2 = !led2; - Thread::wait(1000); + Thread::signal_wait(0x1); + critical.lock(); + printf("science data aquisition started\n"); + wait(3); + printf("science data acquisition ended\n"); + t_hk_store_sd->signal_set(0x10); + critical.unlock(); + wait(3); + printf("Science data written to SD card\n"); } } - -int main() { - Thread thread(led2_thread); - + +void t_hk_main(void const *args) { while (true) { - led1 = !led1; - Thread::wait(500); + Thread::signal_wait(0x1); + critical.lock(); + printf("HK acquisition started\n"); + FUNC_I2C_MASTER_MAIN('1',0x20,24 ); + wait(2); + printf("HK acquisition ended\n"); + critical.unlock(); + wait(2); + printf("HK data written to SD card"); } } + +void t_tmtc_main(void const *args) { + Thread::signal_wait(0x1); + critical.lock(); + printf("tmtc started\n"); + wait(5); + printf("tmtc ended\n"); + critical.unlock(); + wait(5); + printf("tmtc executed\n"); +} \ No newline at end of file
diff -r 209f4db62daf -r 6b1a6941ac87 master.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/master.cpp Thu Sep 18 10:32:43 2014 +0000 @@ -0,0 +1,81 @@ +#include "master.h" + +I2C master (p28,p27); //configure pins p27,p28 as I2C master +Serial pc (USBTX,USBRX); + +struct SensorData //HK_data_structure +{ + char voltage[10]; + char current[10]; + char temp[10]; +} Sensor; + +void FUNC_I2C_MASTER_MAIN(char command, int slave_address, int iterations) +{ + wait(0.5); + printf("1\n"); + bool acknowledge1; + bool acknowledge2; + uint8_t loopvariable2=0; + bool loopvariable1 = true; + bool loopvariable3 = true; + while(loopvariable1) + { +//-------------writing the command to slave-------------------------------------------------------------- + printf("2\n"); + master.frequency(100000); //set clock frequency + master.start(); //initiating the data transfer + acknowledge2 = (bool) master.write(slave_address|0x00); //addressing the slave to write + if(acknowledge2) //proceeding further only if slave is addressed + { + printf("3\n"); + acknowledge1 = (bool) master.write(command); //sending the command to slave + if(acknowledge1) //proceeding further only if sent data is acknowledged + { + printf("acknowledge1=%d\n",acknowledge1); + loopvariable1=false; //if acknowledged, breaking loop in next iteration + +//--------------reading data from slave--------------------------------------------------------------- + while(loopvariable3) + { + master.frequency(100000); //set clock frequency + master.start(); //initiate data transfer + acknowledge1 = (bool) master.write(slave_address | 0x01); //addressing the slave to read + + if(acknowledge1) //proceedong only if slae is addressed + { + loopvariable3 = false; + while(loopvariable2<8) + { + Sensor.voltage[loopvariable2] = receive_byte(); //receiving data if acknowledged + printf(" voltage%d = %x\n",loopvariable2,Sensor.voltage[loopvariable2]); + + Sensor.current[loopvariable2] = receive_byte(); //receiving data if acknowledged + printf(" current%d = %x\n",loopvariable2, Sensor.current[loopvariable2]); + + Sensor.temp[loopvariable2] = receive_byte(); //receiving data if acknowledged + printf(" temperature%d = %x\n",loopvariable2,Sensor.temp[loopvariable2]); + + loopvariable2++; + + } //while(loopvariable2<30) + }//if(acknowledge1==1) + + master.stop(); + }//while(loopvariable3) + }//if(acknowledge1==1) + }//if(acknowledge2==1) + }//while(loopvariable1) + printf("done"); +}//main + + +//----------------function to read and return the data received----------------------------------- +char receive_byte() +{ + + char value; + value = master.read(1); + return(value); //returning the 4 byte floating point number +} +
diff -r 209f4db62daf -r 6b1a6941ac87 master.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/master.h Thu Sep 18 10:32:43 2014 +0000 @@ -0,0 +1,10 @@ +#include "mbed.h" //library +#define get_hk_data '1' //sample telecommands +#define telecommand2 '2' +#define telecommand3 '3' +#define telecommand4 '4' +#define telecommand5 '5' + +const int addr = 0x20; //slave address +char receive_byte(); //funtion to receive float data +void FUNC_I2C_MASTER_MAIN(char command, int slave_address, int iterations); \ No newline at end of file
diff -r 209f4db62daf -r 6b1a6941ac87 mbed-rtos.lib --- a/mbed-rtos.lib Tue Jun 04 16:01:32 2013 +0100 +++ b/mbed-rtos.lib Thu Sep 18 10:32:43 2014 +0000 @@ -1,1 +1,1 @@ -https://mbed.org/users/mbed_official/code/mbed-rtos/ \ No newline at end of file +https://mbed.org/users/mbed_official/code/mbed-rtos/#631c0f1008c3
diff -r 209f4db62daf -r 6b1a6941ac87 mbed.bld --- a/mbed.bld Tue Jun 04 16:01:32 2013 +0100 +++ b/mbed.bld Thu Sep 18 10:32:43 2014 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/mbed_official/code/mbed/builds/ \ No newline at end of file +http://mbed.org/users/mbed_official/code/mbed/builds/552587b429a1 \ No newline at end of file