Skip to content

IWILZ/PicoSem

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PicoSem

Simple library that implements semaphores to exchange data between two cores of a Raspberry Pi Pico under Arduino IDE framework.

The library has been tested only with Raspberry Pi Pico P2040, Arduino IDE 1.8 and 2.0

multicore

Why this project

I'm a beginner with the Pi Pico but from the first glance I understood that the processing speed combined with the presence of the 2 cores can provide an interesting potential compared to the Arduino and ESP16 families.

But in order to be able to delegate different tasks to the 2 cores by transferring information produced during their respective processing, it is necessary to have a system of message exchange and synchronization.

On the other hand, I know the Arduino IDE and C/C++ quite well but I don't have skills in other environments and languages and unfortunately on the net I struggled to find useful information regarding the use of Pi Pico in the Aduino environment.

Perhaps the most interesting document is https://arduino-pico.readthedocs.io/en/latest/multicore.html where at the "Multicore Processing" chapter is described how to manage the dialogue between the 2 cores through the use of semaphores and FIFO queues but the limitation is that any information size is reduced to a single uint32_t variable.

So for this reason I started thinking about a different solution in which the size of the exchange data buffers could be potentially limited only by the available memory.

The concept of this work

As defined in https://en.wikipedia.org/wiki/Semaphore_(programming), a semaphore is "a variable or abstract data type used to control access to a common resource by multiple threads".

This library contains 2 semaphores (integrated into the library itself) to allow coordinated exchange of information between 2 cores by signaling the status of 2 data buffers. It is important to note that the definition of the 2 buffers is left to the user who can thus create them as he likes both in terms of structure and size.

Also, each data buffer (although it is a shared resource) is "unidirectional", meaning that it will be written by one core and read by the other using library functions to coordinate in performing their tasks.

A typical scenario could be a data logger where core0 sends to core1 a request to sample some data using an external sensor and core1 returns these data once they are produced. In this example the communication buffer from core0 to core1 could be limited to a couple of byte while the data sent back to core0 will probably have a bigger size and a different structure.

Installing the library into Arduino framework

To install the library:

  1. download the "zip" file from GitHub using "Code/Download ZIP" in a folder of your PC
  2. open Arduino IDE
  3. select "sketch/include library/add ZIP library"
  4. go to the folder you used to save the zip and select "PicoSem-main.zip"

Now you should see the PicoSem library in your "sketch/include library" list.

How to use the library

First of all include the header file into your sketch and create an istance with:

#include "PicoSem.h"
#define PICO_EMPTY true
PicoSem Sem(PICO_EMPTY,PICO_EMPTY);

And then you can use the following calls remembering that valid values of core are 0 and 1:

void setDataEmptyFor(uint8_t core);

Called to say that the data buffer for core is empty so is ready to accept a new data set.

bool canISendTo(uint8_t core);

true if i can send a new data set to core

void setDataReadyFor(uint8_t core);

Called to say that i have just written a new data set for core

void setDataReadBy(uint8_t core);

Called to say that i've just read the last data set.

bool anyDataFor(uint8_t core);

true if there is a new data set ready for core

For more info see the examples and enjoy!