/**
  ******************************************************************************
  * @file     USART1_CFG.c
  * @author   SINOMCU-AE
  * @brief    USART1 config
  *    
  *          This file provides functions to usart1 initialization:
  *              PA2   --> USART1_TX
  *              PA3   <-- USART1_RX
  *          BaudRate:115200,8 bit,NONE,STOP 1bit
  *          UART RX use DMA transfer.
  *         
  ******************************************************************************
  * @attention 
  *                           DISCLAIMER
  * The present firmware which is for guidance only aims at providing customers
  * with coding information regarding their products in order for them to save
  * time. As a result, SinoMCU shall not be held liable for any direct,indirect
  * or consequential damages with respect to any claims arising from the content 
  * of such firmware and/or the use made by customers of the coding information 
  * contained herein in connection with their products.
  *
  * <h2><center>&copy; Copyright (c) 2021 sinomcu All rights reserved.</center></h2>
  *
  ******************************************************************************
  */

#include "stdio.h"   
#include "USART1_CFG.h"

/**
  * @brief  Retargets the C library printf function to the USART.
  * @param  ch:send byte,file:library,file pointer
  * @retval None
  */
int fputc(int ch, FILE *f) {
#if(defined(EN_PRINT_UART) && EN_PRINT_UART)  
  while(!(MS32_USART_IsActiveFlag_TXE(USART1))){ ; }  // wait TXE set
  MS32_USART_TransmitData8(USART1,ch);
#endif // (defined(EN_PRINT_UART) && EN_PRINT_UART)   
  return ch;
}

/**
  * @brief  uart IO Initialization
  *    PA2   --> USART1_TX
  *    PA3   <-- USART1_RX
  * @param  None
  * @retval None
  */
void uart_gpio_init(void) {
  MS32_GPIO_InitTypeDef GpioInitStruct = {0};

  // PA2   --> USART1_TX
  // PA3   <-- USART1_RX
  MS32_GPIO_StructInit(&GpioInitStruct);    
  GpioInitStruct.Pin = MS32_GPIO_PIN_2;
  GpioInitStruct.Mode = MS32_GPIO_MODE_ALTERNATE;
  GpioInitStruct.Speed = MS32_GPIO_SPEED_FREQ_HIGH;
  GpioInitStruct.OutputType = MS32_GPIO_OUTPUT_PUSHPULL;
  GpioInitStruct.Pull = MS32_GPIO_PULL_UP;
  GpioInitStruct.Alternate = MS32_GPIO_AF_1;
  MS32_GPIO_Init(GPIOA, &GpioInitStruct);             // init function had enable clock

  GpioInitStruct.Pin = MS32_GPIO_PIN_3;
  GpioInitStruct.Mode = MS32_GPIO_MODE_ALTERNATE;
  GpioInitStruct.Speed = MS32_GPIO_SPEED_FREQ_HIGH;
  GpioInitStruct.OutputType = MS32_GPIO_OUTPUT_PUSHPULL;
  GpioInitStruct.Pull = MS32_GPIO_PULL_UP;
  GpioInitStruct.Alternate = MS32_GPIO_AF_1;
  MS32_GPIO_Init(GPIOA, &GpioInitStruct);
}

/**
  * @brief  uart1 Initialization Function
  * @param  uint32_t baudrate
  * @retval None
  */
void usart1_uart_init(uint32_t baudrate) {
  MS32_USART_InitTypeDef UsartInitStruct = {0};

  uart_gpio_init();
  
  // UART parameter configuration
  MS32_USART_StructInit(&UsartInitStruct);
  UsartInitStruct.BaudRate = baudrate;
  UsartInitStruct.DataWidth = MS32_USART_DATAWIDTH_8B;
  UsartInitStruct.StopBits = MS32_USART_STOPBITS_1;
  UsartInitStruct.Parity = MS32_USART_PARITY_NONE;
  UsartInitStruct.TransferDirection = MS32_USART_DIRECTION_TX_RX;
  UsartInitStruct.HardwareFlowControl = MS32_USART_HWCONTROL_NONE;
  UsartInitStruct.OverSampling = MS32_USART_OVERSAMPLING_16;
  MS32_USART_Init(USART1, &UsartInitStruct);          // init function had enable clock
  
  MS32_USART_DisableIT_CTS(USART1);
  MS32_USART_ConfigAsyncMode(USART1);
  MS32_USART_Enable(USART1);

  // UART RX user DMA transfer
  MS32_USART_EnableDMAReq_RX(USART1);  
}

/******************************** END OF FILE *********************************/
