/********************************* (C) РОТЕК ***********************************
 * @module  usart
 * @file    usart.c
 * @version 1.0.0
 * @date    XX.XX.XXXX
 * $brief   Template
 *******************************************************************************
 * @history     Version  Author         Comment
 * XX.XX.XXXX   1.0.0    Telenkov D.A.  First release.
 *******************************************************************************
 */

#include "stm32f4xx.h"  
#include "usart.h"
#include "tinystdio.h"

   
#ifdef __GNUC__    
  #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else    
  #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)    
#endif  


PUTCHAR_PROTOTYPE   
{   
  //ITM_SendChar((u8) ch);
  return ch;   
}   

void putchar_(uint8_t c) {
    //while (!(USER_USART->SR & USART_FLAG_TXE));
    //USER_USART->DR = (uint16_t)c;
#ifdef SWOTRACE
    ITM_SendChar(c);
#endif
}

void putc_(void* p, char c) {
    (void)p;
    putchar_(c);
}

/**
  * @brief  
  * @retval 
  */
void InitUSART( void) {

  GPIO_InitTypeDef  GPIO_InitStructure;
  USART_InitTypeDef USART_InitStructure;
    
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
    
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  GPIO_Init(GPIOB, &GPIO_InitStructure);

  GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_USART1);
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_USART1);

  USART_InitStructure.USART_BaudRate = 115200;
  USART_InitStructure.USART_WordLength = USART_WordLength_9b;
  USART_InitStructure.USART_StopBits = USART_StopBits_1;
  USART_InitStructure.USART_Parity = USART_Parity_Even;
  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
  USART_Init(USER_USART, &USART_InitStructure);

  USART_Cmd(USER_USART, ENABLE);
  
    
  USART_ITConfig(USER_USART, USART_IT_RXNE, DISABLE);
  USART_ITConfig(USER_USART, USART_IT_TXE, DISABLE);

  init_printf(NULL, putc_);
}




/********************************* (C) РОТЕК **********************************/
