How to read i2c with interrupt signal?

I want to read the i2c information with interrupt, but this code I wrote does not work
I can read the data directly after writing,
But with a interrupt, the signal_handler_IN function is not called
How can I fix it?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include "src/i2c.h"
#include <errno.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/signal.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <termios.h>
#include <time.h>
#include <unistd.h>
#define TERMINAL "/dev/i2c-1"

void signal_handler_IN(int status); /* definition of signal handler */

int fd;
struct termios termAttr;
struct sigaction saio;
I2CDevice device;

int main() {

  fd = open(TERMINAL, O_RDWR | O_NOCTTY | O_NDELAY);
  if (fd == -1) {
    perror("open_port: Unable to open port \n");
    exit(1);
  }

  device.bus = fd;
  device.addr = 0x04;
  device.tenbit = 0;
  device.delay = 10;
  device.flags = 0;
  device.page_bytes = 8;
  device.iaddr_bytes = 0;

  /* Configure signal handling */
  sigemptyset(&saio.sa_mask);
  saio.sa_handler = signal_handler_IN;
  saio.sa_flags = 0;
  saio.sa_restorer = NULL;
  sigaction(SIGINT, &saio, NULL);


  fcntl(fd, F_SETFL, FNDELAY);
  fcntl(fd, F_SETOWN, getpid());
  fcntl(fd, F_SETFL, O_ASYNC);

  tcgetattr(fd, &termAttr);
  // baudRate = B115200;          /* Not needed */
  cfsetospeed(&termAttr, B115200);
  termAttr.c_cflag &= ~PARENB;
  termAttr.c_cflag &= ~CSTOPB;
  termAttr.c_cflag &= ~CSIZE;
  termAttr.c_cflag |= CS8;
  termAttr.c_cflag |= (CLOCAL | CREAD);
  termAttr.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
  termAttr.c_iflag &= ~(IXON | IXOFF | IXANY);
  termAttr.c_oflag &= ~OPOST;
  tcsetattr(fd, TCSANOW, &termAttr);
  printf("UART1 configured....\n");

  for (int i = 0; i >= 0; i++) {
    // some code

    char *xstr = "2";
    unsigned char buf[80] = {0};
    i2c_ioctl_write(&device, 0x0, xstr, 2);
    sleep(5);
 
    //i2c_ioctl_read(&device, 0x0, buf, 4);
    // std::cout << "buffer" << buf[0];
  }

  i2c_close(fd);
  exit(0);
}

void signal_handler_IN(int status) {
  unsigned char buf[80] = {0};

  int rdlen;
  rdlen = i2c_ioctl_read(&device, 0x0, buf, 4);
  std::cout << "buffer" << buf[0];
}
Last edited on
Topic archived. No new replies allowed.