This configuration use WiringPi and it use Physical pin mode (That is how I like it to be), input, output and analog could be changed for your taste.
The hardware layer for rpi also include pigpio so I have not remove anything from the original raspberrypi.cpp file.
This hardware layer is for RPi5B and use WiringPi but it have also been tested on RPi4B
WiringPi is not installed by default in Raspberry OS so you need to download the source from GitHub and follow the instructions
how to compile and install it on your RPi5 device. Or maybe that could be included in next release of OpenPLC Runtime.
In other words
./ install.sh rpi will download WiringPi and compile and install it.
//-----------------------------------------------------------------------------
// 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>
#include <pthread.h>
// WiringPi
#include <wiringPi.h>
#include <wiringSerial.h>
// pigpio
// #include <pigpio.h>
#include "ladder.h"
#if !defined(ARRAY_SIZE)
#define ARRAY_SIZE(x) (sizeof((x)) / sizeof((x)[0]))
#endif
#define MAX_INPUT 7
#define MAX_OUTPUT 4
#define MAX_ANALOG_OUT 4
/********************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
****************************************************************/
/*
+-----+-----+---------+------+---+---Pi 5---+---+------+---------+-----+-----+
| BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM |
+-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
| | | 3.3v | | | 1 || 2 | | | 5v | | |
| 2 | 8 | SDA.1 | - | 0 | 3 || 4 | | | 5v | | |
| 3 | 9 | SCL.1 | - | 0 | 5 || 6 | | | 0v | | |
| 4 | 7 | GPIO. 7 | - | 0 | 7 || 8 | 0 | - | TxD | 15 | 14 |
| | | 0v | | | 9 || 10 | 0 | - | RxD | 16 | 15 |
| 17 | 0 | GPIO. 0 | - | 0 | 11 || 12 | 0 | - | GPIO. 1 | 1 | 18 |
| 27 | 2 | GPIO. 2 | - | 0 | 13 || 14 | | | 0v | | |
| 22 | 3 | GPIO. 3 | - | 0 | 15 || 16 | 0 | - | GPIO. 4 | 4 | 23 |
| | | 3.3v | | | 17 || 18 | 0 | - | GPIO. 5 | 5 | 24 |
| 10 | 12 | MOSI | - | 0 | 19 || 20 | | | 0v | | |
| 9 | 13 | MISO | - | 0 | 21 || 22 | 0 | - | GPIO. 6 | 6 | 25 |
| 11 | 14 | SCLK | - | 0 | 23 || 24 | 0 | - | CE0 | 10 | 8 |
| | | 0v | | | 25 || 26 | 0 | - | CE1 | 11 | 7 |
| 0 | 30 | SDA.0 | - | 0 | 27 || 28 | 0 | - | SCL.0 | 31 | 1 |
| 5 | 21 | GPIO.21 | - | 0 | 29 || 30 | | | 0v | | |
| 6 | 22 | GPIO.22 | - | 0 | 31 || 32 | 0 | - | GPIO.26 | 26 | 12 |
| 13 | 23 | GPIO.23 | - | 0 | 33 || 34 | | | 0v | | |
| 19 | 24 | GPIO.24 | - | 0 | 35 || 36 | 0 | - | GPIO.27 | 27 | 16 |
| 26 | 25 | GPIO.25 | - | 0 | 37 || 38 | 0 | - | GPIO.28 | 28 | 20 |
| | | 0v | | | 39 || 40 | 0 | - | GPIO.29 | 29 | 21 |
+-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
| BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM |
+-----+-----+---------+------+---+---Pi 5---+---+------+---------+-----+-----+
*/
//inBufferPinMask: pin mask for each input, which
//means what pin is mapped to that OpenPLC input
int inBufferPinMask[MAX_INPUT] = { 7, 11, 13, 15, 29, 31, 37 };
//outBufferPinMask: pin mask for each output, which
//means what pin is mapped to that OpenPLC output
int outBufferPinMask[MAX_OUTPUT] = { 16, 18, 22, 36 };
//analogOutBufferPinMask: pin mask for the analog PWM
//output of the RaspberryPi
int analogOutBufferPinMask[MAX_ANALOG_OUT] = { 12, 32, 33, 35 };
//-----------------------------------------------------------------------------
// This function is called by the main OpenPLC routine when it is initializing.
// Hardware initialization procedures should be here.
//-----------------------------------------------------------------------------
void initializeHardware()
{
//wiringPiSetup(); // Initialises the system into wiringPi pin mode (default mode)
//wiringPiSetupGpio(); // Initialises the system into GPIO pin mode.
wiringPiSetupPhys(); // Initialises the system into Physical pin mode.
// pigpio
//gpioInitialise(); // Initialises the system into pigpio pin mode
// Other
//piHiPri(99);
//set pins as input
for (int i = 0; i < MAX_INPUT; i++)
{
// WiringPi
pinMode(inBufferPinMask[i], INPUT);
// pigpio
//gpioSetMode(inBufferPinMask[i], PI_INPUT);
if (i != 0 && i != 1) //pull down can't be enabled on the first two pins
{
// WiringPi
pullUpDnControl(inBufferPinMask[i], PUD_DOWN);
// pigpio
//gpioSetPullUpDown(inBufferPinMask[i], PI_PUD_DOWN); //pull down enabled
}
}
//set pins as output
for (int i = 0; i < MAX_OUTPUT; i++)
{
// WiringPi
pinMode(outBufferPinMask[i], OUTPUT);
// pigpio
//gpioSetMode(outBufferPinMask[i], PI_OUTPUT);
}
//set PWM pins as output
for (int i = 0; i < MAX_ANALOG_OUT; i++)
{
// WiringPi
pinMode(analogOutBufferPinMask[i], PWM_OUTPUT);
// pigpio
// 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()
{
// WiringPi
// Not sure if WiringPi has one.
// pigpio
// 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++)
{
// WiringPi
if (bool_input[i/8][i%8] != NULL) *bool_input[i/8][i%8] = digitalRead(inBufferPinMask[i]);
// pigpio
// 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++)
{
// WiringPi
if (bool_output[i/8][i%8] != NULL) digitalWrite(outBufferPinMask[i], *bool_output[i/8][i%8]);
// pigpio
// 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++)
{
// WiringPi
if (int_output[i] != NULL) pwmWrite(analogOutBufferPinMask[i], (*int_output[i] / 64));
// pigpio
// if (int_output[i] != NULL) gpioPWM(analogOutBufferPinMask[i], (*int_output[i] / 64));
}
pthread_mutex_unlock(&bufferLock); //unlock mutex
}
And this part have been updated
compile_program.sh inside scripts folder, look for
-lwiringPi
elif [ "$OPENPLC_PLATFORM" = "rpi" ]; then
echo "Compiling for Raspberry Pi"
echo "Generating object files..."
if [ "$OPENPLC_DRIVER" = "sequent" ]; then
g++ -std=gnu++11 -I ./lib -c Config0.c -lasiodnp3 -lasiopal -lopendnp3 -lopenpal -w -DSEQUENT
else
g++ -std=gnu++11 -I ./lib -c Config0.c -lasiodnp3 -lasiopal -lopendnp3 -lopenpal -w
fi
if [ $? -ne 0 ]; then
echo "Error compiling C files"
echo "Compilation finished with errors!"
exit 1
fi
if [ "$OPENPLC_DRIVER" = "sequent" ]; then
g++ -std=gnu++11 -I ./lib -c Res0.c -lasiodnp3 -lasiopal -lopendnp3 -lopenpal -w -DSEQUENT
else
g++ -std=gnu++11 -I ./lib -c Res0.c -lasiodnp3 -lasiopal -lopendnp3 -lopenpal -w
fi
if [ $? -ne 0 ]; then
echo "Error compiling C files"
echo "Compilation finished with errors!"
exit 1
fi
echo "Generating glueVars..."
./glue_generator
echo "Compiling main program..."
if [ "$OPENPLC_DRIVER" = "sequent" ]; then
g++ -DSEQUENT -std=gnu++11 *.cpp *.o -o openplc -I ./lib -lrt -lwiringPi -lpigpio -lpthread -fpermissive `pkg-config --cflags --libs libmodbus` -lasiodnp3 -lasiopal -lopendnp3 -lopenpal -w
else
g++ -std=gnu++11 *.cpp *.o -o openplc -I ./lib -lrt -lwiringPi -lpigpio -lpthread -fpermissive `pkg-config --cflags --libs libmodbus` -lasiodnp3 -lasiopal -lopendnp3 -lopenpal -w
fi
if [ $? -ne 0 ]; then
echo "Error compiling C files"
echo "Compilation finished with errors!"
exit 1
fi
echo "Compilation finished successfully!"
exit 0