This thread has been locked.
If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.
Hi,
I write a driver code for a smart switch. When I examine the signals are SDA and SCL in scope, I don't see right values.I see slave address byte is 0x60 every sending.
Do you think why I don't see right data?
Is write() function right?
Is there any problem in "ioclt()" function?
Please help me.
Thanks.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <unistd.h>
#include <linux/i2c-dev.h>
#include "linux_gpio.h"
#include <fcntl.h>
#include <sys/stat.h>
int main(void)
{
gpio_export(CONTROL);
gpio_dir_out(CONTROL);
set_gpio_value(CONTROL, 0);
unsigned char TxBuf_backlight [5];
set_gpio_value(CONTROL, 1);
gpio_export(SCREENKEY_INPUT); //GPIO 158 (A18) exported as SW
gpio_dir_in(SCREENKEY_INPUT); //Direction of the GPIO 158 defined as in
//open the device file
int file;
int adapter_nr = 2; /* probably dynamically determined */
char filename[20];
sprintf(filename,"/dev/i2c-%d",adapter_nr);
if ((file = open(filename,O_RDWR)) < 0) {
perror("Error: ");
exit(1);
}
//what device address you want to communicate
int addr = 0x60; /* The I2C address */
printf("I2C device address: 0x%x\n",addr);
if (ioctl(file,I2C_SLAVE,addr) < 0) {
perror("Error: ");
exit(1);
}
//Write to the slave
TxBuf_backlight[0] = 0X10;
if ( write(file,TxBuf_backlight,1) != 1) {
perror("Error: ");
}
//I see "0x60" in scope. This is slave adress.I must have seen 0x10
TxBuf_backlight[1] = 0XEF;
if ( write(file,TxBuf_backlight,1) != 1) {
perror("Error: ");
}
//I see "0x60" in scope again. This is slave adress.I must have seen 0xEF
gpio_unexport(SCREENKEY_INPUT);
set_gpio_value(CONTROL, 0);
return 0;
}