Gabrielle Martin-Fortier / Mbed 2 deprecated Tensiometer_Simulator_waterbench

Dependencies:   mbed

Committer:
eboily1
Date:
Wed Oct 31 16:08:34 2018 +0000
Revision:
2:d0308b3aaf69
Parent:
1:85afd4bd4651
Child:
3:29925a0f88da
with adc working and function to setTension instead of setADC

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