How to use the ESP32 industrial PLC Dual Core

Learn how to work using both ESP32 cores
April 20, 2021 by
How to use the ESP32 industrial PLC Dual Core
Boot & Work Corp. S.L., Marti Guillem Cura

Introduction

When we talk about dual core, it means that there are two identical and independent cores in the same integrated circuit or chip, working at the same speed although each one can be adjusted according to the load and controller that governs it.

ESP32 programmable logic controller includes 2 Xtensa 32-bit LX6 microprocessors, allowing multiprocess options and improved performance. They have capacity for each internal processor, but also the main memory of the system to load their own processes. In this blog, you will see how to work with the dual core option of the ESP32 PLC controllers with the requirements and factors to consider.

Industrial Shields ESP32 PLC industrial controller

Related links

Read


How to make partition scheme in esp32

Read


How to work with 5GHz Wifi

Read


ESP32 PLC family products

Read


How to find your perfect industrial PLC

Requirements

How to work with ESP32 Dual Core

In order to work with the ESP32 dual core, update the Industrial Shields ESP32 packages in Arduino IDE to the latest version (v1.1.0 or newer) before working with the ESP32-based PLCs. Once the set up is done, you will be able to create a second loop function named loop1. 

void setup() {
  // TODO
}
void loop() {
  // Default core tasks
}
void loop1() {
  // Secondary core tasks
}


All the tasks programed on the default loop function will be executed using the first core, while the ones one the loop1 will be working on the second core. 

As you can see, working with the dual core using our Industrial Shields boards is very easy. However, you must take into account some factors that can cause some problems if they are not well implemented as shared resources. To avoid problems, we have implemented a system of traffic lights, which must be used whenever a core wants to access a shared resource with the other core, for example, a global variable, or a type of peripheral (Serial, SPI , I2C, etc.)


Semaphore system

To control the shared resource, industrial shields have implemented a semaphore system where the first core to access the peripheral will block it until it has finished its work. The library implements 3 functions:

  • take(uint32_t timeout = 0)

  • give()

  • isTaken()

The take() function will lock the semaphore while the give() function unlocks it. If any parameters are passed to the take() function, it will lock the semaphore until is released by give(). However, if a certain time is specified, it will lock the resource until the time has elapsed. To know if the semaphore is available, the isTaken() function shall return True or False depending on its status. 


Example

In the following example, you can see how these traffic lights are used with a shared variable. The program will be increasing a variable using both cores at different times. The default core will be increasing the variable every 1.5s while the second one will be doing it each second. With the xPortGetCoreID() function you will be able to know which core is increasing the variable every time.


#include <Semaphore.h>
SemaphoreBinary semaphore;
uint32_t variable = 0;
////////////////////////////////////////////////////////////////////////////////////////////////////
void setup() {
  Serial.begin(115200UL);
  Serial.print("setup running on core ");
  Serial.println(xPortGetCoreID());
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void loop() {
  updateVariable(200);
  printVariable();
  delay(1500);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void loop1() {
  updateVariable(100);
  printVariable();
  delay(1000);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void updateVariable(uint32_t timeout) {
  semaphore.take();
  delay(timeout);
  ++variable;
  semaphore.give();
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void printVariable() {
  semaphore.take();
  Serial.print("core ");
  Serial.print(xPortGetCoreID());
  Serial.print(" variable value: ");
  Serial.println(variable);
  semaphore.give();
}

Have you got more questions about how to use ESP32 dual core industrial controller?

You can always contact us. We are ready to help you.

​Search in our Blog

How to use the ESP32 industrial PLC Dual Core
Boot & Work Corp. S.L., Marti Guillem Cura April 20, 2021

Looking for your ideal Programmable Logic Controller?

Take a look at this product comparison with other industrial controllers Arduino-based. 

We are comparing inputs, outputs, communications and other features with the ones of the relevant brands.


Industrial PLC comparison >>>