/***********************************************************************
 * Copyright (c)  2017 - 2023, Unicmicro Co.,Ltd .
 * All rights reserved.
 * Filename    : app.c
 * Description : app source file
 * Author(s)   : MCU Team
 * version     : V1.0
 * Modify date : 2023-06-13
 ***********************************************************************/
#include  "app.h"
#include  "config.h"
#include  "common.h"
#include  "uart0.h"
#include  "uart1.h"
#include  "um16436_spi.h"


uint32_t g_data_buf[512];

uint8_t uart1_rx_flag = 0;
uint8_t uart1_rx_buf[64];
uint8_t uart1_rx_count = 0;

/************************************************************************
 * function   : uart1_rec_pro
 * Description: uart1_rec_pro 串口接收处理回调函数
 * input : none
 * return: none
 ************************************************************************/
void uart1_rec_pro(void)
{
	uart1_rx_buf[uart1_rx_count++] = uart1_recv_byte();
	if(uart1_rx_count==1)
		uart1_rx_flag = 1;	
}

/************************************************************************
 * function   : uart_init
 * Description: uart_init 串口初始化
 * input : none
 * return: none
 ************************************************************************/
void uart_init(void)
{
	uart1_rx_flag = 0;	
	uart1_init(PCLK,UART1_BAUD_RATE);
	uart1_irq_init(UART1_IRQ_ENABLE,uart1_rec_pro);	
}

/************************************************************************
 * function   : umlcd_clock_auto_calibrate
 * Description: umlcd_clock_auto_calibrate时钟自动校准
 * input : none
 * return: none
 ************************************************************************/
void umlcd_clock_auto_calibrate(void)
{
	umlcd_write_reg(UMLCD_TEST_REG, UMLCD_TESTEN_OFF | UMLCD_LCCTRL_OFF | UMLCD_VLCD_SET_0P9VDD | UMLCD_RCL_CAT_EN_ON);
	delay_us(250);//必要的时间，不能去掉
	umlcd_write_reg(UMLCD_TEST_REG, UMLCD_TESTEN_OFF | UMLCD_LCCTRL_OFF | UMLCD_VLCD_SET_0P9VDD | UMLCD_RCL_CAT_EN_OFF);	
}

/************************************************************************
 * function   : umlcd_init
 * Description: umlcd_init LCD初始化
 * input : none
 * return: none
 ************************************************************************/
void umlcd_init(void)
{
	/* 设置VLCD为0.9VDD	
	   帧频64Hz
	   4COM、36SEG、B类、1/3 Bias、偏置电流100nA
	   快充时间8Clock、快充期间驱动强度40uA
	   使用内部32KHz时钟源，开启时钟
	   开启LCD */
	umlcd_write_reg(UMLCD_TEST_REG, 	UMLCD_TESTEN_OFF | UMLCD_LCCTRL_OFF | UMLCD_VLCD_SET_0P9VDD | UMLCD_RCL_CAT_EN_OFF);			
	umlcd_write_reg(UMLCD_DFSET_REG,	UMLCD_DFSET_EN_ON | UMLCD_DFSET_TYPE_B_64Hz);			
	umlcd_write_reg(UMLCD_LCDSET_REG,	UMLCD_DISPLAY_MODE_36x4 | UMLCD_INV_MODE_TYPE_B | UMLCD_BIAS_SET_ONE_THIRD | UMLCD_IBIAS_SET_100nA);		
	umlcd_write_reg(UMLCD_FCSET_REG, 	UMLCD_FC_PW_CLOCK_8 | UMLCD_FC_DS_40UA);//可以增大快充的时间或者增大快充时的驱动强度来改善显示效果				
	umlcd_write_reg(UMLCD_SYS_CTRL_REG, UMLCD_CLK_SEL_INTERNAL | UMLCD_SPI_LOCK_OFF | UMLCD_I2C_LOCK_OFF | UMLCD_CLK32K_EN_ON | UMLCD_WDT_EN_OFF | UMLCD_CLK_OUT_OFF);	
	umlcd_write_reg(UMLCD_DISPCTRL_REG, UMLCD_FELUSE_EN_OFF | UMLCD_PWEN_ON | UMLCD_LCD_EN_ON);											
	
	/* 全屏清除显示 */
	umlcd_clear_display();																		
}

/************************************************************************
 * function   : umlcd_test
 * Description: umlcd test
 * input : none
 * return: none
 ************************************************************************/
void umlcd_test(void)
{
	umlcd_init();
	umlcd_clock_auto_calibrate();
	
	umlcd_all_display();//全屏显示

	while(1)
	{		
	}
}

/************************************************************************
 * function   : soc_test
 * Description: soc test
 * input : none
 * return: none
 ************************************************************************/
void soc_test(void)
{
	printfS("UM321xA run at %dHz\n\r", SystemCoreClock);
	printfS("umlcd test start\n\r");	

	umlcd_test();
	
}


