8c2952457
김태훈
응용 프로그램 추가
|
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <linux/types.h>
#include <i2c-api.h>
#define I2C_MAX_BUS 8
static i2c_bus_t ds[I2C_MAX_BUS];
static int i2c_bus_cnt = 0;
int i2c_get_major_i2c( int *major )
{
FILE *fp;
char linebuff[128];
char device_name[32];
int tmp_major;
fp = fopen("/proc/devices", "r");
if(fp == NULL) return I2C_NOT_FIND_MAJOR;
while(1)
{
if( fgets(linebuff, sizeof( linebuff) , fp ) == NULL ) break;
if (sscanf(linebuff, "%d %s", &tmp_major, device_name) == 2)
{
if (strcmp("i2c", device_name) == 0)
{
*major = tmp_major;
fclose( fp );
return I2C_NO_ERROR;
}
}
}
fclose( fp );
return I2C_NOT_FIND_MAJOR;
}
int i2c_make_bus_node( int major, int bn )
{
sprintf(ds[bn].node_fname, "/dev/i2cbus%d-%d", getpid(), bn );
mknod(ds[bn].node_fname, (S_IRWXU|S_IRWXG|S_IFCHR), (major<<8) + bn );
ds[bn].fd = open(ds[bn].node_fname, O_RDWR );
", ds[bn].node_fname, bn, ds[bn].fd );
unlink(ds[bn].node_fname);
return ds[bn].fd;
}
//------------------------------------------------------------------------------
// 설명 : I2C API 를 초기화 한다.
//------------------------------------------------------------------------------
int i2c_init( void )
{
int major;
int ret;
int lp;
ret = i2c_get_major_i2c( &major );
if( I2C_NOT_FIND_MAJOR == ret )
{
printf( "can't find nodfile
" );
return -1;
}
//printf( "I2C Major Numner = %d
", major );
i2c_bus_cnt = 0;
for( lp = 0; lp < I2C_MAX_BUS; lp++ )
{
if( i2c_make_bus_node( major, lp ) >= 0 )
{
i2c_bus_cnt++;
}
else
{
ds[lp].fd = -1;
}
}
return 0;
}
//------------------------------------------------------------------------------
// 설명 : 현재 존재하는 I2C BUS 수를 얻는다.
// 반환 : 소켓 수
//------------------------------------------------------------------------------
int i2c_get_bus_count( void )
{
return i2c_bus_cnt;
}
//------------------------------------------------------------------------------
// 설명 : I2C 버스에서 읽는다.
//------------------------------------------------------------------------------
int i2c_read_data( access_data_t * access_data )
{
if( ds[access_data->bus].fd < 0 ) return I2C_NOT_FIND_BUS;
if( access_data->size > sizeof( access_data->data ) ) access_data->size = sizeof( access_data->data );
ioctl( ds[access_data->bus].fd, I2C_SLAVE, access_data->addr );
access_data->size = read( ds[access_data->bus].fd, access_data->data, access_data->size );
if( access_data->size < 0 ) return I2C_READ_DEVICE;
return I2C_NO_ERROR;
}
//------------------------------------------------------------------------------
// 설명 : I2C 버스에 데이터를 써 넣는다.
//------------------------------------------------------------------------------
int i2c_write_data( access_data_t * access_data )
{
if( ds[access_data->bus].fd < 0 ) return I2C_NOT_FIND_BUS;
if( access_data->size > sizeof( access_data->data ) ) access_data->size = sizeof( access_data->data );
ioctl( ds[access_data->bus].fd, I2C_SLAVE, access_data->addr );
access_data->size = write( ds[access_data->bus].fd, access_data->data, access_data->size );
if( access_data->size < 0 ) return I2C_WRITE_DEVICE;
return I2C_NO_ERROR;
}
|