#include "./stm32f1xx_it.h"
#include "./SYSTEM/sys/sys.h"
#include "./SYSTEM/usart/usart.h"
#include "./SYSTEM/delay/delay.h"

#include "./BSP/LED/led.h"
#include "./BSP/USART2/usart2.h"
#include "./BSP/TIME6/time6.h"
#include "./BSP/LED0_PWM/led0_pwm.h"
#include "./BSP/RTC/rtc.h"

#include "./hmi/hmi_cmd.h"
#include "./hmi/hmi_rx_parse.h"
#include "./hmi_cmd_example.h"
#include "./hmi_test.h"

extern TIM_HandleTypeDef TIM6_Handler;    

volatile uint8_t ui_flag;
volatile uint32_t sysTickUptime;
uint32_t tick_ms[5];

uint32_t millis(void)
{
    return sysTickUptime;
}
int main(void)
{
    HAL_Init();
    sys_stm32_clock_init(RCC_PLL_MUL9);     /* 设置时钟, 72Mhz */
    usart_init(115200);                     /* 调试串口 */
    led_init();                             /* 初始化LED */
    usart2_init(921600);                    /* 通信串口  */
    timer6_init(10-1,7200-1);               /* 10->1ms  100->10ms */
    led0_pwm_init(100-1,7200-1);            
    
    while(rtc_init())
	{ 
	}	
    
    hmi_test_init();

    while (1)
    {
        hmi_rx_parse_task();
        if (millis() - tick_ms[0] >= 50)
        {
            tick_ms[0] = millis();
            hmi_update_ui_50ms();
        }

        if (millis() - tick_ms[1] >= 100)
        {
            tick_ms[1] = millis();
            hmi_update_ui_100ms();

        }

        if (millis() - tick_ms[2] >= 1000)
        {
            tick_ms[2] = millis();
            LED1_TOGGLE();
        }
    }
}


void TIM6_IRQHandler(void)
{ 		    		  			       
    if(__HAL_TIM_GET_IT_SOURCE(&TIM6_Handler,TIM_IT_UPDATE)==SET)
    {
        sysTickUptime++;
    }
    __HAL_TIM_CLEAR_IT(&TIM6_Handler, TIM_IT_UPDATE);
}


