#ifndef _BIWAK_UART_H_
#define _BIWAK_UART_H_
//----------------------------------------------------------------------------
///
/// \brief  UART
///
//----------------------------------------------------------------------------


//---Includes-----------------------------------------------------------------


#include <lepto/ring.h>
#include <lepto/signal.h>
#include <biwak/gpio.hpp>
#include <biwak/iostream.h>


//---Forward declarations-----------------------------------------------------


#if defined (MCU_PLATFORM_STM32)
   #include <HALWrapper/stm32_dma.h>    // Usart needs dma
   #include <HALWrapper/stm32_usart.h>
   #include <HALWrapper/stm32_uart.h>
#else
   #define UART_HandleTypeDef    char
#endif


#define UART_BUF_SIZE 0x10


//---Declarations-------------------------------------------------------------


void deinitUartAll();
extern int uartKeeper;

struct SUartConfig;
class CUart;

extern CUart *interruptUart1;
extern CUart *interruptUart2;
extern CUart *interruptUart3;


class CUart:public CIOStream
{
    private:
        // const SUartConfig &m_configData;

        UART_HandleTypeDef *m_pHandle;
        // CGpio gpioRx;
        // CGpio gpioTx;
        uint32_t rxByte;

#if defined( BLOCKS )
        asdasd
        uint8_t   txBuf[UART_BUF_SIZE];
        uint8_t   rxBuf[UART_BUF_SIZE];

        int totalTxSize;
        int currentTxSize;
        uint8_t *txAddress;
#endif

    public:

        CUart( const SUartConfig &_configData );
        ~CUart();
        void deinit();

        //static int getIndex(EUsart _eUsart);
        bool waitBusy();
        int write(const void *buf, int size) override final;

        void flushOutput();
        UART_HandleTypeDef *getHandle()
        {
         return(m_pHandle);
        }
        #if 1 //defined( BLOCKS )
        unsigned char *getRxBytePtr()
        {
           return( (unsigned char *)&rxByte );
        }
        unsigned char getRxByte()
        {
           return(rxByte);
        }
        #endif
        void startReceive();
        void txInterrupt();
        void interrupt();
        CUart *verifyHandle(void *handle);
        void setStdio();
};


//---fin-----------------------------------------------------------------------
#endif // ? ! _BIWAK_UART_H_
