#include "uart_interface.h"
#include <usart.h>

uart_type uart_msg = {
    .rx_count_u8        = 0,
    .tx_count_u8        = 0,
    .rx_finished_flg    = 0,
    .disconnect_flg     = 0,
    .rx_over_time       = 5,
    .rx_over_time_count = 0xFF,
    .disconnect_count   = 0,
};

void uart_rx_ticks(void)
{
    if (uart_msg.rx_over_time_count != 0xFF)
    {
        if (++uart_msg.rx_over_time_count >= uart_msg.rx_over_time)
        {
            uart_msg.rx_over_time_count = 0xFF;
            uart_msg.rx_finished_flg    = 1;
        }
    }

    if (uart_msg.disconnect_count != 0xFFFF)
    {
        if (++uart_msg.disconnect_count >= 2500) // 5000ms
        {
            uart_msg.disconnect_count = 0xFFFF;
            uart_msg.disconnect_flg   = 1;
        }
    }
}

void uart_start_send(uart_type *p_msg)
{
    p_msg->tx_count_u8 = 0;
    UART_SEND_IT_DISABLE();
    LL_USART_TransmitData8(USART1, p_msg->tx[p_msg->tx_count_u8++]);
    UART_RECV_IT_ENABLE();
}
