LTC2945 ported

Dependencies:   mbed

Revision:
2:7e390bcce297
Parent:
1:6d4265aa4184
Child:
3:2ab78e7e8518
--- a/LT_I2C.cpp	Wed Aug 23 11:05:46 2017 +0000
+++ b/LT_I2C.cpp	Fri Aug 25 07:46:48 2017 +0000
@@ -4,21 +4,11 @@
 #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
+I2C *i2c_object = NULL;
 
 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;
@@ -29,13 +19,11 @@
 }
 
 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;
@@ -46,36 +34,26 @@
 }
 
 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)
+    return 1-i2c_object->write(data);
 }
 
 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
 }
 
@@ -83,12 +61,9 @@
     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);
@@ -98,10 +73,8 @@
     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
 }
 
@@ -109,8 +82,6 @@
     uint8_t address,
     uint8_t command,
     uint16_t value) {
-    DEBUG_MSG(__PRETTY_FUNCTION__)
-    CHECK_OBJECT
     int8_t ret=0;
     union
     {
@@ -120,7 +91,6 @@
     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);
@@ -131,10 +101,8 @@
     lt_i2c_stop(); // I2C STOP
     if (ret!=0) //If there was a NAK return 1
     {
-        DEBUG_MSG("NACK")
-        return(1);
+       return(1);
     }
-    DEBUG_MSG("OK")
     return(0);
 }
 
@@ -143,8 +111,6 @@
     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
@@ -160,12 +126,10 @@
     lt_i2c_stop(); // I2C STOP
     if (ret!=0)
     {
-        DEBUG_MSG("ERROR")
         return(1);
     }
     else
     {
-        DEBUG_MSG("OK")
         return(0);  // Success!
     }
 }
@@ -174,31 +138,25 @@
     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
     }
@@ -207,10 +165,8 @@
     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
 }
 
@@ -218,8 +174,6 @@
     uint8_t address,
     uint8_t command,
     uint16_t *value) {
-    DEBUG_MSG(__PRETTY_FUNCTION__)
-    CHECK_OBJECT
     int8_t ret = 0;
     union {
         uint8_t b[2];
@@ -227,26 +181,22 @@
     } 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
     }
@@ -257,10 +207,8 @@
     *value = data.w;
     if (ret != 0) //If NAK
     {
-        DEBUG_MSG("NACK")
         return (1); //return 1
     }
-    DEBUG_MSG("OK")
     return (0); // Return success
 }
 
@@ -269,39 +217,32 @@
     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);
     }
@@ -312,7 +253,6 @@
     }
     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!
 }