i2c: use uint8_t throughout to make code clearer
[contrib/mraa.git] / api / i2c.h
index 628a68a..360f78a 100644 (file)
--- a/api/i2c.h
+++ b/api/i2c.h
@@ -1,7 +1,6 @@
 /*
- * Author: Brendan Le Foll
- *
- * Copyright © 2014 Intel Corporation
+ * Author: Brendan Le Foll <brendan.le.foll@intel.com>
+ * Copyright (c) 2014 Intel Corporation.
  *
  * Permission is hereby granted, free of charge, to any person obtaining
  * a copy of this software and associated documentation files (the
 
 #pragma once
 
+/** @file
+ *
+ * This file defines the i2c interface for libmaa
+ *
+ */
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <stdlib.h>
 #include <stdio.h>
 #include <fcntl.h>
+#include <stdint.h>
+
+#include "maa.h"
+#include "gpio.h"
+
+/**
+ * Opaque pointer definition to the internal struct _i2c
+ */
+typedef struct _i2c* maa_i2c_context;
+
+/** Initialise i2c context, using board defintions
+ *
+ * @param bus i2c bus to use
+ * @return maa_i2c_context i2c context ready for other calls.
+ */
+maa_i2c_context maa_i2c_init(int bus);
+
+/** Initialise i2c context, passing in spi bus to use.
+ *
+ * @param bus The i2c bus to use i.e. /dev/i2c-2 would be "2"
+ * @return maa_i2c_context i2c context ready for other calls.
+ */
+maa_i2c_context maa_i2c_init_raw(unsigned int bus);
+
+/** Sets the frequency of the i2c context
+ *
+ *  @param dev the i2c context
+ *  @param hz The bus frequency in hertz
+ *
+ *  @return maa_result_t the maa result.
+ */
+maa_result_t maa_i2c_frequency(maa_i2c_context dev, int hz);
+
+/** Read from an i2c context
+ *
+ *  @param dev the i2c context
+ *  @param data pointer to the byte array to read data in to
+ *  @param length max number of bytes to read
+ *
+ *  @return maa_result_t the maa result.
+ */
+maa_result_t maa_i2c_read(maa_i2c_context dev, uint8_t *data, int length);
+
+/** Read a single byte from the i2c context
+ *
+ *  @param dev the i2c context
+ *
+ *  @return byte the result of the read or -1 if failed.
+ */
+uint8_t maa_i2c_read_byte(maa_i2c_context dev);
 
-#include "smbus.hpp"
+/** Write to an i2c context
+ *
+ *  @param dev the i2c context
+ *  @param data pointer to the byte array to be written
+ *  @param length the number of bytes to transmit
+ *
+ *  @return maa_result_t the maa result.
+ */
+maa_result_t maa_i2c_write(maa_i2c_context dev, const uint8_t *data, int length);
 
-namespace maa {
+/** Write a single byte to an i2c context
+ *
+ *  @param dev the i2c context
+ *  @data the byte to write
+ *
+ *  @return maa_result_t the maa result.
+ */
+maa_result_t maa_i2c_write_byte(maa_i2c_context dev, const uint8_t data);
 
-/** An I2C Master, used for communicating with I2C slave devices
+/** Sets the i2c context address.
  *
- * Example:
- * @code
- * // Read from I2C slave at address 0x62
+ *  @param dev the i2c context
+ *  @param address The address to set for the slave (ignoring the least
+ *  signifcant bit). If set to 0, the slave will only respond to the
+ *  general call address.
  *
- * #include "maa.h"
+ *  @return maa_result_t the maa result.
+ */
+maa_result_t maa_i2c_address(maa_i2c_context dev, int address);
+
+/** De-inits an maa_i2c_context device
  *
- * I2C i2c(p28, p27);
+ *  @param dev the i2c context
  *
- * int main() {
- *     int address = 0x62;
- *     char data[2];
- *     i2c.read(address, data, 2);
- * }
- * @endcode
+ *  @return maa_result_t the maa result.
  */
-class I2C {
-
-public:
-    enum RxStatus {
-        NoData,
-        MasterGeneralCall,
-        MasterWrite,
-        MasterRead
-    };
-
-    enum Acknowledge {
-        NoACK = 0,
-        ACK   = 1
-    };
-
-    /** Create an I2C Master interface, connected to the specified pins
-     *
-     *  @param sda I2C data line pin
-     *  @param scl I2C clock line pin
-     */
-    I2C(unsigned int sda, unsigned int scl);
-
-    /** Set the frequency of the I2C interface
-     *
-     *  @param hz The bus frequency in hertz
-     */
-    void frequency(int hz);
-
-    /** Read from an I2C slave
-     *
-     * Performs a complete read transaction. The bottom bit of
-     * the address is forced to 1 to indicate a read.
-     *
-     *  @param address 8-bit I2C slave address [ addr | 1 ]
-     *  @param data Pointer to the byte-array to read data in to
-     *  @param length Number of bytes to read
-     *  @param repeated Repeated start, true - don't send stop at end
-     *
-     *  @returns
-     *       0 on success (ack),
-     *   non-0 on failure (nack)
-     */
-    int read(int address, char *data, int length, bool repeated = false);
-
-    /** Read a single byte from the I2C bus
-     *
-     *  @param ack indicates if the byte is to be acknowledged (1 = acknowledge)
-     *
-     *  @returns
-     *    the byte read
-     */
-    int read(int ack);
-
-    /** Write to an I2C slave
-     *
-     * Performs a complete write transaction. The bottom bit of
-     * the address is forced to 0 to indicate a write.
-     *
-     *  @param address 8-bit I2C slave address [ addr | 0 ]
-     *  @param data Pointer to the byte-array data to send
-     *  @param length Number of bytes to send
-     *  @param repeated Repeated start, true - do not send stop at end
-     *
-     *  @returns
-     *       0 on success (ack),
-     *   non-0 on failure (nack)
-     */
-    int write(int address, const char *data, int length, bool repeated = false);
-
-    /** Write single byte out on the I2C bus
-     *  @param data data to write out on bus
-     *
-     *  @returns
-     *    '1' if an ACK was received,
-     *    '0' otherwise
-     */
-    int write(int data);
-
-    /** Creates a start condition on the I2C bus
-     */
-    void start(void);
-
-    /** Creates a stop condition on the I2C bus
-     */
-    void stop(void);
-
-protected:
-    void aquire();
-    int _hz;
-    int i2c_handle;
-};
+maa_result_t maa_i2c_stop(maa_i2c_context dev);
+
+#ifdef __cplusplus
 }
+#endif