-
Notifications
You must be signed in to change notification settings - Fork 0
/
i2c.c
91 lines (68 loc) · 1.6 KB
/
i2c.c
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
88
89
90
91
/*
Example I2C use on linux/raspberry pi
*/
#include <stdio.h>
#include <fcntl.h>
#include <linux/i2c.h>
#include <linux/i2c-dev.h>
#include <stdint.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <stdlib.h>
#include "i2c.h"
/*
* Pinout config for this example
*
* LIS3DH SDA => Raspberry Pi GPIO 2 (Physical pin 3)
* LIS3DH SCL => Raspberry Pi GPIO 3 (Physical pin 5)
*
*/
#define I2C_DEVICE "/dev/i2c-1"
#define I2C_LIS3DH_ADDRESS 0x18 /* Can also be 0x19 */
static int fd;
int i2c_init(void) {
fd = open(I2C_DEVICE, O_RDWR);
if (fd < 0) {
fprintf(stderr, "i2c_init(): could not open device: %s\n", I2C_DEVICE);
return 1;
}
if (ioctl(fd, I2C_SLAVE, I2C_LIS3DH_ADDRESS) < 0) {
fprintf(stderr, "i2c_init(): failed to acquire bus/talk to slave\n");
close(fd);
return 1;
}
return 0;
}
int i2c_read(uint8_t reg, uint8_t *dst, uint32_t size) {
uint8_t cmd[2];
if (size > 1) {
reg |= 0x80; /* AUTO INC */
}
cmd[0] = reg;
cmd[1] = 0x00;
if (write(fd, cmd, 2) != 2) {
fprintf(stderr, "i2c_read(): error write()\n");
return 1;
}
if (read(fd, dst, size) != (int)size) {
fprintf(stderr, "i2c_read(): error read()\n");
return 1;
}
return 0;
}
int i2c_write(uint8_t reg, uint8_t value) {
uint8_t cmd[2];
cmd[0] = reg;
cmd[1] = value;
if (write(fd, cmd, 2) != 2) {
fprintf(stderr, "i2c_write(): error write()\n");
return 1;
}
return 0;
}
int i2c_deinit(void) {
if (fd) {
close(fd);
}
return 0;
}