mraa: rename from maa to mraa
[contrib/mraa.git] / src / i2c / i2c.c
index 400c39c..e58eb70 100644 (file)
 
 #include "i2c.h"
 #include "smbus.h"
+#include "mraa_internal.h"
 
-maa_i2c_context*
-maa_i2c_init()
+struct _i2c {
+    /*@{*/
+    int hz; /**< frequency of communication */
+    int fh; /**< the file handle to the /dev/i2c-* device */
+    int addr; /**< the address of the i2c slave */
+    /*@}*/
+};
+
+mraa_i2c_context
+mraa_i2c_init(int bus)
 {
-    maa_i2c_context* dev = (maa_i2c_context*) malloc(sizeof(maa_i2c_context));
+    int checked_pin = mraa_setup_i2c(bus);
+    if (checked_pin < 0) {
+        switch(checked_pin) {
+            case -1:
+                fprintf(stderr, "No i2c on board\n");
+                return NULL;
+            case -2:
+                fprintf(stderr, "Failed to set-up i2c multiplexer!\n");
+                return NULL;
+            case -3:
+                fprintf(stderr, "Platform Not Initialised");
+                return NULL;
+            default: return NULL;
+        }
+    }
+    return mraa_i2c_init_raw((unsigned int) checked_pin);
+}
+
+mraa_i2c_context
+mraa_i2c_init_raw(unsigned int bus)
+{
+    mraa_i2c_context dev = (mraa_i2c_context) malloc(sizeof(struct _i2c));
     if (dev == NULL)
         return NULL;
 
-    // Galileo only has one I2C master which should be /dev/i2c-0
-    // reliability is a fickle friend!
-    if ((dev->fh = open("/dev/i2c-0", O_RDWR)) < 1) {
-        fprintf(stderr, "Failed to open requested i2c port");
+    char filepath[32];
+    snprintf(filepath, 32, "/dev/i2c-%u", bus);
+    if ((dev->fh = open(filepath, O_RDWR)) < 1) {
+        fprintf(stderr, "Failed to open requested i2c port %s", filepath);
     }
-    return MAA_SUCCESS;
+    return dev;
 }
 
-void
-maa_i2c_frequency(maa_i2c_context* dev, int hz)
+mraa_result_t
+mraa_i2c_frequency(mraa_i2c_context dev, int hz)
 {
     dev->hz = hz;
-}
 
-int
-maa_i2c_receive(maa_i2c_context* dev)
-{
-    return -1;
+    return MRAA_SUCCESS;
 }
 
 int
-maa_i2c_read(maa_i2c_context* dev, char *data, int length)
+mraa_i2c_read(mraa_i2c_context dev, uint8_t* data, int length)
 {
-    // this is the read(3) syscall not maa_i2c_read()
+    // this is the read(3) syscall not mraa_i2c_read()
     if (read(dev->fh, data, length) == length) {
         return length;
     }
-    return -1;
+    return 0;
 }
 
-int
-maa_i2c_read_byte(maa_i2c_context* dev)
+uint8_t
+mraa_i2c_read_byte(mraa_i2c_context dev)
 {
-    int byte;
-    byte = i2c_smbus_read_byte(dev->fh);
+    uint8_t byte = i2c_smbus_read_byte(dev->fh);
     if (byte < 0) {
         return -1;
     }
     return byte;
 }
 
-int
-maa_i2c_write(maa_i2c_context* dev, const char* data, int length)
+mraa_result_t
+mraa_i2c_write(mraa_i2c_context dev, const uint8_t* data, int length)
 {
     if (i2c_smbus_write_i2c_block_data(dev->fh, data[0], length-1, (uint8_t*) data+1) < 0) {
-        fprintf(stderr, "Failed to write to I2CSlave slave\n");
-       return -1;
+        fprintf(stderr, "Failed to write to i2c\n");
+       return MRAA_ERROR_INVALID_HANDLE;
     }
-    return 0;
+    return MRAA_SUCCESS;
 }
 
-int
-maa_i2c_write_byte(maa_i2c_context* dev, int data)
+mraa_result_t
+mraa_i2c_write_byte(mraa_i2c_context dev, const uint8_t data)
 {
     if (i2c_smbus_write_byte(dev->fh, data) < 0) {
-        fprintf(stderr, "Failed to write to I2CSlave slave\n");
-       return -1;
+        fprintf(stderr, "Failed to write to i2c\n");
+       return MRAA_ERROR_INVALID_HANDLE;
     }
-    return 0;
+    return MRAA_SUCCESS;
 }
 
-void
-maa_i2c_address(maa_i2c_context* dev, int addr)
+mraa_result_t
+mraa_i2c_address(mraa_i2c_context dev, int addr)
 {
     dev->addr = addr;
     if (ioctl(dev->fh, I2C_SLAVE_FORCE, addr) < 0) {
         fprintf(stderr, "Failed to set slave address %d\n", addr);
+       return MRAA_ERROR_INVALID_HANDLE;
     }
+    return MRAA_SUCCESS;
 }
 
-void
-maa_i2c_stop(maa_i2c_context* dev)
+mraa_result_t
+mraa_i2c_stop(mraa_i2c_context dev)
 {
     free(dev);
+    return MRAA_SUCCESS;
 }