![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
telecommand working with junk
Fork of cdms_functions by
Diff: main.cpp
- Revision:
- 1:4cd53e695c17
- Parent:
- 0:ceaeb1b4d679
- Child:
- 2:4f10e81030c7
diff -r ceaeb1b4d679 -r 4cd53e695c17 main.cpp --- a/main.cpp Fri Dec 19 06:47:08 2014 +0000 +++ b/main.cpp Sat Dec 20 06:38:45 2014 +0000 @@ -1,8 +1,278 @@ #include "mbed.h" +#include "rtos.h" +#include "mai.h" + + +const int addr = 0x20; //slave address +I2C master (D14,D15); //configure pins p27,p28 as I2C master +Serial pc (USBTX,USBRX); +DigitalOut interrupt(D9); + +InterruptIn data_ready(D10); + + +int reset; +char writedata; +bool write2slave; +bool master_status_write; + +typedef struct +{ + char data; // To avoid dynamic memory allocation + int length; +}i2c_data; + +typedef struct { + char Voltage[9]; + char Current[5]; + char Temperature[2]; + char PanelTemperature[3];//read by the 4 thermistors on solar panels + char BatteryTemperature; //to be populated + char faultpoll; //polled faults + char faultir; //interrupted faults + char power_mode; //power modes + char AngularSpeed[3]; + char Bnewvalue[3]; + + //float magnetometer,gyro=>to be addes +} hk_data; +hk_data decode_data; + + +//Mail<i2c_data,16> i2c_data_receive; +Mail<i2c_data,16> i2c_data_send; + +Thread * ptr_t_i2c; +void FUNC_I2C_MASTER_FSLAVE(char * data,int length) +{ + //printf("press backspace for master to start \n\r"); + //while(1){ + //interrupt = 0; + // if(pc.getc()=='z'){ + wait(0.5); + interrupt = 1; + printf("\nMaster executed\n"); + + + bool ack0 =true; + bool loopvariable0 =true; + //char *data = new char; + //*data = 'b'; + + + + master.frequency(100000); //set clock frequency + //master.start(); + /*while(write2slave) + { *writedata = pc.getc(); + wait(0.5); + + printf("master clk freq setup and addressing slave\n\r"); + master.start(); //initiating the data transfer + master_status_write = (bool) master.write(addr|0x00,writedata,1); + if(master_status_write==0) + { + printf("master has written %c to slave\n\r",*writedata); + write2slave=false; + } + + + }*/ + + while(ack0 && data_ready == 1) + { + //printf("2 master clk freq setup and addressing slave\n\r"); + + master.start(); //initiating the data transfer + ack0 = master.read(addr,data,length); + } + if(!ack0) + { + printf("\n master has read %s from slave\n\r",data); + loopvariable0=false; + } + //master.stop(); + + //printf("done\n\r"); + //delete data; + interrupt=0; +} +void TC_DECODE(char *data_hk) //getting the structure back from hk data sent by bae +{ + for(int i=0;i<=7;i++) + { + decode_data.Voltage[i] = data_hk[i]; + decode_data.Voltage[8] = '\0'; + } + for(int i=0;i<=3;i++) + { + decode_data.Current[i] = data_hk[8+i]; + decode_data.Current[4] = '\0'; + } + decode_data.Temperature[0] = data_hk[12]; + decode_data.Temperature[1] = '\0'; + for(int i=0;i<=1;i++) + { + decode_data.PanelTemperature[i] = data_hk[13+i]; + decode_data.PanelTemperature[2] = '\0'; + } + decode_data.BatteryTemperature = data_hk[15]; + decode_data.faultpoll = data_hk[16]; + decode_data.faultir = data_hk[17]; + decode_data.power_mode = data_hk[18]; + for(int i=0;i<=1;i++) + { + decode_data.AngularSpeed[i] = data_hk[19+i]; + decode_data.AngularSpeed[2] = '\0'; + } + for(int i=0;i<=1;i++) + { + decode_data.Bnewvalue[i] = data_hk[21+i]; + decode_data.Bnewvalue[2] = '\0'; + } + printf("\n voltage %s\n\r",decode_data.Voltage); + printf("\n current %s\n\r",decode_data.Current); + printf("\n faultpoll %c\n\r",decode_data.faultpoll); +} + +void T_I2C_MASTER_FSLAVE(void const *args) +{ + char data_receive[25]; + + while(1) + { + Thread::signal_wait(0x1); + FUNC_I2C_MASTER_FSLAVE(data_receive,25); + void TC_DECODE(); + + /*i2c_data * i2c_data_r = i2c_data_receive.alloc(); + strcpy(i2c_data_r->data , data_receive); + i2c_data_r->length = 25; + i2c_data_receive.put(i2c_data_r);*/ + printf("\n Data received from slave is %s\n\r",data_receive); + } +} + +char command; + +void TC_EXECUTE() +{ osEvent evt = i2c_data_send.get(); + if (evt.status == osEventMail) + { + i2c_data *i2c_data_s = (i2c_data*)evt.value.p; + command = i2c_data_s -> data; + } + + switch(command) + { + case '0': + { + FUNC_MASTER_WRITE ( '1' ) ; + break; } + + case '1': + { + FUNC_MASTER_WRITE ( '2' ) ; + } + break; + case '2': + { + FUNC_MASTER_WRITE ( '3' ) ; + } + break; + case '3': + { + FUNC_MASTER_WRITE ( '4' ) ; + } + break; + case '4': + { + FUNC_MASTER_WRITE ( '5' ) ; + } + break; + case '5': + { + FUNC_MASTER_WRITE ( '6' ) ; + } + break; + case 6: + break; + case 7: + break; + } +} +void GETCOMMAND() +{ char data = pc.getc(); + + + i2c_data * i2c_data_s = i2c_data_send.alloc(); + i2c_data_s->data = data; + i2c_data_s->length = 1; + i2c_data_send.put(i2c_data_s); + TC_EXECUTE(); + i2c_data_send.free(i2c_data_s); +} +void FUNC_MASTER_WRITE(char writedata) +{ //wait(1); + interrupt = 1; + write2slave=true; + + master_status_write = true; + + + master.frequency(100000); //set clock frequency + while(write2slave && interrupt ==1) + { + printf("master clk freq setup and addressing slave\n\r"); + master.start(); //initiating the data transfer + master_status_write = (bool) master.write(addr|0x00,&writedata,1); + if(master_status_write==0) + { + printf("master has written %c to slave\n\r",writedata); + write2slave = 0; + } + } + interrupt = 0; +} + + + +void FUNC_INT() +{ + + ptr_t_i2c->signal_set(0x1); + +} + + + + + + +int main() +{ + + ptr_t_i2c = new Thread(T_I2C_MASTER_FSLAVE); + data_ready.rise(&FUNC_INT); + printf("\nstarted master\n"); + while(1) + { //Thread::wait(9000); + //interrupt = 1; + + GETCOMMAND(); + + //interrupt = 0; + ; + } +} + + void FUNC_TC_ACS_DATAACQ() //103 //send this data to bae and execute there. Send ascii 1. Switch off control algo and rods -{ - //put data in thread +{ //data = 1 ; + //FUNC_WRITE2BAE(); + + //put data in //signal slave }