[Armadeus-commitlog] armadeus branch, master, updated. release-3.3-31-g6315679
Brought to you by:
sszy
|
From: Julien B a. A. <ar...@us...> - 2010-09-27 08:06:07
|
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 631567984dee80bb1097354e5dc129b4888851e2 (commit)
via 8600a6ad4f8c85b6b3a714a984d55114270c42ac (commit)
via 2da68cd6dc7eeeb31b7394f75891ab0177ac6397 (commit)
via 31ce6a2559c8d10bee24a847270aa14aa721f4b6 (commit)
from 9c74dab187adea3c7a806e868c3a99e79c6a9dfd (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 631567984dee80bb1097354e5dc129b4888851e2
Author: Julien Boibessot <jul...@ar...>
Date: Mon Sep 27 10:05:25 2010 +0200
[ASDEVICES] add a helpers module to factorize methods used in other modules
commit 8600a6ad4f8c85b6b3a714a984d55114270c42ac
Author: Julien Boibessot <jul...@ar...>
Date: Mon Sep 27 09:57:48 2010 +0200
[ASDEVICES] as_i2c.h: improves functions description
commit 2da68cd6dc7eeeb31b7394f75891ab0177ac6397
Author: Julien Boibessot <jul...@ar...>
Date: Mon Sep 27 09:56:56 2010 +0200
Indentation
commit 31ce6a2559c8d10bee24a847270aa14aa721f4b6
Author: Julien Boibessot <jul...@ar...>
Date: Thu Sep 23 18:27:51 2010 +0200
[AS_DEVICES] Indentation & beautifying
-----------------------------------------------------------------------
Summary of changes:
target/packages/as_devices/c/Makefile | 15 +-
target/packages/as_devices/c/as_gpio.c | 69 +++----
target/packages/as_devices/c/as_gpio.h | 8 +-
target/packages/as_devices/c/as_i2c.c | 97 ++++++-----
target/packages/as_devices/c/as_i2c.h | 32 +++-
target/packages/as_devices/c/as_pwm.c | 196 ++++++--------------
target/packages/as_devices/c/helpers.c | 76 ++++++++
.../{cpp/as_apf27_pwm.hpp => c/helpers.h} | 71 +++-----
8 files changed, 279 insertions(+), 285 deletions(-)
create mode 100644 target/packages/as_devices/c/helpers.c
copy target/packages/as_devices/{cpp/as_apf27_pwm.hpp => c/helpers.h} (51%)
diff --git a/target/packages/as_devices/c/Makefile b/target/packages/as_devices/c/Makefile
index 1fc7a5c..1164cc2 100644
--- a/target/packages/as_devices/c/Makefile
+++ b/target/packages/as_devices/c/Makefile
@@ -13,13 +13,14 @@ CC:=$(ARMADEUS_TOOLCHAIN_PATH)/arm-linux-gcc
STRIP:=$(ARMADEUS_TOOLCHAIN_PATH)arm-linux-sstrip
CFLAGS+=
-OBJ=as_pwm.o
-OBJ+= as_i2c.o
-OBJ+= as_gpio.o
-OBJ+= as_93lcxx.o
-OBJ+= as_spi.o
-OBJ+= as_max1027.o
-OBJ+= as_max5821.o
+OBJ = as_pwm.o
+OBJ += as_i2c.o
+OBJ += as_gpio.o
+OBJ += as_93lcxx.o
+OBJ += as_spi.o
+OBJ += as_max1027.o
+OBJ += as_max5821.o
+OBJ += helpers.o
LIBRARY=as_devices
LIB_VERS=1
diff --git a/target/packages/as_devices/c/as_gpio.c b/target/packages/as_devices/c/as_gpio.c
index bab67c0..7959313 100644
--- a/target/packages/as_devices/c/as_gpio.c
+++ b/target/packages/as_devices/c/as_gpio.c
@@ -19,8 +19,6 @@
** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
-#include "as_gpio.h"
-
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
@@ -30,6 +28,11 @@
#include <sys/ioctl.h>
#include <linux/ppdev.h>
+/*#define DEBUG*/
+#include "helpers.h"
+#include "as_gpio.h"
+
+
/* IOCTL */
/* direction: 1 output, 0 input */
#define GPIORDDIRECTION _IOR(PP_IOCTL, 0xF0, int)
@@ -54,20 +57,13 @@
#define BUFF_SIZE (300)
-//#define DEBUG
-#ifdef DEBUG
-# define ERROR(fmt, ...) printf(fmt, ##__VA_ARGS__)
-#else
-# define ERROR(fmt, ...) /*fmt, ##__VA_ARGS__*/
-#endif
-
/*------------------------------------------------------------------------------*/
struct as_gpio_device *as_gpio_open(char aPortChar, int aPinNum)
{
struct as_gpio_device *dev;
char gpio_file_path[50];
- int ret=0;
+ int ret = 0;
int file;
int value;
@@ -87,8 +83,7 @@ struct as_gpio_device *as_gpio_open(char aPortChar, int aPinNum)
}
dev = (struct as_gpio_device *)malloc(sizeof(struct as_gpio_device));
- if (dev == NULL)
- {
+ if (dev == NULL) {
ERROR("Can't allocate memory for gpio device structure\n");
goto malloc_error;
}
@@ -99,8 +94,7 @@ struct as_gpio_device *as_gpio_open(char aPortChar, int aPinNum)
value = 1;
ret = ioctl(dev->fpin, GPIOWRMODE, &value); /* set pin on gpio mode */
- if (ret < 0)
- {
+ if (ret < 0) {
ERROR("can't set pin in gpio mode.\n");
goto mode_error;
}
@@ -120,10 +114,10 @@ path_error:
int32_t as_gpio_set_pin_direction(struct as_gpio_device *aDev, int aDirection)
{
- int ret=0;
+ int ret = 0;
int portval;
- portval = (aDirection != 0)?1:0;
+ portval = (aDirection != 0) ? 1 : 0;
ret = ioctl(aDev->fpin, GPIOWRDIRECTION, &portval);
if (ret < 0) {
@@ -137,7 +131,7 @@ int32_t as_gpio_set_pin_direction(struct as_gpio_device *aDev, int aDirection)
int32_t as_gpio_get_pin_direction(struct as_gpio_device *aDev)
{
- int ret=0;
+ int ret = 0;
int portval;
ret = ioctl(aDev->fpin, GPIORDDIRECTION, &portval);
@@ -145,17 +139,17 @@ int32_t as_gpio_get_pin_direction(struct as_gpio_device *aDev)
ERROR("Can't get gpio direction\n");
return ret;
}
- return ((portval !=0)?1:0);
+ return ((portval !=0) ? 1 : 0);
}
/*------------------------------------------------------------------------------*/
int32_t as_gpio_set_pin_value(struct as_gpio_device *aDev, int aValue)
{
- int ret=0;
+ int ret = 0;
int portval;
- portval = (aValue != 0)?1:0;
+ portval = (aValue != 0) ? 1 : 0;
ret = ioctl(aDev->fpin, GPIOWRDATA, &portval);
if (ret < 0) {
ERROR("Can't write gpio data\n");
@@ -169,7 +163,7 @@ int32_t as_gpio_set_pin_value(struct as_gpio_device *aDev, int aValue)
int32_t as_gpio_get_pin_value(struct as_gpio_device *aDev)
{
- int ret=0;
+ int ret = 0;
int portval;
ret = ioctl(aDev->fpin, GPIORDDATA, &portval);
@@ -193,14 +187,11 @@ int32_t as_gpio_blocking_get_pin_value(struct as_gpio_device *aDev,
{
int ret;
char value;
-
fd_set rfds;
struct timeval tv;
int retval;
-
- if (as_gpio_get_irq_mode(aDev) == GPIO_IRQ_MODE_NOINT)
- {
+ if (as_gpio_get_irq_mode(aDev) == GPIO_IRQ_MODE_NOINT) {
ERROR("irq must be configured before\n");
return -1;
}
@@ -213,11 +204,9 @@ int32_t as_gpio_blocking_get_pin_value(struct as_gpio_device *aDev,
/* flush the file */
retval = select(aDev->fpin+1, &rfds, NULL, NULL, &tv);
- if (retval > 0)
- {
+ if (retval > 0) {
ret = read(aDev->fpin, &value, 1);
- if (ret < 0)
- {
+ if (ret < 0) {
ERROR("Can't read pin value\n");
return -1;
}
@@ -235,7 +224,7 @@ int32_t as_gpio_blocking_get_pin_value(struct as_gpio_device *aDev,
int32_t as_gpio_get_pullup_value(struct as_gpio_device *aDev)
{
- int ret=0;
+ int ret = 0;
int portval;
ret = ioctl(aDev->fpin, GPIORDPULLUP, &portval);
@@ -249,14 +238,13 @@ int32_t as_gpio_get_pullup_value(struct as_gpio_device *aDev)
} else {
return 0;
}
-
}
/*------------------------------------------------------------------------------*/
int32_t as_gpio_set_pullup_value(struct as_gpio_device *aDev, int aValue)
{
- int ret=0;
+ int ret = 0;
int portval;
portval = (aValue != 0)?1:0;
@@ -274,7 +262,7 @@ int32_t as_gpio_set_pullup_value(struct as_gpio_device *aDev, int aValue)
int32_t as_gpio_get_irq_mode(struct as_gpio_device *aDev)
{
- int ret=0;
+ int ret = 0;
int portval;
ret = ioctl(aDev->fpin, GPIORDIRQMODE, &portval);
@@ -290,10 +278,9 @@ int32_t as_gpio_get_irq_mode(struct as_gpio_device *aDev)
int32_t as_gpio_set_irq_mode(struct as_gpio_device *aDev, int aMode)
{
- int ret=0;
+ int ret = 0;
int portval;
char buffer[BUFF_SIZE];
-
fd_set rfds;
struct timeval tv;
int retval;
@@ -314,8 +301,7 @@ int32_t as_gpio_set_irq_mode(struct as_gpio_device *aDev, int aMode)
/* flush the file */
retval = select(aDev->fpin, &rfds, NULL, NULL, &tv);
- if (retval > 0)
- {
+ if (retval > 0) {
read(aDev->fpin, buffer, BUFF_SIZE);
}
return 0;
@@ -324,12 +310,16 @@ int32_t as_gpio_set_irq_mode(struct as_gpio_device *aDev, int aMode)
/*------------------------------------------------------------------------------*/
int32_t as_gpio_get_pin_num(struct as_gpio_device *aDev)
-{ return aDev->pin_num;}
+{
+ return aDev->pin_num;
+}
/*------------------------------------------------------------------------------*/
int32_t as_gpio_get_port_letter(struct as_gpio_device *aDev)
-{ return aDev->port_letter;}
+{
+ return aDev->port_letter;
+}
/*------------------------------------------------------------------------------*/
@@ -337,6 +327,7 @@ int32_t as_gpio_close(struct as_gpio_device *aDev)
{
close(aDev->fpin);
free(aDev);
+
return 0;
}
diff --git a/target/packages/as_devices/c/as_gpio.h b/target/packages/as_devices/c/as_gpio.h
index 1a077ce..38b2dac 100644
--- a/target/packages/as_devices/c/as_gpio.h
+++ b/target/packages/as_devices/c/as_gpio.h
@@ -26,7 +26,7 @@
#ifdef __cplusplus
extern "C" {
-#endif // __cplusplus
+#endif /* __cplusplus */
#define GPIO_IRQ_MODE_NOINT (0)
#define GPIO_IRQ_MODE_RISING (1)
@@ -159,12 +159,12 @@ int32_t as_gpio_get_port_letter(struct as_gpio_device *aDev);
*
* @param aDev as_gpio_device pointer structure
*
- * @return pin value if positive or null, error if negative
+ * @return error if negative
*/
int32_t as_gpio_close(struct as_gpio_device *aDev);
#ifdef __cplusplus
}
-#endif // __cplusplus
+#endif /* __cplusplus */
-#endif // __ASGPIO_H__
+#endif /* __ASGPIO_H__ */
diff --git a/target/packages/as_devices/c/as_i2c.c b/target/packages/as_devices/c/as_i2c.c
index 0cb30eb..970bc89 100644
--- a/target/packages/as_devices/c/as_i2c.c
+++ b/target/packages/as_devices/c/as_i2c.c
@@ -30,7 +30,7 @@
#include "as_i2c.h"
-/// Format of path to I2C devices
+/* 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)
@@ -38,82 +38,93 @@
int as_i2c_open(unsigned int i2c_id)
{
- char buf[AS_I2C_DEV_PATH_SIZE];
- snprintf(buf, sizeof(buf), AS_I2C_DEV_PATH_FMT, i2c_id);
+ char buf[AS_I2C_DEV_PATH_SIZE];
- return open(buf, O_RDWR);
+ snprintf(buf, sizeof(buf), AS_I2C_DEV_PATH_FMT, i2c_id);
+
+ return open(buf, O_RDWR);
}
int as_i2c_close(int fd)
{
- return close(fd);
+ return close(fd);
}
int as_i2c_set_slave(int fd, uint8_t addr)
{
- if( ioctl(fd, I2C_SLAVE_FORCE, addr) < 0 )
- return -1;
- return 0;
+ if (ioctl(fd, I2C_SLAVE_FORCE, addr) < 0)
+ 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;
+ 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;
}
int as_i2c_write(int fd, uint8_t addr, const uint8_t *data, size_t n)
{
- 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;
+ 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;
}
int as_i2c_read_reg(int fd, uint8_t addr, uint8_t reg, uint8_t *data, size_t n)
{
- // 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;
+ /* 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;
}
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));
+ uint8_t buf[n+1];
+
+ buf[0] = reg;
+ memcpy(buf+1, data, n);
+
+ return as_i2c_write(fd, addr, buf, sizeof(buf));
}
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;
+ uint8_t val;
+ int ret = as_i2c_read_reg(fd, addr, reg, &val, 1);
+
+ if (ret < 0)
+ return ret;
+
+ return val;
}
int as_i2c_write_reg_byte(int fd, uint8_t addr, uint8_t reg, uint8_t val)
{
- uint8_t buf[2] = { reg, val };
- return as_i2c_write(fd, addr, buf, 2);
-}
-
+ 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 8f967b4..2865631 100644
--- a/target/packages/as_devices/c/as_i2c.h
+++ b/target/packages/as_devices/c/as_i2c.h
@@ -61,44 +61,54 @@ int as_i2c_open(unsigned int i2c_id);
*/
int as_i2c_close(int fd);
-/** @brief Set I2C slave address to be used.
+/** @brief Set chip's I2C slave address (to be used with read() & write() method)
*
* @return 0 on success, -1 on error.
*/
int as_i2c_set_slave(int fd, uint8_t addr);
-/** @brief Read several bytes.
+/** @brief Read several bytes (ioctl() method) from given chip.
+ *
+ * @param addr I2C address of chip to access
*
* @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.
+/** @brief Write several bytes (ioctl() method) to given chip.
+ *
+ * @param addr I2C address of chip to access
*
* @return 0 on success, -1 on error.
*/
int as_i2c_write(int fd, uint8_t addr, const uint8_t *data, size_t n);
-/** @brief Read at a given register address.
+/** @brief Read from given chip at a given register address (ioctl() method).
*
* Send the \e reg byte, then read bytes.
*
+ * @param addr I2C address of chip to access
+ *
* @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);
-/** @brief Write at a given register address.
+/** @brief Write to given chip at a given register address (ioctl() method).
*
* Send the \e reg byte followed by data.
*
+ * @param addr I2C address of chip to access
+ *
* @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);
-/** @brief Read a given register byte.
+/** @brief Read a byte from the given register.
*
* Send the \e reg byte, then read and return a single byte.
*
+ * @param addr I2C address of chip to access
+ *
* @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
@@ -106,19 +116,21 @@ int as_i2c_write_reg(int fd, uint8_t addr, uint8_t reg, const uint8_t *data, siz
*/
int as_i2c_read_reg_byte(int fd, uint8_t addr, uint8_t reg);
-/** @brief Write a given register byte.
+/** @brief Write a byte to the given register.
*
* Send the \e reg byte followed by the \e val byte.
*
+ * @param addr I2C address of chip to access
+ *
* @return 0 on success, -1 on error.
*
- * @note Equivalent to \e as_i2c_write() with a 2-byte buffer.
+ * @note Equivalent to \e as_i2c_write() with a 2-byte buffer (reg + data).
*/
int as_i2c_write_reg_byte(int fd, uint8_t addr, uint8_t reg, uint8_t val);
#ifdef __cplusplus
}
-#endif // __cplusplus
+#endif /* __cplusplus */
-#endif // AS_I2C_H_
+#endif /* AS_I2C_H_ */
diff --git a/target/packages/as_devices/c/as_pwm.c b/target/packages/as_devices/c/as_pwm.c
index 56ac4c5..d0bbaa8 100644
--- a/target/packages/as_devices/c/as_pwm.c
+++ b/target/packages/as_devices/c/as_pwm.c
@@ -1,5 +1,5 @@
/*
-** THE ARMadeus Systems
+** AsDevices - PWM access functions
**
** Copyright (C) 2009-2010 The armadeus systems team
** Fabien Marteau <fab...@ar...>
@@ -18,20 +18,17 @@
** License along with this library; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
+
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h> /* for open() */
#include <unistd.h> /* for write() */
+/* #define DEBUG */
+#include "helpers.h"
#include "as_pwm.h"
-//#define DEBUG
-#ifdef DEBUG
-# define ERROR(fmt, ...) printf(fmt, ##__VA_ARGS__)
-#else
-# define ERROR(fmt, ...) /*fmt, ##__VA_ARGS__*/
-#endif
#define PWM_SYS_PATH "/sys/class/pwm/pwm"
#define FREQUENCY_PATH "frequency"
@@ -41,59 +38,6 @@
#define SIZE_OF_BUFF 50
-/*------------------------------------------------------------------------------*/
-
-/** @brief Write a string value in file
- *
- * @param aFile_handler: file handler
- * @param aValueWrite: string value
- *
- * @return number char wrote, negative value on error
- */
-int writeBuffer(int aFile_handler, char *aValueWrite)
-{
- int ret;
- ret = write(aFile_handler, aValueWrite, strlen(aValueWrite));
- if (ret < 0)
- {
- ERROR("Can't write file ");
- return ret;
- }
- ret = lseek(aFile_handler,0,SEEK_SET);
- if (ret < 0)
- {
- ERROR("lseek error ");
- return ret;
- }
- return ret;
-}
-
-/*------------------------------------------------------------------------------*/
-
-/** @brief read a string value in file
- *
- * @param aFile_handler: file handler
- * @param aValueRead[SIZE_OF_BUFF]: char pointer to read buffer
- *
- * @return number char read, negative value on error
- */
-int readBuffer(int aFile_handler, char *aValueRead)
-{
- int ret;
- ret = read(aFile_handler, aValueRead, SIZE_OF_BUFF);
- if (ret < 0)
- {
- ERROR("Can't read file ");
- return ret;
- }
- ret = lseek(aFile_handler,0,SEEK_SET);
- if (ret < 0)
- {
- ERROR("lseek error ");
- return ret;
- }
- return ret;
-}
/*------------------------------------------------------------------------------*/
@@ -103,34 +47,29 @@ struct as_pwm_device *as_pwm_open(int aPwmNumber)
struct as_pwm_device *dev;
dev = (struct as_pwm_device *)malloc(sizeof(struct as_pwm_device));
- if (dev == NULL)
- {
+ if (dev == NULL) {
ERROR("Can't allocate memory for pwm device structure\n");
return NULL;
}
- /* open pwm files management */
- snprintf(buffer,SIZE_OF_BUFF,"%s%d/%s",PWM_SYS_PATH,aPwmNumber,FREQUENCY_PATH);
- if ((dev->fileFrequency = open(buffer,O_RDWR)) < 0)
- {
+ /* open pwm management files */
+ snprintf(buffer, SIZE_OF_BUFF, "%s%d/%s", PWM_SYS_PATH, aPwmNumber, FREQUENCY_PATH);
+ if ((dev->fileFrequency = open(buffer, O_RDWR)) < 0) {
ERROR("Can't open frequency files");
return NULL;
}
- snprintf(buffer,SIZE_OF_BUFF,"%s%d/%s",PWM_SYS_PATH,aPwmNumber,PERIOD_PATH);
- if ((dev->filePeriod = open(buffer,O_RDWR)) < 0)
- {
+ snprintf(buffer, SIZE_OF_BUFF, "%s%d/%s", PWM_SYS_PATH, aPwmNumber, PERIOD_PATH);
+ if ((dev->filePeriod = open(buffer, O_RDWR)) < 0) {
ERROR("Can't open period files");
return NULL;
}
- snprintf(buffer,SIZE_OF_BUFF,"%s%d/%s",PWM_SYS_PATH,aPwmNumber,DUTY_PATH);
- if((dev->fileDuty = open(buffer,O_RDWR)) < 0)
- {
+ snprintf(buffer, SIZE_OF_BUFF, "%s%d/%s", PWM_SYS_PATH, aPwmNumber, DUTY_PATH);
+ if((dev->fileDuty = open(buffer, O_RDWR)) < 0) {
ERROR("Can't open duty files");
return NULL;
}
- snprintf(buffer,SIZE_OF_BUFF,"%s%d/%s",PWM_SYS_PATH,aPwmNumber,ACTIVE_PATH);
- if((dev->fileActive = open(buffer,O_RDWR)) < 0)
- {
+ snprintf(buffer, SIZE_OF_BUFF, "%s%d/%s", PWM_SYS_PATH, aPwmNumber, ACTIVE_PATH);
+ if((dev->fileActive = open(buffer, O_RDWR)) < 0) {
ERROR("Can't open active files");
return NULL;
}
@@ -140,24 +79,20 @@ struct as_pwm_device *as_pwm_open(int aPwmNumber)
/*------------------------------------------------------------------------------*/
-int32_t as_pwm_set_frequency(struct as_pwm_device *aDev, int aFrequency)
+int32_t as_pwm_set_frequency(struct as_pwm_device *dev, int freq)
{
- char buffer[SIZE_OF_BUFF];
-
- snprintf(buffer,SIZE_OF_BUFF,"%d",aFrequency);
- return writeBuffer(aDev->fileFrequency,buffer);
+ return as_write_buffer(dev->fileFrequency, freq);
}
/*------------------------------------------------------------------------------*/
-int32_t as_pwm_get_frequency(struct as_pwm_device *aDev)
+int32_t as_pwm_get_frequency(struct as_pwm_device *dev)
{
char buffer[SIZE_OF_BUFF];
int ret;
- ret = readBuffer(aDev->fileFrequency, buffer);
- if (ret < 0)
- {
+ ret = as_read_buffer(dev->fileFrequency, buffer);
+ if (ret < 0) {
ERROR("Can't read frequency\n");
return ret;
}
@@ -167,24 +102,20 @@ int32_t as_pwm_get_frequency(struct as_pwm_device *aDev)
/*------------------------------------------------------------------------------*/
-int32_t as_pwm_set_period(struct as_pwm_device *aDev, int aPeriod)
+int32_t as_pwm_set_period(struct as_pwm_device *dev, int period)
{
- char buffer[SIZE_OF_BUFF];
-
- snprintf(buffer,SIZE_OF_BUFF,"%d",aPeriod);
- return writeBuffer(aDev->filePeriod, buffer);
+ return as_write_buffer(dev->filePeriod, period);
}
/*------------------------------------------------------------------------------*/
-int32_t as_pwm_get_period(struct as_pwm_device *aDev)
+int32_t as_pwm_get_period(struct as_pwm_device *dev)
{
char buffer[SIZE_OF_BUFF];
int ret;
- ret = readBuffer(aDev->filePeriod, buffer);
- if (ret < 0)
- {
+ ret = as_read_buffer(dev->filePeriod, buffer);
+ if (ret < 0) {
ERROR("Can't read period\n");
return ret;
}
@@ -194,24 +125,20 @@ int32_t as_pwm_get_period(struct as_pwm_device *aDev)
/*------------------------------------------------------------------------------*/
-int32_t as_pwm_set_duty(struct as_pwm_device *aDev, int aDuty)
+int32_t as_pwm_set_duty(struct as_pwm_device *dev, int duty)
{
- char buffer[SIZE_OF_BUFF];
-
- snprintf(buffer, SIZE_OF_BUFF, "%d", aDuty);
- return writeBuffer( aDev->fileDuty, buffer);
+ return as_write_buffer(dev->fileDuty, duty);
}
/*------------------------------------------------------------------------------*/
-int32_t as_pwm_get_duty(struct as_pwm_device *aDev)
+int32_t as_pwm_get_duty(struct as_pwm_device *dev)
{
char buffer[SIZE_OF_BUFF];
int ret;
- ret = readBuffer(aDev->fileDuty, buffer);
- if (ret < 0)
- {
+ ret = as_read_buffer(dev->fileDuty, buffer);
+ if (ret < 0) {
ERROR("Can't read duty\n");
return ret;
}
@@ -221,36 +148,31 @@ int32_t as_pwm_get_duty(struct as_pwm_device *aDev)
/*------------------------------------------------------------------------------*/
-int32_t as_pwm_set_state(struct as_pwm_device *aDev, int aEnable)
+int32_t as_pwm_set_state(struct as_pwm_device *pwm_dev, int enable)
{
- char one[] = "1";
- char zero[] = "0";
- int ret=0;
-
- if(aEnable)
- {
- ret = writeBuffer(aDev->fileActive,one);
- }else{
- ret = writeBuffer(aDev->fileActive,zero);
+ int ret = 0;
+
+ if (enable) {
+ ret = as_write_buffer(pwm_dev->fileActive, 1);
+ } else {
+ ret = as_write_buffer(pwm_dev->fileActive, 0);
}
- if (ret < 0)
- {
- ERROR("Can't write file Active\n");
- return ret;
+ if (ret < 0) {
+ ERROR("Can't write to pwmX/active\n");
}
+
return ret;
}
/*------------------------------------------------------------------------------*/
-int32_t as_pwm_get_state(struct as_pwm_device *aDev)
+int32_t as_pwm_get_state(struct as_pwm_device *dev)
{
char buffer[SIZE_OF_BUFF];
int ret;
- ret = readBuffer(aDev->fileActive, buffer);
- if (ret < 0)
- {
+ ret = as_read_buffer(dev->fileActive, buffer);
+ if (ret < 0) {
ERROR("Can't read state\n");
return ret;
}
@@ -260,36 +182,32 @@ int32_t as_pwm_get_state(struct as_pwm_device *aDev)
/*------------------------------------------------------------------------------*/
-int32_t as_pwm_close(struct as_pwm_device *aDev)
+int32_t as_pwm_close(struct as_pwm_device *dev)
{
int ret = 0;
- /* open pwm files management */
- ret = close(aDev->fileFrequency);
- if(ret < 0)
- {
- ERROR("Can't close frequency files");
+ /* Close pwm management files */
+ ret = close(dev->fileFrequency);
+ if (ret < 0) {
+ ERROR("Can't close /frequency");
return ret;
}
- ret = close(aDev->filePeriod);
- if(ret < 0)
- {
- ERROR("Can't close period files");
+ ret = close(dev->filePeriod);
+ if (ret < 0) {
+ ERROR("Can't close /period");
return ret;
}
- ret = close(aDev->fileDuty);
- if(ret < 0)
- {
- ERROR("Can't close duty files");
+ ret = close(dev->fileDuty);
+ if (ret < 0) {
+ ERROR("Can't close /duty");
return ret;
}
- ret = close(aDev->fileActive);
- if(ret < 0)
- {
- ERROR("Can't close active files");
+ ret = close(dev->fileActive);
+ if (ret < 0) {
+ ERROR("Can't close /active");
return ret;
}
return ret;
-
}
+
diff --git a/target/packages/as_devices/c/helpers.c b/target/packages/as_devices/c/helpers.c
new file mode 100644
index 0000000..57d63f6
--- /dev/null
+++ b/target/packages/as_devices/c/helpers.c
@@ -0,0 +1,76 @@
+/*
+** AsDevices - Helpers functions for other modules
+**
+** Copyright (C) 2009-2010 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
+*/
+
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <fcntl.h> /* for open() */
+#include <unistd.h> /* for write() */
+
+/* #define DEBUG */
+#include "helpers.h"
+
+#define SIZE_OF_BUFF 50
+
+
+int as_write_buffer(int fd, int value)
+{
+ int ret;
+ char buffer[16];
+ int buffer_len;
+
+ ret = snprintf(buffer, 16, "%d", value);
+ if (ret < 0)
+ return ret;
+
+ buffer_len = strlen(buffer);
+ ret = write(fd, buffer, buffer_len);
+ if (ret < 0) {
+ ERROR("Can't write file ");
+ return ret;
+ }
+ ret = lseek(fd, 0, SEEK_SET);
+ if (ret < 0) {
+ ERROR("lseek error ");
+ return ret;
+ }
+
+ return buffer_len;
+}
+
+int readBuffer(int fd, char *buf)
+{
+ int ret;
+
+ ret = read(fd, buf, SIZE_OF_BUFF);
+ if (ret < 0) {
+ ERROR("Can't read file ");
+ return ret;
+ }
+ ret = lseek(fd, 0, SEEK_SET);
+ if (ret < 0) {
+ ERROR("lseek error ");
+ return ret;
+ }
+
+ return ret;
+}
+
diff --git a/target/packages/as_devices/cpp/as_apf27_pwm.hpp b/target/packages/as_devices/c/helpers.h
similarity index 51%
copy from target/packages/as_devices/cpp/as_apf27_pwm.hpp
copy to target/packages/as_devices/c/helpers.h
index 28d9a6b..2c027ae 100644
--- a/target/packages/as_devices/cpp/as_apf27_pwm.hpp
+++ b/target/packages/as_devices/c/helpers.h
@@ -1,66 +1,51 @@
/*
-** THE ARMadeus Systems
-**
-** Copyright (C) 2009 The armadeus systems team
+** AsDevices - Helpers functions for other modules
+**
+** Copyright (C) 2009-2010 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 __ASAPF27PWM_HPP__
-#define __ASAPF27PWM_HPP__
-
-#include "as_apf27_pwm.h"
+#include <string.h>
#include <stdio.h>
#include <stdlib.h>
+//#include <fcntl.h> /* for open() */
+#include <unistd.h> /* for write() */
-#define PWM_NUMBER NUMBER_OF_PWM
-#define DYNAMIC_TABLE_SIZE PWM_NUMBER
-
-#include "as_dynamic_table.hpp"
+#ifdef DEBUG
+# define ERROR(fmt, ...) printf(fmt, ##__VA_ARGS__)
+#else
+# define ERROR(fmt, ...) /*fmt, ##__VA_ARGS__*/
+#endif
-/** AsApf27Pwm description
+/** @brief Write an int value as string in a /sys/ or /proc interface
*
+ * @param fd: file handler
+ * @param value: int value to write
+ *
+ * @return number of chars written, negative value on error
*/
-class AsApf27Pwm {
-public:
- static AsApf27Pwm * getInstance(int aPwmNumber);
-
- void setFrequency(int aFrequency);
- int getFrequency(void);
-
- void setPeriod(int aPeriod);
- int getPeriod(void);
-
- void setDuty(int aDuty);
- int getDuty(void);
-
- bool activate(bool aEnable);
- bool getState(void);
-
- virtual ~AsApf27Pwm();
-
-protected:
-
- static AsDynamicTable * mInstances;
-
- AsApf27Pwm(int aPwmNumber);
- int mPwmNumber;
-
-};
-
-#endif /* __ASAPF27PWM_HPP__ */
+int as_write_buffer(int fd, int value);
+/** @brief read a string value in file
+ *
+ * @param fd: file handler
+ * @param buf: char pointer to read buffer
+ *
+ * @return number of chars read, negative value on error
+ */
+int as_read_buffer(int fd, char *buf);
hooks/post-receive
--
armadeus
|