fg71
I have look more into how OpenPLC is doing every step from transpilling plc program to structured text to compile.
 
This is happen, the user have made a plc program and click "Transfer program to PLC"
In the project folder it will create subfolder with name build, in this folder you will find files that are created by iec2c command tool.
One of whose files with the name generated_plc.st
 
When you have configure you MCU and will click the button Transfer to PLC at the bottom of the dialog box.
It will use iec2c command tool again but instead with a name plc_prog.st
 
The question I like to ask, if generated_plc.st is equal to plc_prog.st it would make sense to believe only 1 instance of
iec2c execution are needed. If we like to have a optimized upload speed for a program.
 
Why do OpenPLC need 2 execution of iec2c ??
thiagoralves
There are two different iec2c binaries, one for the editor, and another for the arduino platform. They have different libraries. You cannot reuse the same files generated for the editor with the arduino platform.
Reply 0 0
fg71
I see, I try to reuse the files from src folder and try to build a Arduino IDE sketch of it. I do understand that Beremiz4Pico use other path and OpenPLC.
I also see there is headers for custom blocks like arduino and custom hardware. I also see that modbus is included no matter if a plc program use it or not.
I was not able to get everything working from Arduino IDE but I comment out some error and got it to compile.
 
It makes me to believe OpenPLC could upload a plc program to a board in less and 5 seconds.
 
Or there are other things in OpenPLC that is different from Arduino IDE when it comes to upload performance.
I could understand logging to a terminal would effect the performance and maybe only be useful if we like to debug a error.
Reply 0 0
thiagoralves
OpenPLC uses arduino-cli to build everything, so the compilation process time-wise should be very similar to Arduino IDE. Maybe your sketch is getting compiled faster due to it being significantly smaller. On the MatIEC side of things, I also don't like the idea of having two copies of everything. That's something we are changing on the new editor, but it takes a while to properly identify what belongs to where. Anyway, hopefully you'll be able to see some compilation time improvement on the new editor, but for now we're more focused on functionality than performance. Once everything works well, we will start trimming the edges.
Reply 0 0
fg71
I understand, its only me doing things in the process to learn more, its kind of interesting that code part of the program in C similar to Structured text.
With __INIT_LOCATED, __INIT_LOCATED_VALUE, __SET_VAR and __GET_VAR...
 
And to have all files front of you in Arduino IDE, you kind of see all the low level code in new way.
 
I have got OpenPLC blink example converted to Arduino IDE compiled without any error, the problem is I do not have any output for the Blink_LED.
Its not directly clear to me how OpenPLC use __LOCATED_VAR(BOOL,__QX0_3,Q,X,0,3) that is in file LOCATED_VARIABLES.h
I understand that glueVars.h use it.
 
I'm missing something.... 🙂
 
@thiagoralves could you point me where in the C code do pin number stored ??
Reply 0 0
fg71
 
I think I solve it....
 
The file POUS.c the main program have __INIT_LOCATED function there %QX0_3 is set to variable Blink_LED
The file LOCATED_VARIABLES.h contains __LOCATED_VAR(BOOL,__QX0_3,Q,X,0,3)
The void glueVars() in glueVars.c contains
void glueVars()
{
    bool_output[0][3] = __QX0_3;
}
Now I get a blinking led from Arduino IDE on my Arduino Uno board.
 
This was fun 🙂, at last I got something from OpenPLC to upload to the board in less and 5 seconds 🙂
 
Ps. The file attached here should be running from Arduino IDE and will make Arduino Uno's Led to blink, the output use %QX0.3
Reply 0 0
fg71
Here is the log from Arduino IDE its very compact, that is nice I think.
 
FQBN: arduino:avr:uno
Using board 'uno' from platform in folder: C:\Users\fgustafsson_user\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6
Using core 'arduino' from platform in folder: C:\Users\fgustafsson_user\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6

Detecting libraries used...
C:\Users\fgustafsson_user\AppData\Local\Arduino15\packages\arduino\tools\avr-gcc\7.3.0-atmel3.6.1-arduino7/bin/avr-g++ -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10607 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR -IC:\Users\fgustafsson_user\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6\cores\arduino -IC:\Users\fgustafsson_user\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6\variants\standard C:\Users\fgustafsson_user\AppData\Local\arduino\sketches\2C45DEF16574530FE91EC1BBBC2F99A1\sketch\Baremetal.ino.cpp -o nul
Using cached library dependencies for file: C:\Users\fgustafsson_user\AppData\Local\arduino\sketches\2C45DEF16574530FE91EC1BBBC2F99A1\sketch\Config0.c
Using cached library dependencies for file: C:\Users\fgustafsson_user\AppData\Local\arduino\sketches\2C45DEF16574530FE91EC1BBBC2F99A1\sketch\ModbusSlave.cpp
Using cached library dependencies for file: C:\Users\fgustafsson_user\AppData\Local\arduino\sketches\2C45DEF16574530FE91EC1BBBC2F99A1\sketch\POUS.c
Using cached library dependencies for file: C:\Users\fgustafsson_user\AppData\Local\arduino\sketches\2C45DEF16574530FE91EC1BBBC2F99A1\sketch\Res0.c
C:\Users\fgustafsson_user\AppData\Local\Arduino15\packages\arduino\tools\avr-gcc\7.3.0-atmel3.6.1-arduino7/bin/avr-g++ -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10607 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR -IC:\Users\fgustafsson_user\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6\cores\arduino -IC:\Users\fgustafsson_user\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6\variants\standard C:\Users\fgustafsson_user\AppData\Local\arduino\sketches\2C45DEF16574530FE91EC1BBBC2F99A1\sketch\arduino.cpp -o nul
Alternatives for AVR_PWM.h: [AVR_PWM@1.2.0]
ResolveLibrary(AVR_PWM.h)
  -> candidates: [AVR_PWM@1.2.0]
C:\Users\fgustafsson_user\AppData\Local\Arduino15\packages\arduino\tools\avr-gcc\7.3.0-atmel3.6.1-arduino7/bin/avr-g++ -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10607 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR -IC:\Users\fgustafsson_user\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6\cores\arduino -IC:\Users\fgustafsson_user\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6\variants\standard -Ic:\Users\fgustafsson_user\Documents\Arduino\libraries\AVR_PWM\src C:\Users\fgustafsson_user\AppData\Local\arduino\sketches\2C45DEF16574530FE91EC1BBBC2F99A1\sketch\arduino.cpp -o nul
Using cached library dependencies for file: C:\Users\fgustafsson_user\AppData\Local\arduino\sketches\2C45DEF16574530FE91EC1BBBC2F99A1\sketch\debug.c
Using cached library dependencies for file: C:\Users\fgustafsson_user\AppData\Local\arduino\sketches\2C45DEF16574530FE91EC1BBBC2F99A1\sketch\glueVars.c
Generating function prototypes...
C:\Users\fgustafsson_user\AppData\Local\Arduino15\packages\arduino\tools\avr-gcc\7.3.0-atmel3.6.1-arduino7/bin/avr-g++ -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10607 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR -IC:\Users\fgustafsson_user\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6\cores\arduino -IC:\Users\fgustafsson_user\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6\variants\standard -Ic:\Users\fgustafsson_user\Documents\Arduino\libraries\AVR_PWM\src C:\Users\fgustafsson_user\AppData\Local\arduino\sketches\2C45DEF16574530FE91EC1BBBC2F99A1\sketch\Baremetal.ino.cpp -o C:\Users\fgustafsson_user\AppData\Local\Temp\4081717204\sketch_merged.cpp
C:\Users\fgustafsson_user\AppData\Local\Arduino15\packages\builtin\tools\ctags\5.8-arduino11/ctags -u --language-force=c++ -f - --c++-kinds=svpf --fields=KSTtzns --line-directives C:\Users\fgustafsson_user\AppData\Local\Temp\4081717204\sketch_merged.cpp
Compiling sketch...
"C:\\Users\\fgustafsson_user\\AppData\\Local\\Arduino15\\packages\\arduino\\tools\\avr-gcc\\7.3.0-atmel3.6.1-arduino7/bin/avr-g++" -c -g -Os -Wall -Wextra -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -MMD -flto -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10607 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR "-IC:\\Users\\fgustafsson_user\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\avr\\1.8.6\\cores\\arduino" "-IC:\\Users\\fgustafsson_user\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\avr\\1.8.6\\variants\\standard" "-Ic:\\Users\\fgustafsson_user\\Documents\\Arduino\\libraries\\AVR_PWM\\src" "C:\\Users\\fgustafsson_user\\AppData\\Local\\arduino\\sketches\\2C45DEF16574530FE91EC1BBBC2F99A1\\sketch\\Baremetal.ino.cpp" -o "C:\\Users\\fgustafsson_user\\AppData\\Local\\arduino\\sketches\\2C45DEF16574530FE91EC1BBBC2F99A1\\sketch\\Baremetal.ino.cpp.o"
"C:\\Users\\fgustafsson_user\\AppData\\Local\\Arduino15\\packages\\arduino\\tools\\avr-gcc\\7.3.0-atmel3.6.1-arduino7/bin/avr-g++" -c -g -Os -Wall -Wextra -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -MMD -flto -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10607 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR "-IC:\\Users\\fgustafsson_user\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\avr\\1.8.6\\cores\\arduino" "-IC:\\Users\\fgustafsson_user\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\avr\\1.8.6\\variants\\standard" "-Ic:\\Users\\fgustafsson_user\\Documents\\Arduino\\libraries\\AVR_PWM\\src" "C:\\Users\\fgustafsson_user\\AppData\\Local\\arduino\\sketches\\2C45DEF16574530FE91EC1BBBC2F99A1\\sketch\\arduino.cpp" -o "C:\\Users\\fgustafsson_user\\AppData\\Local\\arduino\\sketches\\2C45DEF16574530FE91EC1BBBC2F99A1\\sketch\\arduino.cpp.o"
Using previously compiled file: C:\Users\fgustafsson_user\AppData\Local\arduino\sketches\2C45DEF16574530FE91EC1BBBC2F99A1\sketch\Config0.c.o
Using previously compiled file: C:\Users\fgustafsson_user\AppData\Local\arduino\sketches\2C45DEF16574530FE91EC1BBBC2F99A1\sketch\debug.c.o
Using previously compiled file: C:\Users\fgustafsson_user\AppData\Local\arduino\sketches\2C45DEF16574530FE91EC1BBBC2F99A1\sketch\glueVars.c.o
Using previously compiled file: C:\Users\fgustafsson_user\AppData\Local\arduino\sketches\2C45DEF16574530FE91EC1BBBC2F99A1\sketch\POUS.c.o
Using previously compiled file: C:\Users\fgustafsson_user\AppData\Local\arduino\sketches\2C45DEF16574530FE91EC1BBBC2F99A1\sketch\Res0.c.o
Using previously compiled file: C:\Users\fgustafsson_user\AppData\Local\arduino\sketches\2C45DEF16574530FE91EC1BBBC2F99A1\sketch\ModbusSlave.cpp.o
Compiling libraries...
Compiling library "AVR_PWM"
Compiling core...
Using precompiled core: C:\Users\fgustafsson_user\AppData\Local\arduino\cores\arduino_avr_uno_296616db0eeba71acbe34f405232a382\core.a
Linking everything together...
"C:\\Users\\fgustafsson_user\\AppData\\Local\\Arduino15\\packages\\arduino\\tools\\avr-gcc\\7.3.0-atmel3.6.1-arduino7/bin/avr-gcc" -Wall -Wextra -Os -g -flto -fuse-linker-plugin -Wl,--gc-sections -mmcu=atmega328p -o "C:\\Users\\fgustafsson_user\\AppData\\Local\\arduino\\sketches\\2C45DEF16574530FE91EC1BBBC2F99A1/Baremetal.ino.elf" "C:\\Users\\fgustafsson_user\\AppData\\Local\\arduino\\sketches\\2C45DEF16574530FE91EC1BBBC2F99A1\\sketch\\Baremetal.ino.cpp.o" "C:\\Users\\fgustafsson_user\\AppData\\Local\\arduino\\sketches\\2C45DEF16574530FE91EC1BBBC2F99A1\\sketch\\Config0.c.o" "C:\\Users\\fgustafsson_user\\AppData\\Local\\arduino\\sketches\\2C45DEF16574530FE91EC1BBBC2F99A1\\sketch\\ModbusSlave.cpp.o" "C:\\Users\\fgustafsson_user\\AppData\\Local\\arduino\\sketches\\2C45DEF16574530FE91EC1BBBC2F99A1\\sketch\\POUS.c.o" "C:\\Users\\fgustafsson_user\\AppData\\Local\\arduino\\sketches\\2C45DEF16574530FE91EC1BBBC2F99A1\\sketch\\Res0.c.o" "C:\\Users\\fgustafsson_user\\AppData\\Local\\arduino\\sketches\\2C45DEF16574530FE91EC1BBBC2F99A1\\sketch\\arduino.cpp.o" "C:\\Users\\fgustafsson_user\\AppData\\Local\\arduino\\sketches\\2C45DEF16574530FE91EC1BBBC2F99A1\\sketch\\debug.c.o" "C:\\Users\\fgustafsson_user\\AppData\\Local\\arduino\\sketches\\2C45DEF16574530FE91EC1BBBC2F99A1\\sketch\\glueVars.c.o" "C:\\Users\\fgustafsson_user\\AppData\\Local\\arduino\\sketches\\2C45DEF16574530FE91EC1BBBC2F99A1/..\\..\\cores\\arduino_avr_uno_296616db0eeba71acbe34f405232a382\\core.a" "-LC:\\Users\\fgustafsson_user\\AppData\\Local\\arduino\\sketches\\2C45DEF16574530FE91EC1BBBC2F99A1" -lm
"C:\\Users\\fgustafsson_user\\AppData\\Local\\Arduino15\\packages\\arduino\\tools\\avr-gcc\\7.3.0-atmel3.6.1-arduino7/bin/avr-objcopy" -O ihex -j .eeprom --set-section-flags=.eeprom=alloc,load --no-change-warnings --change-section-lma .eeprom=0 "C:\\Users\\fgustafsson_user\\AppData\\Local\\arduino\\sketches\\2C45DEF16574530FE91EC1BBBC2F99A1/Baremetal.ino.elf" "C:\\Users\\fgustafsson_user\\AppData\\Local\\arduino\\sketches\\2C45DEF16574530FE91EC1BBBC2F99A1/Baremetal.ino.eep"
"C:\\Users\\fgustafsson_user\\AppData\\Local\\Arduino15\\packages\\arduino\\tools\\avr-gcc\\7.3.0-atmel3.6.1-arduino7/bin/avr-objcopy" -O ihex -R .eeprom "C:\\Users\\fgustafsson_user\\AppData\\Local\\arduino\\sketches\\2C45DEF16574530FE91EC1BBBC2F99A1/Baremetal.ino.elf" "C:\\Users\\fgustafsson_user\\AppData\\Local\\arduino\\sketches\\2C45DEF16574530FE91EC1BBBC2F99A1/Baremetal.ino.hex"

Using library AVR_PWM at version 1.2.0 in folder: C:\Users\fgustafsson_user\Documents\Arduino\libraries\AVR_PWM
"C:\\Users\\fgustafsson_user\\AppData\\Local\\Arduino15\\packages\\arduino\\tools\\avr-gcc\\7.3.0-atmel3.6.1-arduino7/bin/avr-size" -A "C:\\Users\\fgustafsson_user\\AppData\\Local\\arduino\\sketches\\2C45DEF16574530FE91EC1BBBC2F99A1/Baremetal.ino.elf"
Sketch uses 4178 bytes (12%) of program storage space. Maximum is 32256 bytes.
Global variables use 385 bytes (18%) of dynamic memory, leaving 1663 bytes for local variables. Maximum is 2048 bytes.
"C:\Users\fgustafsson_user\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino17/bin/avrdude" "-CC:\Users\fgustafsson_user\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino17/etc/avrdude.conf" -v  -patmega328p -carduino "-PCOM6" -b115200 -D "-Uflash:w:C:\Users\fgustafsson_user\AppData\Local\arduino\sketches\2C45DEF16574530FE91EC1BBBC2F99A1/Baremetal.ino.hex:i"

avrdude: Version 6.3-20190619
         Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
         Copyright (c) 2007-2014 Joerg Wunsch

         System wide configuration file is "C:\Users\fgustafsson_user\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino17/etc/avrdude.conf"

         Using Port                    : COM6
         Using Programmer              : arduino
         Overriding Baud Rate          : 115200
         AVR Part                      : ATmega328P
         Chip Erase delay              : 9000 us
         PAGEL                         : PD7
         BS2                           : PC2
         RESET disposition             : dedicated
         RETRY pulse                   : SCK
         serial program mode           : yes
         parallel program mode         : yes
         Timeout                       : 200
         StabDelay                     : 100
         CmdexeDelay                   : 25
         SyncLoops                     : 32
         ByteDelay                     : 0
         PollIndex                     : 3
         PollValue                     : 0x53
         Memory Detail                 :

                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           eeprom        65    20     4    0 no       1024    4      0  3600  3600 0xff 0xff
           flash         65     6   128    0 yes     32768  128    256  4500  4500 0xff 0xff
           lfuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
           hfuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
           efuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
           lock           0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
           calibration    0     0     0    0 no          1    0      0     0     0 0x00 0x00
           signature      0     0     0    0 no          3    0      0     0     0 0x00 0x00

         Programmer Type : Arduino
         Description     : Arduino
         Hardware Version: 3
         Firmware Version: 4.4
         Vtarget         : 0.3 V
         Varef           : 0.3 V
         Oscillator      : 28.800 kHz
         SCK period      : 3.3 us

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.00s

avrdude: Device signature = 0x1e950f (probably m328p)
avrdude: reading input file "C:\Users\fgustafsson_user\AppData\Local\arduino\sketches\2C45DEF16574530FE91EC1BBBC2F99A1/Baremetal.ino.hex"
avrdude: writing flash (4178 bytes):

Writing | ################################################## | 100% 0.67s

avrdude: 4178 bytes of flash written
avrdude: verifying flash memory against C:\Users\fgustafsson_user\AppData\Local\arduino\sketches\2C45DEF16574530FE91EC1BBBC2F99A1/Baremetal.ino.hex:
avrdude: load data flash data from input file C:\Users\fgustafsson_user\AppData\Local\arduino\sketches\2C45DEF16574530FE91EC1BBBC2F99A1/Baremetal.ino.hex:
avrdude: input file C:\Users\fgustafsson_user\AppData\Local\arduino\sketches\2C45DEF16574530FE91EC1BBBC2F99A1/Baremetal.ino.hex contains 4178 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 0.54s

avrdude: verifying ...
avrdude: 4178 bytes of flash verified

avrdude done.  Thank you.
 
Reply 0 0
fg71
Lets try to understand how Arduino extension is working.
 
In the file Baremetal.ino we will include
#define USE_ARDUINO_SKETCH // We will include Arduino extension

#ifdef USE_ARDUINO_SKETCH
    #include "arduino_sketch.h"
#endif
 
Inside the header arduino_sketch.h we need 2 function, sketch_setup() and sketch_loop()
 
ex.
 
sketch_setup() {
  Serial.begin(9600);
}

sketch_loop() {
  Serial.println(digitalRead(13));
}
 
After a more research I discover that USE_ARDUINO_SKETCH is part of builder.py and the file defines.h
 
[203128%2F9196057%2Fmceclip0]
 
So the correct way is to include #define USE_ARDUINO_SKETCH in the defines.h, like this
[203128%2F9196061%2Fmceclip0]
 
That said I couldn't find anywhere there #ifdef ARDUINO_PLATFORM was used so maybe its not used.
 
Summery:
We have activate Arduino extension with macro #define USE_ARDUINO_SKETCH
We have included a Arduino extension in Arduino IDE to read the pin 13 of Arduino board to serial monitor.
Reply 0 0
fg71
From other reading concerning #include "Arduino.h" I do not think we need it for Arduino IDE, but it could maybe be different from arduino-cli
In other words Arduino IDE include that header anyway for basic functionality.
 
Reply 0 0
Reply