Professional Documents
Culture Documents
June 2016
Neuromeka
1
Getting Started with NRMKPlatform
for STEP/PC2
Copyright 2013-2016 Neuromeka
#406, Seongsu IT Center
37, Seongsui-ro 22-gil, Seongdong-gu
Seoul 133-832, South Korea
Tel +82-70-4791-3103 • Fax +82-70-7614-3026
2
Copyright & Trademark Notice
NRMKFoundation, NRMKPlatform, STEP, and the related logos as well as all related files and
documentation are Copyright © 2013-2016 by Neuromeka. All rights reserved.
NRMKFoundation, NRMKPlatform, and STEP are trademarks of Neuromeka. EtherCAT and
the related logos are registered trademark of Beckhoff Automation GmbH. EtherLab and
IgH EtherCAT master are registered trademark of IgH (Ingenieurgemeinshaft). All other
trademarks or registered trademarks mentioned are the properties of their respective
owners.
3
Table of Contents
1. Introduction to NRMKPlatform........................................................................................................................... 8
4.3. CANbus........................................................................................................................................................................ 49
4
List of Figures
Figure 2-9. Scanning host and target IP address with Advanced IP Scanner utility ............... 21
Figure 3-3. Eclipse IDE with examples for NRMKPlatform for STEP/PC2.................................... 27
Figure 3-5. Run XenomaiTest example with its run configuration ............................................. 30
Figure 3-9. Creating a new run configuration from an example configuration ...................... 34
5
Figure 3-10. Launching error in Eclipse .............................................................................................. 35
Figure 4-9. Example or using SERCAN_dump to read CAN frame from SERCAN port............ 50
6
List of Codes
7
1. Introduction to NRMKPlatform
NRMKPlatform consists of two components, called Engine and Target, as shown
in Figure 1-1. For NRMKFoundation please refer to the related documents, e.g.
Getting Started with NRMKFoundation, NRMKFoundation Simulation Guide,
and NRMKFoundation Control Guide.
1
BeagleBone black is a registered trademark of The BeagleBoard.org Foundation.
2
Raspberry Pi is a registered trademark of Raspberry Pi Foundation.
8
Figure 1-1. Components of NRMKPlatform
Depending on application requirements in terms of system size, control
bandwidth and/or interface capability several types of embedded computers
can be used for NRMKPlatform, ranging from single- or multi-core ARM
microcomputers, such as Freescale iMX 6Quad based board (STEP/iMX3) (built
upon 1GHz Quadcore ARM® Cortex-A9 processor), to full-powered newest
Intel computers. Regardless of which target system is employed every system
runs Linux with Xenomai realtime kernel and is pre-configured to run control
applications in hard realtime. In general development on embedded systems is
not too easy as developing on a user-friendly environment. NRMKPlatform
comes with NRMKPlatform SDK providing a complete tool chain to develop
hard realtime embedded control applications in a more familiar development
host, i.e. Windows, Linux, and Mac4. It allows cross-compiling, building, running
as well as debugging those applications running on the target online within a
3
STEP/iMX is used as an embedded controller to run developed target applications using
NRMKFoundation.
4
Mac version of NRMKPlatform is not yet available.
9
development host. Thanks to NRMKFoundation support in NRMKPlatform one
can easily rapid-prototype realtime control applications as well as control
algorithms, test and deploy them.
The main IDE is implemented by Eclipse with CDT (C/C++ Development Tools)
combined with a set of tools for relatime Linux target system. For example, for
STEP/PC2 it consists of:
• GNU cross compiler for i686 target in cygwin
• Real-time Kernel Libraries: Xenomai (Real-time)
10
• Prebuilt EtherLab5 EtherCAT master
• Third-party libraries for NRMKFoundation including Eigen, Poco, etc.
• Online debugger via SSH for STEP/PC2
• Direct execution/downloading binary files to STEP/PC2
The following chapters will describe how to use these tools to generate high-
quality embedded realtime control applications on these targets.
5
EtherLab is an open source EtherCAT master running on Linux, http://etherlab.org/en/ethercat.
11
2. Getting STEP/PC2 Ready
Note) For more information on STEP/PC2 and NRMKPlatform SDK please
check wiki http://wiki.neuromeka.net/index.php?title=NRMKPlatform .
NRMKPlatform for STEP/PC2, is packaged with the following components:
Industrial PC with Intel Celeron Braswell N3150 (Quad core@1.6GHz)6 with
Linux (Ubuntu 14.04) installed and patched with Xenomai realtime extension
Ethernet cable: STEP PC2 has two Ethernet ports with one for EtherCAT slave
connection and another for connection with the host. The Ethernet cable is
used to connect between the host and the target.
NRMKPlatformPC2 CD storing the setup file for NRMKPlatformPC2 Tools
and related documents
6
A general PC with 4th generation Core-i5 Intel CPU is also available upon request.
12
Figure 2-1. STEP/PC2 package
13
2.1. Installing NRMKPlatform SDK
RT control applications running in STEP platforms are best developed with
support of NRMKFoundation. It is hence strongly recommended 7 that you
install NRMKFoundation before installing NRMKPlatform SDK. In this guide it is
assumed that NRMKFoundation has been successfully installed. For installing
NRMKFoundation please refer to Getting Started with NRMKFoundation.
In order to install NRMKPlatformPC2 Tools on the host PC, execute the setup
program SetupNRMKPlatformPC2_v1.x.y_20yymmdd.exe in the setup CD. This
will install the following softwares automatically:
Java Runtime Environment (JRE) for running Eclipse IDE on Windows
Eclipse IDE
Cygwin
GCC cross compiler for i686 on Windows
NRMKPlatformPC2 libraries: Xenomai and EtherCAT library
You can see the welcome screen as shown below.
7
NRMKPlatform can be used without any support of NRMKFoundation as long as one can
develop Linux applications directly on the target systems.
14
Figure 2-2. Welcome to Installation
Clicking Install you are guided to install the required tools automatically. If you
are not sure whether some tools have been already installed or not it is
recommended to reinstall them.
First, users choose the installation directory, the default one is “C:\Program
Files\neuromeka”. Users will be asked to install JRE tool in the dialog shown in
Figure 2-3. If your computer does not have java (JRE or JDK) installed you may
click Yes to install this tool.
15
Figure 2-3. Installing JRE tool
In the next step you need to install the Microsoft Visual C++ redistributable
pakage. This is nesecassy for running several Neuromeka tools.
You can go through all the remaining steps for the other tools and libraries.
16
When everything has been installed successfully you will see the dialog shown
on the right side of Figure 2-5.
Figure 2-5. Installing NRMKPlatform Tools libraries and examples
2.2. Organization of the NRMKPlatform Installation Folder
NRMKPlatformPC Tools is installed by default in the subfolder of the parent
folder of NRMKFoundation. In order to access the installation folder you can
use NRMKLauncher application installed with NRMKFoundation.
Executing NRMKLauncher, click on item Root to browse NRMKFoundation’s
root folder by Windows explorer. If you go upward you will see two installed
folders: NRMKFoundation and NRMKPlatformPC2. NRMKPlatformPC2 is the
installation folder for NRMKPlatformPC2 Tools. Inside this folder there are four
sub-folders. The structure of these folder are explained as follow:
NRMKPlatformPC2
|-----bin
|-----eclipse
|-----i686
17
|-----doc
|-----drivers
|-----example
|-----workspace
Bin: Xenomai library, EtherCAT library and Eclipse IDE.
Doc: relevant document for NRMKPlatformPC Tools
Drivers: drivers, tools for NRMKPlatformPC
Example/workspace: prebuilt examples
In addition to the NRMKPlatformPC folder, the installation has also installed the
gnu toolchain in the default folder C:/cygwin.
2.3. Booting the STEP/PC2
This section guides you to the necessary steps to connect the target to the host,
to boot the target and to check its performance remotely by the host.
The STEP/PC2 is equipped with two RJ45 ports, one of which is used for
EtherCAT communication and the other is for Ethernet8 as in Figure 2-6.
8
In special case user can utilize both two ports for EtherCAT communication.
18
Figure 2-6. Ethernet and EtherCAT ports
By default the Ethernet port has been configured to automatically acquire IP
(DHCP). In case of no Ethernet support one can directly connect to the target
by Ethernet cable as long as the host’s network configuration is configured
properly to have a static IP. The procedure is explained below.
Open Network Connections center in the host PC. In the window shown in
Figure 2-7 right-click on Ethernet adapter and choose Properties.
In Properties dialog choose Internet Protocol Version 4 (TCP/IPv4) connection
and click Properties to open the dialog. Now you need to set the IP address
19
manually9 as shown in Figure 2-8. Set it with any valid IP address, for example
192.168.137.1.
Figure 2-8. Configure IP for host
Now you are ready to boot the target. Turn on the target PC and wait a couple
of minutes after booting completes. After a while run Advanced IP Scanner10
and click Scan toolbar button to retrieve the IP of the host and the target in the
network.
9
You can also use a router to connect the target to the host and let the target obtain the IP.
10
Advanced IP Scanner can be downloaded freely from e.g. http://www.advanced-ip-scanner.com/.
20
Figure 2-9. Scanning host and target IP address with Advanced IP Scanner utility
The target’s name is STEP-TP, as shown in Figure 2-9. For this example its IP is
192.168.137.2 and the host, e.g. of name PRES-PC, has the IP address of
192.168.137.1. Once the host and the target are connected user can access the
target via SSH by SSH client applications like Putty. Putty can be launched from
menu Tools of NRMKLauncher.
Launching PuTTY, select connection type to SSH, input the target’s IP in the
Host Name field, i.e. 192.168.137.124 (or whatever obtained from IP scanner).
Giving a name for this connection in Saved Sessions field, click Save button
and then Open to connect to target.
21
Figure 2-10. SSH connection to target using putty
Since this is the first time you log in the target with PuTTY you will be asked to
confirm several security issues. Click OK to pass the issues and log in. Log in to
the target with username “user” and password “nrmk2013”, and you will see the
PuTTY terminal like in Figure 2-11.
22
Figure 2-11. Log in the target using PuTTY
After connecting the host and the target directly by Ethernet cable one can test
Once the host and the target are connected there are several methods to
deploy the application to the target:
Connecting to SSH server of the target, where the application is developed
on the host and downloaded to the target via SSH protocol for debugging
or running.
Files transferring between the host and the target is done by samba protocol.
The samba server has already been installed on the STEP/PC2 target. In order
to access the target from Windows host PC via samba user can follow the steps
here:
- On host PC start “Windows explorer” (or Windows file mamager), in address
bar input the STEP/PC2 ip address \\192.168.137.124 and press “Enter”.
- Login the STEP/PC2 with id “user” and password “nrmk2013”. The samba
security dialog in shown in Figure 2-12. From now on users can easily transfer
data from/to STEP/PC2.
23
Figure 2-12. Login STEP/PC2 via samba
In case users want to get internet connection for the STEP/PC2 they can connect
Ethernet port to LAN network and repeat the steps from Figure 2-9 to obtain
the actualtarget’s IP.
24
3. Running Your First Target Application
This section explains how to make target applications using the project
template provided with NRMKPlatform SDK.
3.1. Eclipse Configuration
Run NRMKLauncher and you can see Eclipse button active. Click on Eclipse to
run eclipse (as administrator). By default Eclipse will show the Welcome page
as shown in Figure 3-1. If you have already configured Eclipse then it will open
with the default workspace you set.
In any way you may as well change the default workspace by selecting File |
Switch Workspace | Other. Browse the subfolder example\workspace of
NRMKPlatformPC2 for the default workspace (you may choose any folder for
25
workspace).
Eclipse IDE will automatically close and relaunch to update the new workspace.
Then you will see the Eclipse window with several example projects for
NRMKPlatform in panel Project Explorer (as shown in Figure 3-3), which are
summarized in Table 1.
26
Figure 3-3. Eclipse IDE with examples for NRMKPlatform for STEP/PC2
Name Description
[TEMPPLATE-XENO-ECAT] Template project for Xenomai, EtherCAT applications
LPTPort Parallel port control example
rtsercan_test Test application for CAN module in realtime mode
sercan_test Test application for CAN module in normal linux
tpgpio_pwm Example controlling gpio to generate pwm
tpserial Handling serial ports example
XenomaiTest Xenomai simple test (periodic)
Try to build any example project to ensure that all platform tools are working
properly. If it fails to build any example projects you may need to reboot your
27
host computer in order to update system environment for the target.
3.2. Running an Example
This section will explain how to set up an Eclipse project for an application and
to execute it on the remote target. We will try to run the simple example project
XenomaiTest which prints on terminal the actual time with desired period of
one second.
Build the example: in the Project Explorer panel right-click on the
XenomaiTest project and choose Build Configuration | Set Active | 2
Release to change to release mode. Right-click on the project again and
select Build Project. If everything goes fine you will get the “Build Finished”
message without error. You are ready to test your example.
Check the SSH connection: in order to execute the built application on the
STEP/PC2 it should be downloaded to the target. You can do this in Eclipse
via SSH connection. By default one SSH connection has been configured in
the Eclipse IDE. You may confirm this by navigating the menu Windows |
Open Perspective | Other and double-clicking Remote System Explorer in
dialog Open Perspective. You may change the host name (IP) of your
STEP/PC2 target from the default IP address 192.168.137.124 to the actual
one if it is configured as dynamic one.
28
Figure 3-4. Checking the SSH configuration
Get back to C/C++ perspective by selecting Windows | Open Perspective
| Other and C/C++ (default).
29
Figure 3-5. Run XenomaiTest example with its run configuration
It may take a few seconds to download the application to the target. For the
first time you may be asked the password to login the STEP/PC2 as root.
Type “root” in password box (see Figure 3-6) and press OK.
30
Figure 3-6. Entering the root password
Then the console of Eclipse window will display times around 1000 ms as
shown in Figure 3-7.
31
While the application is running you can terminate it by clicking Terminate
button or the red square button in the toolbar of the Console panel as
shown in Figure 3-7.
You can also run the application directly in the target instead of running it inside
Eclipse. As a matter of fact the Eclipse IDE is configured so that when you
execute a selected run configuration in Eclipse the binary is downloaded to the
PC target at directory /home/user/dev/runtest/Release. Hence you can
directly run this without using Eclipse. On PuTTY terminal type sudo
~/dev/runtest/Release/XenomaiTest. You may be asked for password to run
this application. Type “nrmk2013” (or your own password) to activate.
32
3.3. Run Other Aplications
First, set the active configuration to Release and build tpgpio_pwm project.
33
Figure 3-9. Creating a new run configuration from an example configuration
In some cases the application fails to launch with the error dialog in the
following figure. This is mainly due to existence of the same application
executed before and not terminated. Once an application has been executed
it should be terminated properly either by clicking terminate button in
Eclipse or by pressing CTRL + C in the PuTTY terminal. If this error happens
you have to kill the process manually.
One can list current processes running in Linux by ps –A. One can terminate
running process by issuing command sudo pkill tpgpio_pwm. The
command sudo is necessary when you are not logged on as “root”.
34
Figure 3-10. Launching error in Eclipse
35
4. Programming Peripherals on STEP/PC2
STEP/PC2 provides a set of IO functions as shown in Figure 4-1 including 16
GPIO pins. For fieldbus interface it has one CAN ports and one RS422/RS485
port. Furthermore one can connect EtherCAT slaves by the EtherCAT ports11
installed in the STEP/PC2. There are two additional COM ports.
(16 pins) - Output voltage: 0V – 5V
LPT (1) - Shared pins with GPIO
CAN (1) - Support bitrate at 1 Mbps
- CAN 2.0B support
- Isolated transceivers R/W
- Realtime driver support
RS485 (1) - Built on high speed USART module
R/W
- Maximum baudrate of 1.5Mbps
11
By default, STEP/PC2 has one EtherCAT and one Ethernet port. In special case the user can re-
configure to have two working EtherCAT ports.
36
COM Port (2) - COM1, COM2 configured as RS232
- Maximum baudrate of 230400 R/W
Sixteen GPIO pins of the STEP/PC2 are arranged in a DSUB-25P female port.
The actual image of the GPIO port and its pin mapping is shown in Figure 4-2.
37
- GPIO0: pin 12 down to pin 5
- GPIO1: pin 1 to pin 4, and pin 14 to pin 17
- Pin 18, 19: GPIO1 power, be default it is 5V (user can change to 12V by
internal jumper)
- Pin 20-25: GPIO ground
NRMKPlatform provides a complete set of user APIs to make use of these GPIO
functions. The APIs are packaged in a library named NRMKhw which will be
installed with the NRMKPlatform setup. The prototypes of all supported API
functions can be found in file NRMKhw_tp.h loacated in the subfolder
“helper/include/hw” of NRMKFoundation installation folder. All these
functions are listd in Code 1.
1 //GPIO
2 int tp_gpio_init(void);
3 void tp_gpio_setdirout(TP_PORT port, uint8_t regcode);
4 void tp_gpio_setdirin(TP_PORT port, uint8_t regcode);
5 void tp_gpio_set_dir(TP_PORT port, uint8_t regcode);
6
7 uint8_t tp_readport(TP_PORT port);
8 void tp_writeport(TP_PORT port, TP_PORT portval);
9
10 void tp_onpin(TP_PORT port, uint8_t pincode);
11 void tp_offpin(TP_PORT port, uint8_t pincode);
12 void tp_togglepin(TP_PORT port, uint8_t pincode);
13
Code 1. Function prototypes of the GPIO APIs
In addition to the GPIO functions there are several definitions and datatypes
which must be taken into account when using these APIs:
- Port: a group of pins which can be controlled and accessed at the same time.
In STEP/PC2 a port is defined as an enumerated type TP_PORT as in Code 2.
38
- Pin: a single pin in one port. A pin number ranging from 0 to 7 has to be
used with a port number.
1 typedef enum {
2 GPIO_GDIR_INPUT = 0,
3 GPIO_GDIR_OUTPUT = 1,
4 } TP_GPIO_GDIR;
5
6 typedef enum {
7 GPIO_LOW_LEVEL = 0,
8 GPIO_HIGH_LEVEL = 1,
9 } TP_GPIO_LEVEL;
10
11 typedef enum _TP_PORT{
12 port0=0,
13 port1=1
14 } TP_PORT;
Code 2. Additional datatypes
4.1.1. Initialization and closing GPIO
One of the following functions must be called before using any GPIO function
on the STEP/PC2.
- int tp_gpio_init(void): initialize all GPIO ports of the STEP/PC2.
GPIO API functions control and access multiple pins or whole port at the same
time. One can set port direction, write values to or read values from a port.
These are atomic operations which provide higher accessing speed compared
to pin control.
39
ones. Value of 1 of each bit respects to output direction and value of 0 is for not
effected pin. For example, in order to set four pins from 0 to 3 of GPIO1 as
output and keep other pins setting unchaged, user can call:
tp_gpio_setdirout(port1, 0xF).
- void tp_gpio_setdirin(TP_PORT port, uint8_t regcode): set port as input.
Bitwise value of regcode decides which pins in port are set as input ones. Value
of 1 of each bit respects to input direction and value of 0 is for not effected pin.
For example, in order to set four pins from 0 to 3 of GPIO1 as input and keep
other pins setting unchaged, user can call: tp_gpio_setdirin(port1, 0xF).
- void tp_gpio_set_dir(TP_PORT port, uint8_t regcode): select direction for
port. Bitwise value of regcode decides direction of all pins in port. Value of 1 of
each bit respects to output direction and value of 0 is for input pin. For example,
in order to set four LSB pins (0~3) of GPIO1 as input and four pins from 4 to 7
as output, user can call: tp_gpio_set_dir (port1, 0xF0).
- void tp_onpin(TP_PORT port, uint8_t pincode): set the pins masked by
pincode to high level. Bitwise value of pincode decides which pins are affected.
For example, in order to set four pins 0,2,4,6 of GPIO1 to high level and keep
other pins unchanged, user can call: tp_onpin (port1, 0x55). Here 0x55 =
01010101 in which bit 0,2,4,6 has value of 1.
40
- uint8_t tp_readport(TP_PORT port): read value from input port. The return
is an unsigned 8 bit value which indicates pin status of port.
4.1.2. GPIO control example
One simple example which controls GPIO pins is presented in Code 3. In this
example 8 pins of GPIO0 are used to generate 8 PWM signals with xenomai
realtime task. The task’s period is 50us, and therefore the PWM’s period is 100us.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <signal.h>
4 #include <unistd.h>
5 #include <sys/mman.h>
6 #include <sys/io.h>
7
8 #include <native/task.h>
9 #include <native/timer.h>
10 #include <native/mutex.h>
11 #include <rtdk.h>
12
13 #include "NRMKhw_tp.h" //NRMK gpio library
14
15 RT_TASK gpio_out_task;
16 uint8_t allstate=0;
17 TP_PORT test_port=0;
18
19 //output task
20 void gpio_out_run(void *arg)
21 {
22 int state=0;
23 rt_task_set_periodic(NULL, TM_NOW, 5e4); //cycle: 50us
24 while (1)
25 {
26 if (state==0)
27 tp_writeport(test_port, 0xAA);
28 else
29 tp_writeport(test_port, 0x55);
30 state^=1;
31 rt_task_wait_period(NULL);
32 }
33 }
34
35 void catch_signal(int sig)
36 {
37 rt_task_delete(&gpio_out_task);
38 exit(1);
39 }
41
40
41 int main(int argc, char* argv[])
42 {
43 signal(SIGTERM, catch_signal);
44 signal(SIGINT, catch_signal);
45 /* Avoids memory swapping for this program */
46 mlockall(MCL_CURRENT|MCL_FUTURE);
47
48 /*Perform auto-init of rt_print buffers */
49 rt_print_auto_init(1);
50
51 if (tp_gpio_init()!=0) //initialize port
52 {
53 printf("GPIO Init. Error!\n");
54 return 1;
55 }
56
57 tp_gpio_setdirout(test_port, 0xFF);//all pin as output
58
59 printf("Running, check the GPIO state....\n");
60
61 //create and start a realtime periodic task for writing gpio
62 rt_task_create(&gpio_out_task, "gpio_out", 0, 99, 0);
63 rt_task_start(&gpio_out_task, &gpio_out_run, NULL);
64
65 pause();
66
67 rt_task_delete(&gpio_out_task);
68
69 return 0;
70 }
Code 3. STEP/PC2 GPIO control example
The first step to use GPIO library is to include the header file “NRMKhw_tp.h” as
in line 13. This header file contains prototypes of all supported functions which
control GPIO on STEP/PC2.
Before using GPIO function, user have to initialize it by calling function
tp_gpio_init() as in code line 51. Since this example use port0 (test_port) as
output, the function tp_gpio_setdirout() is used with second parameter is 0xFF
which indicates that all pin of port0 will have output direction (see line 57).
PWM generation work is taken by a real time task gpio_out_task which is
created and started in lines 62-63, the task function gpio_out_run is presented
in lines 20-33. In gpio_out_run function, before starting infinity loop, the task
period is set 50us at line 23, rt_task_set_periodic(NULL, TM_NOW, 5e4). It is
42
noted that the unit used for realtime task is nano-second(ns), in order to obtain
50us period, the tick value will be 5e4=50000ns = 50us. In the body of
gpio_out_run function port0’s pins are toggled by functions
tp_writeport(test_port, 0xAA) and tp_writeport(test_port, 0x55). Function
rt_task_wait_period() is used to wait for next period come. The actual PWM
signals on two example pins GPIO0_0 and GPIO0_1 observed from an
oscilloscope arr displayed in Figure 4-3.
43
4.2. Serial communication – COM (RS232) port and RS485
There are two usable COM (RS232) ports and one RS485/422 port avaible in
STEP/PC2. Two COM ports (COM1, COM2) are arrange in separated DSUB-9
connectors while the RS485/RS422 port shares the 9-Pin connector with CAN
port.
Figure 4-4. Serial ports in STEP/PC2
In order to verify the available serial ports on the platform one can use kernel
log:
dmesg | grep tty
Figure 4-5. Check for available serial ports
44
Although there are four serial ports available, only three of them can be used
on STEP/PC2. The ttyS3, ttyS4 are COM1 and COM2 ports on the STEP/PC2
panel, while ttyS0 is the RS485/RS422 port (see Figure 4-6).
Figure 4-6. Serial ports on STEP/PC2
The COM (RS232) ports and RS485 port on STEP/PC2 are controlled by the
NRMK library or standard serial api. The NRMK APIs function prototypes can be
found in “NRMKhw_tp.h” header file as in Code 4.
In these APIs function tp_open_serial_port is the most useful one to open and
setup the serial ports (both RS232 port and RS458 port). Input for this function
is the port name and the communication baudrate, and output is the file
description (fd) for opened serial port. It is noted that on STEP/PC2, due to
physical characteristic, maximum baudrate for RS232 port and RS485 are not
same. For RS232 ports (COM1, COM2) maximum rate is 230400 bps (230.4
Kbps) while that for RS485 port is 1500000 bps (1.5 Mbps). Let’s take a look at
the tpserial example which is included in the install package.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <pthread.h>
4 #include "NRMKhw_tp.h"
5 int fd=-1;
6 void * read_thread(void* arg);
7 void * write_thread(void* arg);
8 //reading thread
9 void * read_thread(void* arg)
10 {
11 int nbytes;
12 char chr;
13 while(1)
14 {
15 nbytes=read(fd,&chr,1);
16 if (nbytes>0){
17 printf("%c", chr);
18 }
19 usleep(1e4);
20 }
21 }
22 //writing thread
23 void * write_thread(void* arg)
24 {
25 int kchr;
26 while(1)
27 {
28 if (NRMKkbhit())
29 {
30 kchr = getchar();
31 write(fd,&kchr,1);
32 }
33 usleep(1e4);
34 }
46
35 }
36 /****************************************************************************
37 * Signal handler
38 ***************************************************************************/
39 void catch_signal(int sig)
40 {
41 if (fd>0) close(fd);
42 exit(1);
43 }
44
45 int main(int argc, char* argv[])
46 {
47 signal(SIGTERM, catch_signal);
48 signal(SIGINT, catch_signal);
49
50 int portNum=1; //COM1 as default
51 char portName[100];
52 unsigned int baud = 115200;
53
54 if (argc>1)
55 portNum = atoi(argv[1]);
56 switch (portNum)
57 {
58 case 0: strcpy(portName, RS485PORT); break;
59 case 1: strcpy(portName, COM1); break;
60 case 2: strcpy(portName, COM2); break;
61 default: strcpy(portName, COM1); break;
62 }
63
64 if (argc>2)baud = atoi(argv[2]);
65
66 if ((portNum==0) && (baud>RS485_BAUD_LIMIT))
67 baud=RS485_BAUD_LIMIT;
68 else if ((portNum>0) && (baud>RS232_BAUD_LIMIT))
69 baud=RS232_BAUD_LIMIT;
70 if (baud<1200)
71 baud=1200;
72
73 printf("Serial example: port=#%s, baud=%i\n", portName, baud);
74
75 fd=tp_open_serial_port(portName, baud);
76 if(fd<0)
77 return 1;
78
79 usleep(1e5);
80 pthread_t tid1,tid2;
81 pthread_create(&tid1, NULL, read_thread, NULL);
82 pthread_create(&tid2, NULL, write_thread, NULL);
83 pthread_join(tid1,NULL);
84 pthread_join(tid2,NULL);
85
86 return 0;
87 }
47
This example controls the serial port, when one types any key on a terminal that
key will be sent via the serial port. When data come into the serial port it is
displayed on the terminal as well. Only the function tp_open_serial_port is used
to open the serial port (line 75). Reading from and writing to serial port are
performed by the standard read/write Linux functions. Two threads are created
for read/write operations. In writing thread (code line 23 – 35) the keyboard is
scanned for input key. If any key is pressed (NRMKkbhit) it will be captured and
sent to the serial port by write function. The first parameter for write function
is file description of serial port, the second parameter is pointer to data buffer
to be sent and the last one is the number of sent bytes. The reading thread
(code line 9 – 21) is a polling loop which wait for data come serial port. The
read system function is used for getting data purpose, nbytes=read(fd,&chr,1).
The first parameter for read function is file description of serial port, the second
parameter is pointer to buffer to save comming data and the last one is the
number of bytes to read. The return value of read function is actual number of
received bytes.
48
4.3. CANbus
There are one CAN port available on STEP/PC2 arranged in one 9-pin connector
with RS485/RS422 port (see Figure 4-6). That is a serial-to-can adapter based
on RS485/RS422, and it is called SERCAN. The driver for this SERCAN does not
work with SocketCAN software, instead we provide its own API functions.
4.3.1. SERCAN utilities
By default, there are several CAN utilities are pre-installed on STEP/PC2. User
can check these utilities by command line:
ls -l /usr/bin/*SERCAN*
Figure 4-7. CAN utilities on STEP/PC2
SERCAN_setBitrate: is tool to configure the working bit rate for SERCAN
module. It can be used to read the current bit rate or set a new one. In order to
read current bit rate, simply call sudo SERCAN_setBitrate in terminal. The
default bit rate of SERCAN module is 1000000 bps (1Mbps). For setting new bit
rate, user should input rate pamameter, e.g. sudo SERCAN_setBitrate 500K, if
the input parameter is valid and it is not the current bite rate value, it will be
updated and printed as in Figure 4-8. The list of valid bit rate parameter include
49
“1M, 500K, 250K, 125K, 50K, 20K, 10K, 5K”.
Figure 4-8. Update new CAN bit rate
SERCAN_send: is a CAN sending tool, which can be used to send a single
CAN frame via SERCAN port. One usage example:
SERCAN_send sercan0 123#0123456789
In this example we send via SERCAN port a CAN frame with ID of 123
(decimal) and five bytes of data: 0x01, 0x23, 0x45, 0x67, and 0x89. It should be
noted that the maximum number of bytes in one CAN frame is eight.
SERCAN_dump: is a CAN reveiving tool. This utility waits for data to
come in the SERCAN port and prints out to the terminal.
SERCAN_dump sercan0
Figure 4-9. Example or using SERCAN_dump to read CAN frame from SERCAN port
RTSERCAN_send and RTSERCAN_dump have same functions as
SERCAN_send, SERCAN_dump except they are using realtime driver.
4.3.2. SERCAN API
SERCAN API is used for programming control SERCAN module. The prototypes
50
of all supported SERCAN API functions can be found in file NRMKsercan.h
loacated in the subfolder “helper/include/hw” of NRMKFoundation installation
folder. All these functions are listed in Code 6.
1 //bit rate
2 int SERCAN_SetBitRate(int fd, char *strBitrate);
3 int32_t SERCAN_GetBitRate(int fd, uint32_t timeOut /* in ms */);
4
5 //normal driver
6 int SERCAN_open(void);
7 int SERCAN_write(int fd, CAN_FRAME TxFrame);
8 int SERCAN_read(int fd, CAN_FRAME *lpRxFrame);
9 void print_CANFrame(CAN_FRAME Rx_Frame);
10 //----------------------------------------
11
12 //rt driver
13 int RTSERCAN_open(void);
14 int RTSERCAN_write(int fd, CAN_FRAME TxFrame);
15 int RTSERCAN_read(int fd, CAN_FRAME *lpRxFrame);
16 void rt_print_CANFrame(CAN_FRAME Rx_Frame);
SERCAN APIs are divide into three groups: bit rate setting, normal CAN driver
and realtime CAN driver.
As its name, bit rate setting functions are used to get or set the bit rate for
SERCAN module:
- SERCAN_GetBitRate(int fd, uint32_t timeOut): get current bit rate.
Normal CAN driver functions use normal Linux driver to control SERCAN
module, these functions are suggested to use in POSIX application.
- SERCAN_open(void): open SERCAN device and return its hanlder for future
51
use. A negative or zero return handler is an invalid one.
All APIs in realtime driver group has same functions as their counterparts in
normal driver groups. The only different is that they are implemented using
xenomai rtdm driver and therefore are suitable for using in realtime applications.
4.3.3. SERCAN example
In setup packge for STEP/PC2, there are two examples controlling wtih SERCAN
module: sercan_test and rtsercan_test. They have same working principle but
using different SERCAN driver. The former use normal CAN driver while the later
use realtime CAN driver. Let’s take a look at the sercan_test example.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <pthread.h>
4
5 #include "NRMKsercan_tp.h"
6 #include "NRMKhw_tp.h"
7
8 int sercan_fd = -1;
9 void * write_thread(void* arg);
10 void * read_thread(void* arg);
11
12 void catch_signal(int sig)
13 {
14 if (sercan_fd>0)
52
15 {
16 close(sercan_fd);
17 }
18 exit(1);
19 }
20 //writing thread
21 void * write_thread(void* arg)
22 {
23 int i;
24 CAN_FRAME cfame;
25 cfame.can_id=0;
26 cfame.can_dlc=8;
27 for (i=0; i<8; ++i) cfame.data[i]=0;
28 cfame.can_id=0x080;
29 cfame.can_dlc=0;
30
31 while(1)
32 {
33 SERCAN_write(sercan_fd, cfame);
34 usleep(1e6); //delay 1s
35 }
36 }
37 //reading thread
38 void * read_thread(void* arg)
39 {
40 CAN_FRAME RxFrame;
41 while(1)
42 {
43 if (SERCAN_read(sercan_fd, &RxFrame)==0)
44 {
45 print_CANFrame(RxFrame);
46 }
47 usleep(1e4);
48 }
49 }
50 int main(int argc, char* argv[])
51 {
52 signal(SIGTERM, catch_signal);
53 signal(SIGINT, catch_signal);
54
55 // open sercan*******************
56 sercan_fd=SERCAN_open();
57 if (sercan_fd <= 0) return 1;
58 //********************************
59 usleep(1e5);
60
61 pthread_t tid1,tid2;
62 pthread_create(&tid1, NULL, read_thread, NULL);
63 pthread_create(&tid2, NULL, write_thread, NULL);
64 pthread_join(tid1,NULL);
65 pthread_join(tid2,NULL);
66
67 return 0;
68 }
Code 7. SERCAN communication example
53
The application structure of sercan_test example is quite similar to tpserial
example. In main function, the SERCAN port is opened with SERCAN_open()
function at line 56. Two thread are created to handle write and read CAN frame.
In write procedure, write_thread from line 21 to line 36, a CAN frame repeatedly
written to SERCAN port at every 1s. In read procude, read_thread from line 38
to line 49, SERCAN port is check for incoming data every 10 ms. If a CAN frame
was received, it will be printed out the screen.
5. Miscellaneous Issues
The STEP/PC2 is pre-configured with two user IDs, the root and the user. The
password for ID root is root and that for user is nrmk2013. When logging in
as root one can access and control any system or hardware stuffs. So this
privilege may become dangerous for the system because the super user may
delete or damage system files accidently. On the other hand the user of ID user
is a normal user which can become super user if needed. In normal situation
the normal user can only access its own files in its own directroty at
“/home/user”. When he/she needs to control system like a super user he/she
should do it as a momentarily super user by sudo command, which means
‘superuser do’. In this case the system requires the password and the user has
to provide the password for user, i.e. nrmk2013. For example the normal user
can change the root password (of ID root) by command sudo passwd12.
12
If a normal user just wants to change its own password, command ‘passwd’ without sudo is
54
Figure 5-1. Change password for user ID
- [sudo] password for user: password for user (nrmk2013) to execute sudo
command
- Enter new UNIX password: new password for root user
- Pretype new UNIX password: retype new password for root user again
Usually each user in Linux system has it own direcroty called home directory
which is the current directory after the user logs in the system or execute
command cd ~ (change directory to home, go to home). The home directory
for root is “/root/”, while that for user is “/home/user/”.
In the home directory of user user there are already several subdirectories and
symbol links (shortcuts) for development purpose.
user home directory
/home/user/
|___dev
|___runtest
|___Release
|___Debug
enough.
55
|___sys
|___RTSERCAN
|___firmware
|___release
|___debug
- /home/user/dev/runtest/Release: store the release binary applications
- /home/user/dev/runtest/Debug: store the debug binary applications
- /home/user/sys/RTSERCAN/firmware: current firmware and update tool
- /home/user/release: symbol link to “~/dev/runtest/Release” directory
- /home/user/debug: symbol link to “~/dev/runtest/Debug” directory
Warning: firmware update is an unrecoverable process, user should backup and
manage current firmware(s) for recovering need.
In order to update new firmware for SERCAN, user have to touch the STEP/PC2
hardware. The proper procedure include:
- Open the top cover of the STEP/PC2
- Locate the bootloader jumper (J_Boot) on SERCAN module, and move
jumper from pin 1-2 to 2-3, as shown in Figure 5-2
56
Figure 5-2. J_Boot configuration for firmware updating
- Power up the STEP/PC2 and press RESET button on SERCAN module
./rtsercan_firmware_update.sh
57
Figure 5-3. Firmware update for SERCAN on STEP/PC2
58