#ifndef __USARTBRIDGETERMINAL_H
#define __USARTBRIDGETERMINAL_H

#include "at32f403a_407.h"
#include "terminal.h"


#define USART_BRIDGE_USART            USART1
#define USART_BRIDGE_SPEED            115200 
#define USART_BRIDGE_IRQn             USART1_IRQn
#define USART_BRIDGE_IRQHandler       USART1_IRQHandler
#define USART_BRIDGE_TX_PIN           GPIO_PINS_9
#define USART_BRIDGE_RX_PIN           GPIO_PINS_10
#define USART_BRIDGE_TX_PORT          GPIOA
#define USART_BRIDGE_RX_PORT          GPIOA
#define USART_BRIDGE_CLK_ENABLE       crm_periph_clock_enable(CRM_USART1_PERIPH_CLOCK, TRUE);
#define USART_BRIDGE_TX_PORT_CLK_ENABLE  crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE);
#define USART_BRIDGE_RX_PORT_CLK_ENABLE  crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE);


//-----------------------------------------------------------------------------

// Connect usartBridge objectt with ANSI C Interfaces

//-----------------------------------------------------------------------------


//Task for freertos function
void task_usartbridge(void *pvParameters);

//Ansi C interface for usartBridge object

void configure_usartbridge();
void processing_usartbridge();
void print_usartbridge(const char * str);
void vTaskUsartBridge(void *params);
void HAL_UART_RxCpltCallbackTerminal(void);
void HAL_UART_TxCpltCallbackTerminal(void);

//-----------------------------------------------------------------------------

// Usart bridge class for terminal

//-----------------------------------------------------------------------------

class UsartBridgeTerminal
{
public:

    UsartBridgeTerminal();
    ~UsartBridgeTerminal();

    void configure();
    void print(const char *str);

public:

    void recvByte();
    void sendByte(uint8_t byte);

private:

    void InitUsart();

public:
    
    char               m_uartByte;
    bool               m_state;
    
private:

    uint32_t m_sizeCache;
    uint32_t m_readIndex;
};


extern UsartBridgeTerminal terminalUsartBridge;


#endif // USARTBRIDGETERMINAL_H

