/******************************************************************************
 * @file     main.c
 * @brief    Main program body
 * @version  V1.8.0
 * @date     31-Dec-2021
 *
 * @note
 * Copyright (C) 2021 Spintrol Limited Corporation. All rights reserved.
 *
 * @attention
 * THIS SOFTWARE JUST PROVIDES CUSTOMERS WITH CODING INFORMATION REGARDING
 * THEIR PRODUCTS, WHICH AIMS AT SAVING TIME FOR THEM. SPINTROL SHALL NOT BE
 * LIABLE FOR THE USE OF THE SOFTWARE. SPINTROL DOES NOT GUARANTEE THE
 * CORRECTNESS OF THIS SOFTWARE AND RESERVES THE RIGHT TO MODIFY THE SOFTWARE
 * WITHOUT NOTIFICATION.
 *
 ******************************************************************************/

#include"spc1168.h"
#include"spd1178.h"
#include<stdio.h>

uint8_t                     u8PREDRIID                        = 0;                   /* PRE-DRIVER mode ID */
uint8_t                     u8PREDRIDATA;                                            /* Data read from PRE-DRIVER */
ErrorStatus                 eErrorState;
uint32_t                    u32SSPBaundrate                   = 1000000;             /* The baundrate of SSP communicating with PRE-DRIVER mode */
uint32_t                    u32Timeout                        = 500;                 /* The number of times try to get PRE-DRIVER mode ID */


 /*************************************************************************************************************************
 *
 * @brief      This case show the usage of PRE-DRIVER OVT detection. When OVT happened, crossponding info will be printed.
 *
 *                                            PRE-DRIVER
 *                                           ------------                        ------
 *                                          |   -----    | Communicate with SSP |      |
 *                                          |  |ISP  |---|----------------------| MCU  |
 *                                          |  |MODEL|   |                      |      |
 *                                          |   -----    |                      |      |
 *                                          |   -------  |                       ------
 *                                          |  |       | |
 *                                          |  |PRE-DRI| |
 *                                          |  |SOCKET | |
 *                                          |   -------  |
 *                                           ------------
 *
 *             Key point:
 *             PRE-DRIVER init include two step,
 *                                              <1> Init the communication circuits(SSP circyits) with MCU, we call this
 *                                                  step 'Init PRE-DRIVER module'.
 *                                              <2> Init PRE-DRIVER socket itself, we call this step 'Power up PRE-DRIVER mode'.
 *
 *************************************************************************************************************************/


int main()
{
  FLASH_WALLOW();
  FLASH_SetTiming(200000000);
  /* Disable flash write access after flash operation had done */
  FLASH_WDIS();

  CLOCK_InitWithRCO(CLOCK_HCLK_200MHZ);

  Delay_Init();

  /*
   * Init the UART
   *
   * 1.Set the GPIO34/35 as UART FUNC
   *
   * 2.Enable the UART CLK
   *
   * 3.Set the rest para
   */
  GPIO_SetPinChannel(GPIO_34,GPIO34_UART_TXD);
  GPIO_SetPinChannel(GPIO_35,GPIO35_UART_RXD);
  CLOCK_EnableModule(UART_MODULE);
  UART_Init(UART,38400);
  
  /* Init PRE-DRIVER module(this step means ) and get the ID */
  eErrorState = HV_Init(u32SSPBaundrate, &u8PREDRIID, u32Timeout);
  if(eErrorState == ERROR)
  {
    printf("Init PRE-DRIVER mode FAIL\n");
    return 0;
  }
  else
  {
    printf("Init PRE-DRIVER mode SUCCESS[ID:%d]\n",u8PREDRIID);
  }
  
  /* Power up PRE-DRIVER mode(VDDG is enable at this step) */
  eErrorState = HV_WriteReg(HV_REG_PWRUPKEY, PWRUPKEY_ALL_PUKEY_(0x9));
  if(eErrorState == ERROR)
  {
    printf("Power up PRE-DRIVER FAIL\n");
    return 0;
  }
  
  /* Delay 100us to wait for the VDDG LDO ready */
  Delay_Us(100);
  
  /* Wait for PRE-DRIVER mode is power ok */
  eErrorState = HV_ReadReg(HV_REG_GLBSTS, &u8PREDRIDATA);
  if((u8PREDRIDATA & 0x3) == 0)
  {
    printf("PRE-DRIVER power on FAIL[%02x]\n",u8PREDRIDATA);
    return 0;
  }
  
  /* additional delay after VDDG ready, for final power settling */
  Delay_Us(100);
  
  /* Set the threshold for over temperature warning */
  eErrorState = HV_WriteReg(HV_REG_LDOGCTL, LDOGCTL_ALL_OTWARNTH_99_DEGREE);
  if(eErrorState == ERROR)
  {
    printf("Set OVT threshold FAIL\n");
    return 0;
  }
  
  while(1)
  {
    /* Read GLBSTS register to get the status data */
    eErrorState = HV_ReadReg(HV_REG_GLBSTS, &u8PREDRIDATA);
    if(eErrorState == ERROR)
    {
      printf("Read GLBSTS FAIL\n");
      return 0;
    }
    
    /* Check if there is OVT ERROR event happened */
    if(u8PREDRIDATA & GLBSTS_ALL_OTERROR_OCCUR)
    {
      printf("OVT ERROR event happended\n");
      /* Clear OVT ERROR status */
      eErrorState = HV_WriteReg(HV_REG_GLBSTSCLR, GLBSTSCLR_ALL_OTERROR_CLEAR);
      if(eErrorState == ERROR)
      {
        printf("Clear GLBSTS FAIL\n");
        return 0;
      }
    }
    
    /* Check if there is OVT WARNING event happened */
    if(u8PREDRIDATA & GLBSTS_ALL_OTWARN_OCCUR)
    {
      printf("OVT WARNING event happended\n");
      /* Clear OVT WARNING status */
      eErrorState = HV_WriteReg(HV_REG_GLBSTSCLR, GLBSTSCLR_ALL_OTWARN_CLEAR);
      if(eErrorState == ERROR)
      {
        printf("Clear GLBSTS FAIL\n");
        return 0;
      }
    }
    
    Delay_Ms(2000);
  }
}
