Gabrielle Martin-Fortier / Mbed 2 deprecated Tensiometer_Simulator_waterbench

Dependencies:   mbed

Committer:
eboily1
Date:
Wed Oct 31 14:52:28 2018 +0000
Revision:
1:85afd4bd4651
Parent:
0:265fff2cfb0a
Child:
2:d0308b3aaf69
change on RX for the EEPROM, ADC I2C not working

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Blanglois 0:265fff2cfb0a 1 #include "mbed.h"
Blanglois 0:265fff2cfb0a 2 #include "I2CSlaveComm.h"
Blanglois 0:265fff2cfb0a 3
Blanglois 0:265fff2cfb0a 4 I2CSlaveCustom slave(D14, D15);
Blanglois 0:265fff2cfb0a 5 I2CSlaveCustom slave2(D2, D8); //use another I2C to emulate the adc
Blanglois 0:265fff2cfb0a 6
Blanglois 0:265fff2cfb0a 7 unsigned char PointOnAddress = 0;
Blanglois 0:265fff2cfb0a 8 unsigned short ReadADCValue = 0;
Blanglois 0:265fff2cfb0a 9 #pragma pack(push,1)
Blanglois 0:265fff2cfb0a 10 struct SmartSensorStruct {
Blanglois 0:265fff2cfb0a 11 char crc; ///< Checksum CRC8
Blanglois 0:265fff2cfb0a 12 char serial[11]; ///< No Série du capteur. Doit demeurer à l'offset 1 dans la structure
Blanglois 0:265fff2cfb0a 13 char gain; ///< Gain à appliquer
Blanglois 0:265fff2cfb0a 14 char sampling_rate; ///< Vitesse de sampling
Blanglois 0:265fff2cfb0a 15 char model; ///< Model de capteur
Blanglois 0:265fff2cfb0a 16 short c1; ///< Consigne c1 de calcul
Blanglois 0:265fff2cfb0a 17 short c2; ///< Consigne c2 de calcul
Blanglois 0:265fff2cfb0a 18 short c3; ///< Consigne c3 de calcul
Blanglois 0:265fff2cfb0a 19 char depth; ///< Profondeur du capteur en mètres ou en pieds
Blanglois 0:265fff2cfb0a 20 short c4; ///< Consigne c4 de calcul
Blanglois 0:265fff2cfb0a 21 unsigned long code;///< Code de détection du type de smartSensor (Salinité ou Tension)
Blanglois 0:265fff2cfb0a 22 }SmartSensorStruct_packed;
Blanglois 0:265fff2cfb0a 23 #pragma pack(pop)
Blanglois 0:265fff2cfb0a 24
Blanglois 0:265fff2cfb0a 25 struct SmartSensorStruct stSensor;
Blanglois 0:265fff2cfb0a 26
Blanglois 0:265fff2cfb0a 27 char RAMBuffer[256]; //simulate EEPROM
Blanglois 0:265fff2cfb0a 28
Blanglois 0:265fff2cfb0a 29 void DoCRC8(char* a_crc8, char b)
Blanglois 0:265fff2cfb0a 30 {
Blanglois 0:265fff2cfb0a 31 char i, j;
Blanglois 0:265fff2cfb0a 32
Blanglois 0:265fff2cfb0a 33 for (i = 0; i < 8; b >>= 1, i++) {
Blanglois 0:265fff2cfb0a 34
Blanglois 0:265fff2cfb0a 35 j = (b ^ (*a_crc8)) & 1;
Blanglois 0:265fff2cfb0a 36 (*a_crc8) >>= 1;
Blanglois 0:265fff2cfb0a 37
Blanglois 0:265fff2cfb0a 38 if (j) (*a_crc8) ^= 0x8C;
Blanglois 0:265fff2cfb0a 39 }
Blanglois 0:265fff2cfb0a 40 }
Blanglois 0:265fff2cfb0a 41
Blanglois 0:265fff2cfb0a 42 void setADC(unsigned short value)
Blanglois 0:265fff2cfb0a 43 {
Blanglois 0:265fff2cfb0a 44 ReadADCValue = value;
Blanglois 0:265fff2cfb0a 45 }
Blanglois 0:265fff2cfb0a 46
Blanglois 0:265fff2cfb0a 47 char ComputeCRC8(char *buff, char len, char start_data)
Blanglois 0:265fff2cfb0a 48 {
Blanglois 0:265fff2cfb0a 49 char crc8 = 0;
Blanglois 0:265fff2cfb0a 50 DoCRC8(&crc8, start_data);
Blanglois 0:265fff2cfb0a 51 while (len--) DoCRC8(&crc8, *buff++);
Blanglois 0:265fff2cfb0a 52
Blanglois 0:265fff2cfb0a 53 return crc8;
Blanglois 0:265fff2cfb0a 54 }
Blanglois 0:265fff2cfb0a 55
Blanglois 0:265fff2cfb0a 56
Blanglois 0:265fff2cfb0a 57 void SaveRamBuffer(char address, char* value, unsigned char length)
Blanglois 0:265fff2cfb0a 58 {
Blanglois 0:265fff2cfb0a 59
Blanglois 0:265fff2cfb0a 60 unsigned char i;
Blanglois 0:265fff2cfb0a 61 for(i = 0; i < length; i++)
Blanglois 0:265fff2cfb0a 62 RAMBuffer[address + i] = value[i];
Blanglois 0:265fff2cfb0a 63 }
Blanglois 0:265fff2cfb0a 64
Blanglois 0:265fff2cfb0a 65 void SaveData(char add, long value)
Blanglois 0:265fff2cfb0a 66 {
Blanglois 0:265fff2cfb0a 67 SaveRamBuffer(add, (char*)&value, 4);
Blanglois 0:265fff2cfb0a 68 }
Blanglois 0:265fff2cfb0a 69
Blanglois 0:265fff2cfb0a 70 void I2C_1Process()
Blanglois 0:265fff2cfb0a 71 {
Blanglois 0:265fff2cfb0a 72 char buf[MAX_WRITE_SIZE + 1];
Blanglois 0:265fff2cfb0a 73 int nbRx = 0;
Blanglois 0:265fff2cfb0a 74
Blanglois 0:265fff2cfb0a 75 for(int i = 0; i < MAX_WRITE_SIZE; i++) buf[i] = 0; // Clear buffer
Blanglois 0:265fff2cfb0a 76
Blanglois 0:265fff2cfb0a 77 int rx = slave.receive();
Blanglois 0:265fff2cfb0a 78
Blanglois 0:265fff2cfb0a 79 switch (rx)
Blanglois 0:265fff2cfb0a 80 {
Blanglois 0:265fff2cfb0a 81 case I2CSlave::ReadAddressed:
eboily1 1:85afd4bd4651 82 slave.write(&RAMBuffer[PointOnAddress], 0xFF - PointOnAddress);
Blanglois 0:265fff2cfb0a 83 break;
Blanglois 0:265fff2cfb0a 84 /*case I2CSlave::WriteGeneral:
Blanglois 0:265fff2cfb0a 85
Blanglois 0:265fff2cfb0a 86
Blanglois 0:265fff2cfb0a 87 break;*/
Blanglois 0:265fff2cfb0a 88 case I2CSlave::WriteAddressed:
eboily1 1:85afd4bd4651 89 int ret = slave.read(buf, 1);
Blanglois 0:265fff2cfb0a 90 PointOnAddress = buf[0];
Blanglois 0:265fff2cfb0a 91 nbRx = slave.getCount();
eboily1 1:85afd4bd4651 92 if (nbRx > 0) //to simulate write on EEPROM need to test
eboily1 1:85afd4bd4651 93 {
eboily1 1:85afd4bd4651 94 ret = slave.read(buf, nbRx);
eboily1 1:85afd4bd4651 95 SaveRamBuffer(PointOnAddress, buf, nbRx);
eboily1 1:85afd4bd4651 96 }
eboily1 1:85afd4bd4651 97 printf("nb rx %d %d\r\n", nbRx, PointOnAddress);
eboily1 1:85afd4bd4651 98
Blanglois 0:265fff2cfb0a 99 break;
Blanglois 0:265fff2cfb0a 100 }
eboily1 1:85afd4bd4651 101 wait_ms(1);
Blanglois 0:265fff2cfb0a 102 }
Blanglois 0:265fff2cfb0a 103
Blanglois 0:265fff2cfb0a 104 void I2C_2Process()
Blanglois 0:265fff2cfb0a 105 {
eboily1 1:85afd4bd4651 106 char buf[MAX_WRITE_SIZE + 1];
Blanglois 0:265fff2cfb0a 107 int rx = slave2.receive();
eboily1 1:85afd4bd4651 108 int nbRx = 0;
Blanglois 0:265fff2cfb0a 109 switch (rx)
Blanglois 0:265fff2cfb0a 110 {
Blanglois 0:265fff2cfb0a 111 case I2CSlave::ReadAddressed:
Blanglois 0:265fff2cfb0a 112 slave2.write((char*)&ReadADCValue, 2);
eboily1 1:85afd4bd4651 113 printf("ReadAddressed 2 end\n");
Blanglois 0:265fff2cfb0a 114 break;
Blanglois 0:265fff2cfb0a 115 /*case I2CSlave::WriteGeneral:
Blanglois 0:265fff2cfb0a 116
Blanglois 0:265fff2cfb0a 117
Blanglois 0:265fff2cfb0a 118 break;*/
eboily1 1:85afd4bd4651 119 case I2CSlave::WriteAddressed:
eboily1 1:85afd4bd4651 120 //to empty read buffer we do nothing with the data
eboily1 1:85afd4bd4651 121 int ret = slave2.read(buf, 1);
eboily1 1:85afd4bd4651 122 nbRx = slave.getCount();
eboily1 1:85afd4bd4651 123 if (nbRx > 0) //to simulate write on EEPROM need to test
eboily1 1:85afd4bd4651 124 {
eboily1 1:85afd4bd4651 125 ret = slave2.read(buf, nbRx);
eboily1 1:85afd4bd4651 126 }
eboily1 1:85afd4bd4651 127 printf("nb rx2 %d %d\r\n", nbRx, PointOnAddress);
eboily1 1:85afd4bd4651 128 break;
Blanglois 0:265fff2cfb0a 129 }
Blanglois 0:265fff2cfb0a 130 }
Blanglois 0:265fff2cfb0a 131
Blanglois 0:265fff2cfb0a 132 void I2CSlaveProcess()
Blanglois 0:265fff2cfb0a 133 {
Blanglois 0:265fff2cfb0a 134 I2C_1Process();
Blanglois 0:265fff2cfb0a 135 I2C_2Process();
Blanglois 0:265fff2cfb0a 136 }
Blanglois 0:265fff2cfb0a 137
Blanglois 0:265fff2cfb0a 138 void InitI2CSlaveComm()
Blanglois 0:265fff2cfb0a 139 {
Blanglois 0:265fff2cfb0a 140 slave.address(0xA0);
Blanglois 0:265fff2cfb0a 141 sprintf(stSensor.serial, "2059123456");
Blanglois 0:265fff2cfb0a 142 stSensor.gain = 0x01;
Blanglois 0:265fff2cfb0a 143 stSensor.sampling_rate = 0x03;
Blanglois 0:265fff2cfb0a 144 stSensor.c1 = -37;
Blanglois 0:265fff2cfb0a 145 stSensor.c2 = 634;
Blanglois 0:265fff2cfb0a 146 stSensor.c3 = -7;
Blanglois 0:265fff2cfb0a 147 stSensor.c4 = -1;
Blanglois 0:265fff2cfb0a 148 stSensor.depth = 0x00;
Blanglois 0:265fff2cfb0a 149 stSensor.model = 2; //Nombre de données à transmettre vers le ST
Blanglois 0:265fff2cfb0a 150 stSensor.code = 0xFFFF; //Type of sensor
Blanglois 0:265fff2cfb0a 151 stSensor.crc = ComputeCRC8(((char *)&stSensor)+1, sizeof(SmartSensorStruct_packed) - 7, 0);
Blanglois 0:265fff2cfb0a 152 SaveRamBuffer(0, (char*)&stSensor, sizeof(SmartSensorStruct_packed));
Blanglois 0:265fff2cfb0a 153
Blanglois 0:265fff2cfb0a 154 slave2.address(0x90);
Blanglois 0:265fff2cfb0a 155 }