sakthi priya amirtharaj
/
BAE_vr2_1_3
i2c working with old hk
Fork of BAE_vr2_1_1 by
slave.cpp@7:f06840d848e3, 2014-12-10 (annotated)
- Committer:
- raizel_varun
- Date:
- Wed Dec 10 06:34:17 2014 +0000
- Revision:
- 7:f06840d848e3
- Parent:
- 5:255b43e8e21a
- Child:
- 9:221d218f4690
f
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sakthipriya | 3:02b45de29c0f | 1 | /* Added fault to hk structure and being sent through I2C. Need to verify the format */ |
greenroshks | 0:8b0d43fe6c05 | 2 | #include "slave.h" |
greenroshks | 0:8b0d43fe6c05 | 3 | #include "HK.h" |
greenroshks | 0:8b0d43fe6c05 | 4 | |
greenroshks | 0:8b0d43fe6c05 | 5 | extern struct SensorData Sensor; |
greenroshks | 1:b4bb78a9b92e | 6 | I2CSlave slave(PTC9,PTC8); //configuring pins p27, p28 as I2Cslave |
greenroshks | 0:8b0d43fe6c05 | 7 | Serial screen (USBTX,USBRX); |
greenroshks | 0:8b0d43fe6c05 | 8 | void write_to_master(char send) //function to write data to master |
greenroshks | 0:8b0d43fe6c05 | 9 | { |
greenroshks | 0:8b0d43fe6c05 | 10 | int acknowledge; |
greenroshks | 0:8b0d43fe6c05 | 11 | int loopvariable4=1; |
greenroshks | 0:8b0d43fe6c05 | 12 | while(loopvariable4) |
greenroshks | 0:8b0d43fe6c05 | 13 | { |
greenroshks | 0:8b0d43fe6c05 | 14 | acknowledge = slave.write(send); //sending the byte to master |
greenroshks | 0:8b0d43fe6c05 | 15 | if(acknowledge==1) |
greenroshks | 0:8b0d43fe6c05 | 16 | { |
greenroshks | 0:8b0d43fe6c05 | 17 | screen.printf(" acknowledge %d sent %u \n",acknowledge,send); |
greenroshks | 0:8b0d43fe6c05 | 18 | loopvariable4=0; |
greenroshks | 0:8b0d43fe6c05 | 19 | } |
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 | void FUNC_I2C_SLAVE_MAIN(int iterations) |
greenroshks | 0:8b0d43fe6c05 | 27 | { |
greenroshks | 0:8b0d43fe6c05 | 28 | |
greenroshks | 0:8b0d43fe6c05 | 29 | screen.printf("\nSlave entered\n"); |
greenroshks | 0:8b0d43fe6c05 | 30 | slave.address(slave_address); //assigning slave address |
greenroshks | 0:8b0d43fe6c05 | 31 | slave.stop(); |
greenroshks | 0:8b0d43fe6c05 | 32 | char Switch_Variable; |
greenroshks | 0:8b0d43fe6c05 | 33 | |
greenroshks | 0:8b0d43fe6c05 | 34 | |
greenroshks | 0:8b0d43fe6c05 | 35 | int loopvariable1=1; |
greenroshks | 0:8b0d43fe6c05 | 36 | int loopvariable2=0; |
greenroshks | 0:8b0d43fe6c05 | 37 | int loopvariable3=1; |
greenroshks | 0:8b0d43fe6c05 | 38 | //initialising dummy sensor data |
greenroshks | 0:8b0d43fe6c05 | 39 | |
greenroshks | 0:8b0d43fe6c05 | 40 | while(loopvariable1) |
greenroshks | 0:8b0d43fe6c05 | 41 | { |
greenroshks | 0:8b0d43fe6c05 | 42 | //to read data from master |
greenroshks | 0:8b0d43fe6c05 | 43 | if(slave.receive()==WriteGeneral) //checking if slave is addressed to write |
greenroshks | 0:8b0d43fe6c05 | 44 | { |
greenroshks | 1:b4bb78a9b92e | 45 | slave.stop(); |
greenroshks | 0:8b0d43fe6c05 | 46 | Switch_Variable=slave.read(); //receiving data |
greenroshks | 0:8b0d43fe6c05 | 47 | screen.printf("switch variable=%d\n",Switch_Variable); |
greenroshks | 0:8b0d43fe6c05 | 48 | slave.stop(); //reset slave to default receiving state |
greenroshks | 0:8b0d43fe6c05 | 49 | loopvariable1=0; |
greenroshks | 0:8b0d43fe6c05 | 50 | //to interpret and write data to master |
greenroshks | 0:8b0d43fe6c05 | 51 | switch(Switch_Variable) |
greenroshks | 0:8b0d43fe6c05 | 52 | { |
greenroshks | 0:8b0d43fe6c05 | 53 | |
greenroshks | 0:8b0d43fe6c05 | 54 | |
greenroshks | 0:8b0d43fe6c05 | 55 | case 1: printf("\nEntered switch\n"); |
greenroshks | 0:8b0d43fe6c05 | 56 | while(loopvariable3) |
greenroshks | 0:8b0d43fe6c05 | 57 | { |
greenroshks | 0:8b0d43fe6c05 | 58 | if(slave.receive()==ReadAddressed) //check if slave is addressed to read |
greenroshks | 0:8b0d43fe6c05 | 59 | { |
greenroshks | 0:8b0d43fe6c05 | 60 | while(loopvariable2<iterations) |
greenroshks | 0:8b0d43fe6c05 | 61 | { |
greenroshks | 0:8b0d43fe6c05 | 62 | if(loopvariable2%3==0) |
greenroshks | 0:8b0d43fe6c05 | 63 | { |
greenroshks | 0:8b0d43fe6c05 | 64 | screen.printf("\nvoltage%d\n",loopvariable2/3); |
greenroshks | 0:8b0d43fe6c05 | 65 | write_to_master(Sensor.Voltage[loopvariable2/3]); |
greenroshks | 0:8b0d43fe6c05 | 66 | } |
greenroshks | 0:8b0d43fe6c05 | 67 | else if(loopvariable2%3==1) |
greenroshks | 0:8b0d43fe6c05 | 68 | { |
greenroshks | 0:8b0d43fe6c05 | 69 | screen.printf("\ncurrent%d\n",loopvariable2/3); |
greenroshks | 0:8b0d43fe6c05 | 70 | write_to_master(Sensor.Current[loopvariable2/3]); |
greenroshks | 0:8b0d43fe6c05 | 71 | } |
greenroshks | 0:8b0d43fe6c05 | 72 | else if(loopvariable2%3==2) |
greenroshks | 0:8b0d43fe6c05 | 73 | { |
greenroshks | 0:8b0d43fe6c05 | 74 | screen.printf("\ntemp%d\n",loopvariable2/3); |
greenroshks | 0:8b0d43fe6c05 | 75 | write_to_master(Sensor.Temperature[loopvariable2/3]); |
greenroshks | 0:8b0d43fe6c05 | 76 | } |
greenroshks | 0:8b0d43fe6c05 | 77 | loopvariable2++; |
sakthipriya | 3:02b45de29c0f | 78 | } |
raizel_varun | 7:f06840d848e3 | 79 | screen.printf("\nfault %c\n",Sensor.faultpoll); |
raizel_varun | 7:f06840d848e3 | 80 | write_to_master(Sensor.faultpoll); |
raizel_varun | 7:f06840d848e3 | 81 | screen.printf("\nfault %c\n",Sensor.faultir); |
raizel_varun | 7:f06840d848e3 | 82 | write_to_master(Sensor.faultir); |
sakthipriya | 5:255b43e8e21a | 83 | screen.printf("\npower mode %c\n",Sensor.power_mode); |
sakthipriya | 5:255b43e8e21a | 84 | write_to_master(Sensor.power_mode); |
greenroshks | 0:8b0d43fe6c05 | 85 | printf("\nExited i2c while loop\n"); |
greenroshks | 0:8b0d43fe6c05 | 86 | slave.stop(); |
greenroshks | 0:8b0d43fe6c05 | 87 | loopvariable3=0; |
greenroshks | 0:8b0d43fe6c05 | 88 | }//if(read addressed) |
greenroshks | 0:8b0d43fe6c05 | 89 | |
greenroshks | 0:8b0d43fe6c05 | 90 | }//while(loopvariable3) |
greenroshks | 0:8b0d43fe6c05 | 91 | |
greenroshks | 0:8b0d43fe6c05 | 92 | break; |
greenroshks | 0:8b0d43fe6c05 | 93 | case 2 : screen.printf(" telecommand 2\n"); |
greenroshks | 0:8b0d43fe6c05 | 94 | break; |
greenroshks | 0:8b0d43fe6c05 | 95 | |
greenroshks | 0:8b0d43fe6c05 | 96 | }//switch case ends |
greenroshks | 0:8b0d43fe6c05 | 97 | } |
greenroshks | 0:8b0d43fe6c05 | 98 | } |
greenroshks | 0:8b0d43fe6c05 | 99 | screen.printf("\nexited slave function\n"); |
greenroshks | 0:8b0d43fe6c05 | 100 | } |
greenroshks | 0:8b0d43fe6c05 | 101 |