Hi Everyone,
I have an application to control sensors via I2C port and send data out thru UARTs. Please let me know if you have the sample codes for accessing these I2C and UARTs in the LxNETTES 3.2. Thanks in advance,
Your helps would be appreciated.
here is some really messy but functional code for opening a serial port for read/write. this code is for using a terminal appliaction over the uart.
if it looks too sloppy I learned most everything from here:
http://www.linuxdocs.org/HOWTOs/Serial-HOWTO.htm
and
http://www.linuxdocs.org/HOWTOs/Serial-Programming-HOWTO/index.html
And from the Book:“Embedded Linux” by Craig Hollabaugh. there is a chapter on i2c in the book also
int OpenSerialPort()
{
int fd;
struct termios newtio,oldtio;
cout << "Opening Serial Port" << endl;
//Open modem device for reading and writing and not as controlling tty
fd = open(DEVICE, O_RDWR | O_NOCTTY );
if (fd < 0)
{
perror(DEVICE);
Error();
}
//tcgetattr(fd,&oldtio); // save current serial port settings
bzero(&newtio, sizeof(newtio)); // clear struct for new port settings
//BAUDRATE: Set bps rate. You could also use cfsetispeed and cfsetospeed.
//CRTSCTS : output hardware flow control (only used if the cable has
// all necessary lines. See sect. 7 of Serial-HOWTO)
//CS8 : 8n1 (8bit,no parity,1 stopbit)
//CLOCAL : local connection, no modem contol
//CREAD : enable receiving characters
newtio.c_cflag = BAUDRATE | CS8 | CLOCAL | CREAD;
/*
IGNPAR : ignore bytes with parity errors
ICRNL : map CR to NL (otherwise a CR input on the other computer
will not terminate input)
otherwise make device raw (no other input processing)
*/
//newtio.c_iflag = IGNPAR | ICRNL;
newtio.c_iflag = IGNPAR;
//Raw output.
newtio.c_oflag = 0;
//ICANON : enable canonical input
//disable all echo functionality, and don't send signals to calling program
//newtio.c_lflag = ICANON;
newtio.c_lflag = 0;
/*
initialize all control characters
default values can be found in /usr/include/termios.h, and are given
in the comments, but we don't need them here
*/
newtio.c_cc[VINTR] = 0; /* Ctrl-c */
newtio.c_cc[VQUIT] = 0; /* Ctrl-\ */
newtio.c_cc[VERASE] = 0; /* del */
newtio.c_cc[VKILL] = 0; /* @ */
newtio.c_cc[VEOF] = 4; /* Ctrl-d */
newtio.c_cc[VTIME] = 0; /* inter-character timer unused */
newtio.c_cc[VMIN] = 1; /* blocking read until 1 character arrives */
newtio.c_cc[VSWTC] = 0; /* '\0' */
newtio.c_cc[VSTART] = 0; /* Ctrl-q */
newtio.c_cc[VSTOP] = 0; /* Ctrl-s */
newtio.c_cc[VSUSP] = 0; /* Ctrl-z */
newtio.c_cc[VEOL] = 0; /* '\0' */
newtio.c_cc[VREPRINT] = 0; /* Ctrl-r */
newtio.c_cc[VDISCARD] = 0; /* Ctrl-u */
newtio.c_cc[VWERASE] = 0; /* Ctrl-w */
newtio.c_cc[VLNEXT] = 0; /* Ctrl-v */
newtio.c_cc[VEOL2] = 0; /* '\0' */
// now clean the modem line and activate the settings for the port
tcflush(fd, TCIFLUSH);
tcsetattr(fd,TCSANOW,&newtio);
fcntl(fd, F_SETFL, FNDELAY);
return fd;
}
void UartReadWriteTest(int uartHandle)
{
int iStop = FALSE;
char buf[255];
int iResult;
//In this example, inputting a 'z' at the beginning of a line will
//exit the program.
while (iStop == FALSE) // loop until we have a terminating condition
{
// read blocks program execution until a line terminating character is
// input, even if more than 255 chars are input. If the number
// of characters read is smaller than the number of chars available,
// subsequent reads will return the remaining chars. res will be set
// to the actual number of characters actually read
iResult = read(uartHandle, buf, 255);
buf[iResult] = 0; // set end of string, so we can printf
printf(":%s:%d
", buf, iResult);
iResult = write(uartHandle, buf, strlen(buf));//write string to the uart
if (iResult < 0)
{
cout << "error writing to port" << endl;
}
if (buf[0] == 'z')
{
iStop = TRUE;
}
}
}
Thanks,
If anyone has any pointers to I2C code for Linux, please let me know. Thanks in advance
Hoa
What platform are you using? LxNETES has i2c drivers for the ns9750 processor. Also, there are plenty of i2c device drivers to look at for examples. See LxNETEX/linux/drivers/i2c . Look in the chips dir for examples of communicating with i2c devices.
Message was edited by: BobF
I’ve got Debian!
Thanks