alex_cmx
I can't find anywhere how to make HelloWorld project.
I have Olimex AM-3352 with Linux installed on it and HelloWorld project.
I have already installed runtime on board and editor on Windows. 
I have such leds:
изображение_2021-03-17_112541.png 
How can I run program and how translate addresses such %IX0.0 to this devices?
Thank you.
Reply 0 0
thiagoralves
You’ll have to write a driver for OpenPLC to be able to control these. OpenPLC drivers are located at OpenPLC_v3/webserver/core/hardware_layers. You can use the blank driver as a guide on how to write yours
Reply 0 0
alex_cmx
You’ll have to write a driver for OpenPLC to be able to control these. OpenPLC drivers are located at OpenPLC_v3/webserver/core/hardware_layers. You can use the blank driver as a guide on how to write yours

Thank you very much. And how can I match my *.st file with cpp driver?
Reply 0 0
thiagoralves
The driver is at the core, and will run independently of the .st file. What the driver will do is update OpenPLC buffers according to the hardware. OpenPLC buffers are linked to PLC locations, and that's how your PLC program (.st file) ends up interacting with hardware. The link is described in glueVars.cpp, but goes around these lines:
%IX0.0 = *bool_input[0][0]
%IX0.1 = *bool_input[0][1]
...
%QX0.0 = *bool_output[0][0]
%QX0.1 = *bool_output[0][1]
...
%IW0 = *int_input[0]
%IW1 = *int_input[1]
...
%QW0 = *int_output[0]
%QW1 = *int_output[1]
Reply 0 0
alex_cmx
Is there any good documentaion of this? To help me not to ask all of my questions and just search it? 
Thank you very much.
Reply 0 0
thiagoralves
The best documentation you’ll find is in the comments inside the blank.cpp and the other drivers.
Reply 0 0
alex_cmx
Ok I understand interfaces from .st file goes to glueVars.cpp Like  изображение_2021-03-18_181346.png    
but how can I make, for example after changing __QX0_0 variable put its value to some file?
I mean where and how works variable cnange handler? what function calls when variable changes?
Reply 0 0
thiagoralves
You don't have to worry about any of that. This is OpenPLC doing its thing. Focus on your driver. The only file you will have to edit is blank.cpp (or any other name you want to give it). The driver is the bridge that connects your hardware IO with these OpenPLC buffers (bool_output[0][0], etc). GlueVars is what connects the OpenPLC buffers with your program variables (%QX0.0, etc). You don't have to change any of that. Just to give you an idea, the following driver increases int_input[0] by one every program scan cycle. If you put this inside blank.cpp and select the blank driver on the hardware layer configuration, you will see that your %IW0 input will increase by one every scan cycle. 
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include "ladder.h"
#include "custom_layer.h"
//-----------------------------------------------------------------------------
// This function is called by the main OpenPLC routine when it is initializing.
// Hardware initialization procedures should be here.
//-----------------------------------------------------------------------------
void initializeHardware()
{
}
//-----------------------------------------------------------------------------
// This function is called by the main OpenPLC routine when it is finalizing.
// Resource clearing procedures should be here.
//-----------------------------------------------------------------------------
void finalizeHardware()
{
}
//-----------------------------------------------------------------------------
// This function is called by the OpenPLC in a loop. Here the internal buffers
// must be updated to reflect the actual Input state. The mutex bufferLock
// must be used to protect access to the buffers on a threaded environment.
//-----------------------------------------------------------------------------
void updateBuffersIn()
{
  pthread_mutex_lock(&bufferLock); //lock mutex
  if (int_input[0] != NULL) 
    *int_input[0] = *int_input[0] + 1;
  pthread_mutex_unlock(&bufferLock); //unlock mutex
}
//-----------------------------------------------------------------------------
// This function is called by the OpenPLC in a loop. Here the internal buffers
// must be updated to reflect the actual Output state. The mutex bufferLock
// must be used to protect access to the buffers on a threaded environment.
//-----------------------------------------------------------------------------
void updateBuffersOut()
{
  pthread_mutex_lock(&bufferLock); //lock mutex
  /*********READING AND WRITING TO I/O**************
  *bool_input[0][0] = read_digital_input(0);
  write_digital_output(0, *bool_output[0][0]);
  *int_input[0] = read_analog_input(0);
  write_analog_output(0, *int_output[0]);
  **************************************************/
  pthread_mutex_unlock(&bufferLock); //unlock mutex
}
Reply 0 0
LeSpocky
alex_cmx wrote:
I can't find anywhere how to make HelloWorld project.
I have Olimex AM-3352 with Linux installed on it and HelloWorld project.
I have already installed runtime on board and editor on Windows. 
I have such leds:
изображение_2021-03-17_112541.png 
How can I run program and how translate addresses such %IX0.0 to this devices?
Thank you.


Those LEDs are controlled by a Linux kernel driver from the led subsystem. Regardless how those are wired, those are claimed by the kernel already. You should not mess with that. If you want to do anyways, change your devicetree file and don't let any led driver use them.

For accessing GPIOs on Linux you should look into libgpiod. That's the official user space library using the modern gpiolib interface of the kernel.

In other words: use libgpiod inside of the OpenPLC hardware layer you are writing.
Reply 0 0
alex_cmx
Wow! Wonderful! It works. I add this изображение_2021-03-21_141946.png 
and It starts working. Thank you very much!
Reply 0 0
Reply