-
Notifications
You must be signed in to change notification settings - Fork 1
/
i2c.c
80 lines (69 loc) · 1.55 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
/**
* @file i2c.c
* Linux I2C Driver for device IO
* Setup I2C port on Raspberry Pi to blast bytes over I2C device descriptor;
* should work on any other Linux I2C device.
*
* @copyright
* Copyright (C) 2017 Real Flight Systems
* @author James F Dougherty <[email protected]>
*/
/**
* @defgroup serial serial
* @addtogroup serial
* @{
*/
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <syslog.h>
#include <errno.h>
#include <linux/types.h>
#include <linux/i2c-dev.h>
static const char *device = "/dev/i2c-1";
#define OLED_DEVICE_ADDRESS 0x3c /* SSD1311 */
/**
* @brief serial port open
*
* Open the I2C device file (e.g. /dev/i2c-1) for read/write
* shutdown via #serial_close
*
* @param device string value for the device file (e.g. "/dev/i2c-1")
* @param highspeed ignored
*/
int serial_open(char* device, int highspeed)
{
int fd = -1;
int ret;
fd = open(device, O_RDWR);
if (fd < 0) {
printf("device not found[%s]\n", device);
return fd;
}
if (ioctl(fd, I2C_SLAVE, OLED_DEVICE_ADDRESS) < 0) {
printf("unable to get bus access to talk to slave[0x%02x]\n",
OLED_DEVICE_ADDRESS);
return fd;
}
printf("%s: i2c mode\n", device);
return fd;
}
/**
* @brief serial port close
*
* Close the I2C port file descriptor
*
* @param sfd serial file descriptor returned from #serial_open
*/
void serial_close(int sfd)
{
close(sfd);
}
/** @}*/