/*
- * Originally from mbed Microcontroller Library
- * Copyright (c) 2006-2013 ARM Limited
- * Copyright (c) 2014 Intel Corporation
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
+ * 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
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#pragma once
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
+#include <stdint.h>
#include "maa.h"
#include "gpio.h"
-typedef struct {
- int hz;
- int fh;
- int addr;
- maa_gpio_context gpio;
-} maa_i2c_context;
+/**
+ * Opaque pointer definition to the internal struct _i2c
+ */
+typedef struct _i2c* maa_i2c_context;
-maa_i2c_context* maa_i2c_init();
+/** 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);
-/** Set the frequency of the I2C interface
+/** Initialise i2c context, passing in spi bus to use.
*
- * @param dev the i2c context
- * @param hz The bus frequency in hertz
+ * @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.
*/
-void maa_i2c_frequency(maa_i2c_context* dev, int hz);
+maa_i2c_context maa_i2c_init_raw(unsigned int bus);
-/** Checks to see if this I2C Slave has been addressed.
+/** Sets the frequency of the i2c context
*
* @param dev the i2c context
- * @returns
- * A status indicating if the device has been addressed, and how
- * - NoData - the slave has not been addressed
- * - ReadAddressed - the master has requested a read from this slave
- * - WriteAddressed - the master is writing to this slave
- * - WriteGeneral - the master is writing to all slave
+ * @param hz The bus frequency in hertz
+ *
+ * @return maa_result_t the maa result.
*/
-int maa_i2c_receive(maa_i2c_context* dev);
+maa_result_t maa_i2c_frequency(maa_i2c_context dev, int hz);
-/** Read from an I2C master.
+/** Read from an i2c context
*
* @param dev the i2c context
* @param data pointer to the byte array to read data in to
- * @param length maximum number of bytes to read
+ * @param length max number of bytes to read
*
- * @returns
- * 0 on success,
- * non-0 otherwise
+ * @return maa_result_t the maa result.
*/
-int maa_i2c_read(maa_i2c_context* dev, char *data, int length);
+maa_result_t maa_i2c_read(maa_i2c_context dev, uint8_t *data, int length);
-/** Read a single byte from an I2C master.
+/** Read a single byte from the i2c context
*
* @param dev the i2c context
- * @returns
- * the byte read
+ *
+ * @return byte the result of the read or -1 if failed.
*/
-int maa_i2c_read_byte(maa_i2c_context* dev);
+uint8_t maa_i2c_read_byte(maa_i2c_context dev);
-/** Write to an I2C master
+/** Write to an i2c context
*
* @param dev the i2c context
- * @param data pointer to the byte array to be transmitted
- * @param length the number of bytes to transmite
+ * @param data pointer to the byte array to be written
+ * @param length the number of bytes to transmit
*
- * @returns
- * 0 on success,
- * non-0 otherwise
+ * @return maa_result_t the maa result.
*/
-int maa_i2c_write(maa_i2c_context* dev, const char *data, int length);
+maa_result_t maa_i2c_write(maa_i2c_context dev, const uint8_t *data, int length);
-/** Write a single byte to an I2C master.
+/** Write a single byte to an i2c context
*
* @param dev the i2c context
* @data the byte to write
*
- * @returns
- * '1' if an ACK was received,
- * '0' otherwise
+ * @return maa_result_t the maa result.
*/
-int maa_i2c_write_byte(maa_i2c_context* dev, int data);
+maa_result_t maa_i2c_write_byte(maa_i2c_context dev, const uint8_t data);
-/** Sets the I2C slave address.
+/** Sets the i2c context address.
*
* @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.
+ *
+ * @return maa_result_t the maa result.
*/
-void maa_i2c_address(maa_i2c_context* dev, int address);
+maa_result_t maa_i2c_address(maa_i2c_context dev, int address);
/** De-inits an maa_i2c_context device
*
* @param dev the i2c context
+ *
+ * @return maa_result_t the maa result.
*/
-void maa_i2c_stop(maa_i2c_context* dev);
+maa_result_t maa_i2c_stop(maa_i2c_context dev);
#ifdef __cplusplus
}