ryannn
Can OpenPLC On Microcontroller run only on bare-metal microcontrollers, or can it also work with microcontrollers running an RTOS, such as FreeRTOS?
Reply 0 0
thiagoralves
OpenPLC runs on top of the Arduino API. Some boards, like ESP32 and ESP8266, have their Arduino Core built on top of FreeRTOS, so you get access to it on those boards for free. However, OpenPLC does not make any use of FreeRTOS functions as it is made to be generic enough to run on any platform that complies with the Arduino API. So if you want to make use of it, you would have to edit the OpenPLC runtime code.
Reply 0 0
fg71
I believe you need as follow to make FreeRTOS support in OpenPLC.
 
The task need to be done in board.cpp file, its here we find the Arduino API.
Next you need to use define preprocessor directives for your board you could include that in board.cpp file
 
One approach to do a prototype of if is collect all files that OpenPLC produce for a simple blink plc program.
Open the Baremetal.ino in Arduino IDE and all other files. Now you could edit any file that is part of the compiling your program.
When you have a working version or know exactly what you need to do. You could add other entry in hals.json and include FreeRTOS as title name.
And when you choose your FreeRTOS platform from OpenPLC it will make your define preprocessor directives to become TRUE.
And the part of standard Arduino API become FALSE
 
In reality you do not remove anything by using a define preprocessor directives you only use things that make sense to FreeRTOS.
Reply 0 0
arupmondal
I have made changes to builder.py and few other changes and put PROGRAM0_body__(&INSTANCE0); code from openPLC into the freeRTOS task and was able to see onboard LED blinking with hals.json change. I am very happy to make progress thus far. I have used stm32 cubeIDE headless compiler and cubeprogrammer headless deploy of hex file into stm32f103 board (all thru changes in builder.py). Can you help me now how do I connect to live debugger since it is kind of detached from openPLC and modbusRTU? How do I integrate modbus serial again for live debug? Thank you.
Reply 0 0
thiagoralves
The debugger depends on Modbus. The code is pretty platform independent if you manage to take out all the Arduino API tied with the serial and Ethernet ports. If you manage to translate that to your platform then you’ll be good.
Reply 0 0
fg71
 
Did you use Arduino_FreeRTOS ??
 
Here is a simple example in Arduino IDE using ArduinoModbus and ModbusRTUServer, it use coil 0x00 to set the LED_BUILTIN to HIGH or LOW
 
#include <ArduinoRS485.h>   // RS485 library for Modbus communication
#include <ArduinoModbus.h>  // Modbus RTU server implementation
#include <Arduino_FreeRTOS.h> // FreeRTOS for multitasking

const int ledPin = LED_BUILTIN; // Define built-in LED pin

// Task function for handling Modbus RTU communication
void ModbusTask(void *pvParameters) {
    Serial1.begin(115200, SERIAL_8N1); // Initialize Modbus RTU over Serial1 with 115200 baud rate

    // Start Modbus RTU server with slave ID 1 and baud rate 9600
    if (!ModbusRTUServer.begin(1, 115200)) {
        vTaskDelete(NULL); // Terminate task if initialization fails
    }

    // Configure a single coil register at address 0x00
    ModbusRTUServer.configureCoils(0x00, 1);

    for (;;) { // Infinite loop for handling Modbus requests
        int packetReceived = ModbusRTUServer.poll(); // Poll for incoming Modbus requests

        // If a valid Modbus request is received, update LED state
        if (packetReceived) {
            digitalWrite(ledPin, ModbusRTUServer.coilRead(0x00) ? HIGH : LOW);
        }

        // Short delay to prevent CPU overload while ensuring responsiveness
        vTaskDelay(5 / portTICK_PERIOD_MS);
    }
}

void setup() {
    pinMode(ledPin, OUTPUT); // Set LED pin as output

    // Create a FreeRTOS task to run the Modbus server
    xTaskCreate(ModbusTask, "ModbusTask", 1024, NULL, 1, NULL);
}

void loop() {
    // Keep FreeRTOS running, loop remains idle
    vTaskDelay(100 / portTICK_PERIOD_MS);
}
 
Reply 0 0
arupmondal
I used stm32cubeide and within the middleware I selected freeRTOS that makes code generation to create freeRTOS.c file for stm32f103 cpu. For stm32u585 cpu, it created app_freeRTOS.
 
/* Includes ------------------------------------------------------------------*/
#include "app_freertos.h"

I still wondering how to handle MD5 checksum and use debugger. If I know what files to look for the changes would help!
 
Thanks
Reply 0 0
arupmondal
Please see file attached
Reply 0 0
fg71
I'm not sure I understand the MD5 checksum, I believe Modbus RTU use CRC-16, I believe you could find it in ModbusSlave.cpp
Reply 0 0
thiagoralves
MD5 checksum is generated by the editor and included either in the defines.h file or on the debug.c file, I’m not sure. The rest of the debugger code is at the ModbusSlave.cpp file as custom Modbus function codes.
Reply 0 0
arupmondal
MD5 checksum is generated by the editor and included either in the defines.h file or on the debug.c file, I’m not sure. The rest of the debugger code is at the ModbusSlave.cpp file as custom Modbus function codes.
Thank you thaigoralves and fg71. It was very helpful and I used most of the code from baremetal.ino and modbusslave.cpp. Created a new file memory.c and put those code. Finally calling the modbus task in freertos with cycle time of 10 msec. I am able to see the live debug values and the chart. However it is not consistent. If I increase the cycle time to 100 msec or anything higher than 10 msec, the chart and live display values are not on correct time. I was expecting to free up CPU load by slowing down the modbus task cycle time. Instead, I see opposite results saying PLC task is taking too long. It seems contradictory to me.
 
My question is:
From the modbus master how do I increase the time delay larger than 10 msec (example 100 ms or 200 msec) to request data from the slave (stm32 micro-controller)
 
below is my code for mobus where modbusTask() and handle_serial() are almost the same as in baremetal.ino

/* USER CODE END Header_StartTask3 */
void StartTask3(void *argument)
{
/* USER CODE BEGIN task3 */
/* Infinite loop */
for(;😉
{
modbusTask();
if (i >= count)
{
 
handle_serial();
HAL_UART_Transmit(&huart1,mb_frame, mb_frame_len, HAL_MAX_DELAY);
i = 0;
 
}
osDelay(10.0);
// osDelay(10);
}
/* USER CODE END task3 */
Reply 0 0
arupmondal
I think I must configure the Modbus master to poll less frequently.
This should be done on the master side, not the slave.
  • So where to Look for a “poll interval”, “scan rate”, or “request interval” setting.
  • Set this to my desired value (e.g., 100 ms or 200 ms).
Reply 0 0
thiagoralves
Reply 0 0
Reply