Raspberry Pico - Comunicación entre cores (2)
Comunicación entre los cores de Raspberry Pico
Utilizando Colas (Queues), este ejemplo al igual que el de la parte 1 comunica ambos cores del Raspberry pico.
Un core (core 1) sensa una tensión y el otro (Core 0) controla un GPIO. Si la tensión sensada es mayor a Vcc / 2 el GPIO presenta estado HI.
#include <stdio.h>
#include "pico/stdlib.h"
#include "pico/multicore.h"
#include "hardware/adc.h"
#include "pico/util/queue.h"
void init_adc();
void init_gpio();
#define GPIO_ON 1
#define GPIO_OFF 0
queue_t queue;
typedef struct queueItem {
uint32_t value;
} item;
const int QUEUE_LENGTH = 10;
void segundo_core() {
bool mayor = false;
item temp;
while(true) {
if (adc_hw->result > 2048) { // si VADC > VCC/2...
if (!mayor) {
temp.value = GPIO_ON;
if (queue_try_add(&queue, &temp)) {
mayor = true;
}
}
} else {
if (mayor) {
temp.value = GPIO_OFF;
if (queue_try_add(&queue, &temp)) {
mayor = false;
}
}
}
}
}
int main()
{
uint32_t estado_led = GPIO_OFF;
queue_init(&queue, sizeof(item), QUEUE_LENGTH); //initialize the queue
stdio_init_all();
multicore_launch_core1(segundo_core);
init_adc();
init_gpio();
item temp;
while (true) {
if ((queue_try_remove(&queue, &temp))) {
estado_led = temp.value;
}
if (estado_led) {
sio_hw->gpio_set |= 1 << 25;
} else {
sio_hw->gpio_clr |= 1 << 25;
}
}
}
void init_adc() {
// ADC GPIO 28 - contínuo
adc_hw->cs = ADC_CS_EN_BITS;
io_bank0_hw->io[28].ctrl = GPIO_FUNC_NULL;
pads_bank0_hw->io[28] = PADS_BANK0_GPIO0_OD_BITS;
adc_hw->cs = (adc_hw->cs & (~(ADC_CS_AINSEL_BITS))) | 2 << ADC_CS_AINSEL_LSB; // canal 2
adc_hw->cs |= ADC_CS_START_MANY_BITS; // conversión contínua
}
void init_gpio() {
// led en gpio 25 - led built-in
io_bank0_hw->io[25].ctrl = 5;
pads_bank0_hw->io[25] = 0;
sio_hw->gpio_oe_set|=1<<25;
}