/********************************** (C) COPYRIGHT *******************************
 * File Name          : main.c
 * Author             : WCH
 * Version            : V1.0.0
 * Date               : 2019/10/15
 * Description        : Main program body.
 *********************************************************************************
 * Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
 * Attention: This software (modified or not) and binary are used for 
 * microcontroller manufactured by Nanjing Qinheng Microelectronics.
 *******************************************************************************/ 

/*
 *@Note
 *multiprocessor communication mode routine:
 *Master:USART2_Tx(PA2)\USART2_Rx(PA3).
 *Slave:USART3_Tx(PB10)\USART3_Rx(PB11).
 *
 *This routine demonstrates that USART2 works as a master, USART3 works as a slave,
 *and USART2 sends address 0x02 to make USART3 exit Silent mode, complete follow-up 
 *communication.
 *
 *     Hardware connection:PA2 -- PB11
 *	           PA3--PB10
 *
 */

#include "debug.h"


/* Global typedef */

/* Global define */

/* Global Variable */ 


/*********************************************************************
 * @fn      USARTx_CFG
 *
 * @brief   Initializes the USART2 & USART3 peripheral.
 *
 * @return  none
 */
void USARTx_CFG(void)
{
	GPIO_InitTypeDef  GPIO_InitStructure={0};
	USART_InitTypeDef USART_InitStructure={0};
	
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2|RCC_APB1Periph_USART3, ENABLE);	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA |RCC_APB2Periph_GPIOB , ENABLE);	
	
  /* USART2 TX-->A.2   RX-->A.3 */
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; 
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;	
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	GPIO_Init(GPIOA, &GPIO_InitStructure);	
  /* USART3 TX-->B.10  RX-->B.11 */
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; 
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;	
	GPIO_Init(GPIOB, &GPIO_InitStructure);
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	GPIO_Init(GPIOB, &GPIO_InitStructure);		
	
	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_No;
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
	
	USART_Init(USART2, &USART_InitStructure); 
	USART_Init(USART3, &USART_InitStructure); 

	USART_Cmd(USART2, ENABLE); 
	USART_Cmd(USART3, ENABLE); 
	USART_SetAddress(USART2, 0x1);
	USART_SetAddress(USART3, 0x2);
	USART_WakeUpConfig(USART3, USART_WakeUp_AddressMark);	
	USART_ReceiverWakeUpCmd(USART3,ENABLE);                       /* USART3 Into Silence */
}

/*********************************************************************
 * @fn      main
 *
 * @brief   Main program.
 *
 * @return  none
 */
int main(void)
{
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	SystemCoreClockUpdate();
	Delay_Init();
	USART_Printf_Init(115200);                                     /* printf using USART1 */
	printf("SystemClk:%d\r\n",SystemCoreClock);
	printf( "ChipID:%08x\r\n", DBGMCU_GetCHIPID() );
	printf("USART MultiProcessor TEST\r\n");
	USARTx_CFG();                                                 /* USART2 & USART3 Initializes */  
 
	while(1)
	{	
	  USART_SendData(USART2, 0x102);                              /* Send USART3 addr */
		while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET) /* waiting for sending finish */
		{	                                                          
		} 
		USART_SendData(USART2, 0xAA);
	while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET) /* waiting for sending finish */
		{	                                                          
		} 	
		if(USART_GetFlagStatus(USART3, USART_FLAG_RXNE) != RESET)
		{
	  if(USART_ReceiveData(USART3) == 0xAA)
		{
		printf("USART3 Receive Data\r\n");
		Delay_Ms(1000);
		}
		}
				
	}
}


