* API headers moved to api/ * smbus file added from libi2c and kernel i2c header cleaned up * fix compilation of swig and use i2c.h header Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
144 lines
3.8 KiB
C++
144 lines
3.8 KiB
C++
/*
|
|
* Author: Brendan Le Foll
|
|
*
|
|
* Copyright © 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 <stdio.h>
|
|
#include <fcntl.h>
|
|
|
|
#include "smbus.hpp"
|
|
|
|
namespace maa {
|
|
|
|
/** An I2C Master, used for communicating with I2C slave devices
|
|
*
|
|
* 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
|
|
*/
|
|
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;
|
|
};
|
|
}
|