![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
i2c slave integrated
Fork of BAE_FRDM_INTEGRATION by
slave.cpp@9:a9de938283f9, 2014-12-15 (annotated)
- Committer:
- sakthipriya
- Date:
- Mon Dec 15 11:21:57 2014 +0000
- Revision:
- 9:a9de938283f9
- Parent:
- 8:667fbc82d634
i2c_slave integrated
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
greenroshks | 8:667fbc82d634 | 1 | /*******************Below is an old version**********************************************************************************************/ |
sakthipriya | 3:02b45de29c0f | 2 | /* Added fault to hk structure and being sent through I2C. Need to verify the format */ |
greenroshks | 8:667fbc82d634 | 3 | /*#include "slave.h" |
greenroshks | 0:8b0d43fe6c05 | 4 | #include "HK.h" |
greenroshks | 0:8b0d43fe6c05 | 5 | |
greenroshks | 0:8b0d43fe6c05 | 6 | extern struct SensorData Sensor; |
greenroshks | 1:b4bb78a9b92e | 7 | I2CSlave slave(PTC9,PTC8); //configuring pins p27, p28 as I2Cslave |
greenroshks | 0:8b0d43fe6c05 | 8 | Serial screen (USBTX,USBRX); |
greenroshks | 0:8b0d43fe6c05 | 9 | void write_to_master(char send) //function to write data to master |
greenroshks | 0:8b0d43fe6c05 | 10 | { |
greenroshks | 0:8b0d43fe6c05 | 11 | int acknowledge; |
greenroshks | 0:8b0d43fe6c05 | 12 | int loopvariable4=1; |
greenroshks | 0:8b0d43fe6c05 | 13 | while(loopvariable4) |
greenroshks | 0:8b0d43fe6c05 | 14 | { |
greenroshks | 0:8b0d43fe6c05 | 15 | acknowledge = slave.write(send); //sending the byte to master |
greenroshks | 0:8b0d43fe6c05 | 16 | if(acknowledge==1) |
greenroshks | 0:8b0d43fe6c05 | 17 | { |
greenroshks | 0:8b0d43fe6c05 | 18 | screen.printf(" acknowledge %d sent %u \n",acknowledge,send); |
greenroshks | 0:8b0d43fe6c05 | 19 | loopvariable4=0; |
greenroshks | 0:8b0d43fe6c05 | 20 | } |
greenroshks | 0:8b0d43fe6c05 | 21 | } |
greenroshks | 0:8b0d43fe6c05 | 22 | } |
greenroshks | 0:8b0d43fe6c05 | 23 | |
greenroshks | 0:8b0d43fe6c05 | 24 | |
greenroshks | 0:8b0d43fe6c05 | 25 | |
greenroshks | 0:8b0d43fe6c05 | 26 | |
greenroshks | 0:8b0d43fe6c05 | 27 | void FUNC_I2C_SLAVE_MAIN(int iterations) |
greenroshks | 0:8b0d43fe6c05 | 28 | { |
greenroshks | 0:8b0d43fe6c05 | 29 | |
greenroshks | 0:8b0d43fe6c05 | 30 | screen.printf("\nSlave entered\n"); |
greenroshks | 0:8b0d43fe6c05 | 31 | slave.address(slave_address); //assigning slave address |
greenroshks | 0:8b0d43fe6c05 | 32 | slave.stop(); |
greenroshks | 0:8b0d43fe6c05 | 33 | char Switch_Variable; |
greenroshks | 0:8b0d43fe6c05 | 34 | |
greenroshks | 0:8b0d43fe6c05 | 35 | |
greenroshks | 0:8b0d43fe6c05 | 36 | int loopvariable1=1; |
greenroshks | 0:8b0d43fe6c05 | 37 | int loopvariable2=0; |
greenroshks | 0:8b0d43fe6c05 | 38 | int loopvariable3=1; |
greenroshks | 0:8b0d43fe6c05 | 39 | //initialising dummy sensor data |
greenroshks | 0:8b0d43fe6c05 | 40 | |
greenroshks | 0:8b0d43fe6c05 | 41 | while(loopvariable1) |
greenroshks | 0:8b0d43fe6c05 | 42 | { |
greenroshks | 0:8b0d43fe6c05 | 43 | //to read data from master |
greenroshks | 0:8b0d43fe6c05 | 44 | if(slave.receive()==WriteGeneral) //checking if slave is addressed to write |
greenroshks | 0:8b0d43fe6c05 | 45 | { |
greenroshks | 1:b4bb78a9b92e | 46 | slave.stop(); |
greenroshks | 0:8b0d43fe6c05 | 47 | Switch_Variable=slave.read(); //receiving data |
greenroshks | 0:8b0d43fe6c05 | 48 | screen.printf("switch variable=%d\n",Switch_Variable); |
greenroshks | 0:8b0d43fe6c05 | 49 | slave.stop(); //reset slave to default receiving state |
greenroshks | 0:8b0d43fe6c05 | 50 | loopvariable1=0; |
greenroshks | 0:8b0d43fe6c05 | 51 | //to interpret and write data to master |
greenroshks | 0:8b0d43fe6c05 | 52 | switch(Switch_Variable) |
greenroshks | 0:8b0d43fe6c05 | 53 | { |
greenroshks | 0:8b0d43fe6c05 | 54 | |
greenroshks | 0:8b0d43fe6c05 | 55 | |
greenroshks | 0:8b0d43fe6c05 | 56 | case 1: printf("\nEntered switch\n"); |
greenroshks | 0:8b0d43fe6c05 | 57 | while(loopvariable3) |
greenroshks | 0:8b0d43fe6c05 | 58 | { |
greenroshks | 0:8b0d43fe6c05 | 59 | if(slave.receive()==ReadAddressed) //check if slave is addressed to read |
greenroshks | 0:8b0d43fe6c05 | 60 | { |
greenroshks | 0:8b0d43fe6c05 | 61 | while(loopvariable2<iterations) |
greenroshks | 0:8b0d43fe6c05 | 62 | { |
greenroshks | 0:8b0d43fe6c05 | 63 | if(loopvariable2%3==0) |
greenroshks | 0:8b0d43fe6c05 | 64 | { |
greenroshks | 0:8b0d43fe6c05 | 65 | screen.printf("\nvoltage%d\n",loopvariable2/3); |
greenroshks | 0:8b0d43fe6c05 | 66 | write_to_master(Sensor.Voltage[loopvariable2/3]); |
greenroshks | 0:8b0d43fe6c05 | 67 | } |
greenroshks | 0:8b0d43fe6c05 | 68 | else if(loopvariable2%3==1) |
greenroshks | 0:8b0d43fe6c05 | 69 | { |
greenroshks | 0:8b0d43fe6c05 | 70 | screen.printf("\ncurrent%d\n",loopvariable2/3); |
greenroshks | 0:8b0d43fe6c05 | 71 | write_to_master(Sensor.Current[loopvariable2/3]); |
greenroshks | 0:8b0d43fe6c05 | 72 | } |
greenroshks | 0:8b0d43fe6c05 | 73 | else if(loopvariable2%3==2) |
greenroshks | 0:8b0d43fe6c05 | 74 | { |
greenroshks | 0:8b0d43fe6c05 | 75 | screen.printf("\ntemp%d\n",loopvariable2/3); |
greenroshks | 0:8b0d43fe6c05 | 76 | write_to_master(Sensor.Temperature[loopvariable2/3]); |
greenroshks | 0:8b0d43fe6c05 | 77 | } |
greenroshks | 0:8b0d43fe6c05 | 78 | loopvariable2++; |
sakthipriya | 3:02b45de29c0f | 79 | } |
raizel_varun | 7:f06840d848e3 | 80 | screen.printf("\nfault %c\n",Sensor.faultpoll); |
raizel_varun | 7:f06840d848e3 | 81 | write_to_master(Sensor.faultpoll); |
raizel_varun | 7:f06840d848e3 | 82 | screen.printf("\nfault %c\n",Sensor.faultir); |
raizel_varun | 7:f06840d848e3 | 83 | write_to_master(Sensor.faultir); |
sakthipriya | 5:255b43e8e21a | 84 | screen.printf("\npower mode %c\n",Sensor.power_mode); |
sakthipriya | 5:255b43e8e21a | 85 | write_to_master(Sensor.power_mode); |
greenroshks | 0:8b0d43fe6c05 | 86 | printf("\nExited i2c while loop\n"); |
greenroshks | 0:8b0d43fe6c05 | 87 | slave.stop(); |
greenroshks | 0:8b0d43fe6c05 | 88 | loopvariable3=0; |
greenroshks | 0:8b0d43fe6c05 | 89 | }//if(read addressed) |
greenroshks | 0:8b0d43fe6c05 | 90 | |
greenroshks | 0:8b0d43fe6c05 | 91 | }//while(loopvariable3) |
greenroshks | 0:8b0d43fe6c05 | 92 | |
greenroshks | 0:8b0d43fe6c05 | 93 | break; |
greenroshks | 0:8b0d43fe6c05 | 94 | case 2 : screen.printf(" telecommand 2\n"); |
greenroshks | 0:8b0d43fe6c05 | 95 | break; |
greenroshks | 0:8b0d43fe6c05 | 96 | |
greenroshks | 0:8b0d43fe6c05 | 97 | }//switch case ends |
greenroshks | 0:8b0d43fe6c05 | 98 | } |
greenroshks | 0:8b0d43fe6c05 | 99 | } |
greenroshks | 0:8b0d43fe6c05 | 100 | screen.printf("\nexited slave function\n"); |
greenroshks | 0:8b0d43fe6c05 | 101 | } |
greenroshks | 8:667fbc82d634 | 102 | */ |
greenroshks | 8:667fbc82d634 | 103 | |
greenroshks | 8:667fbc82d634 | 104 | //******************The following is a new version********************************************************************************/ |
greenroshks | 8:667fbc82d634 | 105 | #include "HK.h" |
greenroshks | 8:667fbc82d634 | 106 | #include "slave.h" |
greenroshks | 8:667fbc82d634 | 107 | |
sakthipriya | 9:a9de938283f9 | 108 | #include "mbed.h" |
sakthipriya | 9:a9de938283f9 | 109 | #include "rtos.h" |
sakthipriya | 9:a9de938283f9 | 110 | #include "slave.h" |
greenroshks | 8:667fbc82d634 | 111 | |
sakthipriya | 9:a9de938283f9 | 112 | void write_to_master(char); //function to write data to master |
greenroshks | 8:667fbc82d634 | 113 | |
greenroshks | 8:667fbc82d634 | 114 | I2CSlave slave(D14,D15); //configuring pins p27, p28 as I2Cslave |
sakthipriya | 9:a9de938283f9 | 115 | |
sakthipriya | 9:a9de938283f9 | 116 | Serial pcslave(USBTX, USBRX); |
sakthipriya | 9:a9de938283f9 | 117 | |
sakthipriya | 9:a9de938283f9 | 118 | |
greenroshks | 8:667fbc82d634 | 119 | |
sakthipriya | 9:a9de938283f9 | 120 | //int temp; |
sakthipriya | 9:a9de938283f9 | 121 | |
sakthipriya | 9:a9de938283f9 | 122 | |
sakthipriya | 9:a9de938283f9 | 123 | void FUNC_I2C_WRITE2CDMS(char *data,int length) |
greenroshks | 8:667fbc82d634 | 124 | { |
sakthipriya | 9:a9de938283f9 | 125 | int slave_status = 1; |
greenroshks | 8:667fbc82d634 | 126 | |
sakthipriya | 9:a9de938283f9 | 127 | |
greenroshks | 8:667fbc82d634 | 128 | while(slave_status) |
greenroshks | 8:667fbc82d634 | 129 | { |
greenroshks | 8:667fbc82d634 | 130 | slave.address(0x20); |
greenroshks | 8:667fbc82d634 | 131 | if(slave.receive()==1) |
greenroshks | 8:667fbc82d634 | 132 | { |
greenroshks | 8:667fbc82d634 | 133 | slave_status=slave.write(data,length); |
sakthipriya | 9:a9de938283f9 | 134 | |
greenroshks | 8:667fbc82d634 | 135 | |
greenroshks | 8:667fbc82d634 | 136 | } |
sakthipriya | 9:a9de938283f9 | 137 | else if(slave.receive()==3 || slave.receive()==2) |
sakthipriya | 9:a9de938283f9 | 138 | { |
sakthipriya | 9:a9de938283f9 | 139 | slave_status=slave.read(data,length); |
sakthipriya | 9:a9de938283f9 | 140 | } |
sakthipriya | 9:a9de938283f9 | 141 | |
greenroshks | 8:667fbc82d634 | 142 | } |
sakthipriya | 9:a9de938283f9 | 143 | printf("\ndone\n\r"); |
greenroshks | 8:667fbc82d634 | 144 | |
greenroshks | 8:667fbc82d634 | 145 | } |
greenroshks | 8:667fbc82d634 | 146 | |
sakthipriya | 9:a9de938283f9 | 147 | |
sakthipriya | 9:a9de938283f9 | 148 | |
sakthipriya | 9:a9de938283f9 | 149 | |
sakthipriya | 9:a9de938283f9 | 150 | |
sakthipriya | 9:a9de938283f9 | 151 | |
sakthipriya | 9:a9de938283f9 | 152 | |
greenroshks | 8:667fbc82d634 | 153 | |
greenroshks | 8:667fbc82d634 | 154 | |
greenroshks | 8:667fbc82d634 | 155 | |
sakthipriya | 9:a9de938283f9 | 156 | |
greenroshks | 8:667fbc82d634 | 157 | |
sakthipriya | 9:a9de938283f9 | 158 |