How to write more that ONE data byte into ioctl file?
-
This may be too silly to ask here , really a hardware question , but I am stuck. I am using ioctl to write to I2C device - LCM1602. This device uses PCF8574 "I2C interface chip" to connect to HD44780 Hitachi LCD controller in 4 bits mode . Setting the HD447890 into 4 bit mode is well documented, but.. After the HD44780 is set into 4 bit mode further configuration need to be set using TWO (I2C sends 8 bits but only 4 bits are valid ) data "packets" after the single I2C address is send. I have this single address plus data working and cannot figure out HOW to add another "buffer" so I can send TWO 8 bits words. The I2C spec requires to wait for ACK after the first 8 bit data is send, so I just cannot add the data into single buffer. PLEASE - THE ATTACHED CODE IS WORK IN PROGRESS, REDUNDANT ETC , BUT IT WORKS! ANY INAPPROPRIATE COMMENTS ON IT WILL BE IGNORED AND ARE NOT WELCOMED. However, I'll appreciate any suggestions on how to send ONE address byte and multiple data bytes in I2C format. Thanks.
// original expanded
// write directly to expander using ioctl file descriptor
// and plain write(fd,data,#)
// parameters
// int file file descriptor
// char data data to write
// int NumberOfCharacters to write
// return 0 when actual # of characters match NumberOfCharacters
int CLASS_LCM1602::expanderWrite(
int file,
char data,
int NumberOfCharacters) {
#ifdef DEBUG
//cout << "\033[1;31mint C_I2C::TestFunction(int a)\033[0m\n";
cout << "*** TRACE file " << __FILE__ << endl;
cout << " Enhanced function " << __FUNCTION__ << endl;
cout << " line " << __LINE__ << endl;
#endif
int NumberOfCharactersWritten;
// why NOBACKLIGHT during initialization - too fast to see anyway (?)
char buffer = data; // | LCD_BACKLIGHT;
//buffer = 0;
//while (1)
{
#ifdef DEBUG
cout << " buffer cast " << (static_cast(buffer) & 0xFF) << endl;
cout << " buffer HEX cast " << hex << buffer << endl;
cout << " buffer + " << hex << +buffer << endl;cout << " function " << \_\_FUNCTION\_\_ << endl; cout << " line " << \_\_LINE\_\_ << endl;
#endif
//sleep(5);
}// NumberOfCharacters defaults to 1
NumberOfCharactersWritten = write(file, &buffer, NumberOfCharacters);
// check characters written
if (NumberOfCharacters == NumberOfCharactersWritten) {
#ifdef DEBUG
cout << "Success NumberOfCharacters written " << NumberOfCharacters
<< endl;
#endif
//exit(1);
return 0;
} else {
cout << "\033[1;31mFailed expanderWrite\033[0m\n"; / -
This may be too silly to ask here , really a hardware question , but I am stuck. I am using ioctl to write to I2C device - LCM1602. This device uses PCF8574 "I2C interface chip" to connect to HD44780 Hitachi LCD controller in 4 bits mode . Setting the HD447890 into 4 bit mode is well documented, but.. After the HD44780 is set into 4 bit mode further configuration need to be set using TWO (I2C sends 8 bits but only 4 bits are valid ) data "packets" after the single I2C address is send. I have this single address plus data working and cannot figure out HOW to add another "buffer" so I can send TWO 8 bits words. The I2C spec requires to wait for ACK after the first 8 bit data is send, so I just cannot add the data into single buffer. PLEASE - THE ATTACHED CODE IS WORK IN PROGRESS, REDUNDANT ETC , BUT IT WORKS! ANY INAPPROPRIATE COMMENTS ON IT WILL BE IGNORED AND ARE NOT WELCOMED. However, I'll appreciate any suggestions on how to send ONE address byte and multiple data bytes in I2C format. Thanks.
// original expanded
// write directly to expander using ioctl file descriptor
// and plain write(fd,data,#)
// parameters
// int file file descriptor
// char data data to write
// int NumberOfCharacters to write
// return 0 when actual # of characters match NumberOfCharacters
int CLASS_LCM1602::expanderWrite(
int file,
char data,
int NumberOfCharacters) {
#ifdef DEBUG
//cout << "\033[1;31mint C_I2C::TestFunction(int a)\033[0m\n";
cout << "*** TRACE file " << __FILE__ << endl;
cout << " Enhanced function " << __FUNCTION__ << endl;
cout << " line " << __LINE__ << endl;
#endif
int NumberOfCharactersWritten;
// why NOBACKLIGHT during initialization - too fast to see anyway (?)
char buffer = data; // | LCD_BACKLIGHT;
//buffer = 0;
//while (1)
{
#ifdef DEBUG
cout << " buffer cast " << (static_cast(buffer) & 0xFF) << endl;
cout << " buffer HEX cast " << hex << buffer << endl;
cout << " buffer + " << hex << +buffer << endl;cout << " function " << \_\_FUNCTION\_\_ << endl; cout << " line " << \_\_LINE\_\_ << endl;
#endif
//sleep(5);
}// NumberOfCharacters defaults to 1
NumberOfCharactersWritten = write(file, &buffer, NumberOfCharacters);
// check characters written
if (NumberOfCharacters == NumberOfCharactersWritten) {
#ifdef DEBUG
cout << "Success NumberOfCharacters written " << NumberOfCharacters
<< endl;
#endif
//exit(1);
return 0;
} else {
cout << "\033[1;31mFailed expanderWrite\033[0m\n"; / -
This may be too silly to ask here , really a hardware question , but I am stuck. I am using ioctl to write to I2C device - LCM1602. This device uses PCF8574 "I2C interface chip" to connect to HD44780 Hitachi LCD controller in 4 bits mode . Setting the HD447890 into 4 bit mode is well documented, but.. After the HD44780 is set into 4 bit mode further configuration need to be set using TWO (I2C sends 8 bits but only 4 bits are valid ) data "packets" after the single I2C address is send. I have this single address plus data working and cannot figure out HOW to add another "buffer" so I can send TWO 8 bits words. The I2C spec requires to wait for ACK after the first 8 bit data is send, so I just cannot add the data into single buffer. PLEASE - THE ATTACHED CODE IS WORK IN PROGRESS, REDUNDANT ETC , BUT IT WORKS! ANY INAPPROPRIATE COMMENTS ON IT WILL BE IGNORED AND ARE NOT WELCOMED. However, I'll appreciate any suggestions on how to send ONE address byte and multiple data bytes in I2C format. Thanks.
// original expanded
// write directly to expander using ioctl file descriptor
// and plain write(fd,data,#)
// parameters
// int file file descriptor
// char data data to write
// int NumberOfCharacters to write
// return 0 when actual # of characters match NumberOfCharacters
int CLASS_LCM1602::expanderWrite(
int file,
char data,
int NumberOfCharacters) {
#ifdef DEBUG
//cout << "\033[1;31mint C_I2C::TestFunction(int a)\033[0m\n";
cout << "*** TRACE file " << __FILE__ << endl;
cout << " Enhanced function " << __FUNCTION__ << endl;
cout << " line " << __LINE__ << endl;
#endif
int NumberOfCharactersWritten;
// why NOBACKLIGHT during initialization - too fast to see anyway (?)
char buffer = data; // | LCD_BACKLIGHT;
//buffer = 0;
//while (1)
{
#ifdef DEBUG
cout << " buffer cast " << (static_cast(buffer) & 0xFF) << endl;
cout << " buffer HEX cast " << hex << buffer << endl;
cout << " buffer + " << hex << +buffer << endl;cout << " function " << \_\_FUNCTION\_\_ << endl; cout << " line " << \_\_LINE\_\_ << endl;
#endif
//sleep(5);
}// NumberOfCharacters defaults to 1
NumberOfCharactersWritten = write(file, &buffer, NumberOfCharacters);
// check characters written
if (NumberOfCharacters == NumberOfCharactersWritten) {
#ifdef DEBUG
cout << "Success NumberOfCharacters written " << NumberOfCharacters
<< endl;
#endif
//exit(1);
return 0;
} else {
cout << "\033[1;31mFailed expanderWrite\033[0m\n"; /Vaclav_ wrote:
PLEASE - THE ATTACHED CODE IS WORK IN PROGRESS, REDUNDANT ETC , BUT IT WORKS! ANY INAPPROPRIATE COMMENTS ON IT WILL BE IGNORED AND ARE NOT WELCOMED.
Hmm, don't you mind such a "thesis" together with unformatted code looks like gross unrespect to guys you ask to help? :sigh: :suss:
-
This may be too silly to ask here , really a hardware question , but I am stuck. I am using ioctl to write to I2C device - LCM1602. This device uses PCF8574 "I2C interface chip" to connect to HD44780 Hitachi LCD controller in 4 bits mode . Setting the HD447890 into 4 bit mode is well documented, but.. After the HD44780 is set into 4 bit mode further configuration need to be set using TWO (I2C sends 8 bits but only 4 bits are valid ) data "packets" after the single I2C address is send. I have this single address plus data working and cannot figure out HOW to add another "buffer" so I can send TWO 8 bits words. The I2C spec requires to wait for ACK after the first 8 bit data is send, so I just cannot add the data into single buffer. PLEASE - THE ATTACHED CODE IS WORK IN PROGRESS, REDUNDANT ETC , BUT IT WORKS! ANY INAPPROPRIATE COMMENTS ON IT WILL BE IGNORED AND ARE NOT WELCOMED. However, I'll appreciate any suggestions on how to send ONE address byte and multiple data bytes in I2C format. Thanks.
// original expanded
// write directly to expander using ioctl file descriptor
// and plain write(fd,data,#)
// parameters
// int file file descriptor
// char data data to write
// int NumberOfCharacters to write
// return 0 when actual # of characters match NumberOfCharacters
int CLASS_LCM1602::expanderWrite(
int file,
char data,
int NumberOfCharacters) {
#ifdef DEBUG
//cout << "\033[1;31mint C_I2C::TestFunction(int a)\033[0m\n";
cout << "*** TRACE file " << __FILE__ << endl;
cout << " Enhanced function " << __FUNCTION__ << endl;
cout << " line " << __LINE__ << endl;
#endif
int NumberOfCharactersWritten;
// why NOBACKLIGHT during initialization - too fast to see anyway (?)
char buffer = data; // | LCD_BACKLIGHT;
//buffer = 0;
//while (1)
{
#ifdef DEBUG
cout << " buffer cast " << (static_cast(buffer) & 0xFF) << endl;
cout << " buffer HEX cast " << hex << buffer << endl;
cout << " buffer + " << hex << +buffer << endl;cout << " function " << \_\_FUNCTION\_\_ << endl; cout << " line " << \_\_LINE\_\_ << endl;
#endif
//sleep(5);
}// NumberOfCharacters defaults to 1
NumberOfCharactersWritten = write(file, &buffer, NumberOfCharacters);
// check characters written
if (NumberOfCharacters == NumberOfCharactersWritten) {
#ifdef DEBUG
cout << "Success NumberOfCharacters written " << NumberOfCharacters
<< endl;
#endif
//exit(1);
return 0;
} else {
cout << "\033[1;31mFailed expanderWrite\033[0m\n"; /You do it like with any write operation to a file descriptor by passing a buffer and it's size:
char buf[2];
// Assuming you need to write two 4 bit values passed as a byte
// with sending the lower nibble first
buf[0] = data & 0x0f;
buf[1] = data >> 4;
write(file, buf, 2); -
You do it like with any write operation to a file descriptor by passing a buffer and it's size:
char buf[2];
// Assuming you need to write two 4 bit values passed as a byte
// with sending the lower nibble first
buf[0] = data & 0x0f;
buf[1] = data >> 4;
write(file, buf, 2);