![](/media/cache/profiles/eb1feb5ab28dec83652dd1e5c673f256.jpg.50x50_q85.jpg)
LTC2945 ported
LT_I2C.cpp
- Committer:
- odtulumbedder
- Date:
- 2017-08-23
- Revision:
- 1:6d4265aa4184
- Parent:
- 0:6be57f391716
- Child:
- 2:7e390bcce297
File content as of revision 1:6d4265aa4184:
#include "LT_I2C.h" /* mbed headers */ #include <mbed.h> #include <I2C.h> /* for debugging */ #ifdef LT_I2C_DEBUG #define ASSERT(x) if (! (x)) { printf("%s:%d %s failed!\n", __FILE__, __LINE__, #x); } #define DEBUG_MSG(x) printf("debugging: %s:%d %s\n", __FILE__, __LINE__, x); #else #define ASSERT(x) #define DEBUG_MSG(x) #endif extern "C" { static I2C *i2c_object = NULL; int lt_i2c_init(int sda, int scl) { DEBUG_MSG(__PRETTY_FUNCTION__) if (i2c_object == NULL) { i2c_object = new I2C((PinName) sda, (PinName) scl); return LT_I2C_INIT_FINE; } else { return LT_I2C_INIT_QUIT_FIRST; } } int lt_i2c_quit() { DEBUG_MSG(__PRETTY_FUNCTION__) delete i2c_object; return LT_I2C_SUCCESS; } int lt_i2c_init_attach(void *p_i2c_object) { DEBUG_MSG(__PRETTY_FUNCTION__) if (i2c_object == NULL) { i2c_object = (I2C *) p_i2c_object; return LT_I2C_INIT_FINE; } else { return LT_I2C_INIT_QUIT_FIRST; } } void *lt_i2c_get_i2c_object() { DEBUG_MSG(__PRETTY_FUNCTION__) return i2c_object; } #define CHECK_OBJECT if (i2c_object == NULL) return LT_I2C_FAILURE; int lt_i2c_start() { DEBUG_MSG(__PRETTY_FUNCTION__) CHECK_OBJECT i2c_object->start(); return LT_I2C_SUCCESS; } int lt_i2c_stop() { DEBUG_MSG(__PRETTY_FUNCTION__) CHECK_OBJECT i2c_object->stop(); return LT_I2C_SUCCESS; } uint8_t lt_i2c_write(uint8_t data) { DEBUG_MSG(__PRETTY_FUNCTION__) CHECK_OBJECT return 1-i2c_object->write(data); // I wish casting will work fine // must return 0 if ACK was received (1- is due that) } uint8_t lt_i2c_read(int8_t ack) { DEBUG_MSG(__PRETTY_FUNCTION__) CHECK_OBJECT return (uint8_t) (i2c_object->read(1-ack)); // act is different in mbed } int8_t lt_i2c_write_byte_data( uint8_t address, uint8_t command, uint8_t value) { DEBUG_MSG(__PRETTY_FUNCTION__) CHECK_OBJECT int8_t ret = 0; if (lt_i2c_start()!=0) //I2C START { DEBUG_MSG("START FAIL") return(1); //Stop and return 0 if START fail } ret |= lt_i2c_write((address<<1)|LT_I2C_WRITE_BIT); // Write 7 bit address with W bit ret|= lt_i2c_write(command); // Set register to be read to command ret|= lt_i2c_write(value); lt_i2c_stop(); // I2C STOP if (ret!=0) //If there was a NAK return 1 { DEBUG_MSG("NACK") return(1); } DEBUG_MSG("OK") return(0); // Return success } int8_t lt_i2c_write_word_data( uint8_t address, uint8_t command, uint16_t value) { DEBUG_MSG(__PRETTY_FUNCTION__) CHECK_OBJECT int8_t ret=0; union { uint8_t b[2]; uint16_t w; } data; data.w = value; if (lt_i2c_start()!=0) //I2C START { DEBUG_MSG("START FAIL") return(1); //Stop and return 0 if START fail } ret |= lt_i2c_write((address<<1)|LT_I2C_WRITE_BIT); // Write 7 bit address with W bit ret|= lt_i2c_write(command); // Set register to be read to command ret|= lt_i2c_write(data.b[1]); //Write MSB ret|= lt_i2c_write(data.b[0]); //Write LSB; lt_i2c_stop(); // I2C STOP if (ret!=0) //If there was a NAK return 1 { DEBUG_MSG("NACK") return(1); } DEBUG_MSG("OK") return(0); } uint8_t lt_i2c_write_block_data( uint8_t address, uint8_t command, uint8_t length, uint8_t *values) { DEBUG_MSG(__PRETTY_FUNCTION__) CHECK_OBJECT int8_t i = length-1; int8_t ret = 0; if (lt_i2c_start()!=0) //I2C START return(1); //Stop and return 0 if START fail ret |= lt_i2c_write((address<<1)|LT_I2C_WRITE_BIT); // Write 7 bit address with W bit ret|= lt_i2c_write(command); // Set register to be read to command while (i>=0) { ret|= lt_i2c_write(values[i]); //Write Value i--; } lt_i2c_stop(); // I2C STOP if (ret!=0) { DEBUG_MSG("ERROR") return(1); } else { DEBUG_MSG("OK") return(0); // Success! } } uint8_t lt_i2c_read_byte_data( uint8_t address, uint8_t command, uint8_t *value) { DEBUG_MSG(__PRETTY_FUNCTION__) CHECK_OBJECT int8_t ret = 0; if (lt_i2c_start() != 0) // I2C START { DEBUG_MSG("START FAIL") return (1); // Stop and return 0 if START fail } ret |= lt_i2c_write((address << 1) | LT_I2C_WRITE_BIT); // Write 7 bit address with W bit if (ret != 0) // If NACK return 1 { DEBUG_MSG("NACK") lt_i2c_stop(); // I2C STOP return (1); } ret |= lt_i2c_write(command); // Set register to be read to command if (ret != 0) // If NACK return 1 { DEBUG_MSG("NACK") lt_i2c_stop(); // I2C STOP return (1); } if (lt_i2c_start() != 0) // I2C repeated START { DEBUG_MSG("NACK") lt_i2c_stop(); // Attempt to issue I2C STOP return (1); // Stop and return 0 if START fail } ret |= lt_i2c_write((address << 1) | LT_I2C_READ_BIT); // Write 7 bit address with R bit *value = lt_i2c_read(LT_I2C_WITH_NACK); // Read byte from buffer with NAK lt_i2c_stop(); // I2C STOP if (ret != 0) // If there was a NACK return 1 { DEBUG_MSG("NACK") return (1); } DEBUG_MSG("OK") return (0); // Return success } uint8_t lt_i2c_read_word_data( uint8_t address, uint8_t command, uint16_t *value) { DEBUG_MSG(__PRETTY_FUNCTION__) CHECK_OBJECT int8_t ret = 0; union { uint8_t b[2]; uint16_t w; } data; if (lt_i2c_start() != 0) //I2C START { DEBUG_MSG("START FAIL") return (1); //Stop and return 0 if START fail } ret |= lt_i2c_write((address << 1) | LT_I2C_WRITE_BIT); // Write 7 bit address with W bit if (ret != 0) //If NACK return 1 { DEBUG_MSG("NACK") lt_i2c_stop(); //I2C STOP return (1); } ret |= lt_i2c_write(command); // Set register to be read to command if (ret != 0) //If NACK return 1 { DEBUG_MSG("NACK") lt_i2c_stop(); //I2C STOP return (1); } if (lt_i2c_start() != 0) //I2C START { DEBUG_MSG("START FAIL") lt_i2c_stop(); //Attempt to issue I2C STOP return (1); //Stop and return 0 if START fail } ret |= lt_i2c_write((address << 1) | LT_I2C_READ_BIT); // Write 7 bit address with R bit data.b[1] = lt_i2c_read(LT_I2C_WITH_ACK); // Read MSB from buffer data.b[0] = lt_i2c_read(LT_I2C_WITH_NACK); // Read LSB from buffer lt_i2c_stop(); //I2C STOP *value = data.w; if (ret != 0) //If NAK { DEBUG_MSG("NACK") return (1); //return 1 } DEBUG_MSG("OK") return (0); // Return success } uint8_t lt_i2c_read_block_data( uint8_t address, uint8_t command, uint8_t length, uint8_t *values) { DEBUG_MSG(__PRETTY_FUNCTION__) CHECK_OBJECT uint8_t i = (length - 1); int8_t ret = 0; if (lt_i2c_start() != 0) //I2C START { DEBUG_MSG("START FAIL") return (1); //Stop and return 0 if START fail } ret |= lt_i2c_write((address << 1) | LT_I2C_WRITE_BIT); //Write 7-bit address with W bit if (ret != 0) //If NACK return 1 { DEBUG_MSG("NACK") lt_i2c_stop(); //I2C STOP return (1); } ret |= lt_i2c_write(command); //Write 8 bit command word if (ret != 0) //If NACK return 1 { DEBUG_MSG("NACK") lt_i2c_stop(); //I2C STOP return (1); } if (lt_i2c_start() != 0) //I2C repeated START { DEBUG_MSG("START FAIL") lt_i2c_stop(); //Attempt to issue I2C STOP return (1); //Stop and return 0 if START fail } ret |= lt_i2c_write((address << 1) | LT_I2C_READ_BIT); //Write 7-bit address with R bit if (ret != 0) //If NACK return 1 { DEBUG_MSG("NACK") lt_i2c_stop(); //I2C STOP return (1); } while (i > 0) //Begin read loop { values[i] = lt_i2c_read(LT_I2C_WITH_ACK); //Read from bus with ACK i--; } values[0] = lt_i2c_read(LT_I2C_WITH_NACK); //Read from bus with NACK for the last one; lt_i2c_stop(); //I2C STOP DEBUG_MSG("OK") return (0); // Success! } }