#include "stm32g0xx_hal.h"
#include "uart_bridge.h"
#include "uart_bridge_cfg.h"
#include <stdio.h>
#include <string.h>

#define TX_BUF_SIZE     60 

UART_HandleTypeDef huart_bridge;
//DMA_HandleTypeDef hdma_ub_rx;
DMA_HandleTypeDef hdma_ub_tx;

//static uint8_t rx_byte;
static char tx_buf[TX_BUF_SIZE];


//
void init_usart(void)
{
    GPIO_InitTypeDef GPIO_InitStruct = {0};
    
    __HAL_RCC_GPIOA_CLK_ENABLE();
    __HAL_RCC_USART2_CLK_ENABLE();
    
    GPIO_InitStruct.Pin       = GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3;
    GPIO_InitStruct.Mode      = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull      = GPIO_PULLUP;
    GPIO_InitStruct.Speed     = GPIO_SPEED_FREQ_VERY_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF1_USART2;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
    
    huart_bridge.Instance           = USART2;
    huart_bridge.Init.BaudRate      = UB_BAUDRATE;
    huart_bridge.Init.WordLength    = UART_WORDLENGTH_8B;
    huart_bridge.Init.StopBits      = UART_STOPBITS_1;
    huart_bridge.Init.Parity        = UART_PARITY_NONE;
    huart_bridge.Init.Mode          = UART_MODE_TX_RX;
    huart_bridge.Init.HwFlowCtl     = UART_HWCONTROL_NONE;
    huart_bridge.Init.OverSampling  = UART_OVERSAMPLING_16;
    huart_bridge.Init.OneBitSampling= UART_ONE_BIT_SAMPLE_DISABLE;
    HAL_UART_Init(&huart_bridge);
    
    HAL_RS485Ex_Init(&huart_bridge, UART_DE_POLARITY_HIGH, 0, 0);

    HAL_UARTEx_SetTxFifoThreshold(&huart_bridge, UART_TXFIFO_THRESHOLD_1_8);
    //HAL_UARTEx_SetRxFifoThreshold(&huart1, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
    HAL_UARTEx_DisableFifoMode(&huart_bridge);
    
    init_dma_usart();
    
#if 1    
    HAL_NVIC_SetPriority(USART2_IRQn, 6, 0);
    HAL_NVIC_EnableIRQ(USART2_IRQn);
    //HAL_UART_Receive_IT(&huart_bridge, &rx_byte, 1);
#endif    
}

//
void init_dma_usart(void)
{
    __HAL_RCC_DMA1_CLK_ENABLE();
    
    hdma_ub_tx.Instance = DMA1_Channel1;
    hdma_ub_tx.Init.Request = DMA_REQUEST_USART2_TX;
    hdma_ub_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
    hdma_ub_tx.Init.PeriphInc = DMA_PINC_DISABLE;
    hdma_ub_tx.Init.MemInc = DMA_MINC_ENABLE;
    hdma_ub_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
    hdma_ub_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
    hdma_ub_tx.Init.Mode = DMA_NORMAL;
    hdma_ub_tx.Init.Priority = DMA_PRIORITY_LOW;
    HAL_DMA_Init(&hdma_ub_tx);
    
    __HAL_LINKDMA(&huart_bridge, hdmatx, hdma_ub_tx);
    
    HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 6, 0);
    HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn);
}

extern "C" {
  
void USART2_IRQHandler(void)
{
    HAL_UART_IRQHandler(&huart_bridge);
    
    //HAL_UART_Receive_IT(&huart_bridge, &rx_byte, 1);
    //sbsTerminal.put_byte((char)(uart.Instance->RDR & (uint16_t)0x01FF));
}

void DMA1_Channel1_IRQHandler(void)
{
    HAL_DMA_IRQHandler(&hdma_ub_tx);
}
}

//
void ub_send_test_byte(void)
{
    const uint8_t byte = 0x31;
    //HAL_StatusTypeDef HAL_UART_Transmit(UART_HandleTypeDef *huart, const uint8_t *pData, uint16_t Size, uint32_t Timeout);
    HAL_UART_Transmit(&huart_bridge, &byte, 1, 200);
    //huart_bridge.Instance->TDR 
}

//
void ub_send_uint(uint32_t val)
{
    sprintf(tx_buf, "{%u}\n", val);
    HAL_UART_Transmit_DMA(&huart_bridge, (uint8_t *)tx_buf, strlen(tx_buf));
    
}

//
void ub_send_turns_pack(uint8_t *buf, uint32_t len)
{
    HAL_UART_Transmit_DMA(&huart_bridge, (uint8_t *)buf, len);
}
