fg71
 
I'm looking at raspberrypi.cpp and was thinking to use #define macro to include both pigpio and wiringpi in the same .cpp
I could then use compile flag -D if the I like to use pigpio ex. -DUSE_PIGPIO
This way I believe the only thing a user need to do is to update compile_program.sh to be able to use any of whose 2 libraries.
 
What do you think of it?....
 
Here is early copy of raspberrypi.cpp
 
//-----------------------------------------------------------------------------
// Copyright 2015 Thiago Alves
//
// Based on the LDmicro software by Jonathan Westhues
// This file is part of the OpenPLC Software Stack.
//
// OpenPLC is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenPLC is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with OpenPLC.  If not, see <http://www.gnu.org/licenses/>.
//------
//
// This file is the hardware layer for the OpenPLC. If you change the platform
// where it is running, you may only need to change this file. All the I/O
// related stuff is here. Basically it provides functions to read and write
// to the OpenPLC internal buffers in order to update I/O state.
// Thiago Alves, Dec 2015
//-----------------------------------------------------------------------------

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#ifdef USE_PIGPIO
    #include <pigpio.h>
#else
    #include <wiringPi.h>
    #include <wiringSerial.h>
#endif

#include <pthread.h>

#include "ladder.h"

#if !defined(ARRAY_SIZE)
    #define ARRAY_SIZE(x) (sizeof((x)) / sizeof((x)[0]))
#endif

#ifdef USE_PIGPIO
    // Using PiGPIO
    #define MAX_INPUT     14
    #define MAX_OUTPUT     11
    #define MAX_ANALOG_OUT  1

//========================[I/O PINS CONFIGURATION]========================
// A good source for RaspberryPi I/O pins information is: http://pinout.xyz
// The buffers below works as an internal mask, so that the
// OpenPLC can access each pin sequentially
//===================================================================

//inBufferPinMask: pin mask for each input, which
//means what pin is mapped to that OpenPLC input
    int inBufferPinMask[MAX_INPUT] = \
        { 2, 3, 4, 17, 27, 22, 10, 9, 11, 5, 6, 13, 19, 26 };

//outBufferPinMask: pin mask for each output, which
//means what pin is mapped to that OpenPLC output
    int outBufferPinMask[MAX_OUTPUT] =  \
        { 14, 15, 23, 24, 25, 8, 7, 12, 16, 20, 21 };

//analogOutBufferPinMask: pin mask for the analog PWM
//output of the RaspberryPi
    int analogOutBufferPinMask[MAX_ANALOG_OUT] = \
        { 18 };

//-----------------------------------------------------------------------------
// This function is called by the main OpenPLC routine when it is initializing.
// Hardware initialization procedures should be here.
//-----------------------------------------------------------------------------
    void initializeHardware() {
        gpioInitialise();
        //piHiPri(99);

        //set pins as input
        for (int i = 0; i < MAX_INPUT; i++) {
            gpioSetMode(inBufferPinMask[i], PI_INPUT);
                //pull down can't be enabled on the first two pins
                if (i != 0 && i != 1) {
                    gpioSetPullUpDown(inBufferPinMask[i], PI_PUD_DOWN); //pull down enabled
                }
        }

        //set pins as output
        for (int i = 0; i < MAX_OUTPUT; i++) {
            gpioSetMode(outBufferPinMask[i], PI_OUTPUT);
        }

        //set PWM pins as output
        for (int i = 0; i < MAX_ANALOG_OUT; i++) {
            gpioSetMode(analogOutBufferPinMask[i], PI_ALT5);
            gpioSetPWMrange(analogOutBufferPinMask[i], 1024);
        }
    }

//-----------------------------------------------------------------------------
// This function is called by the main OpenPLC routine when it is finalizing.
// Resource clearing procedures should be here.
//-----------------------------------------------------------------------------
    void finalizeHardware() {
        gpioTerminate();
    }

//-----------------------------------------------------------------------------
// This function is called by the OpenPLC in a loop. Here the internal buffers
// must be updated to reflect the actual state of the input pins. The mutex buffer_lock
// must be used to protect access to the buffers on a threaded environment.
//-----------------------------------------------------------------------------
    void updateBuffersIn() {
        pthread_mutex_lock(&bufferLock); //lock mutex
            //INPUT
            for (int i = 0; i < MAX_INPUT; i++) {
                if (bool_input[i/8][i%8] != NULL) *bool_input[i/8][i%8] = gpioRead(inBufferPinMask[i]);
            }
        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 state of the output pins. The mutex buffer_lock
// must be used to protect access to the buffers on a threaded environment.
//-----------------------------------------------------------------------------
    void updateBuffersOut() {
        pthread_mutex_lock(&bufferLock); //lock mutex
            //OUTPUT
            for (int i = 0; i < MAX_OUTPUT; i++) {
                if (bool_output[i/8][i%8] != NULL) gpioWrite(outBufferPinMask[i], *bool_output[i/8][i%8]);
            }

            //ANALOG OUT (PWM)
            for (int i = 0; i < MAX_ANALOG_OUT; i++) {
                if (int_output[i] != NULL) gpioPWM(analogOutBufferPinMask[i], (*int_output[i] / 64));
            }
        pthread_mutex_unlock(&bufferLock); //unlock mutex
    }
#else
    //Using WiringPi
    #define MAX_INPUT     <INPUT_TO_DO>
    #define MAX_OUTPUT     <INPUT_TO_DO>
    #define MAX_ANALOG_OUT  <INPUT_TO_DO>

    int inBufferPinMask[MAX_INPUT] = {<INPUT_TO_DO>};
    int outBufferPinMask[MAX_OUTPUT] =  {<INPUT_TO_DO>};
    int analogOutBufferPinMask[MAX_ANALOG_OUT] = {<INPUT_TO_DO>};
    
    void initializeHardware() {
        //wiringSetup();          // WiringPi pin mode
        //wiringPiSetupGpio();    // BCM/GPIO pin mode
        wiringPiSetupPhys();      // Physical pin mode

        for (int i = 0; i < MAX_INPUT; i++) {
            pinMode(inBufferPinMask[i], INPUT);
                if (i != 0 && i != 1) {
                    pullUpDnControl(inBufferPinMask[i], PUD_DOWN);
                }
        }

        for (int i = 0; i < MAX_OUTPUT; i++) {
            pinMode(outBufferPinMask[i], OUTPUT);
        }
    
        for (int i = 0; i < MAX_ANALOG_OUT; i++) {
            pinMode(analogOutBufferPinMask[i], PWM_OUTPUT);
        }
    }
    
    void finalizeHardware() {
    }
    
    void updateBuffersIn() {
        pthread_mutex_lock(&bufferLock);
        for (int i = 0; i < MAX_INPUT; i++) {
            if (bool_input[i/8][i%8] != NULL) *bool_input[i/8][i%8] = digitalRead(inBufferPinMask[i]);
        }
        pthread_mutex_unlock(&bufferLock);
    }
    
    void updateBuffersOut() {
        pthread_mutex_lock(&bufferLock);
            for (int i = 0; i < MAX_OUTPUT; i++) {
                if (bool_output[i/8][i%8] != NULL) digitalWrite(outBufferPinMask[i], *bool_output[i/8][i%8]);
            }
    
            for (int i = 0; i < MAX_ANALOG_OUT; i++) {
                if (int_output[i] != NULL) pwmWrite(analogOutBufferPinMask[i], (*int_output[i] / 64));
            }
        pthread_mutex_unlock(&bufferLock);
    }
#endif
 
thiagoralves
I would rather have only one library available. This is usually transparent for the end user. We don't need to support pigpio if we are going to use wiringpi. Perhaps the ideal approach would be to revert the raspberrypi.cpp file to the version where it was using wiringpi before moving to pigpio. This will keep the same pin numbers and configuration as before.
Reply 0 0
fg71
Okey, I believe you could remove raspberrypi.cpp and replace it with the raspberrypi_old.cpp so you do not need my commit.
When it comes to WiringPi API I could only test RPi4 and RPi5, I do not have ADC hat to test more...
 
I did see in background_installer.sh that you use apt to get WiringPi the default REPO do not have it.
So you need to download the latest version from Github, I believe you could change the version number to do that.
 
The Dockerfile use a old Raspberry Pi OS and do not include WiringPi.
 
If ./install linux is the command to install on Raspberry Pi why do we have ./install rpi
 
Only checking what you like me to do ?
Reply 0 0
thiagoralves
No, the raspberrypi_old is for the old Raspberry Pi models A and B. They have a different header. The raspberrypi.cpp driver is for all the other boards newer than the original A and B models from 2012. This is the commit that replaces the old wiringpi with pigpio: https://github.com/thiagoralves/OpenPLC_v3/commit/b83ccf1f8839737af2c57a0f7a701f048b1bae12
 
Simply reverting the file for the old version will break all the fixes we've done after pigpio has been implemented. So the ideal scenario is to check all the changes and manually revert them on each file that has been changed.
Reply 0 0
thiagoralves
I forgot to answer your last question. The ./install linux should be used to install on generic linux systems. The ./install rpi installs Raspberry Pi specific libraries (like wiringpi) and use a different compilation argument to include the libraries.
Reply 0 0
fg71
Okey I will check the link....
Reply 1 0
fg71
I have made all the change but I'm in command line how could I push, or how could I login to my github from comman line
I have little problem with
fatal: Authentication failed for 'https://github.com/thiagoralves/OpenPLC_v3.git/'
Reply 0 0
thiagoralves
You don't push to my repo, you should push to your fork of my repo first, and then create a Pull Request. If you haven't forked the project yet on your own github, then that's the first step. This will give you a https://github.com/fg71/OpenPLC_v3.git repository which you can clone locally to your machine and make your changes. Or if you don't want to fiddle with command line git, you can even make the changes online on the github website itself. Once you're done with the changes, clone your repository and install it to make sure everything is smooth. After that you can open a Pull Request and I will merge it.
Reply 0 0
fg71
I see, I will test first a clone of your version with the change.
 
I have changed as follow:
  1. backgroud_installer.sh
  2. compile_program.sh
  3. raspberrypi.cpp
  4. Dockerfile
 
Its looks good so far
 
I have a clone on my RPi5 could I use that to merge with my fork on github ??
 
Generating C files...
POUS.c
POUS.h
LOCATED_VARIABLES.h
VARIABLES.csv
Config0.c
Config0.h
Res0.c
Including Siemens S7 Protocol via snap7
Moving Files...
Compiling for Raspberry Pi
Generating object files...
Generating glueVars...
Compiling main program...
Compilation finished successfully!
 
Reply 0 0
thiagoralves
Yes, if you've cloned and installed your fork (not mine) of the OpenPLC runtime and it works just fine, then it is all good to go! Make sure you've tested it on a clean Raspbian install - one that does NOT have the wiringpi library already installed. This will allow us to know for sure that the installer is doing the right thing by installing the correct wiringpi version and that everything is included in the system as it should be. Then just upload a simple blink program to make sure that the GPIO pins are working and we're good to go.
 
Thank you so much for working on this and helping the project 🙂
Reply 0 0
fg71
I have made a fork of your OpenPLC_v3 master.
I have clone my fork
I have update the fork to include my change
 
Still I have some issue with ssh or have not figure out how to setup git from command-line so I could upload the change to my fork.
 
I tested Snap7 with Siemens LOGO, the runtime log below.
[203128%2F9251626%2Fmceclip0]
 
 
Next will be to test all: WiringPi at Raspberry Pi GPIO Pinout
Reply 0 0
thiagoralves
Still I have some issue with ssh or have not figure out how to setup git from command-line so I could upload the change to my fork.
 
Worst case scenario, open the OpenPLC_v3 folder on VSCode and then click on the Source Control button on the left toolbar. It will show you all files that have been changed in the repository and how they were changed. Then you can simply copy/paste the changes (or even the entire files) in the github web editor and commit your changes there on the web browser.
Reply 0 0
fg71
one that does NOT have the wiringpi library already installed
If the user have installed Raspberry Pi OS Bookworm (64 bit), they will not have WiringPi installed.
I checked /usr/include before I was running the install. Or to check is to execute gpio -v
You could also do -> apt list | grep 'wiring' it will return wiringpi/now 3.14 arm64 [installed,local]
meaning this flag [installed,local] means it was not installed from a REPO
Reply 0 0
fg71
First test with LED
The first output use WiringPi 15, (Physical pin 8) that make the breadboard to blick the LED.
Will do more test or all digital WiringPi pin mode
 
Other project I have is to understand WiringPi API, there are many things in the header I'm not sure what they do.
 
Here we make Raspberry Pi 5 to communicate with Siemens LOGO.
The plc program blink is uploaded to RPi5 and blink a LED on bread board, but it also sending the bit state to the LOGO
[203128%2F9251814%2Fmceclip0]
 
The LOGO use variable memory VB0.0 as input to set Q to True/False. (Thanks @davenardella)
Reply 0 0
thiagoralves
%MW0 location is not used by the raspberrypi.cpp driver. You need to use locations like %IX0.0 and %QX0.0
Reply 0 0
Reply