Private
Public Access
2
0

MAA version 0.2.0 moves to a standard C API

* Removed all C++ code and renamed all .cxx extensions to .c
* All functions are renamed to maa_ and modules are for example called maa_pwm
* Cmake can now 'make doc' using a Doxyfile.in to create documentation
* examples/ have been updated but swig generated API is untested

Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
This commit is contained in:
Brendan Le Foll
2014-04-27 21:17:54 +01:00
parent 5a270191b5
commit a5a407e4b5
23 changed files with 389 additions and 736 deletions

169
api/i2c.h
View File

@@ -18,119 +18,84 @@
#pragma once
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include "gpio.h"
#include "smbus.hpp"
typedef struct {
int hz;
int fh;
int addr;
gpio_t gpio;
} i2c_t;
namespace maa {
int maa_i2c_init(i2c_t* dev);
/** An I2C Master, used for communicating with I2C slave devices
/** Set the frequency of the I2C interface
*
* Example:
* @code
* // Read from I2C slave at address 0x62
*
* #include "maa.h"
*
* I2C i2c(p28, p27);
*
* int main() {
* int address = 0x62;
* char data[2];
* i2c.read(address, data, 2);
* }
* @endcode
* @param hz The bus frequency in hertz
*/
class I2C {
void maa_i2c_frequency(i2c_t* dev, int hz);
public:
enum RxStatus {
NoData,
MasterGeneralCall,
MasterWrite,
MasterRead
};
/** Checks to see if this I2C Slave has been addressed.
*
* @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
*/
int maa_i2c_receive(i2c_t* dev);
enum Acknowledge {
NoACK = 0,
ACK = 1
};
/** Read from an I2C master.
*
* @param data pointer to the byte array to read data in to
* @param length maximum number of bytes to read
*
* @returns
* 0 on success,
* non-0 otherwise
*/
int maa_i2c_read(i2c_t* dev, char *data, int length);
/** 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);
/** Read a single byte from an I2C master.
*
* @returns
* the byte read
*/
int maa_i2c_read_byte(i2c_t* dev);
/** Set the frequency of the I2C interface
*
* @param hz The bus frequency in hertz
*/
void frequency(int hz);
/** Write to an I2C master.
*
* @param data pointer to the byte array to be transmitted
* @param length the number of bytes to transmite
*
* @returns
* 0 on success,
* non-0 otherwise
*/
int maa_i2c_write(i2c_t* dev, const char *data, int length);
/** 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);
/** Write a single byte to an I2C master.
*
* @data the byte to write
*
* @returns
* '1' if an ACK was received,
* '0' otherwise
*/
int maa_i2c_write_byte(i2c_t* dev, int data);
/** 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);
/** Sets the I2C slave address.
*
* @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.
*/
void maa_i2c_address(i2c_t* dev, int address);
/** 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;
};
}
/** De-inits an i2c_t device
*/
void maa_i2c_stop(i2c_t* dev);