Skip to content

File main.cpp

File List > DmaReceive-SerialEcho > main.cpp

Go to the documentation of this file

Source Code

#include "daisy_seed.h"

using namespace daisy;

const auto kUartPeripheral = UartHandler::Config::Peripheral::USART_1;
const auto kUartRxPin = seed::D14;
const uint32_t kUartBaudRate = 31250;
const size_t kUartBufferSize = 256;

DaisySeed hardware;
uint8_t uart_buffer[kUartBufferSize];
FIFO<uint8_t, kUartBufferSize> tx_queue;

void RxCallback(uint8_t*            data,
                size_t              size,
                void*               context,
                UartHandler::Result res)
{
    auto space_in_queue = tx_queue.GetCapacity() - tx_queue.GetNumElements();
    // Make sure we don't overflow (overflow bytes will be dropped)
    if(size <= space_in_queue)
    {
        // push each byte into the queue
        for(size_t i = 0; i < size; i++)
            tx_queue.PushBack(data[i]);
    }
}

int main()
{
    hardware.Init(true);

    hardware.StartLog(true);

    UartHandler::Config uart_config;
    UartHandler         uart;
    uart_config.baudrate      = kUartBaudRate;
    uart_config.pin_config.rx = kUartRxPin;
    uart_config.periph        = kUartPeripheral;
    uart_config.parity        = UartHandler::Config::Parity::NONE;
    uart_config.stopbits      = UartHandler::Config::StopBits::BITS_1;
    uart_config.wordlength    = UartHandler::Config::WordLength::BITS_8;
    uart_config.mode          = UartHandler::Config::Mode::RX;
    uart.Init(uart_config);

    hardware.PrintLine("Starting UART Receiver");
    dsy_dma_clear_cache_for_buffer(uart_buffer, kUartBufferSize);
    uart.DmaListenStart(uart_buffer, kUartBufferSize, RxCallback, nullptr);

    while(true)
    {
        if(!uart.IsListening())
        {
            hardware.PrintLine("Restarting UART Receiver");
            dsy_dma_clear_cache_for_buffer(uart_buffer, kUartBufferSize);
            uart.DmaListenStart(
                uart_buffer, kUartBufferSize, RxCallback, nullptr);
        }

        while(!tx_queue.IsEmpty())
        {
            uint8_t byte = tx_queue.PopFront();
            hardware.PrintLine("%d", byte);
        }

        hardware.SetLed((System::GetNow() & 511) < 255);
    }
}