Skip to content

File uart.h

File List > external-docs > libDaisy > src > per > uart.h

Go to the documentation of this file

Source Code

/* 
TODO
- Overflow handling, etc. for Rx Queue.
*/

#pragma once
#ifndef DSY_UART_H
#define DSY_UART_H 
#include "daisy_core.h"

#if !UNIT_TEST
#include "util/hal_map.h"
#endif

namespace daisy
{
class UartHandler
{
  public:
    struct Config
    {
        enum class Peripheral
        {
            USART_1,
            USART_2,
            USART_3,
            UART_4,
            UART_5,
            USART_6,
            UART_7,
            UART_8,
            LPUART_1,
        };

        enum class StopBits
        {
            BITS_0_5,
            BITS_1,
            BITS_1_5,
            BITS_2,
        };

        enum class Parity
        {
            NONE,
            EVEN,
            ODD,
        };

        enum class Mode
        {
            RX,
            TX,
            TX_RX,
        };

        enum class WordLength
        {
            BITS_7,
            BITS_8,
            BITS_9,
        };

        struct
        {
            Pin tx;   
            Pin rx;   
        } pin_config; 
        Config()
        {
            // user must init periph, pin_config, and mode
            stopbits   = StopBits::BITS_1;
            parity     = Parity::NONE;
            wordlength = WordLength::BITS_8;
            baudrate   = 31250;
        }

        Peripheral periph;
        StopBits   stopbits;
        Parity     parity;
        Mode       mode;
        WordLength wordlength;
        uint32_t   baudrate;
    };


    UartHandler() : pimpl_(nullptr) {}
    UartHandler(const UartHandler& other) = default;
    UartHandler& operator=(const UartHandler& other) = default;

    enum class Result
    {
        OK, 
        ERR 
    };

    enum class DmaDirection
    {
        RX, 
        TX  
    };

    Result Init(const Config& config);

    const Config& GetConfig() const;

    typedef void (*StartCallbackFunctionPtr)(void* context);
    typedef void (*EndCallbackFunctionPtr)(void* context, Result result);

    typedef void (*CircularRxCallbackFunctionPtr)(uint8_t* data,
                                                  size_t   size,
                                                  void*    context,
                                                  Result   result);

    Result BlockingTransmit(uint8_t* buff, size_t size, uint32_t timeout = 100);

    Result
    BlockingReceive(uint8_t* buffer, uint16_t size, uint32_t timeout = 100);

    Result DmaTransmit(uint8_t*                              buff,
                       size_t                                size,
                       UartHandler::StartCallbackFunctionPtr start_callback,
                       UartHandler::EndCallbackFunctionPtr   end_callback,
                       void*                                 callback_context);

    Result DmaReceive(uint8_t*                              buff,
                      size_t                                size,
                      UartHandler::StartCallbackFunctionPtr start_callback,
                      UartHandler::EndCallbackFunctionPtr   end_callback,
                      void*                                 callback_context);

    Result DmaListenStart(uint8_t*                      buff,
                          size_t                        size,
                          CircularRxCallbackFunctionPtr cb,
                          void*                         callback_context);

    Result DmaListenStop();

    bool IsListening() const;

    int CheckError();

    int PollReceive(uint8_t* buff, size_t size, uint32_t timeout);

    Result PollTx(uint8_t* buff, size_t size);

    class Impl; 
  private:
    Impl* pimpl_;
};

extern "C"
{
    void dsy_uart_global_init();
};

} // namespace daisy

#endif