//
void RCC_Configuration(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA |
RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
}

void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
#ifdef VECT_TAB_RAM

NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else

NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);

NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}


void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}

void USART_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
USART_ClockInitTypeDef USART_ClockInitStructure;
 
#if 1
USART_ClockInitStructure.USART_Clock = USART_Clock_Disable;
USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;
USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;
USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;

USART_ClockInit(USART1, &USART_ClockInitStructure);
#endif
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No ;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

USART_Init(USART1, &USART_InitStructure);

USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);


USART_Cmd(USART1, ENABLE);
}

void USART1_IRQHandler(void)
{
u16 i; 
if(USART_GetFlagStatus(USART1,USART_IT_RXNE)==SET)
{
i = USART_ReceiveData(USART1);
USART_SendData(USART1,i);
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
{
}
}
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{ 
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
}
}


int main(void)
{
  /*!< At this stage the microcontroller clock setting is already configured, 
       this is done through SystemInit() function which is called from startup
       file (startup_stm32f10x_xx.s) before to branch to application main.
       To reconfigure the default setting of SystemInit() function, refer to
       system_stm32f10x.c file
     */     
       
  /* System Clocks Configuration */
  RCC_Configuration();
       
  /* NVIC configuration */
  NVIC_Configuration();
  
  /* Configure the GPIO ports */
  GPIO_Configuration();

  USART_Configuration();
  
  //GPIO_SetBits(GPIO_LED,DS1_PIN|DS2_PIN|DS3_PIN|DS4_PIN);/*关闭所有的LED指示灯*/
/* USART1 and USART2 configuration ------------------------------------------------------*/

  /* USART1 and USART2 configured as follow:
        - BaudRate = 9600 baud  
        - Word Length = 8 Bits
        - One Stop Bit
        - No parity
        - Hardware flow control disabled (RTS and CTS signals)
        - Receive and transmit enabled
  */
  //USART_InitStructure.USART_BaudRate = 115200;               /*设置波特率为115200*/
  //USART_InitStructure.USART_WordLength = USART_WordLength_8b;/*设置数据位为8*/
  //USART_InitStructure.USART_StopBits = USART_StopBits_1;     /*设置停止位为1位*/
  //USART_InitStructure.USART_Parity = USART_Parity_No;        /*无奇偶校验*/
  //USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;/*无硬件流控*/
  //USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;  /*发送和接收*/

  /*配置串口1 */
  //USART_Init(USART1, &USART_InitStructure);
  /*配置串口2*/
  //USART_Init(USART2, &USART_InitStructure);
  
  /*使能串口1的发送和接收中断*/
 // USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
 // USART_ITConfig(USART1, USART_IT_TXE, ENABLE);

  /*使能串口2的发送和接收中断*/
  //USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
  //USART_ITConfig(USART2, USART_IT_TXE, ENABLE);

  /* 使能串口1 */
  //USART_Cmd(USART1, ENABLE);
  /* 使能串口2 */
 // USART_Cmd(USART2, ENABLE);

  /* Wait until end of transmission from USART1 to USART2 */
  //while(RxCounter2 < RxBufferSize2)
  {
  }

  /* Wait until end of transmission from USART2 to USART1 */
 // while(RxCounter1 < RxBufferSize1)
  {
  }
  
  /* Check the received data with the send ones */
 // TransferStatus1 = Buffercmp(TxBuffer2, RxBuffer1, RxBufferSize1);
  /* TransferStatus1 = PASSED, if the data transmitted from USART2 and  
     received by USART1 are the same */
  /* TransferStatus1 = FAILED, if the data transmitted from USART2 and 
     received by USART1 are different */
 // TransferStatus2 = Buffercmp(TxBuffer1, RxBuffer2, RxBufferSize2);
  /* TransferStatus2 = PASSED, if the data transmitted from USART1 and  
     received by USART2 are the same */
  /* TransferStatus2 = FAILED, if the data transmitted from USART1 and 
     received by USART2 are different */

  while (1)
  {
  //     if(TransferStatus1 == PASSED)
  //     {
  //	        GPIO_ResetBits(GPIO_LED,DS1_PIN);/*点亮DS1,串口1接收的数据与串口2发送的数据相同*/
  //     }
  //     else if(TransferStatus1 == FAILED)
  //     {
  //	        GPIO_ResetBits(GPIO_LED,DS2_PIN);/*点亮DS2,串口1接收的数据与串口2发送的数据不相同*/
  //     }
  //
  //     if(TransferStatus2 == PASSED)
  //     {
  //	        GPIO_ResetBits(GPIO_LED,DS3_PIN);/*点亮DS3,串口2接收的数据与串口1发送的数据相同*/
  //     }
  //     else if(TransferStatus2 == FAILED)
  //     {
  //	        GPIO_ResetBits(GPIO_LED,DS3_PIN);/*点亮DS3,串口2接收的数据与串口1发送的数据不相同*/
  //     } 
       
  }
}