/**
  ******************************************************************************
  * @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.
  *         
  ******************************************************************************
  * @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 GPIO_InitStruct = {0};
  MS32_AHB1_GRP1_EnableClock(MS32_AHB1_GRP1_PERIPH_GPIOA);

  // USART1 GPIO Configuration
  // PA2   --> USART1_TX
  // PA3   <-- USART1_RX
  MS32_GPIO_StructInit(&GPIO_InitStruct);    
  GPIO_InitStruct.Pin = MS32_GPIO_PIN_2;
  GPIO_InitStruct.Mode = MS32_GPIO_MODE_ALTERNATE;
  GPIO_InitStruct.Speed = MS32_GPIO_SPEED_FREQ_HIGH;
  GPIO_InitStruct.OutputType = MS32_GPIO_OUTPUT_PUSHPULL;
  GPIO_InitStruct.Pull = MS32_GPIO_PULL_UP;
  GPIO_InitStruct.Alternate = MS32_GPIO_AF_1;
  MS32_GPIO_Init(GPIOA, &GPIO_InitStruct);

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

/**
  * @brief  USART1 Initialization Function
  * @param  uint32_t baudrate
  * @retval None
  */
void USART1_UART_Init(uint32_t baudrate)
{
  MS32_USART_InitTypeDef USART_InitStruct = {0};

  UART_GPIO_Init();

  MS32_APB1_GRP2_EnableClock(MS32_APB1_GRP2_PERIPH_USART1);    
  
  // UART parameter configuration
  MS32_USART_StructInit(&USART_InitStruct);
  USART_InitStruct.BaudRate = baudrate;
  USART_InitStruct.DataWidth = MS32_USART_DATAWIDTH_8B;
  USART_InitStruct.StopBits = MS32_USART_STOPBITS_1;
  USART_InitStruct.Parity = MS32_USART_PARITY_NONE;
  USART_InitStruct.TransferDirection = MS32_USART_DIRECTION_TX_RX;
  USART_InitStruct.HardwareFlowControl = MS32_USART_HWCONTROL_NONE;
  USART_InitStruct.OverSampling = MS32_USART_OVERSAMPLING_16;
  MS32_USART_Init(USART1, &USART_InitStruct);
  MS32_USART_DisableIT_CTS(USART1);
  MS32_USART_ConfigAsyncMode(USART1);
  MS32_USART_Enable(USART1);
}

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