/********************************** (C) COPYRIGHT *******************************
 * File Name          : main.c
 * Author             : Wingcool
 * Version            : V1.0.0
 * Date               : 2024/01/05
 * Description        : Main program body.
 *********************************************************************************/

#include "TaskProcessCom.h"
//#include <ch32x035_usbfs_device.h>
#include "debug.h"
#include "tmos.h"

//#include "sgm452.h"
//#include "sgm832.h"
#include "uart_ht7315.h"

// void GPIOInit()
// {
//     GPIO_InitTypeDef GPIO_InitStructure = {0};
//     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
//     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
//     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
//     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
//     GPIO_Init(GPIOB, &GPIO_InitStructure);
// }

/*********************************************************************
 * @fn      IWDG_Init
 *
 * @brief   Initializes IWDG.
 *
 * @param   IWDG_Prescaler: specifies the IWDG Prescaler value.
 *            IWDG_Prescaler_4: IWDG prescaler set to 4.
 *            IWDG_Prescaler_8: IWDG prescaler set to 8.
 *            IWDG_Prescaler_16: IWDG prescaler set to 16.
 *            IWDG_Prescaler_32: IWDG prescaler set to 32.
 *            IWDG_Prescaler_64: IWDG prescaler set to 64.
 *            IWDG_Prescaler_128: IWDG prescaler set to 128.
 *            IWDG_Prescaler_256: IWDG prescaler set to 256.
 *          Reload: specifies the IWDG Reload value.
 *            This parameter must be a number between 0 and 0x0FFF.
 *
 * @return  none
 */
void IWDG_Feed_Init(u16 prer, u16 rlr)
{
    IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);
    IWDG_SetPrescaler(prer);
    IWDG_SetReload(rlr);
    IWDG_ReloadCounter();
    IWDG_Enable();
}

void GPIO_Toggle_INIT(void)
{
    GPIO_InitTypeDef GPIO_InitStructure = {0};

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOB, &GPIO_InitStructure);


    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOB, &GPIO_InitStructure);
}

int main(void)
{
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
    SystemCoreClockUpdate();
    //tmos_init();

    Delay_Init();
    //USART_Printf_Init(115200);
    USART_Printf_Init(9600);

    printf("\n[Start]: ====================>\n");
    printf("[SystemClk]: %lu\r\n", SystemCoreClock);  //defalut 96Mhz
    printf( "[ChipID]: %08x\r\n", (unsigned int)DBGMCU_GetCHIPID() );


#ifdef USBCONFIG
    //ConfigInit();
#endif
    //CheckFlag();

//**--[Uart2 Init]--//
    //Uart2_Rs232_GPIO_Init();
    //printf("1.[Uart2 Init]: Ok! \n");

//**--[Uart6710 Init]--//
    //Uart_u6710_GPIO_Init();
    //DMA_INIT();

//**--[Usb Init]--//
    //USBFS_RCC_Init( );
    //USBFS_Device_Init( ENABLE , PWR_VDD_SupplyVoltage());
    //USB_Sleep_Wakeup_CFG( );
    //printf("2.[Usb Init]: Ok! \n");

//**--[IIC Init]--//
    IICTouchInit();
    printf("3.[IIC Init]: Ok! \n");

//**--[Display Init]--//
    //OLED_Init();
    //OLED_DisplayTest();
    //printf("4.[Display Init]: Null! \n");

//**--[SGM452 Init]--//
    //sgm452_GPIO_I2cChannel_Init();
    //sgm452_T6710_Proc();
    //sgm452_T1826_Proc();
    //printf("5.[SGM452 Init]: Ok! \n");

//**--[sgm832 Init]--//
    //sgm832_GPIO_I2cChannel_Init();
    //printf("6.[SGM832 Init]: Ok! \n");

//**--[Key Init]--//
    //KeyADC_GPIO_Init();
    //KeyLed_GPIO_Init();
    //printf("7.[Key Init]: Ok! \n");

    //IWDG_Feed_Init( IWDG_Prescaler_16, 3000 );

    //UINT8 InitFlag = 0xAA;
    //USART4_SendData(&InitFlag, 1);
//**--[GG]--//

    //printf("8.[IIC_Slave Init]: Ok! \n");
    //IIC_Slave_Init( 400000, I2C1Adderss);
    //printf("[End]: <========== \n");

    while(1)
    {
        //IWDG_ReloadCounter();   //Feed dog
        //TMOS_SystemProcess();
        ms933x_media_service();
        Delay_Ms(20);
    }
}
