#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/uart.h"
#define UART0_TX_PIN 0
#define UART0_RX_PIN 1
#define UART1_TX_PIN 5
#define UART1_RX_PIN 6
#define BUFFER_SIZE 50
void uart_send_and_receive(uart_inst_t *uart_tx, uart_inst_t *uart_rx, const char *message)
{
char buffer[BUFFER_SIZE];
uint8_t index = 0;
uart_puts(uart_tx, message);
printf("Sent -> %s", message);
while (uart_is_readable_within_us(uart_rx, 500000))
{
char c = uart_getc(uart_rx);
buffer[index++] = c;
if (c == '\n' || index >= BUFFER_SIZE - 1)
{
break;
}
}
buffer[index] = '\0';
if (index > 0)
{
printf("Received -> %s\n", buffer);
}
else
{
printf("No response received.\n");
}
}
int main()
{
stdio_init_all();
printf("UART0 and UART1 Test Starting...\n");
uart_init(uart0, 115200);
gpio_set_function(UART0_TX_PIN, GPIO_FUNC_UART);
gpio_set_function(UART0_RX_PIN, GPIO_FUNC_UART);
uart_init(uart1, 115200);
gpio_set_function(UART1_TX_PIN, GPIO_FUNC_UART);
gpio_set_function(UART1_RX_PIN, GPIO_FUNC_UART);
char message0[BUFFER_SIZE];
uint8_t i = 0;
while (true)
{
while (uart_is_readable(uart0))
{
char c = uart_getc(uart0);
message0[i] = c;
i++;
}
message0[i] = '\0';
i = 0;
uart_send_and_receive(uart0, uart1, message0);
sleep_ms(1000);
}
return 0;
}