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.
Dependencies: mbed
I2CSlaveComm.cpp
- Committer:
- Blanglois
- Date:
- 2018-10-30
- Revision:
- 0:265fff2cfb0a
- Child:
- 1:85afd4bd4651
File content as of revision 0:265fff2cfb0a:
#include "mbed.h" #include "I2CSlaveComm.h" I2CSlaveCustom slave(D14, D15); I2CSlaveCustom slave2(D2, D8); //use another I2C to emulate the adc unsigned char PointOnAddress = 0; unsigned short ReadADCValue = 0; #pragma pack(push,1) struct SmartSensorStruct { char crc; ///< Checksum CRC8 char serial[11]; ///< No Série du capteur. Doit demeurer à l'offset 1 dans la structure char gain; ///< Gain à appliquer char sampling_rate; ///< Vitesse de sampling char model; ///< Model de capteur short c1; ///< Consigne c1 de calcul short c2; ///< Consigne c2 de calcul short c3; ///< Consigne c3 de calcul char depth; ///< Profondeur du capteur en mètres ou en pieds short c4; ///< Consigne c4 de calcul unsigned long code;///< Code de détection du type de smartSensor (Salinité ou Tension) }SmartSensorStruct_packed; #pragma pack(pop) struct SmartSensorStruct stSensor; char RAMBuffer[256]; //simulate EEPROM void DoCRC8(char* a_crc8, char b) { char i, j; for (i = 0; i < 8; b >>= 1, i++) { j = (b ^ (*a_crc8)) & 1; (*a_crc8) >>= 1; if (j) (*a_crc8) ^= 0x8C; } } void setADC(unsigned short value) { ReadADCValue = value; } char ComputeCRC8(char *buff, char len, char start_data) { char crc8 = 0; DoCRC8(&crc8, start_data); while (len--) DoCRC8(&crc8, *buff++); return crc8; } void SaveRamBuffer(char address, char* value, unsigned char length) { unsigned char i; for(i = 0; i < length; i++) RAMBuffer[address + i] = value[i]; } void SaveData(char add, long value) { SaveRamBuffer(add, (char*)&value, 4); } void I2C_1Process() { char buf[MAX_WRITE_SIZE + 1]; int nbRx = 0; for(int i = 0; i < MAX_WRITE_SIZE; i++) buf[i] = 0; // Clear buffer int rx = slave.receive(); switch (rx) { case I2CSlave::ReadAddressed: printf("%02x\n", RAMBuffer[PointOnAddress]); slave.write(&RAMBuffer[PointOnAddress], 0xF0 - PointOnAddress); printf("ReadAddressed 1\n"); break; /*case I2CSlave::WriteGeneral: break;*/ case I2CSlave::WriteAddressed: slave.read(buf, MAX_WRITE_SIZE + 1); PointOnAddress = buf[0]; nbRx = slave.getCount(); //PcUart.printf("nb rx %d\r\n", nbRx); if (nbRx > 0) SaveRamBuffer(PointOnAddress, &buf[1], nbRx); printf("WriteAddressed 1\n"); break; } } void I2C_2Process() { int rx = slave2.receive(); switch (rx) { case I2CSlave::ReadAddressed: slave2.write((char*)&ReadADCValue, 2); printf("ReadAddressed 2 end\n"); break; /*case I2CSlave::WriteGeneral: break;*/ /*case I2CSlave::WriteAddressed: //do nothing break;*/ } } void I2CSlaveProcess() { I2C_1Process(); I2C_2Process(); } void InitI2CSlaveComm() { slave.address(0xA0); sprintf(stSensor.serial, "2059123456"); stSensor.gain = 0x01; stSensor.sampling_rate = 0x03; stSensor.c1 = -37; stSensor.c2 = 634; stSensor.c3 = -7; stSensor.c4 = -1; stSensor.depth = 0x00; stSensor.model = 2; //Nombre de données à transmettre vers le ST stSensor.code = 0xFFFF; //Type of sensor stSensor.crc = ComputeCRC8(((char *)&stSensor)+1, sizeof(SmartSensorStruct_packed) - 7, 0); SaveRamBuffer(0, (char*)&stSensor, sizeof(SmartSensorStruct_packed)); slave2.address(0x90); }