![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
i2c while removed working
Fork of CDMS_tc2 by
main.cpp@2:c75bb488d17c, 2014-12-20 (annotated)
- Committer:
- sakthipriya
- Date:
- Sat Dec 20 06:23:00 2014 +0000
- Revision:
- 2:c75bb488d17c
- Parent:
- 1:24c54d213671
- Child:
- 3:6c1ed31073f0
decode hk data
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sakthipriya | 0:252ce694a71e | 1 | #include "mbed.h" |
sakthipriya | 0:252ce694a71e | 2 | #include "rtos.h" |
sakthipriya | 0:252ce694a71e | 3 | |
sakthipriya | 0:252ce694a71e | 4 | const int addr = 0x20; //slave address |
sakthipriya | 0:252ce694a71e | 5 | I2C master (D14,D15); //configure pins p27,p28 as I2C master |
sakthipriya | 0:252ce694a71e | 6 | Serial pc (USBTX,USBRX); |
sakthipriya | 0:252ce694a71e | 7 | DigitalOut interrupt(D9); |
sakthipriya | 0:252ce694a71e | 8 | InterruptIn data_ready(D10); |
sakthipriya | 0:252ce694a71e | 9 | |
sakthipriya | 0:252ce694a71e | 10 | int reset; |
sakthipriya | 0:252ce694a71e | 11 | |
sakthipriya | 0:252ce694a71e | 12 | typedef struct |
sakthipriya | 0:252ce694a71e | 13 | { |
sakthipriya | 0:252ce694a71e | 14 | char data; // To avoid dynamic memory allocation |
sakthipriya | 0:252ce694a71e | 15 | int length; |
sakthipriya | 0:252ce694a71e | 16 | }i2c_data; |
sakthipriya | 0:252ce694a71e | 17 | |
sakthipriya | 0:252ce694a71e | 18 | //Mail<i2c_data,16> i2c_data_receive; |
sakthipriya | 0:252ce694a71e | 19 | Mail<i2c_data,16> i2c_data_send; |
sakthipriya | 0:252ce694a71e | 20 | |
sakthipriya | 0:252ce694a71e | 21 | Thread * ptr_t_i2c; |
sakthipriya | 0:252ce694a71e | 22 | void FUNC_I2C_MASTER_FSLAVE(char * data,int length) |
sakthipriya | 0:252ce694a71e | 23 | { |
sakthipriya | 0:252ce694a71e | 24 | //printf("press backspace for master to start \n\r"); |
sakthipriya | 0:252ce694a71e | 25 | //while(1){ |
sakthipriya | 0:252ce694a71e | 26 | //interrupt = 0; |
sakthipriya | 0:252ce694a71e | 27 | // if(pc.getc()=='z'){ |
sakthipriya | 1:24c54d213671 | 28 | //wait(0.5); |
sakthipriya | 0:252ce694a71e | 29 | interrupt = 1; |
sakthipriya | 0:252ce694a71e | 30 | printf("\nMaster executed\n"); |
sakthipriya | 0:252ce694a71e | 31 | |
sakthipriya | 0:252ce694a71e | 32 | |
sakthipriya | 0:252ce694a71e | 33 | bool ack0 =true; |
sakthipriya | 0:252ce694a71e | 34 | bool loopvariable0 =true; |
sakthipriya | 0:252ce694a71e | 35 | //char *data = new char; |
sakthipriya | 0:252ce694a71e | 36 | //*data = 'b'; |
sakthipriya | 0:252ce694a71e | 37 | |
sakthipriya | 0:252ce694a71e | 38 | |
sakthipriya | 0:252ce694a71e | 39 | |
sakthipriya | 0:252ce694a71e | 40 | master.frequency(100000); //set clock frequency |
sakthipriya | 0:252ce694a71e | 41 | //master.start(); |
sakthipriya | 0:252ce694a71e | 42 | /*while(write2slave) |
sakthipriya | 0:252ce694a71e | 43 | { *writedata = pc.getc(); |
sakthipriya | 0:252ce694a71e | 44 | wait(0.5); |
sakthipriya | 0:252ce694a71e | 45 | |
sakthipriya | 0:252ce694a71e | 46 | printf("master clk freq setup and addressing slave\n\r"); |
sakthipriya | 0:252ce694a71e | 47 | master.start(); //initiating the data transfer |
sakthipriya | 0:252ce694a71e | 48 | master_status_write = (bool) master.write(addr|0x00,writedata,1); |
sakthipriya | 0:252ce694a71e | 49 | if(master_status_write==0) |
sakthipriya | 0:252ce694a71e | 50 | { |
sakthipriya | 0:252ce694a71e | 51 | printf("master has written %c to slave\n\r",*writedata); |
sakthipriya | 0:252ce694a71e | 52 | write2slave=false; |
sakthipriya | 0:252ce694a71e | 53 | } |
sakthipriya | 0:252ce694a71e | 54 | |
sakthipriya | 0:252ce694a71e | 55 | |
sakthipriya | 0:252ce694a71e | 56 | }*/ |
sakthipriya | 0:252ce694a71e | 57 | |
sakthipriya | 1:24c54d213671 | 58 | while(ack0 && data_ready ==1 ) |
sakthipriya | 0:252ce694a71e | 59 | { |
sakthipriya | 0:252ce694a71e | 60 | //printf("2 master clk freq setup and addressing slave\n\r"); |
sakthipriya | 0:252ce694a71e | 61 | |
sakthipriya | 0:252ce694a71e | 62 | master.start(); //initiating the data transfer |
sakthipriya | 0:252ce694a71e | 63 | ack0 = master.read(addr,data,length); |
sakthipriya | 0:252ce694a71e | 64 | } |
sakthipriya | 0:252ce694a71e | 65 | if(!ack0) |
sakthipriya | 0:252ce694a71e | 66 | { |
sakthipriya | 0:252ce694a71e | 67 | printf("\n master has read %s from slave\n\r",data); |
sakthipriya | 0:252ce694a71e | 68 | loopvariable0=false; |
sakthipriya | 0:252ce694a71e | 69 | } |
sakthipriya | 0:252ce694a71e | 70 | //master.stop(); |
sakthipriya | 0:252ce694a71e | 71 | |
sakthipriya | 0:252ce694a71e | 72 | //printf("done\n\r"); |
sakthipriya | 0:252ce694a71e | 73 | //delete data; |
sakthipriya | 0:252ce694a71e | 74 | interrupt=0; |
sakthipriya | 1:24c54d213671 | 75 | printf("\n blahblah\n\r"); |
sakthipriya | 0:252ce694a71e | 76 | } |
sakthipriya | 2:c75bb488d17c | 77 | |
sakthipriya | 2:c75bb488d17c | 78 | typedef struct { |
sakthipriya | 2:c75bb488d17c | 79 | char Voltage[9]; |
sakthipriya | 2:c75bb488d17c | 80 | char Current[5]; |
sakthipriya | 2:c75bb488d17c | 81 | char Temperature[2]; |
sakthipriya | 2:c75bb488d17c | 82 | char PanelTemperature[3];//read by the 4 thermistors on solar panels |
sakthipriya | 2:c75bb488d17c | 83 | char BatteryTemperature; //to be populated |
sakthipriya | 2:c75bb488d17c | 84 | char faultpoll; //polled faults |
sakthipriya | 2:c75bb488d17c | 85 | char faultir; //interrupted faults |
sakthipriya | 2:c75bb488d17c | 86 | char power_mode; //power modes |
sakthipriya | 2:c75bb488d17c | 87 | char AngularSpeed[3]; |
sakthipriya | 2:c75bb488d17c | 88 | char Bnewvalue[3]; |
sakthipriya | 2:c75bb488d17c | 89 | |
sakthipriya | 2:c75bb488d17c | 90 | //float magnetometer,gyro=>to be addes |
sakthipriya | 2:c75bb488d17c | 91 | } hk_data; |
sakthipriya | 2:c75bb488d17c | 92 | hk_data decode_data; |
sakthipriya | 2:c75bb488d17c | 93 | |
sakthipriya | 2:c75bb488d17c | 94 | void TC_DECODE(char *data_hk) //getting the structure back from hk data sent by bae |
sakthipriya | 2:c75bb488d17c | 95 | { |
sakthipriya | 2:c75bb488d17c | 96 | for(int i=0;i<=7;i++) |
sakthipriya | 2:c75bb488d17c | 97 | { |
sakthipriya | 2:c75bb488d17c | 98 | decode_data.Voltage[i] = data_hk[i]; |
sakthipriya | 2:c75bb488d17c | 99 | decode_data.Voltage[8] = '\0'; |
sakthipriya | 2:c75bb488d17c | 100 | } |
sakthipriya | 2:c75bb488d17c | 101 | for(int i=0;i<=3;i++) |
sakthipriya | 2:c75bb488d17c | 102 | { |
sakthipriya | 2:c75bb488d17c | 103 | decode_data.Current[i] = data_hk[8+i]; |
sakthipriya | 2:c75bb488d17c | 104 | decode_data.Current[4] = '\0'; |
sakthipriya | 2:c75bb488d17c | 105 | } |
sakthipriya | 2:c75bb488d17c | 106 | decode_data.Temperature[0] = data_hk[12]; |
sakthipriya | 2:c75bb488d17c | 107 | decode_data.Temperature[1] = '\0'; |
sakthipriya | 2:c75bb488d17c | 108 | for(int i=0;i<=1;i++) |
sakthipriya | 2:c75bb488d17c | 109 | { |
sakthipriya | 2:c75bb488d17c | 110 | decode_data.PanelTemperature[i] = data_hk[13+i]; |
sakthipriya | 2:c75bb488d17c | 111 | decode_data.PanelTemperature[2] = '\0'; |
sakthipriya | 2:c75bb488d17c | 112 | } |
sakthipriya | 2:c75bb488d17c | 113 | decode_data.BatteryTemperature = data_hk[15]; |
sakthipriya | 2:c75bb488d17c | 114 | decode_data.faultpoll = data_hk[16]; |
sakthipriya | 2:c75bb488d17c | 115 | decode_data.faultir = data_hk[17]; |
sakthipriya | 2:c75bb488d17c | 116 | decode_data.power_mode = data_hk[18]; |
sakthipriya | 2:c75bb488d17c | 117 | for(int i=0;i<=1;i++) |
sakthipriya | 2:c75bb488d17c | 118 | { |
sakthipriya | 2:c75bb488d17c | 119 | decode_data.AngularSpeed[i] = data_hk[19+i]; |
sakthipriya | 2:c75bb488d17c | 120 | decode_data.AngularSpeed[2] = '\0'; |
sakthipriya | 2:c75bb488d17c | 121 | } |
sakthipriya | 2:c75bb488d17c | 122 | for(int i=0;i<=1;i++) |
sakthipriya | 2:c75bb488d17c | 123 | { |
sakthipriya | 2:c75bb488d17c | 124 | decode_data.Bnewvalue[i] = data_hk[21+i]; |
sakthipriya | 2:c75bb488d17c | 125 | decode_data.Bnewvalue[2] = '\0'; |
sakthipriya | 2:c75bb488d17c | 126 | } |
sakthipriya | 2:c75bb488d17c | 127 | printf("\n voltage %s\n\r",decode_data.Voltage); |
sakthipriya | 2:c75bb488d17c | 128 | printf("\n current %s\n\r",decode_data.Current); |
sakthipriya | 2:c75bb488d17c | 129 | printf("\n faultpoll %c\n\r",decode_data.faultpoll); |
sakthipriya | 2:c75bb488d17c | 130 | } |
sakthipriya | 2:c75bb488d17c | 131 | |
sakthipriya | 0:252ce694a71e | 132 | |
sakthipriya | 0:252ce694a71e | 133 | void T_I2C_MASTER_FSLAVE(void const *args) |
sakthipriya | 0:252ce694a71e | 134 | { |
sakthipriya | 0:252ce694a71e | 135 | char data_receive[25]; |
sakthipriya | 0:252ce694a71e | 136 | while(1) |
sakthipriya | 0:252ce694a71e | 137 | { |
sakthipriya | 0:252ce694a71e | 138 | Thread::signal_wait(0x1); |
sakthipriya | 1:24c54d213671 | 139 | |
sakthipriya | 1:24c54d213671 | 140 | |
sakthipriya | 0:252ce694a71e | 141 | FUNC_I2C_MASTER_FSLAVE(data_receive,25); |
sakthipriya | 0:252ce694a71e | 142 | /*i2c_data * i2c_data_r = i2c_data_receive.alloc(); |
sakthipriya | 0:252ce694a71e | 143 | strcpy(i2c_data_r->data , data_receive); |
sakthipriya | 0:252ce694a71e | 144 | i2c_data_r->length = 25; |
sakthipriya | 0:252ce694a71e | 145 | i2c_data_receive.put(i2c_data_r);*/ |
sakthipriya | 0:252ce694a71e | 146 | printf("\n Data received from slave is %s\n\r",data_receive); |
sakthipriya | 2:c75bb488d17c | 147 | TC_DECODE(data_receive); |
sakthipriya | 0:252ce694a71e | 148 | } |
sakthipriya | 0:252ce694a71e | 149 | } |
sakthipriya | 0:252ce694a71e | 150 | |
sakthipriya | 0:252ce694a71e | 151 | void FUNC_INT() |
sakthipriya | 0:252ce694a71e | 152 | { |
sakthipriya | 0:252ce694a71e | 153 | |
sakthipriya | 0:252ce694a71e | 154 | ptr_t_i2c->signal_set(0x1); |
sakthipriya | 0:252ce694a71e | 155 | |
sakthipriya | 0:252ce694a71e | 156 | } |
sakthipriya | 0:252ce694a71e | 157 | |
sakthipriya | 0:252ce694a71e | 158 | char writedata; |
sakthipriya | 0:252ce694a71e | 159 | bool write2slave; |
sakthipriya | 0:252ce694a71e | 160 | bool master_status_write; |
sakthipriya | 0:252ce694a71e | 161 | void FUNC_MASTER_WRITE() |
sakthipriya | 0:252ce694a71e | 162 | { //wait(1); |
sakthipriya | 0:252ce694a71e | 163 | write2slave=true; |
sakthipriya | 0:252ce694a71e | 164 | |
sakthipriya | 0:252ce694a71e | 165 | |
sakthipriya | 0:252ce694a71e | 166 | |
sakthipriya | 0:252ce694a71e | 167 | char data = pc.getc(); |
sakthipriya | 0:252ce694a71e | 168 | interrupt = 1; |
sakthipriya | 0:252ce694a71e | 169 | i2c_data * i2c_data_s = i2c_data_send.alloc(); |
sakthipriya | 0:252ce694a71e | 170 | i2c_data_s->data = data; |
sakthipriya | 0:252ce694a71e | 171 | i2c_data_s->length = 1; |
sakthipriya | 0:252ce694a71e | 172 | i2c_data_send.put(i2c_data_s); |
sakthipriya | 0:252ce694a71e | 173 | master_status_write = true; |
sakthipriya | 0:252ce694a71e | 174 | |
sakthipriya | 0:252ce694a71e | 175 | // interrupt = 1; |
sakthipriya | 0:252ce694a71e | 176 | |
sakthipriya | 0:252ce694a71e | 177 | osEvent evt = i2c_data_send.get(); |
sakthipriya | 0:252ce694a71e | 178 | if (evt.status == osEventMail) |
sakthipriya | 0:252ce694a71e | 179 | { |
sakthipriya | 0:252ce694a71e | 180 | i2c_data *i2c_data_s = (i2c_data*)evt.value.p; |
sakthipriya | 0:252ce694a71e | 181 | writedata = i2c_data_s -> data; |
sakthipriya | 0:252ce694a71e | 182 | master.frequency(100000); //set clock frequency |
sakthipriya | 1:24c54d213671 | 183 | while(write2slave && interrupt ==1 ) |
sakthipriya | 0:252ce694a71e | 184 | { |
sakthipriya | 0:252ce694a71e | 185 | printf("master clk freq setup and addressing slave\n\r"); |
sakthipriya | 0:252ce694a71e | 186 | master.start(); //initiating the data transfer |
sakthipriya | 0:252ce694a71e | 187 | master_status_write = (bool) master.write(addr|0x00,&writedata,1); |
sakthipriya | 0:252ce694a71e | 188 | if(master_status_write==0) |
sakthipriya | 0:252ce694a71e | 189 | { |
sakthipriya | 0:252ce694a71e | 190 | printf("master has written %c to slave\n\r",writedata); |
sakthipriya | 0:252ce694a71e | 191 | write2slave=false; |
sakthipriya | 0:252ce694a71e | 192 | } |
sakthipriya | 0:252ce694a71e | 193 | i2c_data_send.free(i2c_data_s); |
sakthipriya | 0:252ce694a71e | 194 | } |
sakthipriya | 0:252ce694a71e | 195 | } |
sakthipriya | 0:252ce694a71e | 196 | interrupt = 0; |
sakthipriya | 0:252ce694a71e | 197 | } |
sakthipriya | 0:252ce694a71e | 198 | |
sakthipriya | 0:252ce694a71e | 199 | |
sakthipriya | 0:252ce694a71e | 200 | |
sakthipriya | 0:252ce694a71e | 201 | int main() |
sakthipriya | 0:252ce694a71e | 202 | { |
sakthipriya | 1:24c54d213671 | 203 | interrupt=0; |
sakthipriya | 0:252ce694a71e | 204 | ptr_t_i2c = new Thread(T_I2C_MASTER_FSLAVE); |
sakthipriya | 0:252ce694a71e | 205 | data_ready.rise(&FUNC_INT); |
sakthipriya | 0:252ce694a71e | 206 | printf("\nstarted master\n"); |
sakthipriya | 0:252ce694a71e | 207 | while(1) |
sakthipriya | 0:252ce694a71e | 208 | { //Thread::wait(9000); |
sakthipriya | 0:252ce694a71e | 209 | //interrupt = 1; |
sakthipriya | 0:252ce694a71e | 210 | |
sakthipriya | 0:252ce694a71e | 211 | FUNC_MASTER_WRITE(); |
sakthipriya | 0:252ce694a71e | 212 | //interrupt = 0; |
sakthipriya | 0:252ce694a71e | 213 | ; |
sakthipriya | 0:252ce694a71e | 214 | } |
sakthipriya | 0:252ce694a71e | 215 | } |