Hello readers, I hope your all are doing great. We know that a Raspberry Pi Pico module comes with multiple inbuilt features for example onboard memory, processing units, GPIOs or General Purpose Input Outputs (used to control and receive inputs from various electronic peripherals) etc.
In our previous tutorials, we discussed how to access GPIO pins of the Raspberry Pi Pico module for both input as well as output operations.
In this tutorial, we are going to discuss another important feature of the Raspberry Pi Pico module (RP2040) which is Dual Core Processor. The Pico board features with 133MHz ARM Cortex-M0+, Dual Core Processor. This dual-core feature makes the Pico module capable of multiple thread execution or multithreading.
Now before writing the MicroPython program let’s first understand the concept of the dual-core processor in the Raspberry Pi Pico module.
Fig. 1 raspberry Pi Pico dual-core programming
A core is the basic unit of any processor which is responsible for executing program instructions. A multi core processor comes with the features of executing multiple tasks at a time. Multithreading is the ability of a processing unit to provide multiple threads of execution simultaneously (operating system supported). In multithreading, threads share their resources with each other. So this dual-core processor feature results in increased processing speed.
Raspberry Pi Pico (RP2040) module is having two processing cores, Core0 and Core1. In the default mode of Raspberry Pi Pico program execution, Core_0 executes all the tasks and Core1 remains idle or on standby mode. Using both the cores of RP2040 provides us with two threads of execution and hence a more powerful project with better processing speed.
Both core0 and core1 execute their assigned tasks independent of each other while sharing all the resources like memory space and program code with each other. Sharing the memory location between two cores can create race conditions and hence can cause trouble when mutual memory accessing is not assured. On the other hand, sharing program code with each other (core0 and core1) may sound troublesome but practically it is not. The reason is fetching code is a read instruction that does not create a race condition.
Fig. 2 Core_0 and Core_1 communication
As we mentioned above, sharing memory space with two cores simultaneously can cause race conditions. So, to make the cores to communicate with each other the Raspberry Pi Pico module is featured with two individual ‘First In First Out’ (FIFO) structures. Each core can access only one FIFO structure so both core have their own FIFO structure to write codes which helps in avoiding race condition or writing to the same memory location simultaneously.
You can follow the given link for detailed study on Raspberry Pi Pico: https://www.theengineeringprojects.com/2022/04/getting-started-with-raspberry-pi-pico.html
The development environment we are using is Thonny IDE, to program the Raspberry Pi Pico board for accessing the dual core feature with MicroPython programming language.
So, before writing the MicroPython program user need to install the respective development environment.
We already published a tutorial on how to install and access Thonny IDE for Raspberry Pi Pico programming using MicroPython programming language. You can find the details at the given link address: https://www.theengineeringprojects.com/2022/04/installing-thonny-ide-for-raspberry-pi-pico-programming.html
Now let’ write a MicroPython program with Thonny IDE to access raspberry Pi Pico’s both cores:
In this example code we using just a simple “print()” commands to print the messages from each core for testing purpose.
Fig. 3 Importing necessary libraries
Fig. 4 declaring thread lock object
Fig. 5 Task for Core_1
Fig. 6 Core_1 thread
Fig. 7 Task for default core (core_0)
The MicroPython code with thonny IDE for Raspberry Pi Pico is written below:
import machine
import utime # access internal clock of raspberry Pi Pico
import _thread # to access threading function
spLock = _thread.allocate_lock() # creating semaphore
def core1_task():
while True:
spLock.acquire() # acquiring semaphore lock
print( "message from core_1")
utime.sleep(0.5) # 0.5 sec or 500us delay
spLock.release()
_thread.start_new_thread(core1_task, ())
while True:
spLock.acquire()
print( "message from Core_0 ")
utime.sleep( 0.5)
spLock.release()
Fig. 8 Enabling Shell
The result obtained from the above code is attached below. Where we can see the messages received or executed by both the cores as per the instructions provided in the micropython code.
Fig. 9 output printed on shell
Let’s take another example where we will interface some peripheral LEDs and will toggle those LEDs using different threads of execution or cores.
Fig.10 importing necessary libraries
Fig. 11 declaring led objects
Fig. 12 Toggling LED with core_1
Fig. 13 toggling LED with core_0
from machine import Pin
import utime # access internal clock of raspberry Pi Pico
import _thread # to access threading function
# declaring led object
led_0 = Pin( 14, Pin.OUT ) # led object for core_0
led_1 = Pin( 15, Pin.OUT ) # led object for core_1
spLock = _thread.allocate_lock() # creating semaphore lock
def core1_task():
while True:
spLock.acquire() # acquiring semaphore lock
print( " message from core_1" )
led_1.value( 1)
utime.sleep( 0.5) # 0.5 sec or 500us delay
led_1.value(0)
spLock.release()
_thread.start_new_thread( core1_task, () )
while True:
spLock.acquire()
print( "message from Core_0" )
led_0.value(1)
utime.sleep( 0.5)
led_0.value(0)
spLock.release()
In the results attached below we can that two LEDs are attached with raspberry Pi Pico boar. Green (GPIO14) and Red (GPIO15) LEDs represent the output of Core_1 and Core_0 respectively.
Fig. 14 Core_1 output (GPIO 14)
Fig. 15 Core_0 Output (GPIO15)
In this tutorial, we learned how to access both the cores of the raspberry pi Pico module and to execute task or control peripherals with individual cores. We also learned the concept of threading and multithreading.
This concludes the tutorial. I hope you found this of some help and also hope to see you soon with a new tutorial on raspberry Pi Pico programming.