I2C not yet integrated
Tested working with single and differential voltages.
Connect SCL (pin 11) to D15 Connect SDA (pin 10) to D14 Connect pin 16 to +5v Connect pin 9 to gnd
Diff: LT_I2C.cpp
- Revision:
- 2:c9e727dcd00e
- Parent:
- 1:4e4194db7cd6
diff -r 4e4194db7cd6 -r c9e727dcd00e LT_I2C.cpp --- a/LT_I2C.cpp Tue Nov 29 03:20:35 2016 +0000 +++ b/LT_I2C.cpp Tue Dec 06 18:34:46 2016 +0000 @@ -92,10 +92,20 @@ Serial pc1(USBTX, USBRX, 9600); -I2C i2c(I2C_SDA , I2C_SCL); +LT_I2C::LT_I2C() { + i2c = new I2C(I2C_SDA, I2C_SCL); + i2c->frequency(400000); //maximum from datasheet for ltc2991 +} + +LT_I2C::LT_I2C(PinName sda_, PinName scl_) { + sda = sda_; + scl = scl_; + i2c = new I2C(sda_ , scl_); + i2c->frequency(400000); //maximum from datasheet for ltc2991 +} // Read a byte of data at register specified by "command", store in "value" -int8_t i2c_read_byte_data(uint8_t address, uint8_t command, uint8_t *value) +int8_t LT_I2C::i2c_read_byte_data(uint8_t address, uint8_t command, uint8_t *value) { int8_t ret = 0; @@ -104,8 +114,8 @@ char v[1]; v[0] = 0; - ret |= i2c.write((address<<1) | I2C_WRITE_BIT, cmd1, 1, true); - ret |= i2c.read((address<<1) | I2C_READ_BIT, v, 1, false); + ret |= i2c->write((address<<1) | I2C_WRITE_BIT, cmd1, 1, true); + ret |= i2c->read((address<<1) | I2C_READ_BIT, v, 1, false); *value = v[0]; @@ -116,13 +126,13 @@ } // Write a byte of data to register specified by "command" -int8_t i2c_write_byte_data(uint8_t address, uint8_t command, uint8_t value) +int8_t LT_I2C::i2c_write_byte_data(uint8_t address, uint8_t command, uint8_t value) { int8_t ret = 0; char cmd[2]; cmd[0] = command; cmd[1] = value; - ret |= i2c.write((address<<1) | I2C_WRITE_BIT, cmd, 2, false); + ret |= i2c->write((address<<1) | I2C_WRITE_BIT, cmd, 2, false); if (ret == 0) { return 0; //success @@ -130,100 +140,7 @@ return(1); } -//// Read a byte of data at register specified by "command", store in "value" -//int8_t i2c_read_byte_data(uint8_t address, uint8_t command, uint8_t *value) -//{ -// checkDevice(); -// int8_t ret = 1; -// -// i2c.start(); // I2C START -// ret &= i2c.write((address<<1) | I2C_WRITE_BIT); // Write 7 bit address with W bit -// pc1.printf("[read] ret 0: %d\n", ret); -// ret &= i2c.write(command); // Set register to be read to command -// pc1.printf("[read] ret 1: %d\n", ret); -// i2c.start(); //2nd start condition -// ret &= i2c.write((address<<1) | I2C_READ_BIT); // Write 7 bit address with R bit -// pc1.printf("[read] ret 2: %d\n", ret); -// *value = i2c.read(WITH_NACK); // Read byte from buffer with *NAK* -// i2c.stop(); // I2C STOP -// if (ret == 1) { -// return 0; //success -// } -// return(1); -//} -// -//// Write a byte of data to register specified by "command" -//int8_t i2c_write_byte_data(uint8_t address, uint8_t command, uint8_t value) -//{ -// checkDevice(); -// int8_t ret = 1; -// -// i2c.start(); // I2C START -// ret &= i2c.write((address<<1) | I2C_WRITE_BIT); // Write 7 bit address with W bit -// pc1.printf("[write] ret 0: %d\n", ret); -// ret &= i2c.write(command); // Set register to be read to command -// pc1.printf("[write] ret 1: %d\n", ret); -// ret &= i2c.write(value); -// pc1.printf("[write] ret 2: %d\n", ret); -// i2c.stop(); -// if (ret == 1) { -// return 0; //success -// } -// return(1); -//} - -// Read a 16-bit word of data from register specified by "command" -//int8_t i2c_read_word_data(uint8_t address, uint8_t command, uint16_t *value) -//{ -// int8_t ret = 1; -// -// union { -// uint8_t b[2]; -// uint16_t w; -// } data; -// -// i2c.start(); -// ret &= i2c.write((address << 1) | I2C_WRITE_BIT); // Write 7 bit address with W bit -// ret &= i2c.write(command); // Set register to be read to command -// i2c.start(); //2nd start condition -// ret &= i2c.write((address<<1) | I2C_READ_BIT); // Write 7 bit address with R bit -// -// data.b[1] = i2c.read(WITH_ACK); // Read MSB from buffer -// data.b[0] = i2c.read(WITH_NACK); // Read LSB from buffer -// i2c.stop(); -// -// *value = data.w; -// if (ret == 1) { -// return 0; -// } -// return(1); -//} -// -//// Write a 16-bit word of data to register specified by "command" -//int8_t i2c_write_word_data(uint8_t address, uint8_t command, uint16_t value) -//{ -// int8_t ret = 1; -// -// union { -// uint8_t b[2]; -// uint16_t w; -// } data; -// data.w = value; -// -// i2c.start(); -// ret &= i2c.write((address<<1) | I2C_WRITE_BIT); // Write 7 bit address with W bit -// ret &= i2c.write(command); // Set register to be read to command -// ret &= i2c.write(data.b[1]); // Write MSB -// ret &= i2c.write(data.b[0]); // Write LSB; -// -// i2c.stop(); // I2C STOP -// if (ret == 1) { -// return 0; -// } -// return(1); -//} - -int8_t i2c_read_word_data(uint8_t address, uint8_t command, uint16_t *value) +int8_t LT_I2C::i2c_read_word_data(uint8_t address, uint8_t command, uint16_t *value) { int8_t ret = 0; @@ -237,10 +154,10 @@ writedata[0] = command; //msb writedata[1] = command+1; //lsb - ret |= i2c.write((address << 1) | I2C_WRITE_BIT, &writedata[0], 1, true); - ret |= i2c.read((address << 1) | I2C_READ_BIT, &data.b[1], 1, false); - ret |= i2c.write((address << 1) | I2C_WRITE_BIT, &writedata[1], 1, true); - ret |= i2c.read((address << 1) | I2C_READ_BIT, &data.b[0], 1, false); + ret |= i2c->write((address << 1) | I2C_WRITE_BIT, &writedata[0], 1, true); + ret |= i2c->read((address << 1) | I2C_READ_BIT, &data.b[1], 1, false); + ret |= i2c->write((address << 1) | I2C_WRITE_BIT, &writedata[1], 1, true); + ret |= i2c->read((address << 1) | I2C_READ_BIT, &data.b[0], 1, false); *value = data.w; @@ -252,7 +169,7 @@ } // Write a 16-bit word of data to register specified by "command" -int8_t i2c_write_word_data(uint8_t address, uint8_t command, uint16_t value) +int8_t LT_I2C::i2c_write_word_data(uint8_t address, uint8_t command, uint16_t value) { int8_t ret = 0; @@ -268,7 +185,7 @@ cmd[1] = data.b[1]; cmd[2] = data.b[0]; - ret |= i2c.write((address<<1) | I2C_WRITE_BIT, cmd, 3, false); + ret |= i2c->write((address<<1) | I2C_WRITE_BIT, cmd, 3, false); if (ret == 0) { return 0;