#include "bsp_rc.h"
#include "main.h"
#include "calc_lib.h"

extern UART_HandleTypeDef huart3;
extern DMA_HandleTypeDef hdma_usart3_rx;

void RC_Init(uint8_t *rx_buf, uint16_t dma_buf_num)
{

    //enable the DMA transfer for the receiver request
    SET_BIT(huart3.Instance->CR3, USART_CR3_DMAR);

    //enalbe idle interrupt
    //__HAL_UART_ENABLE_IT(&huart3, UART_IT_IDLE);

		DMA1_Stream1->CR |= (1<<4);
    //disable DMA
    __HAL_DMA_DISABLE(&hdma_usart3_rx);
    while(hdma_usart3_rx.Instance->CR & DMA_SxCR_EN)
    {
        __HAL_DMA_DISABLE(&hdma_usart3_rx);
    }

    hdma_usart3_rx.Instance->PAR = (uint32_t) & (USART3->DR);
    //memory buffer 1
 
    hdma_usart3_rx.Instance->M0AR = (uint32_t)(rx_buf);
    //memory buffer 2
 
    //hdma_usart3_rx.Instance->M1AR = (uint32_t)(rx2_buf);
    //data length

    hdma_usart3_rx.Instance->NDTR = dma_buf_num;
    //enable double memory buffer

    //SET_BIT(hdma_usart3_rx.Instance->CR, DMA_SxCR_DBM);

    //enable DMA

    __HAL_DMA_ENABLE(&hdma_usart3_rx);


}
void RC_unable(void)
{
    __HAL_UART_DISABLE(&huart3);
}
void RC_restart(uint16_t dma_buf_num)
{
    __HAL_UART_DISABLE(&huart3);
    __HAL_DMA_DISABLE(&hdma_usart3_rx);

    hdma_usart3_rx.Instance->NDTR = dma_buf_num;
		user_delay_ms(3);
    __HAL_DMA_ENABLE(&hdma_usart3_rx);
    __HAL_UART_ENABLE(&huart3);

}







