Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
I2CSlaveComm.cpp@0:265fff2cfb0a, 2018-10-30 (annotated)
- Committer:
- Blanglois
- Date:
- Tue Oct 30 15:52:54 2018 +0000
- Revision:
- 0:265fff2cfb0a
- Child:
- 1:85afd4bd4651
I2C Communication with ST
Who changed what in which revision?
| User | Revision | Line number | New 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: |
| Blanglois | 0:265fff2cfb0a | 82 | printf("%02x\n", RAMBuffer[PointOnAddress]); |
| Blanglois | 0:265fff2cfb0a | 83 | slave.write(&RAMBuffer[PointOnAddress], 0xF0 - PointOnAddress); |
| Blanglois | 0:265fff2cfb0a | 84 | printf("ReadAddressed 1\n"); |
| Blanglois | 0:265fff2cfb0a | 85 | break; |
| Blanglois | 0:265fff2cfb0a | 86 | /*case I2CSlave::WriteGeneral: |
| Blanglois | 0:265fff2cfb0a | 87 | |
| Blanglois | 0:265fff2cfb0a | 88 | |
| Blanglois | 0:265fff2cfb0a | 89 | break;*/ |
| Blanglois | 0:265fff2cfb0a | 90 | case I2CSlave::WriteAddressed: |
| Blanglois | 0:265fff2cfb0a | 91 | slave.read(buf, MAX_WRITE_SIZE + 1); |
| Blanglois | 0:265fff2cfb0a | 92 | PointOnAddress = buf[0]; |
| Blanglois | 0:265fff2cfb0a | 93 | nbRx = slave.getCount(); |
| Blanglois | 0:265fff2cfb0a | 94 | //PcUart.printf("nb rx %d\r\n", nbRx); |
| Blanglois | 0:265fff2cfb0a | 95 | if (nbRx > 0) |
| Blanglois | 0:265fff2cfb0a | 96 | SaveRamBuffer(PointOnAddress, &buf[1], nbRx); |
| Blanglois | 0:265fff2cfb0a | 97 | printf("WriteAddressed 1\n"); |
| Blanglois | 0:265fff2cfb0a | 98 | break; |
| Blanglois | 0:265fff2cfb0a | 99 | } |
| Blanglois | 0:265fff2cfb0a | 100 | } |
| Blanglois | 0:265fff2cfb0a | 101 | |
| Blanglois | 0:265fff2cfb0a | 102 | void I2C_2Process() |
| Blanglois | 0:265fff2cfb0a | 103 | { |
| Blanglois | 0:265fff2cfb0a | 104 | int rx = slave2.receive(); |
| Blanglois | 0:265fff2cfb0a | 105 | switch (rx) |
| Blanglois | 0:265fff2cfb0a | 106 | { |
| Blanglois | 0:265fff2cfb0a | 107 | case I2CSlave::ReadAddressed: |
| Blanglois | 0:265fff2cfb0a | 108 | slave2.write((char*)&ReadADCValue, 2); |
| Blanglois | 0:265fff2cfb0a | 109 | printf("ReadAddressed 2 end\n"); |
| Blanglois | 0:265fff2cfb0a | 110 | break; |
| Blanglois | 0:265fff2cfb0a | 111 | /*case I2CSlave::WriteGeneral: |
| Blanglois | 0:265fff2cfb0a | 112 | |
| Blanglois | 0:265fff2cfb0a | 113 | |
| Blanglois | 0:265fff2cfb0a | 114 | break;*/ |
| Blanglois | 0:265fff2cfb0a | 115 | /*case I2CSlave::WriteAddressed: |
| Blanglois | 0:265fff2cfb0a | 116 | //do nothing |
| Blanglois | 0:265fff2cfb0a | 117 | break;*/ |
| Blanglois | 0:265fff2cfb0a | 118 | } |
| Blanglois | 0:265fff2cfb0a | 119 | } |
| Blanglois | 0:265fff2cfb0a | 120 | |
| Blanglois | 0:265fff2cfb0a | 121 | void I2CSlaveProcess() |
| Blanglois | 0:265fff2cfb0a | 122 | { |
| Blanglois | 0:265fff2cfb0a | 123 | I2C_1Process(); |
| Blanglois | 0:265fff2cfb0a | 124 | I2C_2Process(); |
| Blanglois | 0:265fff2cfb0a | 125 | } |
| Blanglois | 0:265fff2cfb0a | 126 | |
| Blanglois | 0:265fff2cfb0a | 127 | void InitI2CSlaveComm() |
| Blanglois | 0:265fff2cfb0a | 128 | { |
| Blanglois | 0:265fff2cfb0a | 129 | slave.address(0xA0); |
| Blanglois | 0:265fff2cfb0a | 130 | sprintf(stSensor.serial, "2059123456"); |
| Blanglois | 0:265fff2cfb0a | 131 | stSensor.gain = 0x01; |
| Blanglois | 0:265fff2cfb0a | 132 | stSensor.sampling_rate = 0x03; |
| Blanglois | 0:265fff2cfb0a | 133 | stSensor.c1 = -37; |
| Blanglois | 0:265fff2cfb0a | 134 | stSensor.c2 = 634; |
| Blanglois | 0:265fff2cfb0a | 135 | stSensor.c3 = -7; |
| Blanglois | 0:265fff2cfb0a | 136 | stSensor.c4 = -1; |
| Blanglois | 0:265fff2cfb0a | 137 | stSensor.depth = 0x00; |
| Blanglois | 0:265fff2cfb0a | 138 | stSensor.model = 2; //Nombre de données à transmettre vers le ST |
| Blanglois | 0:265fff2cfb0a | 139 | stSensor.code = 0xFFFF; //Type of sensor |
| Blanglois | 0:265fff2cfb0a | 140 | stSensor.crc = ComputeCRC8(((char *)&stSensor)+1, sizeof(SmartSensorStruct_packed) - 7, 0); |
| Blanglois | 0:265fff2cfb0a | 141 | SaveRamBuffer(0, (char*)&stSensor, sizeof(SmartSensorStruct_packed)); |
| Blanglois | 0:265fff2cfb0a | 142 | |
| Blanglois | 0:265fff2cfb0a | 143 | |
| Blanglois | 0:265fff2cfb0a | 144 | slave2.address(0x90); |
| Blanglois | 0:265fff2cfb0a | 145 | } |