![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
cdms b4 check
Dependencies: SDFileSystem mbed-rtos mbed
Fork of CDMS_BAE_1 by
i2c.cpp@10:f9d11e047bb1, 2015-03-11 (annotated)
- Committer:
- sakthipriya
- Date:
- Wed Mar 11 15:58:24 2015 +0000
- Revision:
- 10:f9d11e047bb1
- Parent:
- 8:4acac3bf5e9b
cdms b4 check
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sakthipriya | 7:60e940b7a36b | 1 | #include "mbed.h" |
sakthipriya | 7:60e940b7a36b | 2 | #include "rtos.h" |
sakthipriya | 7:60e940b7a36b | 3 | #include "i2c.h" |
sakthipriya | 7:60e940b7a36b | 4 | DigitalOut interrupt(D9); |
sakthipriya | 7:60e940b7a36b | 5 | const int addr = 0x20; //slave address |
sakthipriya | 7:60e940b7a36b | 6 | I2C master (D14,D15); //configure pins p27,p28 as I2C master |
sakthipriya | 7:60e940b7a36b | 7 | Serial pc (USBTX,USBRX); |
sakthipriya | 7:60e940b7a36b | 8 | //DigitalOut interrupt(D9); |
sakthipriya | 7:60e940b7a36b | 9 | //InterruptIn data_ready(D10); |
sakthipriya | 7:60e940b7a36b | 10 | |
sakthipriya | 7:60e940b7a36b | 11 | int reset; |
sakthipriya | 7:60e940b7a36b | 12 | Timer t; |
sakthipriya | 7:60e940b7a36b | 13 | Timer t1; |
sakthipriya | 7:60e940b7a36b | 14 | Timer t2; |
sakthipriya | 7:60e940b7a36b | 15 | Timer t3; |
sakthipriya | 7:60e940b7a36b | 16 | |
sakthipriya | 7:60e940b7a36b | 17 | typedef struct |
sakthipriya | 7:60e940b7a36b | 18 | { |
sakthipriya | 7:60e940b7a36b | 19 | char data; // To avoid dynamic memory allocation |
sakthipriya | 7:60e940b7a36b | 20 | int length; |
sakthipriya | 7:60e940b7a36b | 21 | }i2c_data; |
sakthipriya | 7:60e940b7a36b | 22 | |
sakthipriya | 7:60e940b7a36b | 23 | //Mail<i2c_data,16> i2c_data_receive; |
sakthipriya | 7:60e940b7a36b | 24 | Mail<i2c_data,16> i2c_data_send; |
sakthipriya | 7:60e940b7a36b | 25 | |
sakthipriya | 7:60e940b7a36b | 26 | //Thread * ptr_t_i2c; |
sakthipriya | 7:60e940b7a36b | 27 | void FUNC_I2C_MASTER_FSLAVE(char * data,int length) |
sakthipriya | 7:60e940b7a36b | 28 | { |
sakthipriya | 7:60e940b7a36b | 29 | |
sakthipriya | 7:60e940b7a36b | 30 | |
sakthipriya | 7:60e940b7a36b | 31 | bool ack0 =true; |
sakthipriya | 7:60e940b7a36b | 32 | |
sakthipriya | 7:60e940b7a36b | 33 | interrupt = 1; |
sakthipriya | 7:60e940b7a36b | 34 | t1.start(); |
sakthipriya | 7:60e940b7a36b | 35 | //wait_ms(20); |
sakthipriya | 7:60e940b7a36b | 36 | ack0 = master.read(addr|1,data,length); |
sakthipriya | 7:60e940b7a36b | 37 | t1.stop(); |
sakthipriya | 7:60e940b7a36b | 38 | |
sakthipriya | 7:60e940b7a36b | 39 | |
sakthipriya | 7:60e940b7a36b | 40 | if(!ack0) |
sakthipriya | 7:60e940b7a36b | 41 | { |
sakthipriya | 7:60e940b7a36b | 42 | printf("\n master has read %s from slave\n\r",data); |
sakthipriya | 7:60e940b7a36b | 43 | |
sakthipriya | 7:60e940b7a36b | 44 | } |
sakthipriya | 7:60e940b7a36b | 45 | //master.stop(); |
sakthipriya | 7:60e940b7a36b | 46 | |
sakthipriya | 7:60e940b7a36b | 47 | printf("\n%d\n\r",t1.read_us()); |
sakthipriya | 7:60e940b7a36b | 48 | t1.reset(); |
sakthipriya | 7:60e940b7a36b | 49 | storedata(data); |
sakthipriya | 7:60e940b7a36b | 50 | interrupt=0; |
sakthipriya | 7:60e940b7a36b | 51 | |
sakthipriya | 7:60e940b7a36b | 52 | } |
sakthipriya | 7:60e940b7a36b | 53 | |
sakthipriya | 7:60e940b7a36b | 54 | typedef struct { |
sakthipriya | 7:60e940b7a36b | 55 | char Voltage[9]; |
sakthipriya | 7:60e940b7a36b | 56 | char Current[5]; |
sakthipriya | 7:60e940b7a36b | 57 | char Temperature[2]; |
sakthipriya | 7:60e940b7a36b | 58 | char PanelTemperature[3];//read by the 4 thermistors on solar panels |
sakthipriya | 7:60e940b7a36b | 59 | char BatteryTemperature; //to be populated |
sakthipriya | 7:60e940b7a36b | 60 | char faultpoll; //polled faults |
sakthipriya | 7:60e940b7a36b | 61 | char faultir; //interrupted faults |
sakthipriya | 7:60e940b7a36b | 62 | char power_mode; //power modes |
sakthipriya | 7:60e940b7a36b | 63 | char AngularSpeed[3]; |
sakthipriya | 7:60e940b7a36b | 64 | char Bnewvalue[3]; |
sakthipriya | 7:60e940b7a36b | 65 | |
sakthipriya | 7:60e940b7a36b | 66 | //float magnetometer,gyro=>to be addes |
sakthipriya | 7:60e940b7a36b | 67 | } hk_data; |
sakthipriya | 7:60e940b7a36b | 68 | hk_data decode_data; |
sakthipriya | 7:60e940b7a36b | 69 | |
sakthipriya | 8:4acac3bf5e9b | 70 | void TC_DECODE(char *data_hk) //getting the structure back from hk data sent by bae |
sakthipriya | 7:60e940b7a36b | 71 | { |
sakthipriya | 7:60e940b7a36b | 72 | for(int i=0;i<=7;i++) |
sakthipriya | 7:60e940b7a36b | 73 | { |
sakthipriya | 7:60e940b7a36b | 74 | decode_data.Voltage[i] = data_hk[i]; |
sakthipriya | 7:60e940b7a36b | 75 | decode_data.Voltage[8] = '\0'; |
sakthipriya | 7:60e940b7a36b | 76 | } |
sakthipriya | 7:60e940b7a36b | 77 | for(int i=0;i<=3;i++) |
sakthipriya | 7:60e940b7a36b | 78 | { |
sakthipriya | 7:60e940b7a36b | 79 | decode_data.Current[i] = data_hk[8+i]; |
sakthipriya | 7:60e940b7a36b | 80 | decode_data.Current[4] = '\0'; |
sakthipriya | 7:60e940b7a36b | 81 | } |
sakthipriya | 7:60e940b7a36b | 82 | decode_data.Temperature[0] = data_hk[12]; |
sakthipriya | 7:60e940b7a36b | 83 | decode_data.Temperature[1] = '\0'; |
sakthipriya | 7:60e940b7a36b | 84 | for(int i=0;i<=1;i++) |
sakthipriya | 7:60e940b7a36b | 85 | { |
sakthipriya | 7:60e940b7a36b | 86 | decode_data.PanelTemperature[i] = data_hk[13+i]; |
sakthipriya | 7:60e940b7a36b | 87 | decode_data.PanelTemperature[2] = '\0'; |
sakthipriya | 7:60e940b7a36b | 88 | } |
sakthipriya | 7:60e940b7a36b | 89 | decode_data.BatteryTemperature = data_hk[15]; |
sakthipriya | 7:60e940b7a36b | 90 | decode_data.faultpoll = data_hk[16]; |
sakthipriya | 7:60e940b7a36b | 91 | decode_data.faultir = data_hk[17]; |
sakthipriya | 7:60e940b7a36b | 92 | decode_data.power_mode = data_hk[18]; |
sakthipriya | 7:60e940b7a36b | 93 | for(int i=0;i<=1;i++) |
sakthipriya | 7:60e940b7a36b | 94 | { |
sakthipriya | 7:60e940b7a36b | 95 | decode_data.AngularSpeed[i] = data_hk[19+i]; |
sakthipriya | 7:60e940b7a36b | 96 | decode_data.AngularSpeed[2] = '\0'; |
sakthipriya | 7:60e940b7a36b | 97 | } |
sakthipriya | 7:60e940b7a36b | 98 | for(int i=0;i<=1;i++) |
sakthipriya | 7:60e940b7a36b | 99 | { |
sakthipriya | 7:60e940b7a36b | 100 | decode_data.Bnewvalue[i] = data_hk[21+i]; |
sakthipriya | 7:60e940b7a36b | 101 | decode_data.Bnewvalue[2] = '\0'; |
sakthipriya | 7:60e940b7a36b | 102 | } |
sakthipriya | 7:60e940b7a36b | 103 | printf("\n voltage %s\n\r",decode_data.Voltage); |
sakthipriya | 7:60e940b7a36b | 104 | printf("\n current %s\n\r",decode_data.Current); |
sakthipriya | 7:60e940b7a36b | 105 | printf("\n faultpoll %c\n\r",decode_data.faultpoll); |
sakthipriya | 8:4acac3bf5e9b | 106 | } |
sakthipriya | 7:60e940b7a36b | 107 | |
sakthipriya | 7:60e940b7a36b | 108 | |
sakthipriya | 7:60e940b7a36b | 109 | |
sakthipriya | 7:60e940b7a36b | 110 | char writedata; |
sakthipriya | 7:60e940b7a36b | 111 | bool write2slave; |
sakthipriya | 7:60e940b7a36b | 112 | bool master_status_write; |
sakthipriya | 7:60e940b7a36b | 113 | void FUNC_MASTER_WRITE() |
sakthipriya | 7:60e940b7a36b | 114 | { //wait(1); |
sakthipriya | 7:60e940b7a36b | 115 | write2slave=true; |
sakthipriya | 7:60e940b7a36b | 116 | |
sakthipriya | 7:60e940b7a36b | 117 | |
sakthipriya | 7:60e940b7a36b | 118 | |
sakthipriya | 7:60e940b7a36b | 119 | char data = pc.getc(); |
sakthipriya | 7:60e940b7a36b | 120 | interrupt = 1; |
sakthipriya | 7:60e940b7a36b | 121 | t.start(); |
sakthipriya | 7:60e940b7a36b | 122 | t3.start(); |
sakthipriya | 7:60e940b7a36b | 123 | wait_ms(20); |
sakthipriya | 7:60e940b7a36b | 124 | i2c_data * i2c_data_s = i2c_data_send.alloc(); |
sakthipriya | 7:60e940b7a36b | 125 | i2c_data_s->data = data; |
sakthipriya | 7:60e940b7a36b | 126 | i2c_data_s->length = 1; |
sakthipriya | 7:60e940b7a36b | 127 | i2c_data_send.put(i2c_data_s); |
sakthipriya | 7:60e940b7a36b | 128 | master_status_write = true; |
sakthipriya | 7:60e940b7a36b | 129 | |
sakthipriya | 7:60e940b7a36b | 130 | // interrupt = 1; |
sakthipriya | 7:60e940b7a36b | 131 | |
sakthipriya | 7:60e940b7a36b | 132 | osEvent evt = i2c_data_send.get(); |
sakthipriya | 7:60e940b7a36b | 133 | if (evt.status == osEventMail) |
sakthipriya | 7:60e940b7a36b | 134 | { |
sakthipriya | 7:60e940b7a36b | 135 | i2c_data *i2c_data_s = (i2c_data*)evt.value.p; |
sakthipriya | 7:60e940b7a36b | 136 | writedata = i2c_data_s -> data; |
sakthipriya | 7:60e940b7a36b | 137 | t.stop(); |
sakthipriya | 7:60e940b7a36b | 138 | //t3.start(); |
sakthipriya | 7:60e940b7a36b | 139 | master_status_write = (bool) master.write(addr|0x00,&writedata,1); |
sakthipriya | 7:60e940b7a36b | 140 | t3.stop(); |
sakthipriya | 7:60e940b7a36b | 141 | if(master_status_write==0) |
sakthipriya | 7:60e940b7a36b | 142 | { |
sakthipriya | 7:60e940b7a36b | 143 | printf("master has written %c to slave\n\r",writedata); |
sakthipriya | 7:60e940b7a36b | 144 | write2slave=false; |
sakthipriya | 7:60e940b7a36b | 145 | } |
sakthipriya | 7:60e940b7a36b | 146 | i2c_data_send.free(i2c_data_s); |
sakthipriya | 7:60e940b7a36b | 147 | printf("\n%d\n",t.read_us()); |
sakthipriya | 7:60e940b7a36b | 148 | t.reset(); |
sakthipriya | 7:60e940b7a36b | 149 | printf("\n%d\n",t3.read_us()); |
sakthipriya | 7:60e940b7a36b | 150 | t3.reset(); |
sakthipriya | 7:60e940b7a36b | 151 | } |
sakthipriya | 7:60e940b7a36b | 152 | interrupt = 0; |
sakthipriya | 7:60e940b7a36b | 153 | } |
sakthipriya | 7:60e940b7a36b | 154 | |
sakthipriya | 7:60e940b7a36b | 155 | |
sakthipriya | 7:60e940b7a36b | 156 | |
sakthipriya | 7:60e940b7a36b | 157 |