I'm road testing the Harting MICA Complete IIoT Starter Kit.
Now - finally - let's create a C program that talks to the I/O lines of the MICA.
This is the end goal of this blog series (hang on, there's an extra one that shows a solution that uses much less memory and disk space): How to set up an interactive environment where you can write code, deploy, test and debug.
There are only two additional steps to do:
- make the GPIO dependencies (we need 3 Linux libraries and an API header file) available on Windows
- make a program that talks to the I/O lines, build it and run it on the MICA.
Set Up GPIO Library and Header Files in Windows
The program will use shared libraries - the libraries we depend on are referenced but aren't linked in the executable.
At run time, Linux will take care that our program can access these libraries and can execute the functions provided by them.
But the linker needs to know these libraries, so we have to make them available on Windows at build-time.
There are two groups we have to provide:
- the mica gpio library and the header file that describes its API: libmica-gpio and mica_gpio.h
- three libraries the gpio library depends on: libudev, libhidapi-libusb and libusb-1.0
All these files are installed in the Debian Stretch container during the setup done in part 2.
To get them downloaded to windows, you can use an sFTP client (such as winSCP).
For this moment, I have copied all the files to a single directory on my Windows computer.
Here are the locations of the files you have to get (for those with a version number in their name - no worries, get the one that you find. We create a symbolic link later on):
While you are on the sFTP server, you might as well get the example file we'll use in our project:
You can copy it to the same directory for now. We'll move it into the Eclipse project later.
Once the files are moved over, we want to make symbolic links to the ones that have a version number at the end of their name.
That takes care that our code is not dependent on a specific release of the libraries.
I'm expecting that you run a Windows version that supports it ( Vista, 7, 8 and 10 - you'd know by now, because the installation of the tool chain of post 3 would have failed if your system doesn't support it).
Open a Windows command prompt as ADMINISTRATOR and navigate to the directory where you downloaded the libraries.
Then execute these commands:
mklink libhidapi-libusb.so libhidapi-libusb.so.0.0.0 mklink libudev.so libudev.so.1.6.5 mklink libusb-1.0.so libusb-1.0.so.0.1.0
That's it for the dependency installation. Let's start developing.
Create a GPIO Eclipse Project
Open DS-5, and create a new C project, mica_gpio.
Choose empty project, select your tool chain and click finish.
In the root, create a new folder named src.
Copy the main.c that you downloaded earlier on to the src folder in Eclipse.
You can copy it with Windows explorer, then right-click on the src folder in Eclipse and select paste.
After that, you can delete the file from your lib folder - it's not needed there.
Next, add the location of the headerfile to the Eclipse include path.
Then, add that same folder to the library search path, and add the libraries to the list below the path.
Libraries to add:
Type this exactly as above, without lib at the beginning or .so at the end. The tool chain will take care of that.
It will look in your path folder for the libs at link time.
Open the main.c file in the src folder and have a look at the code. This will help you to understand what's happening when you execute the program later.
Now try to build the project by right-clicking on it and selecting Build Project.
This should finish without any errors.
16:29:08 **** Build of configuration Debug for project mica_gpio **** make all 'Building file: ../src/main.c' 'Invoking: GCC C Compiler 5.3.1 [arm-linux-gnueabihf]' arm-linux-gnueabihf-gcc.exe -ID:\users\jancu\Documents\elektronica\Harting\mica\libs -O0 -g -Wall -c -fmessage-length=0 -MMD -MP -MF"src/main.d" -MT"src/main.o" -o "src/main.o" "../src/main.c" 'Finished building: ../src/main.c' ' ' 'Building target: mica_gpio' 'Invoking: GCC C Linker 5.3.1 [arm-linux-gnueabihf]' arm-linux-gnueabihf-gcc.exe -L"D:\users\jancu\Documents\elektronica\Harting\mica\libs" -o "mica_gpio" ./src/main.o -lmica-gpio -lhidapi-libusb -lusb-1.0 -ludev 'Finished building target: mica_gpio' ' ' 16:29:11 Build Finished (took 2s.780ms)
Execute from the Debugger and Directly from the MICA
Just like when we tried the Hello, world! C++ program, we have to create a Debug configuration.
We can copy the one made in post 3 (right-click on it and select Duplicate), and adapt it:
The only two things you need to change is the name and the location of the binary file.
Then click Apply.
You are now running and stepping through your first GPIO C program.
Take your time to step through the code, watch the variables change, look at the App console output while doing that.
Put a voltmeter on the I/O wire to get that real feeling for what's happening.
As a last step, let's run the executable direct on the MICA, without using Eclipse to launch it.
First, take care that the program you were debugging has exited. Run it to the end, until the debug buttons are grayed out and the Debug Console tells the application is terminated.
Then, use a terminal (e.g.: PuTTY) to SSH into the Debian Stretch container (we've done that a few times before in the previous posts).
go to /root/bin
Execute your program and look at the output.
You should see this in your console:
That was the whole thing. I hope the explanation is clear and you're able to repeat it without issues.
I'll prepare the bonus post where a lighter weight container is used to host this program.