Raspberry Pico - Comunicación entre cores (1)
Comunicación entre los cores de Raspberry Pico
Utilizando FIFO, este ejemplo 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"
void init_adc();
void init_gpio();
#define GPIO_ON 1
#define GPIO_OFF 0
void segundo_core() {
bool mayor = true;
while(true) {
if (adc_hw->result > 2048) { // si VADC > VCC/2...
if (!mayor) {
if (multicore_fifo_wready()) {
multicore_fifo_push_blocking(GPIO_ON);
mayor = true;
}
}
} else {
if (mayor) {
if (multicore_fifo_wready()) {
multicore_fifo_push_blocking(GPIO_OFF);
mayor = false;
}
}
}
}
}
int main()
{
uint32_t estado_led = GPIO_OFF;
stdio_init_all();
multicore_launch_core1(segundo_core);
init_adc();
init_gpio();
while (true) {
if (multicore_fifo_rvalid()) {
estado_led = multicore_fifo_pop_blocking();
}
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;
}