[Armadeus-commitlog] SF.net SVN: armadeus: [625] trunk/target/packages/ch7024ctrl/ch7024.c
Brought to you by:
sszy
|
From: <th...@us...> - 2007-08-27 18:35:41
|
Revision: 625
http://armadeus.svn.sourceforge.net/armadeus/?rev=625&view=rev
Author: thom25
Date: 2007-08-26 09:47:58 -0700 (Sun, 26 Aug 2007)
Log Message:
-----------
add several comments. Used as tutorial for I2C access
Modified Paths:
--------------
trunk/target/packages/ch7024ctrl/ch7024.c
Modified: trunk/target/packages/ch7024ctrl/ch7024.c
===================================================================
--- trunk/target/packages/ch7024ctrl/ch7024.c 2007-08-26 14:27:43 UTC (rev 624)
+++ trunk/target/packages/ch7024ctrl/ch7024.c 2007-08-26 16:47:58 UTC (rev 625)
@@ -17,7 +17,7 @@
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
**
-** ch7024: manage the Svideo output controller CH7024
+** ch7024: manage the Svideo controller CH7024
**
** author: th...@us...
*/
@@ -30,7 +30,7 @@
#include <stdlib.h>
#include <string.h>
-#include <linux/i2c.h> // seems to make trouble during compiling ....
+#include <linux/i2c.h>
#include <linux/i2c-dev.h>
#include <sys/ioctl.h>
@@ -54,24 +54,31 @@
printf ("other: menu\n");
}
-/* Read a byte on the I2C bus
+/* Read a byte on the I2C bus
+ This is done by writing the register address
+ we want to access and then by reading this register
@param fd: file descriptor of the device
- @param reg: registre to access
+ @param reg: register to access
@param buf: buffer used to store the result
@return : -1 in case of error otherwise 0
*/
int read_byte( int fd, unsigned char reg, unsigned char *buf )
-{
+{
+ // create an I2C write message (only one byte: the address)
struct i2c_msg msg = { CH7024_I2C_SLAVE_ADDR, 0, 1, buf };
+ // create a I2C IOCTL request
struct i2c_rdwr_ioctl_data rdwr = { &msg, 1 };
buf[0] = reg; // select reg to read
+
+ // write the desired register address
if ( ioctl( fd, I2C_RDWR, &rdwr ) < 0 ){
printf("Write error\n");
return -1;
}
msg.flags = I2C_M_RD; // read
-
+
+ // read the result and write it in buf[0]
if ( ioctl( fd, I2C_RDWR, &rdwr ) < 0 ){
printf("Read error\n");
return -1;
@@ -81,14 +88,18 @@
/* Write a byte on the I2C bus
@param fd: file descriptor of the device
- @param reg: registre to access
+ @param reg: register to access
@param value: value to write
@return : -1 in case of error otherwise 0
*/
int write_byte(int fd, unsigned char reg, unsigned char value)
{
- unsigned char buf[2] = {reg,value};
- struct i2c_msg msg = { CH7024_I2C_SLAVE_ADDR, 0, sizeof(buf), buf };
+ unsigned char buf[2] = {reg,value}; // initialise a data buffer with
+ // address and data
+
+ // create an I2C write message
+ struct i2c_msg msg = { CH7024_I2C_SLAVE_ADDR, 0, sizeof(buf), buf };
+ // create a I2C IOCTL request
struct i2c_rdwr_ioctl_data rdwr = { &msg, 1 };
#ifdef DEBUG_CH7024
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|