/*
* (RP240 Processor) ** (RP240 Processor) **
*
* WARNING: on WOKWI, for the Raspberry Pi Pico
* ONLY A SINGLE CORE IS SIMULATED
*
* (RP240 Processor) ** (RP240 Processor) **
*
*
*
*
* StaticMulticore-FreeRTOS_2.ino
*
* https://raw.githubusercontent.com/earlephilhower/arduino-pico/master/libraries/FreeRTOS/examples/StaticMulticore-FreeRTOS
*
*
*
* The code in this example is mostly derived from the official FreeRTOS
* code examples.
*
* For more information on static allocation and to read the original
* code visit the following links:
* https://www.freertos.org/Static_Vs_Dynamic_Memory_Allocation.html
* https://www.freertos.org/xTaskCreateStatic.html
* https://www.freertos.org/xSemaphoreCreateMutexStatic.html
*
*
*/
/*
* (RP240 Processor) ** (RP240 Processor) **
*
* WARNING: on WOKWI, for the Raspberry Pi Pico
* ONLY A SINGLE CORE IS SIMULATED
*
* (RP240 Processor) ** (RP240 Processor) **
*
*/
#include <FreeRTOS.h>
#include <task.h>
#include <semphr.h>
//
// GLOBALS:
#define SERIAL_PORT Serial1
//
const uint16_t BLINK_ON_TIME = 250;
const uint16_t BLINK_OFF_TIME = 500;
//
const uint8_t Gk_pin_LED8 = 9;
const uint32_t Gk_LED_mask32 = 1ul << Gk_pin_LED8;
//
// Dimensions of the buffer that the task being created will use as its stack.
// NOTE: This is the number of words the stack will hold, not the number of
// bytes. For example, if each stack item is 32-bits, and this is set to 100,
// then 400 bytes (100 * 32-bits) will be allocated
#define STACK_SIZE 200
/* Structure that will hold the TCB of the task being created. */
StaticTask_t xTaskBuffer_A;
StaticTask_t xTaskBuffer_B;
//
// Buffer that the task being created will use as its stack. Note this is
// an array of StackType_t variables. The size of StackType_t is dependent on
// the RTOS port
//
StackType_t xStack_A[STACK_SIZE];
StackType_t xStack_B[STACK_SIZE];
SemaphoreHandle_t xSemaphore = NULL;
StaticSemaphore_t xMutexBuffer;
/*
* (RP240 Processor) ** (RP240 Processor) **
*
* WARNING: on WOKWI, for the Raspberry Pi Pico
* ONLY A SINGLE CORE IS SIMULATED
*
* (RP240 Processor) ** (RP240 Processor) **
*
*/
// ======================================================================================
// start of functions prototypes declarations
//
uint8_t readGPIOport(uint8_t);
void writeGPIOport(uint8_t, bool);
//
// end of functions prototypes declarations
// ======================================================================================
/*
* (RP240 Processor) ** (RP240 Processor) **
*
* WARNING: on WOKWI, for the Raspberry Pi Pico
* ONLY A SINGLE CORE IS SIMULATED
*
* (RP240 Processor) ** (RP240 Processor) **
*
*/
uint8_t readGPIOport(uint8_t gpio, uint32_t GPIOmask32) {
//
// impose masking on the state of all GPIOs
// so as to obtain only the value of -gpio- output
uint32_t GPIOstates32 = gpio_get_all() & GPIOmask32;
// returns 0 or 1
return bitRead(GPIOstates32, gpio);
//
} // readGPIOport()
/*
* (RP240 Processor) ** (RP240 Processor) **
*
* WARNING: on WOKWI, for the Raspberry Pi Pico
* ONLY A SINGLE CORE IS SIMULATED
*
* (RP240 Processor) ** (RP240 Processor) **
*
*/
void writeGPIOport(uint8_t gpio, uint32_t GPIOmask32, bool value) {
//
if (value) {
// set to ONE the -gpio- out
gpio_set_mask(GPIOmask32);
//
} else {
// set to ZERO the -gpio- out
gpio_clr_mask(GPIOmask32);
//
} // if-else
//
} // writeGPIOport()
/*
* (RP240 Processor) ** (RP240 Processor) **
*
* WARNING: on WOKWI, for the Raspberry Pi Pico
* ONLY A SINGLE CORE IS SIMULATED
*
* (RP240 Processor) ** (RP240 Processor) **
*
*/
void led_ON(void * pvParameters) {
//
(void) pvParameters;
//
while (1) {
//
xSemaphoreTake(xSemaphore, (TickType_t) portMAX_DELAY);
//
digitalWrite(Gk_pin_LED8, HIGH);
delay(BLINK_ON_TIME);
//
xSemaphoreGive(xSemaphore);
//
} // while
//
} // led_ON()
/*
* (RP240 Processor) ** (RP240 Processor) **
*
* WARNING: on WOKWI, for the Raspberry Pi Pico
* ONLY A SINGLE CORE IS SIMULATED
*
* (RP240 Processor) ** (RP240 Processor) **
*
*/
void led_OFF(void *pvParameters) {
//
(void) pvParameters;
//
while (1) {
//
xSemaphoreTake(xSemaphore, (TickType_t) portMAX_DELAY);
//
digitalWrite(Gk_pin_LED8, LOW);
delay(BLINK_OFF_TIME);
//
xSemaphoreGive(xSemaphore);
//
} // while
//
} // led_OFF()
/*
* (RP240 Processor) ** (RP240 Processor) **
*
* WARNING: on WOKWI, for the Raspberry Pi Pico
* ONLY A SINGLE CORE IS SIMULATED
*
* (RP240 Processor) ** (RP240 Processor) **
*
*/
void setup() {
//
SERIAL_PORT.begin(115200);
pinMode(Gk_pin_LED8, OUTPUT);
//
// Create a mutex semaphore without using any dynamic memory
// allocation. The mutex's data structures will be saved into
// the xMutexBuffer variable
//
xSemaphore = xSemaphoreCreateMutexStatic(&xMutexBuffer);
//
xTaskCreateStatic(led_ON, "led_ON", STACK_SIZE, NULL, configMAX_PRIORITIES - 1, xStack_A, &xTaskBuffer_A);
xTaskCreateStatic(led_OFF, "led_OFF", STACK_SIZE, NULL, configMAX_PRIORITIES - 1, xStack_B, &xTaskBuffer_B);
//
} //
/*
* (RP240 Processor) ** (RP240 Processor) **
*
* WARNING: on WOKWI, for the Raspberry Pi Pico
* ONLY A SINGLE CORE IS SIMULATED
*
* (RP240 Processor) ** (RP240 Processor) **
*
*/
void loop() {
//
SERIAL_PORT.println("Hello!");
//
delay(1000);
//
} // loop()
/*
* (RP240 Processor) ** (RP240 Processor) **
*
* WARNING: on WOKWI, for the Raspberry Pi Pico
* ONLY A SINGLE CORE IS SIMULATED
*
* (RP240 Processor) ** (RP240 Processor) **
*
*/