![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
i2c while removed working
Fork of CDMS_tc2 by
main.cpp@3:6c1ed31073f0, 2015-01-31 (annotated)
- Committer:
- sakthipriya
- Date:
- Sat Jan 31 14:14:01 2015 +0000
- Revision:
- 3:6c1ed31073f0
- Parent:
- 2:c75bb488d17c
i2c while removed working
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 | 3:6c1ed31073f0 | 11 | Timer t; |
sakthipriya | 3:6c1ed31073f0 | 12 | Timer t1; |
sakthipriya | 3:6c1ed31073f0 | 13 | Timer t2; |
sakthipriya | 3:6c1ed31073f0 | 14 | Timer t3; |
sakthipriya | 0:252ce694a71e | 15 | |
sakthipriya | 0:252ce694a71e | 16 | typedef struct |
sakthipriya | 0:252ce694a71e | 17 | { |
sakthipriya | 0:252ce694a71e | 18 | char data; // To avoid dynamic memory allocation |
sakthipriya | 0:252ce694a71e | 19 | int length; |
sakthipriya | 0:252ce694a71e | 20 | }i2c_data; |
sakthipriya | 0:252ce694a71e | 21 | |
sakthipriya | 0:252ce694a71e | 22 | //Mail<i2c_data,16> i2c_data_receive; |
sakthipriya | 0:252ce694a71e | 23 | Mail<i2c_data,16> i2c_data_send; |
sakthipriya | 0:252ce694a71e | 24 | |
sakthipriya | 0:252ce694a71e | 25 | Thread * ptr_t_i2c; |
sakthipriya | 0:252ce694a71e | 26 | void FUNC_I2C_MASTER_FSLAVE(char * data,int length) |
sakthipriya | 0:252ce694a71e | 27 | { |
sakthipriya | 3:6c1ed31073f0 | 28 | |
sakthipriya | 0:252ce694a71e | 29 | |
sakthipriya | 0:252ce694a71e | 30 | bool ack0 =true; |
sakthipriya | 0:252ce694a71e | 31 | |
sakthipriya | 3:6c1ed31073f0 | 32 | interrupt = 1; |
sakthipriya | 3:6c1ed31073f0 | 33 | t1.start(); |
sakthipriya | 3:6c1ed31073f0 | 34 | //wait_ms(20); |
sakthipriya | 3:6c1ed31073f0 | 35 | ack0 = master.read(addr|1,data,length); |
sakthipriya | 3:6c1ed31073f0 | 36 | t1.stop(); |
sakthipriya | 3:6c1ed31073f0 | 37 | |
sakthipriya | 3:6c1ed31073f0 | 38 | |
sakthipriya | 0:252ce694a71e | 39 | if(!ack0) |
sakthipriya | 0:252ce694a71e | 40 | { |
sakthipriya | 0:252ce694a71e | 41 | printf("\n master has read %s from slave\n\r",data); |
sakthipriya | 3:6c1ed31073f0 | 42 | |
sakthipriya | 0:252ce694a71e | 43 | } |
sakthipriya | 0:252ce694a71e | 44 | //master.stop(); |
sakthipriya | 3:6c1ed31073f0 | 45 | printf("\n%d\n\r",t1.read_us()); |
sakthipriya | 3:6c1ed31073f0 | 46 | t1.reset(); |
sakthipriya | 0:252ce694a71e | 47 | |
sakthipriya | 0:252ce694a71e | 48 | interrupt=0; |
sakthipriya | 3:6c1ed31073f0 | 49 | |
sakthipriya | 0:252ce694a71e | 50 | } |
sakthipriya | 2:c75bb488d17c | 51 | |
sakthipriya | 2:c75bb488d17c | 52 | typedef struct { |
sakthipriya | 2:c75bb488d17c | 53 | char Voltage[9]; |
sakthipriya | 2:c75bb488d17c | 54 | char Current[5]; |
sakthipriya | 2:c75bb488d17c | 55 | char Temperature[2]; |
sakthipriya | 2:c75bb488d17c | 56 | char PanelTemperature[3];//read by the 4 thermistors on solar panels |
sakthipriya | 2:c75bb488d17c | 57 | char BatteryTemperature; //to be populated |
sakthipriya | 2:c75bb488d17c | 58 | char faultpoll; //polled faults |
sakthipriya | 2:c75bb488d17c | 59 | char faultir; //interrupted faults |
sakthipriya | 2:c75bb488d17c | 60 | char power_mode; //power modes |
sakthipriya | 2:c75bb488d17c | 61 | char AngularSpeed[3]; |
sakthipriya | 2:c75bb488d17c | 62 | char Bnewvalue[3]; |
sakthipriya | 2:c75bb488d17c | 63 | |
sakthipriya | 2:c75bb488d17c | 64 | //float magnetometer,gyro=>to be addes |
sakthipriya | 2:c75bb488d17c | 65 | } hk_data; |
sakthipriya | 2:c75bb488d17c | 66 | hk_data decode_data; |
sakthipriya | 2:c75bb488d17c | 67 | |
sakthipriya | 3:6c1ed31073f0 | 68 | /*void TC_DECODE(char *data_hk) //getting the structure back from hk data sent by bae |
sakthipriya | 2:c75bb488d17c | 69 | { |
sakthipriya | 2:c75bb488d17c | 70 | for(int i=0;i<=7;i++) |
sakthipriya | 2:c75bb488d17c | 71 | { |
sakthipriya | 2:c75bb488d17c | 72 | decode_data.Voltage[i] = data_hk[i]; |
sakthipriya | 2:c75bb488d17c | 73 | decode_data.Voltage[8] = '\0'; |
sakthipriya | 2:c75bb488d17c | 74 | } |
sakthipriya | 2:c75bb488d17c | 75 | for(int i=0;i<=3;i++) |
sakthipriya | 2:c75bb488d17c | 76 | { |
sakthipriya | 2:c75bb488d17c | 77 | decode_data.Current[i] = data_hk[8+i]; |
sakthipriya | 2:c75bb488d17c | 78 | decode_data.Current[4] = '\0'; |
sakthipriya | 2:c75bb488d17c | 79 | } |
sakthipriya | 2:c75bb488d17c | 80 | decode_data.Temperature[0] = data_hk[12]; |
sakthipriya | 2:c75bb488d17c | 81 | decode_data.Temperature[1] = '\0'; |
sakthipriya | 2:c75bb488d17c | 82 | for(int i=0;i<=1;i++) |
sakthipriya | 2:c75bb488d17c | 83 | { |
sakthipriya | 2:c75bb488d17c | 84 | decode_data.PanelTemperature[i] = data_hk[13+i]; |
sakthipriya | 2:c75bb488d17c | 85 | decode_data.PanelTemperature[2] = '\0'; |
sakthipriya | 2:c75bb488d17c | 86 | } |
sakthipriya | 2:c75bb488d17c | 87 | decode_data.BatteryTemperature = data_hk[15]; |
sakthipriya | 2:c75bb488d17c | 88 | decode_data.faultpoll = data_hk[16]; |
sakthipriya | 2:c75bb488d17c | 89 | decode_data.faultir = data_hk[17]; |
sakthipriya | 2:c75bb488d17c | 90 | decode_data.power_mode = data_hk[18]; |
sakthipriya | 2:c75bb488d17c | 91 | for(int i=0;i<=1;i++) |
sakthipriya | 2:c75bb488d17c | 92 | { |
sakthipriya | 2:c75bb488d17c | 93 | decode_data.AngularSpeed[i] = data_hk[19+i]; |
sakthipriya | 2:c75bb488d17c | 94 | decode_data.AngularSpeed[2] = '\0'; |
sakthipriya | 2:c75bb488d17c | 95 | } |
sakthipriya | 2:c75bb488d17c | 96 | for(int i=0;i<=1;i++) |
sakthipriya | 2:c75bb488d17c | 97 | { |
sakthipriya | 2:c75bb488d17c | 98 | decode_data.Bnewvalue[i] = data_hk[21+i]; |
sakthipriya | 2:c75bb488d17c | 99 | decode_data.Bnewvalue[2] = '\0'; |
sakthipriya | 2:c75bb488d17c | 100 | } |
sakthipriya | 2:c75bb488d17c | 101 | printf("\n voltage %s\n\r",decode_data.Voltage); |
sakthipriya | 2:c75bb488d17c | 102 | printf("\n current %s\n\r",decode_data.Current); |
sakthipriya | 2:c75bb488d17c | 103 | printf("\n faultpoll %c\n\r",decode_data.faultpoll); |
sakthipriya | 3:6c1ed31073f0 | 104 | }*/ |
sakthipriya | 2:c75bb488d17c | 105 | |
sakthipriya | 0:252ce694a71e | 106 | |
sakthipriya | 0:252ce694a71e | 107 | void T_I2C_MASTER_FSLAVE(void const *args) |
sakthipriya | 0:252ce694a71e | 108 | { |
sakthipriya | 0:252ce694a71e | 109 | char data_receive[25]; |
sakthipriya | 0:252ce694a71e | 110 | while(1) |
sakthipriya | 0:252ce694a71e | 111 | { |
sakthipriya | 0:252ce694a71e | 112 | Thread::signal_wait(0x1); |
sakthipriya | 1:24c54d213671 | 113 | |
sakthipriya | 1:24c54d213671 | 114 | |
sakthipriya | 0:252ce694a71e | 115 | FUNC_I2C_MASTER_FSLAVE(data_receive,25); |
sakthipriya | 0:252ce694a71e | 116 | /*i2c_data * i2c_data_r = i2c_data_receive.alloc(); |
sakthipriya | 0:252ce694a71e | 117 | strcpy(i2c_data_r->data , data_receive); |
sakthipriya | 0:252ce694a71e | 118 | i2c_data_r->length = 25; |
sakthipriya | 0:252ce694a71e | 119 | i2c_data_receive.put(i2c_data_r);*/ |
sakthipriya | 0:252ce694a71e | 120 | printf("\n Data received from slave is %s\n\r",data_receive); |
sakthipriya | 3:6c1ed31073f0 | 121 | //TC_DECODE(data_receive); |
sakthipriya | 0:252ce694a71e | 122 | } |
sakthipriya | 0:252ce694a71e | 123 | } |
sakthipriya | 0:252ce694a71e | 124 | |
sakthipriya | 0:252ce694a71e | 125 | void FUNC_INT() |
sakthipriya | 0:252ce694a71e | 126 | { |
sakthipriya | 0:252ce694a71e | 127 | |
sakthipriya | 0:252ce694a71e | 128 | ptr_t_i2c->signal_set(0x1); |
sakthipriya | 0:252ce694a71e | 129 | |
sakthipriya | 0:252ce694a71e | 130 | } |
sakthipriya | 0:252ce694a71e | 131 | |
sakthipriya | 0:252ce694a71e | 132 | char writedata; |
sakthipriya | 0:252ce694a71e | 133 | bool write2slave; |
sakthipriya | 0:252ce694a71e | 134 | bool master_status_write; |
sakthipriya | 0:252ce694a71e | 135 | void FUNC_MASTER_WRITE() |
sakthipriya | 0:252ce694a71e | 136 | { //wait(1); |
sakthipriya | 0:252ce694a71e | 137 | write2slave=true; |
sakthipriya | 0:252ce694a71e | 138 | |
sakthipriya | 0:252ce694a71e | 139 | |
sakthipriya | 0:252ce694a71e | 140 | |
sakthipriya | 0:252ce694a71e | 141 | char data = pc.getc(); |
sakthipriya | 0:252ce694a71e | 142 | interrupt = 1; |
sakthipriya | 3:6c1ed31073f0 | 143 | t.start(); |
sakthipriya | 3:6c1ed31073f0 | 144 | t3.start(); |
sakthipriya | 3:6c1ed31073f0 | 145 | wait_ms(20); |
sakthipriya | 0:252ce694a71e | 146 | i2c_data * i2c_data_s = i2c_data_send.alloc(); |
sakthipriya | 0:252ce694a71e | 147 | i2c_data_s->data = data; |
sakthipriya | 0:252ce694a71e | 148 | i2c_data_s->length = 1; |
sakthipriya | 0:252ce694a71e | 149 | i2c_data_send.put(i2c_data_s); |
sakthipriya | 0:252ce694a71e | 150 | master_status_write = true; |
sakthipriya | 0:252ce694a71e | 151 | |
sakthipriya | 0:252ce694a71e | 152 | // interrupt = 1; |
sakthipriya | 0:252ce694a71e | 153 | |
sakthipriya | 0:252ce694a71e | 154 | osEvent evt = i2c_data_send.get(); |
sakthipriya | 0:252ce694a71e | 155 | if (evt.status == osEventMail) |
sakthipriya | 0:252ce694a71e | 156 | { |
sakthipriya | 0:252ce694a71e | 157 | i2c_data *i2c_data_s = (i2c_data*)evt.value.p; |
sakthipriya | 0:252ce694a71e | 158 | writedata = i2c_data_s -> data; |
sakthipriya | 3:6c1ed31073f0 | 159 | t.stop(); |
sakthipriya | 3:6c1ed31073f0 | 160 | //t3.start(); |
sakthipriya | 0:252ce694a71e | 161 | master_status_write = (bool) master.write(addr|0x00,&writedata,1); |
sakthipriya | 3:6c1ed31073f0 | 162 | t3.stop(); |
sakthipriya | 0:252ce694a71e | 163 | if(master_status_write==0) |
sakthipriya | 0:252ce694a71e | 164 | { |
sakthipriya | 0:252ce694a71e | 165 | printf("master has written %c to slave\n\r",writedata); |
sakthipriya | 0:252ce694a71e | 166 | write2slave=false; |
sakthipriya | 0:252ce694a71e | 167 | } |
sakthipriya | 0:252ce694a71e | 168 | i2c_data_send.free(i2c_data_s); |
sakthipriya | 3:6c1ed31073f0 | 169 | printf("\n%d\n",t.read_us()); |
sakthipriya | 3:6c1ed31073f0 | 170 | t.reset(); |
sakthipriya | 3:6c1ed31073f0 | 171 | printf("\n%d\n",t3.read_us()); |
sakthipriya | 3:6c1ed31073f0 | 172 | t3.reset(); |
sakthipriya | 0:252ce694a71e | 173 | } |
sakthipriya | 3:6c1ed31073f0 | 174 | interrupt = 0; |
sakthipriya | 0:252ce694a71e | 175 | } |
sakthipriya | 0:252ce694a71e | 176 | |
sakthipriya | 0:252ce694a71e | 177 | |
sakthipriya | 0:252ce694a71e | 178 | |
sakthipriya | 0:252ce694a71e | 179 | int main() |
sakthipriya | 0:252ce694a71e | 180 | { |
sakthipriya | 1:24c54d213671 | 181 | interrupt=0; |
sakthipriya | 0:252ce694a71e | 182 | ptr_t_i2c = new Thread(T_I2C_MASTER_FSLAVE); |
sakthipriya | 0:252ce694a71e | 183 | data_ready.rise(&FUNC_INT); |
sakthipriya | 0:252ce694a71e | 184 | printf("\nstarted master\n"); |
sakthipriya | 3:6c1ed31073f0 | 185 | master.frequency(100000); |
sakthipriya | 0:252ce694a71e | 186 | while(1) |
sakthipriya | 0:252ce694a71e | 187 | { //Thread::wait(9000); |
sakthipriya | 0:252ce694a71e | 188 | //interrupt = 1; |
sakthipriya | 0:252ce694a71e | 189 | |
sakthipriya | 0:252ce694a71e | 190 | FUNC_MASTER_WRITE(); |
sakthipriya | 0:252ce694a71e | 191 | //interrupt = 0; |
sakthipriya | 0:252ce694a71e | 192 | ; |
sakthipriya | 0:252ce694a71e | 193 | } |
sakthipriya | 0:252ce694a71e | 194 | } |