Private
Public Access
2
0

maa: Refactor maa build system and partly implement i2c

* 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>
This commit is contained in:
Brendan Le Foll
2014-04-10 16:53:19 +01:00
parent 8b715745fc
commit 9994ea855c
16 changed files with 654 additions and 62 deletions

View File

@@ -1,4 +1,15 @@
add_library (maa i2c/i2c.cxx maa.cxx)
include_directories(
${PROJECT_SOURCE_DIR}/api
${PROJECT_SOURCE_DIR}/include
)
set (maa_LIB_SRCS
${PROJECT_SOURCE_DIR}/src/maa.cxx
${PROJECT_SOURCE_DIR}/src/i2c/i2c.cxx
${PROJECT_SOURCE_DIR}/src/i2c/smbus.c
)
add_library (maa STATIC ${maa_LIB_SRCS})
add_subdirectory(python)
add_subdirectory(javascript)

View File

@@ -26,11 +26,17 @@
*/
#include "i2c.h"
#include "smbus.h"
using namespace maa;
I2C::I2C(unsigned int sda, unsigned int scl)
{
// Galileo only has one I2C device which is always /dev/i2c-0
// reliability is a fickle friend!
if (i2c_handle = open("/dev/i2c-0", O_RDWR) < 1) {
fprintf(stderr, "Failed to open requested i2c port");
}
}
void
@@ -48,7 +54,11 @@ I2C::read(int address, char *data, int length, bool repeated)
int
I2C::read(int ack)
{
return 0;
int byte;
if (byte = i2c_smbus_read_byte(i2c_handle) < 0) {
return -1;
}
return byte;
}
int

View File

@@ -1,137 +0,0 @@
/*
* Copyright (C) Intel Corporation.
*
* 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.
*/
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;
};
}

420
src/i2c/smbus.c Normal file
View File

@@ -0,0 +1,420 @@
// Library: libi2c API
// File: smbus.c
/*! \file
*
* $LastChangedDate: 2009-09-09 09:44:12 -0600 (Wed, 09 Sep 2009) $
* $Rev: 130 $
*
* \brief System Management Bus (SMBus) over I<sup>2</sup>C communication
* interface.
*
* \author Robin Knight (robin.knight@roadnarrows.com)
*
* \par Copyright:
* (C) 2009. RoadNarrows LLC.
* (http://www.roadnarrows.com) \n
* All Rights Reserved
*
* <hr>
* \par Original Source and Copyright:
* See i2c-dev.h.
*
* <hr>
*/
// Permission is hereby granted, without written agreement and without
// license or royalty fees, to use, copy, modify, and distribute this
// software and its documentation for any purpose, provided that
// (1) The above copyright notice and the following two paragraphs
// appear in all copies of the source code and (2) redistributions
// including binaries reproduces these notices in the supporting
// documentation. Substantial modifications to this software may be
// copyrighted by their authors and need not follow the licensing terms
// described here, provided that the new terms are clearly indicated in
// all files where they apply.
//
// IN NO EVENT SHALL THE AUTHOR, ROADNARROWS LLC, OR ANY MEMBERS/EMPLOYEES
// OF ROADNARROW LLC OR DISTRIBUTORS OF THIS SOFTWARE BE LIABLE TO ANY
// PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
// DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
// EVEN IF THE AUTHORS OR ANY OF THE ABOVE PARTIES HAVE BEEN ADVISED OF
// THE POSSIBILITY OF SUCH DAMAGE.
//
// THE AUTHOR AND ROADNARROWS LLC SPECIFICALLY DISCLAIM ANY WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
// "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO
// PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
//
////////////////////////////////////////////////////////////////////////////////
#include "smbus.h"
#ifdef __cplusplus
extern "C" {
#endif
/*!
* \brief Execute an SMBus IOCTL.
*
* \param fd File descriptor to opened SMBus device.
* \param read_write Operation access type.
* \param command Operation command or immediate data.
* \param size Data size.
* \param [in,out] data Read/write/control data
*
* \return
* Returns \h_ge 0 on success.
* Else errno is set appropriately and -1 is returned.
*/
int i2c_smbus_access(int fd, uint8_t read_write, uint8_t command,
int size, i2c_smbus_data_t *data)
{
i2c_smbus_ioctl_data_t args;
args.read_write = read_write;
args.command = command;
args.size = size;
args.data = data;
return ioctl(fd, I2C_SMBUS, &args);
}
/*!
* \brief Write a quick value to the SMBus.
*
* \param fd File descriptor to opened SMBus device.
* \param value Value to write
*
* \return
* Returns \h_ge 0 on success.
* Else errno is set appropriately and -1 is returned.
*/
int i2c_smbus_write_quick(int fd, uint8_t value)
{
return i2c_smbus_access(fd, value, I2C_NOCMD, I2C_SMBUS_QUICK, NULL);
}
/*!
* \brief Read an immediate byte from the SMBus.
*
* \param fd File descriptor to opened SMBus device.
*
* \return
* Returns read byte on on success.
* Else errno is set appropriately and -1 is returned.
*/
int i2c_smbus_read_byte(int fd)
{
i2c_smbus_data_t data;
int rc;
rc = i2c_smbus_access(fd, I2C_SMBUS_READ, I2C_NOCMD, I2C_SMBUS_BYTE, &data);
return rc>=0? 0x0FF & data.byte: -1;
}
/*!
* \brief Write an immediate byte to the SMBus.
*
* \param fd File descriptor to opened SMBus device.
* \param value Byte value to write.
*
* \return
* Returns \h_ge 0 on success.
* Else errno is set appropriately and -1 is returned.
*/
int i2c_smbus_write_byte(int fd, uint8_t value)
{
return i2c_smbus_access(fd, I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
}
/*!
* \brief Read a data byte from the SMBus.
*
* \param fd File descriptor to opened SMBus device.
* \param command Command to SMBus device.
*
* \return
* Returns read byte on on success.
* Else errno is set appropriately and -1 is returned.
*/
int i2c_smbus_read_byte_data(int fd, uint8_t command)
{
i2c_smbus_data_t data;
int rc;
rc = i2c_smbus_access(fd, I2C_SMBUS_READ, command, I2C_SMBUS_BYTE_DATA,
&data);
return rc>=0? 0x0FF & data.byte: -1;
}
/*!
* \brief Write a data byte to the SMBus.
*
* \param fd File descriptor to opened SMBus device.
* \param command Command to SMBus device.
* \param value Byte value to write.
*
* \return
* Returns \h_ge 0 on success.
* Else errno is set appropriately and -1 is returned.
*/
int i2c_smbus_write_byte_data(int fd, uint8_t command, uint8_t value)
{
i2c_smbus_data_t data;
data.byte = value;
return i2c_smbus_access(fd, I2C_SMBUS_WRITE, command,
I2C_SMBUS_BYTE_DATA, &data);
}
/*!
* \brief Read a data 2-byte word from the SMBus.
*
* \param fd File descriptor to opened SMBus device.
* \param command Command to SMBus device.
*
* \return
* Returns read 2-byte word on on success.
* Else errno is set appropriately and -1 is returned.
*/
int i2c_smbus_read_word_data(int fd, uint8_t command)
{
i2c_smbus_data_t data;
int rc;
rc = i2c_smbus_access(fd, I2C_SMBUS_READ, command, I2C_SMBUS_WORD_DATA,
&data);
return rc>=0? 0x0FFFF & data.word: -1;
}
/*!
* \brief Write a data 2-byte word to the SMBus.
*
* \param fd File descriptor to opened SMBus device.
* \param command Command to SMBus device.
* \param value Word value to write.
*
* \return
* Returns \h_ge 0 on success.
* Else errno is set appropriately and -1 is returned.
*/
int i2c_smbus_write_word_data(int fd, uint8_t command, unsigned short value)
{
i2c_smbus_data_t data;
data.word = value;
return i2c_smbus_access(fd, I2C_SMBUS_WRITE, command,
I2C_SMBUS_WORD_DATA, &data);
}
/*!
* \brief Issue a 2-byte word process call (write/read) to the SMBus.
*
* \param fd File descriptor to opened SMBus device.
* \param command Command to SMBus device.
* \param value Word value to write.
*
* \return
* Returns read 2-byte word on on success.
* Else errno is set appropriately and -1 is returned.
*/
int i2c_smbus_process_call(int fd, uint8_t command, unsigned short value)
{
i2c_smbus_data_t data;
int rc;
data.word = value;
rc = i2c_smbus_access(fd, I2C_SMBUS_WRITE, command, I2C_SMBUS_PROC_CALL,
&data);
return rc>=0? 0x0FFFF & data.word: -1;
}
/*!
* \brief Read a block of data from the SMBus.
*
* \param fd File descriptor to opened SMBus device.
* \param command Command to SMBus device.
* \param [out] values Buffer to hold the block of read byte values.\n
* Must be large enough to receive the data.
*
* \return
* On success, returns \h_ge 0 the number of bytes read, excluding any header
* fields. Else errno is set appropriately and -1 is returned.
*/
int i2c_smbus_read_block_data(int fd, uint8_t command, uint8_t *values)
{
i2c_smbus_data_t data;
int i;
int rc;
rc = i2c_smbus_access(fd, I2C_SMBUS_READ, command, I2C_SMBUS_BLOCK_DATA,
&data);
if( rc >= 0 )
{
for(i=1; i<=data.block[0]; ++i)
{
values[i-1] = data.block[i];
}
rc = data.block[0];
}
return rc;
}
/*!
* \brief Write a data block to the SMBus.
*
* \param fd File descriptor to opened SMBus device.
* \param command Command to SMBus device.
* \param length Length of buffer (bytes) to write.
* \param [in] values Buffer of data to write.
*
* \return
* Returns \h_ge 0 on success.
* Else errno is set appropriately and -1 is returned.
*/
int i2c_smbus_write_block_data(int fd, uint8_t command, uint8_t length,
const uint8_t *values)
{
i2c_smbus_data_t data;
int i;
if( length > I2C_SMBUS_BLOCK_MAX )
{
length = I2C_SMBUS_BLOCK_MAX;
}
for(i=1; i<=length; i++)
{
data.block[i] = values[i-1];
}
data.block[0] = length;
return i2c_smbus_access(fd, I2C_SMBUS_WRITE, command,
I2C_SMBUS_BLOCK_DATA, &data);
}
/*!
* \brief Read a block of data from the SMBus via low-level I<sup>2</sup>C.
*
* \param fd File descriptor to opened SMBus device.
* \param command Command to SMBus device.
* \param [out] values Buffer to hold the block of read byte values.\n
* Must be large enough to receive the data.
*
* \return
* On success, returns \h_ge 0 the number of bytes read, excluding any header
* fields. Else errno is set appropriately and -1 is returned.
*/
int i2c_smbus_read_i2c_block_data(int fd, uint8_t command, uint8_t *values)
{
i2c_smbus_data_t data;
int i;
int rc;
rc = i2c_smbus_access(fd, I2C_SMBUS_READ, command, I2C_SMBUS_I2C_BLOCK_DATA,
&data);
if( rc >= 0 )
{
for(i=1; i<=data.block[0]; i++)
{
values[i-1] = data.block[i];
}
rc = data.block[0];
}
return rc;
}
/*!
* \brief Write a block of data to the SMBus via low-level I<sup>2</sup>C.
*
* \param fd File descriptor to opened SMBus device.
* \param command Command to SMBus device.
* \param length Length of buffer (bytes) to write.
* \param [in] values Buffer of data to write.
*
* \return
* Returns \h_ge 0 on success.
* Else errno is set appropriately and -1 is returned.
*/
int i2c_smbus_write_i2c_block_data(int fd, uint8_t command, uint8_t length,
const uint8_t *values)
{
i2c_smbus_data_t data;
int i;
if( length > I2C_SMBUS_I2C_BLOCK_MAX )
{
length = I2C_SMBUS_I2C_BLOCK_MAX;
}
for(i=1; i<=length; i++)
{
data.block[i] = values[i-1];
}
data.block[0] = length;
return i2c_smbus_access(fd, I2C_SMBUS_WRITE, command,
I2C_SMBUS_I2C_BLOCK_DATA, &data);
}
/*!
* \brief Issue a block process call (write/read) to the SMBus.
*
* \param fd File descriptor to opened SMBus device.
* \param command Command to SMBus device.
* \param length Length of buffer (bytes) to write.
* \param [in,out] values Buffer of data to write and to hold the block of
* read byte values.\n
* Must be large enough to receive the data.
*
* \return
* On success, returns \h_ge 0 the number of bytes read, excluding any header
* fields. Else errno is set appropriately and -1 is returned.
*/
int i2c_smbus_block_process_call(int fd, uint8_t command, uint8_t length,
uint8_t *values)
{
i2c_smbus_data_t data;
int i;
int rc;
if( length > I2C_SMBUS_BLOCK_MAX )
{
length = I2C_SMBUS_BLOCK_MAX;
}
for(i=1; i<=length; i++)
{
data.block[i] = values[i-1];
}
data.block[0] = length;
rc = i2c_smbus_access(fd, I2C_SMBUS_WRITE, command, I2C_SMBUS_BLOCK_PROC_CALL,
&data);
if( rc >= 0 )
{
for(i=1; i<=data.block[0]; i++)
{
values[i-1] = data.block[i];
}
rc = data.block[0];
}
return rc;
}
#ifdef __cplusplus
}
#endif

View File

@@ -16,7 +16,7 @@ include_directories(
SET_SOURCE_FILES_PROPERTIES(maajs.i PROPERTIES CPLUSPLUS ON)
SET_SOURCE_FILES_PROPERTIES(maajs.i PROPERTIES SWIG_FLAGS "-node")
SWIG_ADD_MODULE(maajs javascript maajs.i ../maa.cxx ../i2c/i2c.cxx)
SWIG_ADD_MODULE(maajs javascript ${maa_LIB_SRCS})
SWIG_LINK_LIBRARIES(maajs ${NODE_LIBRARIES})
set_target_properties(maajs PROPERTIES

View File

@@ -1,34 +0,0 @@
/*
* Copyright (C) Intel Corporation.
*
* 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 "i2c/i2c.h"
#define MAA_LIBRARY_VERSION 1
int get_version();

View File

@@ -1,2 +1,6 @@
%{
#include "maa.h"
%}
%include "maa.h"
%include "i2c/i2c.h"
%include "i2c.h"

View File

@@ -8,5 +8,5 @@ include_directories(
SET_SOURCE_FILES_PROPERTIES(pymaa.i PROPERTIES CPLUSPLUS ON)
SWIG_ADD_MODULE(pymaa python pymaa.i ../maa.cxx ../i2c/i2c.cxx)
SWIG_ADD_MODULE(pymaa python pymaa.i ${maa_LIB_SRCS})
SWIG_LINK_LIBRARIES(pymaa ${PYTHON_LIBRARIES})

View File

@@ -1,6 +1,3 @@
%module pymaa
%{
#include "maa.h"
%}
%include ../maa.i