Jak na: Rozhraní UART
Modul Core Module má 3 rozhraní UART. Signály jednotlivých kanálů se jmenují TXDx a RXDx, kde x je 0, 1 nebo 2.
Pozice signálů najdete ve výkresu s pinoutem modulu.
Odkazy
- Modul SDK pro UART
- Příklad v repozitáři na GitHubu
Zápis na UART
Synchronní zápis
Funkce twr_uart_write potřebuje vědět, kolik bajtů má odeslat, proto použijte sizeof(uart_tx).
Tento příklad zapíše Hello world přes UART1 s přenosovou rychlostí 115200.
Formát je 8 datových bitů, bez parity, 1 stop bit.
Zápis je blokující: program počká, dokud neskončí.
Příklad kódu: synchronní zápis na UART
#include <application.h>
void application_init()
{
twr_uart_init(TWR_UART_UART1, TWR_UART_BAUDRATE_115200, TWR_UART_SETTING_8N1);
char uart_tx[] = "Hello world\r\n";
twr_uart_write(TWR_UART_UART1, uart_tx, strlen(uart_tx));
}
Asynchronní zápis
Je to trochu složitější: musíte vytvořit strukturu FIFO a pole pro buffer FIFO, pak FIFO inicializovat a přiřadit k rozhraní UART.
V tomto příkladu ukazujeme pouze asynchronní zápis.
Příklad kódu: asynchronní zápis na UART
#include <application.h>
twr_fifo_t tx_fifo;
uint8_t tx_fifo_buffer[32];
void uart_handler(twr_uart_channel_t channel, twr_uart_event_t event, void *param)
{
if (event == TWR_UART_EVENT_ASYNC_WRITE_DONE)
{
// here you can for example send more data
}
}
void application_init()
{
twr_uart_init(TWR_UART_UART1, TWR_UART_BAUDRATE_115200, TWR_UART_SETTING_8N1);
twr_uart_set_event_handler(TWR_UART_UART1, uart_handler, NULL);
twr_fifo_init(&tx_fifo, tx_fifo_buffer, sizeof(tx_fifo_buffer));
// We set only TX FIFO, for RX_FIFO we pass NULL
twr_uart_set_async_fifo(TWR_UART_UART1, &tx_fifo, NULL);
char uart_tx[] = "Hello world\r\n";
twr_uart_async_write(TWR_UART_UART1, uart_tx, strlen(uart_tx));
}
Čtení z UART
Přijaté bajty můžete opět číst dvěma způsoby: synchronně ve své úloze, nebo asynchronně pomocí callbacků.
Synchronní čtení
Příklad kódu: synchronní čtení z UART
void application_task()
{
// Define receive buffer
uint8_t uart_rx[32];
// Synchronous reading
size_t number_of_rx_bytes = twr_uart_read(TWR_UART_UART1, uart_rx, sizeof(uart_rx), 500);
char uart_tx[32];
snprintf(uart_tx, sizeof(uart_tx), "RX bytes: %d\r\n", number_of_rx_bytes);
twr_uart_write(TWR_UART_UART1, uart_tx, strlen(uart_tx));
twr_scheduler_plan_current_now();
}
Asynchronní čtení
Tento příklad asynchronně odesílá a přijímá data na TWR_UART_UART1 s přenosovou rychlostí 115200.
Formát je 8 datových bitů, bez parity, 1 stop bit.
Tento příklad nešetří energii. Když spustíte čtení z UART funkcí twr_uart_async_read_start, modul Core Module neusne, dokud nezavoláte twr_uart_async_read_stop.
Příklad kódu: asynchronní čtení z UART
#include <application.h>
twr_fifo_t tx_fifo;
twr_fifo_t rx_fifo;
uint8_t tx_fifo_buffer[64];
uint8_t rx_fifo_buffer[64];
void uart_handler(twr_uart_channel_t channel, twr_uart_event_t event, void *param)
{
uint8_t rx_data[32];
if (event == TWR_UART_EVENT_ASYNC_WRITE_DONE)
{
// here you can for example send more data
}
if (event == TWR_UART_EVENT_ASYNC_READ_DATA)
{
// Read data from FIFO by a single byte as you receive it
size_t number_of_rx_bytes = twr_uart_async_read(TWR_UART_UART1, rx_data, sizeof(rx_data));
char uart_tx[32];
snprintf(uart_tx, sizeof(uart_tx), "RX: %d\r\n", number_of_rx_bytes);
twr_uart_async_write(TWR_UART_UART1, uart_tx, strlen(uart_tx));
}
if (event == TWR_UART_EVENT_ASYNC_READ_TIMEOUT)
{
// No data received during set timeout period
char uart_tx[] = "Timeout\r\n";
twr_uart_async_write(TWR_UART_UART1, uart_tx, strlen(uart_tx));
// You can also read received bytes here instead of TWR_UART_EVENT_ASYNC_READ_DATA
}
}
void application_init()
{
twr_uart_init(TWR_UART_UART1, TWR_UART_BAUDRATE_115200, TWR_UART_SETTING_8N1);
twr_uart_set_event_handler(TWR_UART_UART1, uart_handler, NULL);
twr_fifo_init(&tx_fifo, tx_fifo_buffer, sizeof(tx_fifo_buffer));
twr_fifo_init(&rx_fifo, rx_fifo_buffer, sizeof(rx_fifo_buffer));
twr_uart_set_async_fifo(TWR_UART_UART1, &tx_fifo, &rx_fifo);
twr_uart_async_read_start(TWR_UART_UART1, 500);
char uart_tx[] = "Hello world\r\n";
twr_uart_async_write(TWR_UART_UART1, uart_tx, strlen(uart_tx));
}