/**
 * @file main.c
 * @author WHXY
 * @brief
 * @version 0.1
 * @date 2021-04-22
 *
 * @copyright Copyright (c) 2021
 *
 * 代码许可和免责信息
 * 武汉芯源半导体有限公司授予您使用所有编程代码示例的非专属的版权许可，您可以由此
 * 生成根据您的特定需要而定制的相似功能。根据不能被排除的任何法定保证，武汉芯源半
 * 导体有限公司及其程序开发商和供应商对程序或技术支持（如果有）不提供任何明示或暗
 * 含的保证或条件，包括但不限于暗含的有关适销性、适用于某种特定用途和非侵权的保证
 * 或条件。
 * 无论何种情形，武汉芯源半导体有限公司及其程序开发商或供应商均不对下列各项负责，
 * 即使被告知其发生的可能性时，也是如此：数据的丢失或损坏；直接的、特别的、附带的
 * 或间接的损害，或任何后果性经济损害；或利润、业务、收入、商誉或预期可节省金额的
 * 损失。
 * 某些司法辖区不允许对直接的、附带的或后果性的损害有任何的排除或限制，因此某些或
 * 全部上述排除或限制可能并不适用于您。
 *
*******************************************************************************/

/******************************************************************************
 * Include files
 ******************************************************************************/
#include "../inc/main.h"

/******************************************************************************
 * Local pre-processor symbols/macros ('#define')
 ******************************************************************************/
#define ENTER_LOWPOWER()     do{\
                                 __RCC_UART2_CLK_DISABLE();\
                                 PD02_ANALOG_ENABLE();\
                                 PC12_ANALOG_ENABLE();\
                                 __DSB();\
                                 __WFI();}while(0)

#define EXIT_LOWPOWER()      do{\
                                 __RCC_UART2_CLK_ENABLE();\
                                 PD02_DIGTAL_ENABLE();\
                                 PC12_DIGTAL_ENABLE();}while(0)

/******************************************************************************
 * Global variable definitions (declared in header file with 'extern')
 ******************************************************************************/

/******************************************************************************
 * Local type definitions ('typedef')
 ******************************************************************************/

/******************************************************************************
 * Local function prototypes ('static')
 ******************************************************************************/
void RCC_Configuration(void);
void GPIO_Configuration(void);
void NVIC_Configuration(void);
void LCD_Configuration(void);
void LowPowerSetup(void);

/******************************************************************************
 * Local variable definitions ('static')                                      *
 ******************************************************************************/

/******************************************************************************
 * Local pre-processor symbols/macros ('#define')
 ******************************************************************************/

/*****************************************************************************
 * Function implementation - global ('extern') and local ('static')
 ******************************************************************************/
extern uint16_t Key1Count;

/**
 * @brief
 * @return int32_t
 */
int32_t main(void)
{
    RCC_Configuration();
    LogInit();
    GPIO_Configuration();
    LCD_Configuration();

    NVIC_Configuration();


    printf("\r\n");
    printf("The initialization is complete!\r\n");
    printf("The system is going to deepsleep.\r\n");
    printf("The Debug uart will be closed.\r\n");

    SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;

    while (1)
    {
        ENTER_LOWPOWER();
        EXIT_LOWPOWER();
        if(PA04_GETVALUE() == 0)
        {
            Key1Count++;
        }
        printf("The system is waked up\r\n");
        switch (Key1Count)
        {
            case 1:
                CW_LCD->RAM0 = 0x01;
                break;
            case 2:
                CW_LCD->RAM0 = 0x03;
                break;
            case 3:
                CW_LCD->RAM0 = 0x07;
                break;
            case 4:
                CW_LCD->RAM0 = 0x0f;
                break;
            case 5:
                CW_LCD->RAM0 = 0x10f;
                break;
            case 6:
                CW_LCD->RAM0 = 0x30f;
                break;
            case 7:
                CW_LCD->RAM0 = 0x70f;
                break;
            case 8:
                CW_LCD->RAM0 = 0xf0f;
                break;
            case 9:
                CW_LCD->RAM0 = 0x0f0f0f0f;
                break;
            case 10:
                CW_LCD->RAM1 = 0x000f;
                CW_LCD->RAM8 = 0x0f;
                break;
            case 11:
                CW_LCD->RAM8 = 0x0f0f0f;

                break;
            case 12:
                CW_LCD->RAM8 = 0x0f0f0f0f;
                CW_LCD->RAM9 = 0x0f;

                break;
            case 13:
                CW_LCD->RAM9 = 0x0f0f0f;
                break;
            case 14:
                CW_LCD->RAM9 = 0x0f0f0f0f;
                CW_LCD->RAM1 = 0x0f0f;
                break;
            case 15:
                CW_LCD->RAM1 = 0x0f0f0f0f;
                break;
            case 16:
                CW_LCD->RAM0 = 0;
                CW_LCD->RAM1 = 0;
                CW_LCD->RAM8 = 0;
                CW_LCD->RAM9 = 0;
                Key1Count = 0;
                break;
        }
    }
}


void LowPowerSetup(void)
{

}

/**
  * @brief  Configures the different system clocks.
  * @param  None
  * @retval None
  */
void RCC_Configuration(void)
{
    /* 0. HSI使能并校准 */
//  RCC_HSI_OUT();
    RCC_HSI_Enable(RCC_HSIOSC_DIV6);

    /* 1. 设置HCLK和PCLK的分频系数　*/
    RCC_HCLKPRS_Config(RCC_HCLK_DIV1);
    RCC_PCLKPRS_Config(RCC_PCLK_DIV1);

    RCC_SystemCoreClockUpdate(8000000);

    /* 4. 配置外设时钟 */
//  __RCC_GPIOF_CLK_ENABLE();
//  __RCC_GPIOE_CLK_ENABLE();
//  __RCC_GPIOD_CLK_ENABLE();
//  __RCC_GPIOC_CLK_ENABLE();
//  __RCC_GPIOB_CLK_ENABLE();
//  __RCC_GPIOA_CLK_ENABLE();
//  __RCC_LCD_CLK_ENABLE();

}

/**
  * @brief  Configure the GPIO Pins.
  * @param  None
  * @retval None
  */
void GPIO_Configuration(void)
{
    GPIO_InitTypeDef GPIO_InitStruct = {0};

    /* 配置PA4为按键输入 */

    GPIO_InitStruct.IT = GPIO_IT_FALLING;
    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
    GPIO_InitStruct.Pins = GPIO_PIN_4;

    __RCC_GPIOA_CLK_ENABLE();
    GPIO_Init(CW_GPIOA, &GPIO_InitStruct);
}

/**
  * @brief  Configure the nested vectored interrupt controller.
  * @param  None
  * @retval None
  */
void NVIC_Configuration(void)
{
    __disable_irq();
//  NVIC_EnableIRQ(WDT_IRQn);
    NVIC_SetPriority(GPIOA_IRQn, 0);
//  NVIC_SetPriority(SysTick_IRQn, 1);
    NVIC_EnableIRQ(GPIOA_IRQn);
    __enable_irq();
}


void LCD_Configuration(void)
{
    LCD_InitTypeDef LCD_InitStruct = {0};

    LCD_InitStruct.LCD_Bias = LCD_Bias_1_3;
    LCD_InitStruct.LCD_ClockSource = LCD_CLOCK_SOURCE_LSI;
    LCD_InitStruct.LCD_Duty = LCD_Duty_1_4;
    LCD_InitStruct.LCD_ScanFreq = LCD_SCAN_FREQ_256HZ;
    LCD_InitStruct.LCD_VoltageSource = LCD_VoltageSource_Internal;

    __RCC_LCD_CLK_ENABLE();
    RCC_LSI_Enable();
    LCD_Init(&LCD_InitStruct);     //基本配置
    // BTL004 LCD 对应的连接
//     PA12 COM3
//     PA11 COM2
//     PA10 COM1
//     PA09 COM0
//     PA08 SEG0
//     PC09 SEG1
//     PC08 SEG2
//     PC07 SEG3
//     PC06 SEG4
//     PB15 SEG5
//     PB14 SEG6
//     PB13 SEG7
//     PB12 SEG8
//     PB11 SEG9
//     PB10 SEG10           
//     PC05 SEG13
//     PC04 SEG14
//     PA07 SEG15
//     PA06 SEG16
//     PA03 SEG17
    // 分配引脚
    LCD_COMConfig(LCD_COM0 | LCD_COM1 | LCD_COM2 | LCD_COM3, ENABLE);
    LCD_SEG0to23Config(0x0003E7FF, ENABLE);
    



    CW_LCD->RAM[0] = 0;
    CW_LCD->RAM[1] = 0;
    CW_LCD->RAM2 = 0;
    CW_LCD->RAM3 = 0;
    CW_LCD->RAM4 = 0;
    CW_LCD->RAM5 = 0;
    CW_LCD->RAM6 = 0;
    CW_LCD->RAM7 = 0;
    CW_LCD->RAM8 = 0;
    CW_LCD->RAM9 = 0;
    CW_LCD->RAM10 = 0;
    CW_LCD->RAM11 = 0;
    CW_LCD->RAM12 = 0;
    CW_LCD->RAM13 = 0;

    LCD_Cmd(ENABLE);
    CW_LCD->RAM0 = 0x0f0f;
    LCD_ContrastConfig(LCD_Contrast_Level_2);
    LCD_DriveVoltageConfig(LCD_INRS_LEVEL_0);
}


/******************************************************************************
 * EOF (not truncated)
 ******************************************************************************/
#ifdef USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
    /* USER CODE BEGIN 6 */
    /* User can add his own implementation to report the file name and line number,
       tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
    /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
