[Armadeus-commitlog] armadeus branch, master, updated. latestrelease-155-ga18d9be
Brought to you by:
sszy
|
From: Fabien M <fa...@us...> - 2009-11-20 11:21:16
|
This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "armadeus".
The branch, master has been updated
via a18d9bea9373c5a9d745a2fab38050b877f21513 (commit)
via f7e04d02048a583f9b3e63256f8002163d3d48ea (commit)
via 697d371e3542b57acbfbeb322c81b898e4e8272c (commit)
via 05c71828927d8baaf7a62f77aeb9f1065b8101ec (commit)
from 70db9a135716bee90b209a7e6d3bef279693497a (commit)
Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.
- Log -----------------------------------------------------------------
commit a18d9bea9373c5a9d745a2fab38050b877f21513
Author: Fabien Marteau <fab...@ar...>
Date: Fri Nov 20 12:14:23 2009 +0100
[as_devices] first version of as_spi compile
commit f7e04d02048a583f9b3e63256f8002163d3d48ea
Author: Fabien Marteau <fab...@ar...>
Date: Fri Nov 20 10:20:10 2009 +0100
[as_devices] as_93lcxx lib compile, not tested
commit 697d371e3542b57acbfbeb322c81b898e4e8272c
Author: Fabien Marteau <fab...@ar...>
Date: Thu Nov 19 23:47:24 2009 +0100
[as_devices] Adding spidev API
commit 05c71828927d8baaf7a62f77aeb9f1065b8101ec
Author: Fabien Marteau <fab...@ar...>
Date: Thu Nov 19 23:02:06 2009 +0100
[as_devices] Adding API for as_93lcxx memory functions
-----------------------------------------------------------------------
Summary of changes:
target/packages/as_devices/c/Makefile | 2 +
target/packages/as_devices/c/as_93lcxx.c | 246 ++++++++++++++++
target/packages/as_devices/c/as_93lcxx.h | 141 +++++++++
target/packages/as_devices/c/as_i2c.c | 304 ++++++--------------
target/packages/as_devices/c/as_i2c.h | 157 +++++++---
target/packages/as_devices/c/as_spi.c | 168 +++++++++++
target/packages/as_devices/c/as_spi.h | 130 +++++++++
target/packages/as_devices/cpp/as_apf27_gpio.cpp | 4 +-
.../packages/as_devices/cpp/as_dynamic_table.hpp | 2 +
target/packages/as_devices/cpp/as_i2c.cpp | 8 +-
10 files changed, 890 insertions(+), 272 deletions(-)
create mode 100644 target/packages/as_devices/c/as_93lcxx.c
create mode 100644 target/packages/as_devices/c/as_93lcxx.h
create mode 100644 target/packages/as_devices/c/as_spi.c
create mode 100644 target/packages/as_devices/c/as_spi.h
diff --git a/target/packages/as_devices/c/Makefile b/target/packages/as_devices/c/Makefile
index c893427..b78f67c 100644
--- a/target/packages/as_devices/c/Makefile
+++ b/target/packages/as_devices/c/Makefile
@@ -9,6 +9,8 @@ LDFLAGS=
OBJ=as_apf27_pwm.o
OBJ+= as_i2c.o
OBJ+= as_apf27_gpio.o
+OBJ+= as_93lcxx.o
+OBJ+= as_spi.o
LIBRARY=as_devices
LIB_VERS=1
diff --git a/target/packages/as_devices/c/as_93lcxx.c b/target/packages/as_devices/c/as_93lcxx.c
new file mode 100644
index 0000000..5caf3de
--- /dev/null
+++ b/target/packages/as_devices/c/as_93lcxx.c
@@ -0,0 +1,246 @@
+/*
+ * This software is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of the
+ * License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ *
+ * Copyright (C) 2009 Fabien Marteau <fab...@ar...>
+ *
+ */
+
+#include "as_93lcxx.h"
+#include "as_spi.h"
+#include <stdlib.h>
+#include <stdio.h>
+
+/* define start bits + opcode */
+#define READ (6)
+#define EWEN (4)
+#define EWEN_ADDR (3)
+#define ERASE (7)
+#define ERAL (6)
+#define ERAL_ADDR (2)
+#define WRITE (5)
+#define WRAL (4)
+#define WRAL_ADDR (1)
+#define EWDS (4)
+#define EWDS_ADDR (0)
+
+/** @brief Private functions to ease making spi instructions
+ */
+
+uint8_t address_len(uint8_t aType, uint8_t aWord_size)
+{
+ switch(aType)
+ {
+ case 46:
+ if( aWord_size == 16)
+ return 6;
+ else
+ return 7;
+ case 56:
+ case 66:
+ if( aWord_size == 16)
+ return 8;
+ else
+ return 9;
+ default:
+ return 0;
+ }
+}
+
+struct as_93lcxx_device * as_93lcxx_open(unsigned char *aSpidev_filename,
+ uint8_t aType,
+ uint32_t aSpeed,
+ uint8_t aWord_size)
+{
+ int ret=0;
+ struct as_93lcxx_device *dev;
+
+ /* open spidev bus */
+ ret = as_spi_open(aSpidev_filename);
+ if(ret < 0)
+ return NULL;
+
+ /* verify datas */
+ if(!( (aType == 46) || (aType == 56) || (aType == 66)))
+ return NULL;
+ if( (aWord_size < 1) || (aWord_size > 16))
+ return NULL;
+
+ dev = (struct as_93lcxx_device *)malloc(sizeof(struct as_93lcxx_device));
+ if(dev == NULL)
+ return NULL;
+
+ /* fill in spidev structure */
+ dev->spidev_filename = aSpidev_filename;
+ dev->type = aType;
+ dev->speed = aSpeed;
+ dev->word_size = aWord_size;
+
+ return dev;
+}
+
+void as_93lcxx_close(struct as_93lcxx_device *aDev)
+{
+ as_spi_close(aDev->fd);
+ free(aDev->spidev_filename);
+ free(aDev);
+}
+
+int32_t as_93lcxx_read(struct as_93lcxx_device *aDev, uint16_t aAddress)
+{
+ uint32_t data_out,msg=0;
+ int add_length;
+
+ add_length = address_len(aDev->type,aDev->word_size);
+
+ /* forge message */
+ msg = ((READ << (add_length+ (aDev->word_size)))
+ | (aAddress << (aDev->word_size)));
+
+ data_out = as_spi_msg(aDev->fd, msg,
+ 3 + add_length + aDev->word_size,
+ aDev->speed);
+
+ if( aDev->word_size == 8)
+ return data_out & 0xff;
+ else
+ return data_out & 0xffff;
+}
+
+int32_t as_93lcxx_ewen(struct as_93lcxx_device *aDev)
+{
+ uint32_t msg=0;
+ int add_length;
+
+ add_length = address_len(aDev->type,aDev->word_size);
+
+ /* forge message */
+ msg = ((EWEN << add_length)
+ |(EWEN_ADDR << (add_length-2))
+ );
+
+ as_spi_msg(aDev->fd, msg,
+ 3 + add_length,
+ aDev->speed);
+ return 0;
+}
+
+int32_t as_93lcxx_erase(struct as_93lcxx_device *aDev, uint16_t aAddress)
+{
+ uint32_t msg=0;
+ int add_length;
+
+ add_length = address_len(aDev->type,aDev->word_size);
+
+ /* forge message */
+ msg = ((ERASE << (add_length + aDev->word_size ))
+ | (aAddress << (aDev->word_size)));
+
+ as_spi_msg(aDev->fd, msg,
+ 3 + add_length + aDev->word_size,
+ aDev->speed);
+ return 0;
+
+}
+
+int32_t as_93lcxx_erase_all(struct as_93lcxx_device *aDev)
+{
+ uint32_t msg=0;
+ int add_length;
+
+ add_length = address_len(aDev->type,aDev->word_size);
+
+ /* forge message */
+ msg = ((ERAL << add_length)
+ |(ERAL_ADDR << (add_length-2))
+ );
+
+ as_spi_msg(aDev->fd, msg,
+ 3 + add_length + aDev->word_size,
+ aDev->speed);
+
+ /* TODO: wait for RDY/BSY_n */
+
+ return 0;
+
+
+}
+
+int32_t as_93lcxx_write(struct as_93lcxx_device *aDev,
+ uint16_t aAddress,
+ uint16_t aValue)
+{
+ uint32_t msg=0;
+ int add_length;
+
+ add_length = address_len(aDev->type,aDev->word_size);
+
+ /* forge message */
+ msg = ((WRITE << (add_length+ (aDev->word_size)))
+ | (aAddress << (aDev->word_size))
+ | aValue
+ );
+
+ as_spi_msg(aDev->fd, msg,
+ 3 + add_length + aDev->word_size,
+ aDev->speed);
+
+ /* TODO: mait for RDY/BSY_n */
+
+ return 0;
+}
+
+int32_t as_93lcxx_write_all(struct as_93lcxx_device *aDev,
+ uint16_t aValue)
+{
+ uint32_t msg=0;
+ int add_length;
+
+ add_length = address_len(aDev->type,aDev->word_size);
+
+ /* forge message */
+ msg = ((WRAL << (add_length+ (aDev->word_size)))
+ |(WRAL_ADDR << ((aDev->word_size) + (add_length-2)))
+ |aValue
+ );
+
+ as_spi_msg(aDev->fd, msg,
+ 3 + add_length + aDev->word_size,
+ aDev->speed);
+
+ /* TODO: mait for RDY/BSY_n */
+
+ return 0;
+}
+
+int32_t as_93lcxx_ewds(struct as_93lcxx_device *aDev)
+{
+ uint32_t msg=0;
+ int add_length;
+
+ add_length = address_len(aDev->type,aDev->word_size);
+
+ /* forge message */
+ msg = ((EWDS << add_length)
+ |(EWDS_ADDR << (add_length-2))
+ );
+
+ as_spi_msg(aDev->fd, msg,
+ 3 + add_length,
+ aDev->speed);
+ return 0;
+}
+
+
diff --git a/target/packages/as_devices/c/as_93lcxx.h b/target/packages/as_devices/c/as_93lcxx.h
new file mode 100644
index 0000000..06ebd7c
--- /dev/null
+++ b/target/packages/as_devices/c/as_93lcxx.h
@@ -0,0 +1,141 @@
+/*
+ * This software is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of the
+ * License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ *
+ * Copyright (C) 2009 Fabien Marteau <fab...@ar...>
+ *
+ */
+
+
+#ifndef AS_93LCXX_H_
+#define AS_93LCXX_H_
+
+
+/** @file
+ * @brief Provide standard commands to drive 93LCxx eeprom (from microchip)
+ *
+ *
+ */
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <inttypes.h>
+#include <sys/types.h>
+#include <stdio.h>
+
+/**
+ * Store eeprom parameters
+ */
+struct as_93lcxx_device {
+ unsigned char *spidev_filename; /**< filename of the spidev /dev file */
+ uint8_t type; /**< type of the eeprom : 46, 56 or 66 (see DS) */
+ uint32_t speed; /**< SCLK speed, in Hz */
+ uint8_t word_size; /**< word size for transaction, 8 or 16 */
+ int fd; /**< File handler for spidev bus */
+};
+
+/** @brief open the device
+ *
+ * @param spidev_filename path to spidev
+ * @type type of the eeprom
+ * @speed clock speed in Hz
+ * @word_size word size for transaction
+ *
+ * @return as_93lcxx_device struct pointer on success, NULL on error
+ */
+struct as_93lcxx_device * as_93lcxx_open(unsigned char *aSpidev_filename,
+ uint8_t aType,
+ uint32_t aSpeed,
+ uint8_t aWord_size);
+
+/** @brief close the device
+ *
+ * @param spidev_filename path to spidev
+ *
+ */
+void as_93lcxx_close(struct as_93lcxx_device *aDev);
+
+/** @brief read a value in address given
+ *
+ * @param dev 93LCxx structure
+ * @param address addresse of register to be read
+ *
+ * @return positive register value on success, negative value on error.
+ */
+int32_t as_93lcxx_read(struct as_93lcxx_device *aDev, uint16_t aAddress);
+
+/** @brief enable write on eeprom
+ *
+ * @param dev 93LCxx structure
+ *
+ * @return positive value on success, negative value on error
+ */
+int32_t as_93lcxx_ewen(struct as_93lcxx_device *aDev);
+
+/** @brief Force all data bits of the specified iaddress to 1
+ *
+ * @param dev 93LCxx structure
+ * @param address addresse of register to be erased
+ *
+ * @return positive register value on success, negative value on error.
+ */
+int32_t as_93lcxx_erase(struct as_93lcxx_device *aDev, uint16_t aAddress);
+
+/** @brief Force all bits in eeprom to 1
+ *
+ * @param dev 93LCxx structure
+ *
+ * @return positive register value on success, negative value on error.
+ */
+int32_t as_93lcxx_erase_all(struct as_93lcxx_device *aDev);
+
+/** @brief write a value in given address
+ *
+ * @param dev 93LCxx structure
+ * @param address addresse of register to be read
+ * @param value value to be wrote
+ *
+ * @return positive register value on success, negative value on error.
+ */
+int32_t as_93lcxx_write(struct as_93lcxx_device *aDev,
+ uint16_t aAddress,
+ uint16_t aValue);
+
+/** @brief write the entire memory array with value given
+ *
+ * @param dev 93LCxx structure
+ * @param value value to be wrote
+ *
+ * @return positive register value on success, negative value on error.
+ */
+int32_t as_93lcxx_write_all(struct as_93lcxx_device *aDev,
+ uint16_t aValue);
+
+/** @brief disable write on eeprom
+ *
+ * @param dev 93LCxx structure
+ *
+ * @return positive value on success, negative value on error
+ */
+int32_t as_93lcxx_ewds(struct as_93lcxx_device *aDev);
+
+
+#ifdef __cplusplus
+}
+#endif // __cplusplus
+
+#endif // AS_93LCXX_H_
diff --git a/target/packages/as_devices/c/as_i2c.c b/target/packages/as_devices/c/as_i2c.c
index 8d79ffd..0cb30eb 100644
--- a/target/packages/as_devices/c/as_i2c.c
+++ b/target/packages/as_devices/c/as_i2c.c
@@ -1,257 +1,119 @@
/*
-** THE ARMadeus Systems
-**
-** Copyright (C) 2009 The armadeus systems team
-** Fabien Marteau <fab...@ar...>
-**
-** This library is free software; you can redistribute it and/or
-** modify it under the terms of the GNU Lesser General Public
-** License as published by the Free Software Foundation; either
-** version 2.1 of the License, or (at your option) any later version.
-**
-** This library is distributed in the hope that it will be useful,
-** but WITHOUT ANY WARRANTY; without even the implied warranty of
-** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-** Lesser General Public License for more details.
-**
-** You should have received a copy of the GNU Lesser General Public
-** License along with this library; if not, write to the Free Software
-** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-**
-** TODO: manage force correctly
-*/
+ * This software is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of the
+ * License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ *
+ * Copyright (C) 2009 Benoît Ryder <be...@ry...>
+ *
+ */
-#include "as_i2c.h"
#include <stdio.h>
-#include <stdlib.h>
-#include <linux/i2c.h>
-#include <linux/i2c-dev.h>
-#include <sys/ioctl.h>
-#include <sys/types.h>
-#include <sys/stat.h>
+#include <string.h>
#include <fcntl.h>
+#include <sys/ioctl.h>
#include <unistd.h>
+#include <linux/i2c.h>
+#include <linux/i2c-dev.h>
-#define I2C_DEV_PATH "/dev/i2c-"
-
-/* global static variable */
-static int mFHandlerI2c[NUMBER_OF_I2C];
+#include "as_i2c.h"
-/*------------------------------------------------------------------------------*/
-/** Init i2c bus
- *
- * @param aBusNumber i2c bus number
- * @return error code
- */
-int
-as_i2c_init(int aBusNumber)
-{
- char buffer[40];
- /* make path */
- snprintf(buffer,39,"%s%d",I2C_DEV_PATH,aBusNumber);
- /* open i2c /dev */
- if((mFHandlerI2c[aBusNumber] = open(buffer,O_RDWR))<0)
- {
- perror("Open error : ");
- return -1;
- }
- return 0;
-}
+/// Format of path to I2C devices
+#define AS_I2C_DEV_PATH_FMT "/dev/i2c-%u"
+#define _STR(x) #x
+#define AS_I2C_DEV_PATH_SIZE (sizeof(AS_I2C_DEV_PATH_FMT)-2+sizeof(_STR(AS_I2C_DEV_COUNT))-1)
-/*------------------------------------------------------------------------------*/
-/** read byte on i2c bus
- * @param aBusNumber i2c bus number
- * @param aChipAddr chip address
- * @param aReg Register number
- * @param *aBuf char buffer
- * @return error code
- */
-int
-as_read_byte_data(int aBusNumber,
- unsigned char aChipAddr,
- unsigned char aReg,
- unsigned char *aBuf)
+int as_i2c_open(unsigned int i2c_id)
{
- // create an I2C write message (only one byte: the address)
- struct i2c_msg msg = { aChipAddr, 0, 1, aBuf };
- // create a I2C IOCTL request
- struct i2c_rdwr_ioctl_data rdwr = { &msg, 1 };
-
- aBuf[0] = aReg; // select aReg to read
-
- /* configure I2C slave */
- if ( ioctl(mFHandlerI2c[aBusNumber], I2C_SLAVE_FORCE, aChipAddr) < 0){
- perror("Ioctl error: ");
- return -1;
- }
-
- // write the desired aRegister address
- if ( ioctl( mFHandlerI2c[aBusNumber], I2C_RDWR, &rdwr ) < 0 ){
- printf("Write error\n");
- return -1;
- }
- msg.flags = I2C_M_RD; // read
-
- // read the result and write it in aBuf[0]
- if ( ioctl( mFHandlerI2c[aBusNumber], I2C_RDWR, &rdwr ) < 0 ){
- printf("Read error\n");
- return -1;
- }
- return 0;
+ char buf[AS_I2C_DEV_PATH_SIZE];
+ snprintf(buf, sizeof(buf), AS_I2C_DEV_PATH_FMT, i2c_id);
+ return open(buf, O_RDWR);
}
-/*------------------------------------------------------------------------------*/
-
-/** write byte on i2c bus
- * @param aChipAddr chip address
- * @param aReg Register number
- * @param aBuf char value to write
- * @return error return
- */
-int
-as_write_byte_data(int aBusNumber,
- unsigned char aChipAddr,
- unsigned char aReg,
- unsigned char aValue)
+int as_i2c_close(int fd)
{
- unsigned char buf[2] = {aReg,aValue}; // initialise a data buffer with
- // address and data
-
- // create an I2C write message
- struct i2c_msg msg = { aChipAddr, 0, sizeof(buf), buf };
- // create a I2C IOCTL request
- struct i2c_rdwr_ioctl_data rdwr = { &msg, 1 };
-
- /* configure I2C slave */
- if ( ioctl(mFHandlerI2c[aBusNumber],I2C_SLAVE_FORCE, aChipAddr) < 0){
- perror(">Ioctl error: ");
- return -1;
- }
-
- if ( ioctl( mFHandlerI2c[aBusNumber], I2C_RDWR, &rdwr ) < 0 ){
- printf("Write error\n");
- return -1;
- }
- return 0;
+ return close(fd);
}
-/*------------------------------------------------------------------------------*/
-
-/** write byte on i2c bus
- * @param aChipAddr chip address
- * @param aBuf char value to write
- * @return error return
- */
-int
-as_write_byte(int aBusNumber,
- unsigned char aChipAddr,
- unsigned char aValue)
+int as_i2c_set_slave(int fd, uint8_t addr)
{
- unsigned char buf[1] = {aValue}; // initialise a data buffer with
- // address and data
-
- // create an I2C write message
- struct i2c_msg msg = { aChipAddr, 0, sizeof(buf), buf };
- // create a I2C IOCTL request
- struct i2c_rdwr_ioctl_data rdwr = { &msg, 1 };
-
- /* configure I2C slave */
- if ( ioctl(mFHandlerI2c[aBusNumber], I2C_SLAVE_FORCE, aChipAddr) < 0){
- perror("Ioctl error: ");
- return -1;
- }
+ if( ioctl(fd, I2C_SLAVE_FORCE, addr) < 0 )
+ return -1;
+ return 0;
+}
- if ( ioctl( mFHandlerI2c[aBusNumber], I2C_RDWR, &rdwr ) < 0 ){
- printf("Write error\n");
- return -1;
- }
- return 0;
+int as_i2c_read(int fd, uint8_t addr, uint8_t *data, size_t n)
+{
+ struct i2c_msg msg = { addr, I2C_M_RD, n, data };
+ struct i2c_rdwr_ioctl_data rdwr = { &msg, 1 };
+ if( ioctl(fd, I2C_RDWR, &rdwr) < 0 )
+ return -1;
+ return 0;
}
-/*------------------------------------------------------------------------------*/
-/** Write several byte on I2C
- * @param aBusNumber i2c bus number
- * @param aChipAddr Chip address
- * @param aBuff message
- * @param aSize size of message
- * @return error code
- */
-int
-as_write_multiple_bytes(int aBusNumber,
- unsigned char aChipAddr,
- unsigned char *aBuff,
- int aSize)
+int as_i2c_write(int fd, uint8_t addr, const uint8_t *data, size_t n)
{
- // create an I2C write message
- struct i2c_msg msg = { aChipAddr, 0, aSize, aBuff };
- // create a I2C IOCTL request
- struct i2c_rdwr_ioctl_data rdwr = { &msg, 1 };
-
- /* configure I2C slave */
- if ( ioctl(mFHandlerI2c[aBusNumber], I2C_SLAVE_FORCE, aChipAddr) < 0){
- perror("Ioctl error: ");
- return -1;
- }
-
- if ( ioctl( mFHandlerI2c[aBusNumber], I2C_RDWR, &rdwr ) < 0 ){
- printf("Write error\n");
- return -1;
- }
-
- return 0;
+ struct i2c_msg msg = { addr, 0, n, (uint8_t *)data };
+ struct i2c_rdwr_ioctl_data rdwr = { &msg, 1 };
+ if( ioctl(fd, I2C_RDWR, &rdwr) < 0 )
+ return -1;
+ return 0;
}
-/*------------------------------------------------------------------------------*/
-/** read several bytes on I2C
- * @param aBusNumber i2c bus number
- * @param aChipAddr Chip address
- * @param aBuff message
- * @param aSize size of message
- * @return error code
- */
-int
-as_read_multiple_bytes(int aBusNumber,
- unsigned char aChipAddr,
- unsigned char *aBuff,
- int aSize)
+int as_i2c_read_reg(int fd, uint8_t addr, uint8_t reg, uint8_t *data, size_t n)
{
- // create an I2C read message
- struct i2c_msg msg = { aChipAddr,
- I2C_M_RD,
- aSize,
- aBuff };
- // create a I2C IOCTL request
- struct i2c_rdwr_ioctl_data rdwr = { &msg, 1 };
+ // write reg
+ struct i2c_msg msg = { addr, 0, 1, ® };
+ struct i2c_rdwr_ioctl_data rdwr = { &msg, 1 };
+ if( ioctl(fd, I2C_RDWR, &rdwr) < 0 )
+ return -1;
+ // read data
+ msg.flags = I2C_M_RD;
+ msg.len = n;
+ msg.buf = data;
+ if( ioctl(fd, I2C_RDWR, &rdwr) < 0 )
+ return -2;
+ return 0;
+}
- /* configure I2C slave */
- if ( ioctl(mFHandlerI2c[aBusNumber], I2C_SLAVE_FORCE, aChipAddr) < 0){
- perror("Ioctl error: ");
- return -1;
- }
+int as_i2c_write_reg(int fd, uint8_t addr, uint8_t reg, const uint8_t *data, size_t n)
+{
+ uint8_t buf[n+1];
+ buf[0] = reg;
+ memcpy(buf+1, data, n);
+ return as_i2c_write(fd, addr, buf, sizeof(buf));
+}
- if ( ioctl( mFHandlerI2c[aBusNumber], I2C_RDWR, &rdwr ) < 0 ){
- printf("Write error\n");
- return -1;
- }
- return 0;
+int as_i2c_read_reg_byte(int fd, uint8_t addr, uint8_t reg)
+{
+ uint8_t val;
+ int ret = as_i2c_read_reg(fd, addr, reg, &val, 1);
+ if( ret < 0 )
+ return ret;
+ return val;
}
-/*------------------------------------------------------------------------------*/
-
-/** Close i2c bus files
- * @param aBusNumber i2c bus number
- * @return error code
- */
-int
-as_close(int aBusNumber)
+int as_i2c_write_reg_byte(int fd, uint8_t addr, uint8_t reg, uint8_t val)
{
- return close(mFHandlerI2c[aBusNumber]);
+ uint8_t buf[2] = { reg, val };
+ return as_i2c_write(fd, addr, buf, 2);
}
+
diff --git a/target/packages/as_devices/c/as_i2c.h b/target/packages/as_devices/c/as_i2c.h
index 678a649..f0ec945 100644
--- a/target/packages/as_devices/c/as_i2c.h
+++ b/target/packages/as_devices/c/as_i2c.h
@@ -1,64 +1,131 @@
/*
-** THE ARMadeus Systems
-**
-** Copyright (C) 2009 The armadeus systems team
-** Fabien Marteau <fab...@ar...>
-**
-** This library is free software; you can redistribute it and/or
-** modify it under the terms of the GNU Lesser General Public
-** License as published by the Free Software Foundation; either
-** version 2.1 of the License, or (at your option) any later version.
-**
-** This library is distributed in the hope that it will be useful,
-** but WITHOUT ANY WARRANTY; without even the implied warranty of
-** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-** Lesser General Public License for more details.
-**
-** You should have received a copy of the GNU Lesser General Public
-** License along with this library; if not, write to the Free Software
-** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-*/
-
-#ifndef __ASI2C_H__
-#define __ASI2C_H__
+ * This software is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of the
+ * License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ *
+ * Copyright (C) 2009 Benoît Ryder <be...@ry...>
+ *
+ */
+
+
+#ifndef AS_I2C_H_
+#define AS_I2C_H_
+
+/** @file
+ * @brief Provide access to I2C devices.
+ *
+ * Once opened with as_i2c_open(), the I2C slave address to talk with should be
+ * set using as_i2c_set_slave(). Then, standard \e read(), \e write() and
+ * close() can be used. Otherwise, methods provided by this module can be used.
+ * These methods does not use standard <em>read()/write</em> methods nor
+ * combined transactions (several read/write messages in the same transaction).
+ *
+ * @note All methods will leave \e errno unchanged on system call errors, which
+ * will happen when -1 is returned.
+ */
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
-#define NUMBER_OF_I2C 2
+#include <inttypes.h>
+#include <sys/types.h>
+
+
+/// Number of I2C busses on system
+#define AS_I2C_DEV_COUNT 2
+
+
+/** @brief Open an I2C bus.
+ *
+ * \e i2c_id is the ID of the I2C bus to open, it should strictly lower than
+ * \e AS_I2C_DEV_COUNT.
+ *
+ * @return the file descriptor of the opened device, -1 on error.
+ */
+int as_i2c_open(unsigned int i2c_id);
+
+/** @brief Close an I2C device.
+ *
+ * @return 0 on success, -1 on error.
+ *
+ * @note This is an alias of the standard \e close() system call.
+ */
+int as_i2c_close(int fd);
+
+/** @brief Set I2C slave address to be used.
+ *
+ * @return 0 on success, -1 on error.
+ */
+int as_i2c_set_slave(int fd, uint8_t addr);
+
+
+/** @brief Read several bytes.
+ *
+ * @return 0 on success, -1 on error.
+ */
+int as_i2c_read(int fd, uint8_t addr, uint8_t *data, size_t n);
+
+/** @brief Write several bytes.
+ *
+ * @return 0 on success, -1 on error.
+ */
+int as_i2c_write(int fd, uint8_t addr, const uint8_t *data, size_t n);
-int as_i2c_init(int aBusNumber);
-int as_read_byte_data(int aBusNumber,
- unsigned char aChipAddr,
- unsigned char aReg,
- unsigned char *aBuf);
+/** @brief Read at a given register address.
+ *
+ * Send the \e reg byte, then read bytes.
+ *
+ * @return 0 on success, -1 on write error (\e reg byte), -2 on read error.
+ */
+int as_i2c_read_reg(int fd, uint8_t addr, uint8_t reg, uint8_t *data, size_t n);
-int as_write_byte_data(int aBusNumber,
- unsigned char aChipAddr,
- unsigned char aReg,
- unsigned char aValue);
+/** @brief Write at a given register address.
+ *
+ * Send the \e reg byte followed by data.
+ *
+ * @return 0 on success, -1 on error.
+ */
+int as_i2c_write_reg(int fd, uint8_t addr, uint8_t reg, const uint8_t *data, size_t n);
-int as_write_byte(int aBusNumber,
- unsigned char aChipAddr,
- unsigned char aValue);
-int as_write_multiple_bytes(int aBusNumber,
- unsigned char aChipAddr,
- unsigned char *aBuff,
- int aSize);
+/** @brief Read a given register byte.
+ *
+ * Send the \e reg byte, then read and return a single byte.
+ *
+ * @return the read byte, -1 on write error (\e reg byte), -2 on read error.
+ *
+ * @note Equivalent to \e as_i2c_read_reg() with a 1-byte buffer, but return
+ * the read byte.
+ */
+int as_i2c_read_reg_byte(int fd, uint8_t addr, uint8_t reg);
-int as_read_multiple_bytes(int aBusNumber,
- unsigned char aChipAddr,
- unsigned char *aBuff,
- int aSize);
+/** @brief Write a given register byte.
+ *
+ * Send the \e reg byte followed by the \e val byte.
+ *
+ * @return 0 on success, -1 on error.
+ *
+ * @note Equivalent to \e as_i2c_write() with a 2-byte buffer.
+ */
+int as_i2c_write_reg_byte(int fd, uint8_t addr, uint8_t reg, uint8_t val);
-int as_close(int aBusNumber);
#ifdef __cplusplus
}
#endif // __cplusplus
-#endif // __ASI2C_H__
+#endif // AS_I2C_H_
diff --git a/target/packages/as_devices/c/as_spi.c b/target/packages/as_devices/c/as_spi.c
new file mode 100644
index 0000000..5d82054
--- /dev/null
+++ b/target/packages/as_devices/c/as_spi.c
@@ -0,0 +1,168 @@
+/*
+ * This software is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of the
+ * License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ *
+ * Copyright (C) 2009 Fabien Marteau <fab...@ar...>
+ *
+ */
+
+#include "as_spi.h"
+#include <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <fcntl.h>
+#include <string.h>
+
+#include <sys/ioctl.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+
+#include <linux/types.h>
+#include <linux/spi/spidev.h>
+
+
+int as_spi_open(const unsigned char *spidev_name)
+{
+ int fd;
+ fd = open((char *)spidev_name, O_RDWR);
+ if (fd < 0) {
+ perror("open");
+ return -1;
+ }
+ return fd;
+}
+
+int as_spi_set_mode(int fd, uint8_t mode)
+{
+ /* TODO */
+ return -1;
+}
+
+int as_spi_set_lsb(int fd, uint8_t lsb)
+{
+ /* TODO */
+ return -1;
+}
+
+int as_spi_set_bits_per_word(int fd, uint8_t bits)
+{
+ /* TODO */
+ return -1;
+}
+
+int as_spi_set_speed(int fd)
+{
+ /* TODO */
+ return -1;
+}
+
+int as_spi_get_mode(int fd)
+{
+ uint8_t mode;
+
+ if (ioctl(fd, SPI_IOC_RD_MODE, &mode) < 0) {
+ perror("SPI rd_mode");
+ return -1;
+ }
+ return mode;
+}
+
+int as_spi_get_lsb(int fd)
+{
+ uint8_t lsb;
+
+ if (ioctl(fd, SPI_IOC_RD_LSB_FIRST, &lsb) < 0) {
+ perror("SPI rd_lsb_fist");
+ return -1;
+ }
+
+ return lsb;
+}
+
+int as_spi_get_bits_per_word(int fd)
+{
+ uint8_t bits;
+ if (ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits) < 0) {
+ perror("SPI bits_per_word");
+ return-1;
+ }
+ return bits;
+}
+
+int as_spi_get_speed(int fd)
+{
+ uint8_t speed;
+ if (ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed) < 0) {
+ perror("SPI max_speed_hz");
+ return -1;
+ }
+ return speed;
+}
+
+uint32_t as_spi_msg(int aFd,
+ uint32_t aMsg,
+ size_t aLen,
+ uint32_t aSpeed)
+{
+ uint32_t msg;
+ size_t len;
+ struct spi_ioc_transfer xfer[1];
+ unsigned char buf[32];
+ unsigned char buf_read[32];
+
+ int status;
+ int i;
+ msg = aMsg;
+ len = aLen;
+
+ memset(xfer, 0, sizeof xfer);
+ memset(buf, 0, sizeof buf);
+ memset(buf_read, 0, sizeof buf_read);
+
+ if (aLen > sizeof buf)
+ len = sizeof buf;
+ for (i = len;i > 0;i--)
+ {
+ buf[i-1] = msg & 0x01;
+ msg = msg >> 1;
+ }
+
+ xfer[0].tx_buf = (__u64) buf;
+ xfer[0].len = len;
+ xfer[0].rx_buf = (__u64) buf_read;
+ xfer[0].speed_hz = aSpeed;
+ xfer[0].bits_per_word = 1;
+
+ status = ioctl(aFd, SPI_IOC_MESSAGE(1), xfer);
+ if (status < 0) {
+ perror("SPI_IOC_MESSAGE");
+ return 0;
+ }
+ for (i = 0;i < len;i++)
+ {
+ msg = msg | buf_read[i];
+ msg = msg << 1;
+ }
+ return msg;
+}
+
+void as_spi_close(int fd)
+{
+ close(fd);
+ return;
+}
+
+
+
diff --git a/target/packages/as_devices/c/as_spi.h b/target/packages/as_devices/c/as_spi.h
new file mode 100644
index 0000000..bde5890
--- /dev/null
+++ b/target/packages/as_devices/c/as_spi.h
@@ -0,0 +1,130 @@
+/*
+ * This software is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of the
+ * License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ *
+ * Copyright (C) 2009 Fabien Marteau <fab...@ar...>
+ *
+ */
+
+
+#ifndef AS_SPI_H_
+#define AS_SPI_H_
+
+/** @file
+ * @brief Provide access to SPI Bus.
+ *
+ */
+
+#ifdef __cplusplus
+extern "C" {
+#endif // __cplusplus
+
+#include <inttypes.h>
+#include <sys/types.h>
+
+/** @brief Open a SPI bus.
+ *
+ * @return the file descriptor of the opened device, -1Â on error
+ */
+int as_spi_open(const unsigned char *spidev_name);
+
+/** @brief Set spi bus mode
+ *
+ * @param fd spidev file handler
+ * @param mode
+ *
+ * @return mode if positive value, negative value on error
+ *
+ */
+int as_spi_set_mode(int fd, uint8_t mode);
+
+/** @brief Set bits order
+ *
+ * @param fd spidev file handler
+ * @param lsb if 1 lsb first, else msb first
+ *
+ * @return lsb first if positive, msb first if 0, negative value on error
+ */
+int as_spi_set_lsb(int fd, uint8_t lsb);
+
+/** @brief Set bits per word
+ *
+ * @param fd spidev file handler
+ * @param bits number of bits per word
+ *
+ * @return bit per word if positive value, negative value on error
+ */
+int as_spi_set_bits_per_word(int fd, uint8_t bits);
+
+/** @brief Get spi bus mode
+ *
+ * @param fd spidev file handler
+ *
+ * @return mode if positive value, negative value on error
+ *
+ */
+int as_spi_get_mode(int fd);
+
+/** @brief Get bits order
+ *
+ * @param fd spidev file handler
+ *
+ * @return lsb first if positive, msb first if 0, negative value on error
+ */
+int as_spi_get_lsb(int fd);
+
+/** @brief Get clock bus speed
+ *
+ * @param fd spidev file handler
+ *
+ * @return speed in Hz, negative value on error
+ */
+int as_spi_get_speed(int fd);
+
+
+/** @brief Get bits per word
+ *
+ * @param fd spidev file handler
+ *
+ * @return bit per word if positive value, negative value on error
+ */
+int as_spi_get_bits_per_word(int fd);
+
+/** @brief Forge arbitrary length message (32bits max) and send it
+ *
+ * @param fd spidev file handler
+ * @param msg right adjusted message
+ * @param len lenght of the message
+ * @param speed clock speed in Hz
+ *
+ * @return message read
+ */
+uint32_t as_spi_msg(int fd,
+ uint32_t msg,
+ size_t len,
+ uint32_t speed);
+
+/** @brief Close a SPI bus.
+ *
+ */
+void as_spi_close(int fd);
+
+
+#ifdef __cplusplus
+}
+#endif // __cplusplus
+
+#endif // AS_SPI_H_
+
diff --git a/target/packages/as_devices/cpp/as_apf27_gpio.cpp b/target/packages/as_devices/cpp/as_apf27_gpio.cpp
index 040f987..f9b19e9 100644
--- a/target/packages/as_devices/cpp/as_apf27_gpio.cpp
+++ b/target/packages/as_devices/cpp/as_apf27_gpio.cpp
@@ -20,7 +20,7 @@
*/
#include "as_apf27_gpio.hpp"
-static AsDynamicTable AsApf27Gpio::mInstances = new AsDynamicTable();
+AsDynamicTable * AsApf27Gpio::mInstances = new AsDynamicTable();
/*------------------------------------------------------------------------------*/
/** Singleton constructor
@@ -47,7 +47,7 @@ AsApf27Gpio::getInstance(char aPortLetter)
AsApf27Gpio::AsApf27Gpio(char aPortLetter)
{
mPortLetter = aPortLetter;
- as_apf27_gpio_init(aPortChar);
+ as_apf27_gpio_init(aPortLetter);
}
/*------------------------------------------------------------------------------*/
diff --git a/target/packages/as_devices/cpp/as_dynamic_table.hpp b/target/packages/as_devices/cpp/as_dynamic_table.hpp
index 7a18e8e..024c00c 100644
--- a/target/packages/as_devices/cpp/as_dynamic_table.hpp
+++ b/target/packages/as_devices/cpp/as_dynamic_table.hpp
@@ -22,6 +22,8 @@
#ifndef __ASDYNAMICTABLE_HPP_
#define __ASDYNAMICTABLE_HPP_
+#include <stdio.h>
+
class AsDynamicTable {
public:
AsDynamicTable()
diff --git a/target/packages/as_devices/cpp/as_i2c.cpp b/target/packages/as_devices/cpp/as_i2c.cpp
index fdbeb7d..cadad77 100644
--- a/target/packages/as_devices/cpp/as_i2c.cpp
+++ b/target/packages/as_devices/cpp/as_i2c.cpp
@@ -31,13 +31,13 @@ AsI2c *
AsI2c::getInstance(int aBusNumber)
{
AsI2c * instance;
- instance = mInstances->getInstance(aBusNumber);
+ instance = (AsI2c *)mInstances->getInstance(aBusNumber);
if(instance == NULL)
{
- instance = AsI2c(aBusNumber);
+ instance = new AsI2c(aBusNumber);
mInstances->setInstance(instance,aBusNumber);
}
- return instance
+ return instance;
}
/*------------------------------------------------------------------------------*/
@@ -51,7 +51,7 @@ AsI2c::AsI2c(int aBusNumber)
ret = as_i2c_init(aBusNumber);
if (ret < 0)
{
- cout << "AsI2C initialization error" << endl;
+ std::cout << "AsI2C initialization error" << std::endl;
}
}
/*------------------------------------------------------------------------------*/
hooks/post-receive
--
armadeus
|