/****************************************************************************************************
  * @brief      : 	JZ2440v2开发板串口相关源文件
  * @version    : 	V0.0
  * @note       : 	无
  * @history    : 	无
*****************************************************************************************************/

#include "s3c2440_soc.h"
#include "uart.h"
#include "function.h"

/**********************************************************************************
  * @brief       : 	初始化串口0相关资源
  * @param[in]   : 	无
  * @param[out]  : 	无
  * @return      : 	无
  * @others      : 	115200,8n1
***********************************************************************************/
void InitUart0(void)
{
	/* 设置引脚用于串口 */
	/* GPH2,3用于TxD0, RxD0 */
	GPHCON &= ~((3<<4) | (3<<6));
	GPHCON |= ((2<<4) | (2<<6));

	GPHUP &= ~((1<<2) | (1<<3));  /* 使能内部上拉 */
	

	/* 设置波特率 */
	/* UBRDIVn = (int)( UART clock / ( buad rate x 16) ) –1
	 *  UART clock = 50M
	 *  UBRDIVn = (int)( 50000000 / ( 115200 x 16) ) –1 = 26
	 */
	UCON0 = 0x00000005; /* PCLK,中断/查询模式 */
	UBRDIV0 = 26;

	/* 设置数据格式 */
	ULCON0 = 0x00000003; /* 8n1: 8个数据位, 无较验位, 1个停止位 */

	/*  */

}

/**********************************************************************************
  * @brief       : 	串口0输出一个字符
  * @param[in]   : 	c	待输出字符
  * @param[out]  : 	无
  * @return      : 	无
  * @others      : 	115200,8n1
***********************************************************************************/
int PutChar(int c)
{
	/* UTRSTAT0 */
	/* UTXH0 */

	while (!(UTRSTAT0 & (1<<2)));
	UTXH0 = (unsigned char)c;
	return 0;
	
}

/**********************************************************************************
  * @brief       : 	串口0获得一个字符
  * @param[in]   : 	无
  * @param[out]  : 	无
  * @return      : 	返回获得的数据
  * @others      : 	115200,8n1
***********************************************************************************/
int GetChar(void)
{
	while (!(UTRSTAT0 & (1<<0)))
	{
		if(isClickReturn())
		{
			MainPage();
			return 0;
		}
	}
	return URXH0;
}

/**********************************************************************************
  * @brief       : 	串口0输出一个字符串
  * @param[in]   : 	s	待输出字符串指针
  * @param[out]  : 	无
  * @return      : 	无
  * @others      : 	115200,8n1
***********************************************************************************/
int Puts(const char *s)
{
	while (*s)
	{
		PutChar(*s);
		s++;
	}
	return 0;
}

