Page Menu
Home
FreeBSD
Search
Configure Global Search
Log In
Files
F4862417
main.cpp
marc.priggemeyer_gmail.com (Marc Priggemeyer)
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Authored By
marc.priggemeyer_gmail.com
Jul 16 2019, 3:15 PM
2019-07-16 15:15:15 (UTC+0)
Size
2 KB
Referenced Files
None
Subscribers
None
main.cpp
View Options
#include
<sys/cdefs.h>
#include
<stdio.h>
#include
<stdlib.h>
#include
<fcntl.h>
#include
<sys/ioctl.h>
#include
<errno.h>
#include
<unistd.h>
#include
<dev/iicbus/iic.h>
int
reset_dev
(
int
fd
)
{
struct
iiccmd
cmd
;
int
error
;
cmd
.
slave
=
1
<<
1
;
//don't care
cmd
.
last
=
1
;
cmd
.
count
=
0
;
error
=
ioctl
(
fd
,
I2CRSTCARD
,
&
cmd
);
return
error
;
}
void
scan
(
int
fd
)
{
int
i
;
uint8_t
buf
[
2
]
=
{
0
,
0
};
struct
iic_msg
msg
[
2
];
struct
iic_rdwr_data
rdwr
;
msg
[
0
].
flags
=
!
IIC_M_RD
;
msg
[
0
].
len
=
sizeof
(
buf
);
msg
[
0
].
buf
=
buf
;
msg
[
1
].
flags
=
IIC_M_RD
;
msg
[
1
].
len
=
sizeof
(
buf
);
msg
[
1
].
buf
=
buf
;
rdwr
.
nmsgs
=
2
;
setbuf
(
stdout
,
0
);
for
(
i
=
1
;
i
<
127
;
i
++
)
{
msg
[
0
].
slave
=
i
<<
1
;
msg
[
1
].
slave
=
i
<<
1
;
rdwr
.
msgs
=
msg
;
if
(
ioctl
(
fd
,
I2CRDWR
,
&
rdwr
)
>=
0
)
printf
(
" 0x%02x "
,
i
);
else
printf
(
"."
);
}
printf
(
"
\n
"
);
}
void
test1_resetcontroller
(
int
fd
,
int
addr
)
{
int
result
;
result
=
reset_dev
(
fd
);
printf
(
"reset returned %d
\n
"
,
result
);
printf
(
"test finished
\n
"
);
}
void
test2_startstop
(
int
fd
,
int
addr
)
{
struct
iiccmd
cmd
;
int
error
;
error
=
reset_dev
(
fd
);
if
(
error
!=
0
)
{
printf
(
"test faild, could not reset controller (%d)
\n
"
,
error
);
return
;
}
cmd
.
slave
=
addr
<<
1
;
cmd
.
last
=
1
;
cmd
.
count
=
0
;
error
=
ioctl
(
fd
,
I2CSTART
,
&
cmd
);
if
(
error
!=
0
)
{
printf
(
"I2CSTART failed, will not I2CSTOP (%d)
\n
"
,
error
);
}
printf
(
"test finished
\n
"
);
ioctl
(
fd
,
I2CSTOP
,
&
cmd
);
}
void
test3_readwrite
(
int
fd
,
int
addr
)
{
struct
iic_msg
msg
;
struct
iic_rdwr_data
rdwr
;
uint8_t
buf
;
int
error
;
error
=
reset_dev
(
fd
);
if
(
error
!=
0
)
{
printf
(
"test faild, could not reset controller (%d)
\n
"
,
error
);
return
;
}
msg
.
buf
=
&
buf
;
msg
.
len
=
1
;
msg
.
flags
=
IIC_M_RD
;
msg
.
slave
=
addr
<<
1
;
rdwr
.
msgs
=
&
msg
;
rdwr
.
nmsgs
=
1
;
error
=
error
=
ioctl
(
fd
,
I2CRDWR
,
&
rdwr
);
if
(
error
!=
0
)
{
printf
(
"I2CRDWR failed (%d)
\n
"
,
error
);
}
printf
(
"read: 0x%x
\n
"
,
buf
);
printf
(
"test finished
\n
"
);
}
int
main
(
int
argc
,
char
**
argv
)
{
int
fd
;
if
(
argc
<
3
)
{
perror
(
"usage: ./a.out device test addr"
);
perror
(
"device: path to the i2c device"
);
perror
(
"test: index of test to perform >=1"
);
perror
(
"addr: device address on the i2c bus (in hex)"
);
return
-1
;
}
int
testnum
=
strtol
(
argv
[
2
],
0
,
10
);
if
(
(
errno
==
EINVAL
)
||
(
errno
==
ERANGE
)
)
{
perror
(
"Invalid test number"
);
return
-1
;
}
int
addr
=
strtol
(
argv
[
3
],
0
,
16
);
if
(
(
errno
==
EINVAL
)
||
(
errno
==
ERANGE
)
)
{
perror
(
"Invalid test number"
);
return
-1
;
}
const
char
*
dev
=
argv
[
1
];
if
(
(
fd
=
open
(
dev
,
O_RDWR
))
<
0
)
{
perror
(
"Could not open"
);
exit
(
-1
);
}
switch
(
testnum
){
case
0
:
scan
(
fd
);
break
;
case
1
:
test1_resetcontroller
(
fd
,
addr
);
break
;
case
2
:
test2_startstop
(
fd
,
addr
);
break
;
case
3
:
test3_readwrite
(
fd
,
addr
);
break
;
default
:
perror
(
"invalid test"
);
exit
(
-1
);
break
;
}
close
(
fd
);
exit
(
0
);
}
File Metadata
Details
Attached
Mime Type
text/x-c
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2043432
Default Alt Text
main.cpp (2 KB)
Attached To
Mode
D16698: First draft HID over I2C support (Mouse only)
Attached
Detach File
Event Timeline
Log In to Comment