Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
osie
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Nikola Balog
osie
Commits
3bab7efa
Commit
3bab7efa
authored
Mar 16, 2022
by
Ivan Tyagov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Move I2C related code of MOD-IO to a separate file for clear code.
parent
49a15f6d
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
184 additions
and
185 deletions
+184
-185
coupler/opc-ua-server/mod_io_i2c.h
coupler/opc-ua-server/mod_io_i2c.h
+183
-0
coupler/opc-ua-server/server.c
coupler/opc-ua-server/server.c
+1
-185
No files found.
coupler/opc-ua-server/mod_io_i2c.h
0 → 100644
View file @
3bab7efa
// global relay state
uint8_t
I2C_0_RELAYS_STATE
=
0
;
// state of 4 relays at I2C slave 0
uint8_t
I2C_1_RELAYS_STATE
=
0
;
// state of 4 relays at I2C slave 1
// XXX: what if we have more than 2 MOd-IOs / I2C slaves, we need better structure
// the default addresses of MOD-IOs
static
char
*
DEFAULT_I2C_0_ADDR
=
"0x58"
;
// the list of attached I2C slaves
const
int
DEFAULT_I2C_SLAVE_ADDR
=
0x58
;
// XXX: make dynamic array
int
I2C_SLAVE_ADDR_LIST
[]
=
{
0
,
0
,
0
,
0
,
0
};
// the block device at host machine
static
char
*
DEFAULT_I2C_BLOCK_DEVICE_NAME
=
"/dev/i2c-1"
;
char
*
I2C_BLOCK_DEVICE_NAME
;
// global virtual mode needed for testing on x86 platform
bool
I2C_VIRTUAL_MODE
=
0
;
static
int
getI2CSlaveListLength
()
{
/*
* Return ONLY registred I2C slaves
*/
int
i
;
int
addr
;
int
counter
=
0
;
int
length
=
sizeof
(
I2C_SLAVE_ADDR_LIST
)
/
sizeof
(
int
);
for
(
i
=
0
;
i
<
length
;
i
++
)
{
addr
=
I2C_SLAVE_ADDR_LIST
[
i
];
if
(
addr
!=
0
)
{
counter
++
;
}
}
return
counter
;
}
static
int
setRelayState
(
int
command
,
int
i2c_addr
)
{
/*
* Set relays' state over I2C
*/
int
file
;
char
filename
[
20
];
if
(
I2C_VIRTUAL_MODE
)
{
// we're in a virtual mode, likely on x86 platform or without I2C support
// simply do nothing
return
0
;
}
// step 1: open device
file
=
open
(
I2C_BLOCK_DEVICE_NAME
,
O_RDWR
);
if
(
file
<
0
)
{
/* ERROR HANDLING; you can check errno to see what went wrong */
printf
(
"Error opening i2c device (0x%x).
\n
"
,
i2c_addr
);
exit
(
1
);
}
// step 2: address the slave by its address
if
(
ioctl
(
file
,
I2C_SLAVE
,
i2c_addr
)
<
0
)
{
/* ERROR HANDLING; you can check errno to see what went wrong */
printf
(
"Error addressing i2c slave (0x%x).
\n
"
,
i2c_addr
);
exit
(
1
);
}
// step 3: write command over I2c
__u8
reg
=
0x10
;
/* Device register to access */
char
buf
[
10
];
buf
[
0
]
=
reg
;
buf
[
1
]
=
command
;
//0x00 -all off, 0x0F - all 4 on
if
(
write
(
file
,
buf
,
3
)
!=
3
)
{
/* ERROR HANDLING: i2c transaction failed */
printf
(
"Error writing to i2c slave (0x%x).
\n
"
,
i2c_addr
);
}
close
(
file
);
}
static
int
getDigitalInputState
(
int
i2c_addr
,
char
**
digital_input
)
{
/*
* get digital input state over I2C
*/
int
file
;
char
filename
[
20
];
// step 1: open device
file
=
open
(
I2C_BLOCK_DEVICE_NAME
,
O_RDWR
);
if
(
file
<
0
)
{
/* ERROR HANDLING; you can check errno to see what went wrong */
printf
(
"Error opening i2c device (0x%x).
\n
"
,
i2c_addr
);
exit
(
1
);
}
// step 2: address the slave by its address
if
(
ioctl
(
file
,
I2C_SLAVE
,
i2c_addr
)
<
0
)
{
/* ERROR HANDLING; you can check errno to see what went wrong */
printf
(
"Error addressing i2c slave (0x%x).
\n
"
,
i2c_addr
);
exit
(
1
);
}
// step 3: write command over I2c
__u8
read_reg
=
0x20
;
/* Device register to access */
char
read_buf
[
2
];
read_buf
[
0
]
=
read_reg
;
if
(
write
(
file
,
read_buf
,
1
)
!=
1
)
{
/* ERROR HANDLING: i2c transaction failed */
printf
(
"Error writing to i2c slave (0x%x).
\n
"
,
i2c_addr
);
}
if
(
read
(
file
,
read_buf
,
1
)
!=
1
)
{
/* ERROR HANDLING: i2c transaction failed */
printf
(
"Error reading digital input from i2c slave (0x%x).
\n
"
,
i2c_addr
);
}
else
{
/* read_buf[0] contains the read byte */
*
digital_input
=
&
read_buf
[
0
];
}
close
(
file
);
}
static
int
getAnalogInputStateAIN
(
int
i2c_addr
,
int
**
analog_input
,
uint8_t
read_reg
)
{
/*
* get digital input state over I2C
*/
int
file
;
char
filename
[
20
];
// step 1: open device
file
=
open
(
I2C_BLOCK_DEVICE_NAME
,
O_RDWR
);
if
(
file
<
0
)
{
/* ERROR HANDLING; you can check errno to see what went wrong */
printf
(
"Error opening i2c device (0x%x).
\n
"
,
i2c_addr
);
exit
(
1
);
}
// step 2: address the slave by its address
if
(
ioctl
(
file
,
I2C_SLAVE
,
i2c_addr
)
<
0
)
{
/* ERROR HANDLING; you can check errno to see what went wrong */
printf
(
"Error addressing i2c slave (0x%x).
\n
"
,
i2c_addr
);
exit
(
1
);
}
// step 3: write command over I2c
//__u8 read_reg = 0x30; /* Device register to access */
char
read_buf
[
10
];
read_buf
[
0
]
=
read_reg
;
if
(
write
(
file
,
read_buf
,
1
)
!=
1
)
{
/* ERROR HANDLING: i2c transaction failed */
printf
(
"Error writing to i2c slave (0x%x).
\n
"
,
i2c_addr
);
}
if
(
read
(
file
,
read_buf
,
2
)
!=
2
)
{
/* ERROR HANDLING: i2c transaction failed */
printf
(
"Error reading analog input from i2c slave (0x%x).
\n
"
,
i2c_addr
);
}
else
{
int
analog_data
=
0
;
// based on https://github.com/OLIMEX/OLINUXINO/blob/master/SOFTWARE/A13/MOD-IO/main.c
// since ADC is 10 bit we need to read and convert accordingly 2 bytes
analog_data
=
read_buf
[
1
];
analog_data
<<=
8
;
analog_data
|=
read_buf
[
0
];
*
analog_input
=
&
analog_data
;
}
close
(
file
);
}
coupler/opc-ua-server/server.c
View file @
3bab7efa
...
...
@@ -24,7 +24,7 @@
#include <argp.h>
#include <string.h>
#include "common.h"
#include "mod_io_i2c.h"
// The default port of OPC-UA server
const
int
DEFAULT_OPC_UA_PORT
=
4840
;
...
...
@@ -98,190 +98,6 @@ static error_t parse_opt(int key, char *arg, struct argp_state *state)
static
struct
argp
argp
=
{
options
,
parse_opt
,
args_doc
,
doc
,
0
,
0
,
0
};
// global relay state
uint8_t
I2C_0_RELAYS_STATE
=
0
;
// state of 4 relays at I2C slave 0
uint8_t
I2C_1_RELAYS_STATE
=
0
;
// state of 4 relays at I2C slave 1
// XXX: what if we have more than 2 MOd-IOs / I2C slaves, we need better structure
// the default addresses of MOD-IOs
static
char
*
DEFAULT_I2C_0_ADDR
=
"0x58"
;
// the list of attached I2C slaves
const
int
DEFAULT_I2C_SLAVE_ADDR
=
0x58
;
// XXX: make dynamic array
int
I2C_SLAVE_ADDR_LIST
[]
=
{
0
,
0
,
0
,
0
,
0
};
// the block device at host machine
static
char
*
DEFAULT_I2C_BLOCK_DEVICE_NAME
=
"/dev/i2c-1"
;
char
*
I2C_BLOCK_DEVICE_NAME
;
// global virtual mode needed for testing on x86 platform
bool
I2C_VIRTUAL_MODE
=
0
;
static
int
getI2CSlaveListLength
()
{
/*
* Return ONLY registred I2C slaves
*/
int
i
;
int
addr
;
int
counter
=
0
;
int
length
=
sizeof
(
I2C_SLAVE_ADDR_LIST
)
/
sizeof
(
int
);
for
(
i
=
0
;
i
<
length
;
i
++
)
{
addr
=
I2C_SLAVE_ADDR_LIST
[
i
];
if
(
addr
!=
0
)
{
counter
++
;
}
}
return
counter
;
}
static
int
setRelayState
(
int
command
,
int
i2c_addr
)
{
/*
* Set relays' state over I2C
*/
int
file
;
char
filename
[
20
];
if
(
I2C_VIRTUAL_MODE
)
{
// we're in a virtual mode, likely on x86 platform or without I2C support
// simply do nothing
return
0
;
}
// step 1: open device
file
=
open
(
I2C_BLOCK_DEVICE_NAME
,
O_RDWR
);
if
(
file
<
0
)
{
/* ERROR HANDLING; you can check errno to see what went wrong */
printf
(
"Error opening i2c device (0x%x).
\n
"
,
i2c_addr
);
exit
(
1
);
}
// step 2: address the slave by its address
if
(
ioctl
(
file
,
I2C_SLAVE
,
i2c_addr
)
<
0
)
{
/* ERROR HANDLING; you can check errno to see what went wrong */
printf
(
"Error addressing i2c slave (0x%x).
\n
"
,
i2c_addr
);
exit
(
1
);
}
// step 3: write command over I2c
__u8
reg
=
0x10
;
/* Device register to access */
char
buf
[
10
];
buf
[
0
]
=
reg
;
buf
[
1
]
=
command
;
//0x00 -all off, 0x0F - all 4 on
if
(
write
(
file
,
buf
,
3
)
!=
3
)
{
/* ERROR HANDLING: i2c transaction failed */
printf
(
"Error writing to i2c slave (0x%x).
\n
"
,
i2c_addr
);
}
close
(
file
);
}
static
int
getDigitalInputState
(
int
i2c_addr
,
char
**
digital_input
)
{
/*
* get digital input state over I2C
*/
int
file
;
char
filename
[
20
];
// step 1: open device
file
=
open
(
I2C_BLOCK_DEVICE_NAME
,
O_RDWR
);
if
(
file
<
0
)
{
/* ERROR HANDLING; you can check errno to see what went wrong */
printf
(
"Error opening i2c device (0x%x).
\n
"
,
i2c_addr
);
exit
(
1
);
}
// step 2: address the slave by its address
if
(
ioctl
(
file
,
I2C_SLAVE
,
i2c_addr
)
<
0
)
{
/* ERROR HANDLING; you can check errno to see what went wrong */
printf
(
"Error addressing i2c slave (0x%x).
\n
"
,
i2c_addr
);
exit
(
1
);
}
// step 3: write command over I2c
__u8
read_reg
=
0x20
;
/* Device register to access */
char
read_buf
[
2
];
read_buf
[
0
]
=
read_reg
;
if
(
write
(
file
,
read_buf
,
1
)
!=
1
)
{
/* ERROR HANDLING: i2c transaction failed */
printf
(
"Error writing to i2c slave (0x%x).
\n
"
,
i2c_addr
);
}
if
(
read
(
file
,
read_buf
,
1
)
!=
1
)
{
/* ERROR HANDLING: i2c transaction failed */
printf
(
"Error reading digital input from i2c slave (0x%x).
\n
"
,
i2c_addr
);
}
else
{
/* read_buf[0] contains the read byte */
*
digital_input
=
&
read_buf
[
0
];
}
close
(
file
);
}
static
int
getAnalogInputStateAIN
(
int
i2c_addr
,
int
**
analog_input
,
uint8_t
read_reg
)
{
/*
* get digital input state over I2C
*/
int
file
;
char
filename
[
20
];
// step 1: open device
file
=
open
(
I2C_BLOCK_DEVICE_NAME
,
O_RDWR
);
if
(
file
<
0
)
{
/* ERROR HANDLING; you can check errno to see what went wrong */
printf
(
"Error opening i2c device (0x%x).
\n
"
,
i2c_addr
);
exit
(
1
);
}
// step 2: address the slave by its address
if
(
ioctl
(
file
,
I2C_SLAVE
,
i2c_addr
)
<
0
)
{
/* ERROR HANDLING; you can check errno to see what went wrong */
printf
(
"Error addressing i2c slave (0x%x).
\n
"
,
i2c_addr
);
exit
(
1
);
}
// step 3: write command over I2c
//__u8 read_reg = 0x30; /* Device register to access */
char
read_buf
[
10
];
read_buf
[
0
]
=
read_reg
;
if
(
write
(
file
,
read_buf
,
1
)
!=
1
)
{
/* ERROR HANDLING: i2c transaction failed */
printf
(
"Error writing to i2c slave (0x%x).
\n
"
,
i2c_addr
);
}
if
(
read
(
file
,
read_buf
,
2
)
!=
2
)
{
/* ERROR HANDLING: i2c transaction failed */
printf
(
"Error reading analog input from i2c slave (0x%x).
\n
"
,
i2c_addr
);
}
else
{
int
analog_data
=
0
;
// based on https://github.com/OLIMEX/OLINUXINO/blob/master/SOFTWARE/A13/MOD-IO/main.c
// since ADC is 10 bit we need to read and convert accordingly 2 bytes
analog_data
=
read_buf
[
1
];
analog_data
<<=
8
;
analog_data
|=
read_buf
[
0
];
*
analog_input
=
&
analog_data
;
}
close
(
file
);
}
void
addIntegerVariableNode
(
UA_Server
*
server
,
char
*
node_id
,
char
*
node_description
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment