/**************************************************************************//**
 * @file     gatedrv_hw.c
 * @brief    This file provides GATEDRV high-level firmware functions.
 * @version  VXX.XX.XX
 * @date     XX-XXX-XXXX
 *
 * @note
 * Copyright (C) 2022 Spintrol Electronic Technology (Shanghai) Co., Ltd.. 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 "gatedrv_hw.h"




/****************************************************************************//**
 * @brief      Enable Gate
 *
 * @param[in]  none
 *
 * @return     none
 *
 *******************************************************************************/
void GATEDRV_EnableGate(void)
{
  GPIO_WritePin(GPIO_GATEDRV_EN, GPIO_LEVEL_HIGH);
}




/****************************************************************************//**
 * @brief      Disable Gate
 *
 * @param[in]  none
 *
 * @return     none
 *
 *******************************************************************************/
void GATEDRV_DisableGate(void)
{
  GPIO_WritePin(GPIO_GATEDRV_EN, GPIO_LEVEL_LOW);
}




/****************************************************************************//**
 * @brief      Enable selected signals as Fatal Fault
 *
 * @param[in]  u16EnableFlags :  Selected signal flags
 * @param[out] pu16ResultFlags:  Pointer to the signal result flags
 *
 * @return     ERROR or SUCCESS
 *
 *******************************************************************************/
ErrorStatus GATEDRV_EnableFatalFault(uint16_t u16EnableFlags, uint16_t *pu16ResultFlags)
{
  ErrorStatus error = SUCCESS;
  uint8_t u8EnableStatus = 0;
  
  error = GATEDRV_ReadREG(GATEDRV_REG_FFAULTSEL0_ADDR, &u8EnableStatus);
  if(error == ERROR) return ERROR;
  error = GATEDRV_WriteREG(GATEDRV_REG_FFAULTSEL0_ADDR,(uint8_t)(u16EnableFlags & 0xFF) | u8EnableStatus);
  if(error == ERROR) return ERROR;
  
  GATEDRV_ReadREG(GATEDRV_REG_FFAULTSEL1_ADDR, &u8EnableStatus);
  if(error == ERROR) return ERROR;
  GATEDRV_WriteREG(GATEDRV_REG_FFAULTSEL1_ADDR,(uint8_t)(u16EnableFlags >> 8) | u8EnableStatus);
  if(error == ERROR) return ERROR;
  
  /* Read register value back */
  GATEDRV_ReadREG(GATEDRV_REG_FFAULTSEL1_ADDR, &u8EnableStatus);
  if(error == ERROR) return ERROR;
  *pu16ResultFlags = u8EnableStatus;
  *pu16ResultFlags <<= 8;
  
  GATEDRV_ReadREG(GATEDRV_REG_FFAULTSEL0_ADDR, &u8EnableStatus);
  if(error == ERROR) return ERROR;
  *pu16ResultFlags |= u8EnableStatus;
  
  return SUCCESS;
}




/****************************************************************************//**
 * @brief      Disable selected signals as Fatal Fault
 *
 * @param[in]  u16EnableFlags :  Selected signal flags
 * @param[out] pu16ResultFlags:  Pointer to the signal result flags
 *
 * @return     ERROR or SUCCESS
 *
 *******************************************************************************/
ErrorStatus GATEDRV_DisableFatalFault(uint16_t u16DisableFlags, uint16_t *pu16ResultFlags)
{
  ErrorStatus error = SUCCESS;
  uint8_t u8EnableStatus = 0;
  
  error = GATEDRV_ReadREG(GATEDRV_REG_FFAULTSEL0_ADDR, &u8EnableStatus);
  if(error == ERROR) return ERROR;
  error = GATEDRV_WriteREG(GATEDRV_REG_FFAULTSEL0_ADDR,(uint8_t)((~u16DisableFlags) & 0xFF) & u8EnableStatus);
  if(error == ERROR) return ERROR;

  error = GATEDRV_ReadREG(GATEDRV_REG_FFAULTSEL1_ADDR, &u8EnableStatus);
  if(error == ERROR) return ERROR;
  error = GATEDRV_WriteREG(GATEDRV_REG_FFAULTSEL1_ADDR,(uint8_t)((~u16DisableFlags) >> 8) & u8EnableStatus);
  if(error == ERROR) return ERROR;
  
  /* Read register value back */
  error = GATEDRV_ReadREG(GATEDRV_REG_FFAULTSEL1_ADDR, &u8EnableStatus);
  if(error == ERROR) return ERROR;
  *pu16ResultFlags = u8EnableStatus;
  *pu16ResultFlags <<= 8;
  
  error = GATEDRV_ReadREG(GATEDRV_REG_FFAULTSEL0_ADDR, &u8EnableStatus);
  if(error == ERROR) return ERROR;
  *pu16ResultFlags |= u8EnableStatus;
  
  return SUCCESS;
}




/****************************************************************************//**
 * @brief      Enable selected signals as General Fault
 *
 * @param[in]  u16EnableFlags :  Selected signal flags
 * @param[out] pu16ResultFlags:  Pointer to the signal result flags
 *
 * @return     ERROR or SUCCESS
 *
 *******************************************************************************/
ErrorStatus GATEDRV_EnableGeneralFault(uint16_t u16EnableFlags, uint16_t *pu16ResultFlags)
{
  ErrorStatus error = SUCCESS;
  uint8_t u8EnableStatus = 0;
  
  error = GATEDRV_ReadREG(GATEDRV_REG_GFAULTSEL0_ADDR, &u8EnableStatus);
  if(error == ERROR) return ERROR;
  error = GATEDRV_WriteREG(GATEDRV_REG_GFAULTSEL0_ADDR,(uint8_t)(u16EnableFlags & 0xFF) | u8EnableStatus);
  if(error == ERROR) return ERROR;

  error = GATEDRV_ReadREG(GATEDRV_REG_GFAULTSEL1_ADDR, &u8EnableStatus);
  if(error == ERROR) return ERROR;
  error = GATEDRV_WriteREG(GATEDRV_REG_GFAULTSEL1_ADDR,(uint8_t)(u16EnableFlags >> 8) | u8EnableStatus);
  if(error == ERROR) return ERROR;
  
  /* Read register value back */
  error = GATEDRV_ReadREG(GATEDRV_REG_GFAULTSEL1_ADDR, &u8EnableStatus);
  if(error == ERROR) return ERROR;
  *pu16ResultFlags = u8EnableStatus;
  *pu16ResultFlags <<= 8;
  
  error = GATEDRV_ReadREG(GATEDRV_REG_GFAULTSEL0_ADDR, &u8EnableStatus);
  if(error == ERROR) return ERROR;
  *pu16ResultFlags |= u8EnableStatus;
  
  return SUCCESS;
}




/****************************************************************************//**
 * @brief      Disable selected signals as General Fault
 *
 * @param[in]  u16EnableFlags :  Selected signal flags
 * @param[out] pu16ResultFlags:  Pointer to the signal result flags
 *
 * @return     ERROR or SUCCESS
 *
 *******************************************************************************/
ErrorStatus GATEDRV_DisableGeneralFault(uint16_t u16DisableFlags, uint16_t *pu16ResultFlags)
{
  ErrorStatus error = SUCCESS;
  uint8_t u8EnableStatus = 0;
  
  error = GATEDRV_ReadREG(GATEDRV_REG_GFAULTSEL0_ADDR, &u8EnableStatus);
  if(error == ERROR) return ERROR;
  error = GATEDRV_WriteREG(GATEDRV_REG_GFAULTSEL0_ADDR,(uint8_t)((~u16DisableFlags) & 0xFF) & u8EnableStatus);
  if(error == ERROR) return ERROR;

  error = GATEDRV_ReadREG(GATEDRV_REG_GFAULTSEL1_ADDR, &u8EnableStatus);
  if(error == ERROR) return ERROR;
  error = GATEDRV_WriteREG(GATEDRV_REG_GFAULTSEL1_ADDR,(uint8_t)((~u16DisableFlags) >> 8) & u8EnableStatus);
  if(error == ERROR) return ERROR;
  
  /* Read register value back */
  error = GATEDRV_ReadREG(GATEDRV_REG_GFAULTSEL1_ADDR, &u8EnableStatus);
  if(error == ERROR) return ERROR;
  *pu16ResultFlags = u8EnableStatus;
  *pu16ResultFlags <<= 8;
  
  error = GATEDRV_ReadREG(GATEDRV_REG_GFAULTSEL0_ADDR, &u8EnableStatus);
  if(error == ERROR) return ERROR;
  *pu16ResultFlags |= u8EnableStatus;
  
  return SUCCESS;
}




/****************************************************************************//**
 * @brief      Get Fault flag value
 *
 * @param[in]  none
 * @param[out] pu32ResultFlags:  Pointer to the signal result flags
 *
 * @return     ERROR or SUCCESS
 *
 *******************************************************************************/
ErrorStatus GATEDRV_GetFaultFlag(uint32_t *pu32ResultFlags)
{
  ErrorStatus error = SUCCESS;
  uint8_t u8FaultFlags = 0;
  
  error = GATEDRV_ReadREG(GATEDRV_REG_GLBSTS1_ADDR, &u8FaultFlags);
  if(error == ERROR) return ERROR;
  *pu32ResultFlags = (uint32_t)(u8FaultFlags & 0x7F);
  
  error = GATEDRV_ReadREG(GATEDRV_REG_FAULTSTS0_ADDR, &u8FaultFlags);
  if(error == ERROR) return ERROR;
  *pu32ResultFlags |= (uint32_t)(u8FaultFlags) << 8;
  
  error = GATEDRV_ReadREG(GATEDRV_REG_FAULTSTS1_ADDR, &u8FaultFlags);
  if(error == ERROR) return ERROR;
  *pu32ResultFlags |= (uint32_t)(u8FaultFlags) << 16;

  return SUCCESS;
}




/****************************************************************************//**
 * @brief      Clear specified fault flags
 *
 * @param[in]  u16ClearFlags:  Selected fault flags
 *
 * @return     ERROR or SUCCESS
 *
 *******************************************************************************/
ErrorStatus GATEDRV_ClearFaultFlag(uint16_t u16ClearFlags)
{
  ErrorStatus error = SUCCESS;
  
  error = GATEDRV_WriteREG(GATEDRV_REG_FAULTCLR0_ADDR, (uint8_t)(u16ClearFlags & 0xFF));
  if(error == ERROR) return ERROR;

  return GATEDRV_WriteREG(GATEDRV_REG_FAULTCLR1_ADDR, (uint8_t)(u16ClearFlags >> 8));
}




/****************************************************************************//**
 * @brief      Set VDDG voltage
 *
 * @param[in]  eVDDGVal:  VDDG value
 *
 * @return     ERROR or SUCCESS
 *
 *******************************************************************************/
ErrorStatus GATEDRV_SetVDDG(GATEDRV_VDDG_VoltageEnum eVDDGVal)
{
  ErrorStatus error = SUCCESS;
  uint8_t u8Val = 0;
  
  error = GATEDRV_ReadREG(GATEDRV_REG_LDOGCTL_ADDR, &u8Val);
  if(error == ERROR) return ERROR;
  
  /* Clear Old Setting */
  u8Val &= (uint8_t)(~LDOGCTL_ALL_VREFSEL_Msk);
  /* Set New Setting */
  u8Val |= (uint8_t)(eVDDGVal << LDOGCTL_ALL_VREFSEL_Pos);
  
  return GATEDRV_WriteREG(GATEDRV_REG_LDOGCTL_ADDR, u8Val);
}




/****************************************************************************//**
 * @brief      Set Charge Pump Frequency
 *
 * @param[in]  eCPFreqVal:  Charge Pump Frequency value
 *
 * @return     ERROR or SUCCESS
 *
 *******************************************************************************/
ErrorStatus GATEDRV_SetChargePumpFrequency(GATEDRV_CP_FrequecnyEnum eCPFreqVal)
{
  ErrorStatus error = SUCCESS;
  uint8_t u8Val = 0;
  
  error = GATEDRV_ReadREG(GATEDRV_REG_CPCTL_ADDR, &u8Val);
  if(error == ERROR) return ERROR;
  
  /* Clear Old Setting */
  u8Val &= (uint8_t)(~CPCTL_ALL_CLKDIV_Msk);
  /* Set New Setting */
  u8Val |= (uint8_t)(eCPFreqVal << CPCTL_ALL_CLKDIV_Pos);
  
  return GATEDRV_WriteREG(GATEDRV_REG_CPCTL_ADDR, u8Val);
}




/****************************************************************************//**
 * @brief      Enable BOD for VDDG
 *
 * @param[in]  none
 *
 * @return     ERROR or SUCCESS
 *
 *******************************************************************************/
ErrorStatus GATEDRV_EnableVDDGBOD(void)
{
  ErrorStatus error = SUCCESS;
  uint8_t u8Val = 0;
  
  error = GATEDRV_ReadREG(GATEDRV_REG_BODCTL_ADDR, &u8Val);
  if(error == ERROR) return ERROR;
  
  /* Clear Old Setting */
  u8Val &= (uint8_t)(~BODCTL_ALL_VDDGBODEN_Msk);
  /* Set New Setting */
  u8Val |= (uint8_t)(BODCTL_ALL_VDDGBODEN_ENABLE);
  
  return GATEDRV_WriteREG(GATEDRV_REG_BODCTL_ADDR, u8Val);
}




/****************************************************************************//**
 * @brief      Disable BOD for VDDG
 *
 * @param[in]  none
 *
 * @return     ERROR or SUCCESS
 *
 *******************************************************************************/
ErrorStatus GATEDRV_DisableVDDGBOD(void)
{
  ErrorStatus error = SUCCESS;
  uint8_t u8Val = 0;
  
  error = GATEDRV_ReadREG(GATEDRV_REG_BODCTL_ADDR, &u8Val);
  if(error == ERROR) return ERROR;
  
  /* Clear Old Setting */
  u8Val &= (uint8_t)(~BODCTL_ALL_VDDGBODEN_Msk);
  /* Set New Setting */
  u8Val |= (uint8_t)(BODCTL_ALL_VDDGBODEN_DISABLE);
  
  return GATEDRV_WriteREG(GATEDRV_REG_BODCTL_ADDR, u8Val);
}




/****************************************************************************//**
 * @brief      Enable BOD for PVDD0
 *
 * @param[in]  none
 *
 * @return     ERROR or SUCCESS
 *
 *******************************************************************************/
ErrorStatus GATEDRV_EnablePVDD0BOD(void)
{
  ErrorStatus error = SUCCESS;
  uint8_t u8Val = 0;
  
  error = GATEDRV_ReadREG(GATEDRV_REG_BODCTL_ADDR, &u8Val);
  if(error == ERROR) return ERROR;
  
  /* Clear Old Setting */
  u8Val &= (uint8_t)(~BODCTL_ALL_PVDD0BODEN_Msk);
  /* Set New Setting */
  u8Val |= (uint8_t)(BODCTL_ALL_PVDD0BODEN_ENABLE);
  
  return GATEDRV_WriteREG(GATEDRV_REG_BODCTL_ADDR, u8Val);
}




/****************************************************************************//**
 * @brief      Enable BOD for PVDD0
 *
 * @param[in]  none
 *
 * @return     ERROR or SUCCESS
 *
 *******************************************************************************/
ErrorStatus GATEDRV_DisablePVDD0BOD(void)
{
  ErrorStatus error = SUCCESS;
  uint8_t u8Val = 0;
  
  error = GATEDRV_ReadREG(GATEDRV_REG_BODCTL_ADDR, &u8Val);
  if(error == ERROR) return ERROR;
  
  /* Clear Old Setting */
  u8Val &= (uint8_t)(~BODCTL_ALL_PVDD0BODEN_Msk);
  /* Set New Setting */
  u8Val |= (uint8_t)(BODCTL_ALL_PVDD0BODEN_DISABLE);
  
  return GATEDRV_WriteREG(GATEDRV_REG_BODCTL_ADDR, u8Val);
}




/****************************************************************************//**
 * @brief      Set BOD threshold for VDDG TOO HIGH to toggle from 0 to 1
 *
 * @param[in]  eBODTriggerVal:  Threshold value
 *
 * @return     ERROR or SUCCESS
 *
 *******************************************************************************/
ErrorStatus GATEDRV_SetVDDGTooHighTriggerThreshold(GATEDRV_VDDG_BODThresholdEnum eBODTriggerVal)
{
  ErrorStatus error = SUCCESS;
  uint8_t u8Val = 0;
  
  error = GATEDRV_ReadREG(GATEDRV_REG_BODVDDGHVTH_ADDR, &u8Val);
  if(error == ERROR) return ERROR;
  
  /* Clear Old Setting */
  u8Val &= (uint8_t)(~BODVDDGHVTH_ALL_L2H_Msk);
  /* Set New Setting */
  u8Val |= (uint8_t)(eBODTriggerVal << BODVDDGHVTH_ALL_L2H_Pos);
  
  return GATEDRV_WriteREG(GATEDRV_REG_BODVDDGHVTH_ADDR, u8Val);
}




/****************************************************************************//**
 * @brief      Set BOD threshold for VDDG TOO HIGH to toggle from 1 to 0
 *
 * @param[in]  eBODTriggerVal:  Threshold value
 *
 * @return     ERROR or SUCCESS
 *
 *******************************************************************************/
ErrorStatus GATEDRV_SetVDDGTooHighReleaseThreshold(GATEDRV_VDDG_BODThresholdEnum eBODTriggerVal)
{
  ErrorStatus error = SUCCESS;
  uint8_t u8Val = 0;
  
  error = GATEDRV_ReadREG(GATEDRV_REG_BODVDDGHVTH_ADDR, &u8Val);
  if(error == ERROR) return ERROR;
  
  /* Clear Old Setting */
  u8Val &= (uint8_t)(~BODVDDGHVTH_ALL_H2L_Msk);
  /* Set New Setting */
  u8Val |= (uint8_t)(eBODTriggerVal << BODVDDGHVTH_ALL_H2L_Pos);
  
  return GATEDRV_WriteREG(GATEDRV_REG_BODVDDGHVTH_ADDR, u8Val);
}




/****************************************************************************//**
 * @brief      Set BOD threshold for VDDG TOO LOW to toggle from 0 to 1
 *
 * @param[in]  eBODTriggerVal:  Threshold value
 *
 * @return     ERROR or SUCCESS
 *
 *******************************************************************************/
ErrorStatus GATEDRV_SetVDDGTooLowTriggerThreshold(GATEDRV_VDDG_BODThresholdEnum eBODTriggerVal)
{
  ErrorStatus error = SUCCESS;
  uint8_t u8Val = 0;
  
  error = GATEDRV_ReadREG(GATEDRV_REG_BODVDDGLVTH_ADDR, &u8Val);
  if(error == ERROR) return ERROR;
  
  /* Clear Old Setting */
  u8Val &= (uint8_t)(~BODVDDGLVTH_ALL_L2H_Msk);
  /* Set New Setting */
  u8Val |= (uint8_t)(eBODTriggerVal << BODVDDGLVTH_ALL_L2H_Pos);
  
  return GATEDRV_WriteREG(GATEDRV_REG_BODVDDGLVTH_ADDR, u8Val);
}




/****************************************************************************//**
 * @brief      Set BOD threshold for VDDG TOO LOW to toggle from 1 to 0
 *
 * @param[in]  eBODTriggerVal:  Threshold value
 *
 * @return     ERROR or SUCCESS
 *
 *******************************************************************************/
ErrorStatus GATEDRV_SetVDDGTooLowReleaseThreshold(GATEDRV_VDDG_BODThresholdEnum eBODTriggerVal)
{
  ErrorStatus error = SUCCESS;
  uint8_t u8Val = 0;
  
  error = GATEDRV_ReadREG(GATEDRV_REG_BODVDDGLVTH_ADDR, &u8Val);
  if(error == ERROR) return ERROR;
  
  /* Clear Old Setting */
  u8Val &= (uint8_t)(~BODVDDGLVTH_ALL_H2L_Msk);
  /* Set New Setting */
  u8Val |= (uint8_t)(eBODTriggerVal << BODVDDGLVTH_ALL_H2L_Pos);
  
  return GATEDRV_WriteREG(GATEDRV_REG_BODVDDGLVTH_ADDR, u8Val);
}




/****************************************************************************//**
 * @brief      Set BOD threshold for PVDD0 TOO HIGH to toggle from 0 to 1
 *
 * @param[in]  eBODTriggerVal:  Threshold value
 *
 * @return     ERROR or SUCCESS
 *
 *******************************************************************************/
ErrorStatus GATEDRV_SetPVDD0TooHighTriggerThreshold(GATEDRV_PVDD0_BODThresholdEnum eBODTriggerVal)
{
  ErrorStatus error = SUCCESS;
  uint8_t u8Val = 0;
  
  error = GATEDRV_ReadREG(GATEDRV_REG_BODPVDD0HVTH_ADDR, &u8Val);
  if(error == ERROR) return ERROR;
  
  /* Clear Old Setting */
  u8Val &= (uint8_t)(~BODPVDD0HVTH_ALL_L2H_Msk);
  /* Set New Setting */
  u8Val |= (uint8_t)(eBODTriggerVal << BODPVDD0HVTH_ALL_L2H_Pos);
  
  return GATEDRV_WriteREG(GATEDRV_REG_BODPVDD0HVTH_ADDR, u8Val);
}




/****************************************************************************//**
 * @brief      Set BOD threshold for PVDD0 TOO HIGH to toggle from 1 to 0
 *
 * @param[in]  eBODTriggerVal:  Threshold value
 *
 * @return     ERROR or SUCCESS
 *
 *******************************************************************************/
ErrorStatus GATEDRV_SetPVDD0TooHighReleaseThreshold(GATEDRV_PVDD0_BODThresholdEnum eBODTriggerVal)
{
  ErrorStatus error = SUCCESS;
  uint8_t u8Val = 0;
  
  error = GATEDRV_ReadREG(GATEDRV_REG_BODPVDD0HVTH_ADDR, &u8Val);
  if(error == ERROR) return ERROR;
  
  /* Clear Old Setting */
  u8Val &= (uint8_t)(~BODPVDD0HVTH_ALL_H2L_Msk);
  /* Set New Setting */
  u8Val |= (uint8_t)(eBODTriggerVal << BODPVDD0HVTH_ALL_H2L_Pos);
  
  return GATEDRV_WriteREG(GATEDRV_REG_BODPVDD0HVTH_ADDR, u8Val);
}




/****************************************************************************//**
 * @brief      Set BOD threshold for PVDD0 TOO LOW to toggle from 0 to 1
 *
 * @param[in]  eBODTriggerVal:  Threshold value
 *
 * @return     ERROR or SUCCESS
 *
 *******************************************************************************/
ErrorStatus GATEDRV_SetPVDD0TooLowTriggerThreshold(GATEDRV_PVDD0_BODThresholdEnum eBODTriggerVal)
{
  ErrorStatus error = SUCCESS;
  uint8_t u8Val = 0;
  
  error = GATEDRV_ReadREG(GATEDRV_REG_BODPVDD0LVTH_ADDR, &u8Val);
  if(error == ERROR) return ERROR;
  
  /* Clear Old Setting */
  u8Val &= (uint8_t)(~BODPVDD0LVTH_ALL_L2H_Msk);
  /* Set New Setting */
  u8Val |= (uint8_t)(eBODTriggerVal << BODPVDD0LVTH_ALL_L2H_Pos);
  
  return GATEDRV_WriteREG(GATEDRV_REG_BODPVDD0LVTH_ADDR, u8Val);
}




/****************************************************************************//**
 * @brief      Set BOD threshold for PVDD0 TOO LOW to toggle from 1 to 0
 *
 * @param[in]  eBODTriggerVal:  Threshold value
 *
 * @return     ERROR or SUCCESS
 *
 *******************************************************************************/
ErrorStatus GATEDRV_SetPVDD0TooLowReleaseThreshold(GATEDRV_PVDD0_BODThresholdEnum eBODTriggerVal)
{
  ErrorStatus error = SUCCESS;
  uint8_t u8Val = 0;
  
  error = GATEDRV_ReadREG(GATEDRV_REG_BODPVDD0LVTH_ADDR, &u8Val);
  if(error == ERROR) return ERROR;
  
  /* Clear Old Setting */
  u8Val &= (uint8_t)(~BODPVDD0LVTH_ALL_H2L_Msk);
  /* Set New Setting */
  u8Val |= (uint8_t)(eBODTriggerVal << BODPVDD0LVTH_ALL_H2L_Pos);
  
  return GATEDRV_WriteREG(GATEDRV_REG_BODPVDD0LVTH_ADDR, u8Val);
}




/****************************************************************************//**
 * @brief      Set Pre-Driver Over Temperature Threshold
 *
 * @param[in]  eTempratureVal:  Temprature Threshold value
 *
 * @return     ERROR or SUCCESS
 *
 *******************************************************************************/
ErrorStatus GATEDRV_SetPreDriverOverTemperatureThreshold(GATEDRV_TemperatureThresholdEnum eTempratureVal)
{
  ErrorStatus error = SUCCESS;
  uint8_t u8Val = 0;
  
  error = GATEDRV_ReadREG(GATEDRV_REG_LDOGCTL_ADDR, &u8Val);
  if(error == ERROR) return ERROR;
  
  /* Clear Old Setting */
  u8Val &= (uint8_t)(~LDOGCTL_ALL_VOTREFSEL_Msk);
  /* Set New Setting */
  u8Val |= (uint8_t)(eTempratureVal << LDOGCTL_ALL_VOTREFSEL_Pos);
  
  return GATEDRV_WriteREG(GATEDRV_REG_LDOGCTL_ADDR, u8Val);
}




/****************************************************************************//**
 * @brief      Set Buck Over Temperature Threshold
 *
 * @param[in]  eTempratureVal:  Temprature Threshold value
 *
 * @return     ERROR or SUCCESS
 *
 *******************************************************************************/
ErrorStatus GATEDRV_SetBuckOverTemperatureThreshold(GATEDRV_TemperatureThresholdEnum eTempratureVal)
{
  ErrorStatus error = SUCCESS;
  uint8_t u8Val = 0;
  
  error = GATEDRV_ReadREG(GATEDRV_REG_BUCKCTL0_ADDR, &u8Val);
  if(error == ERROR) return ERROR;
  
  /* Clear Old Setting */
  u8Val &= (uint8_t)(~BUCKCTL0_ALL_VOTREFSEL_Msk);
  /* Set New Setting */
  u8Val |= (uint8_t)(eTempratureVal << BUCKCTL0_ALL_VOTREFSEL_Pos);
  
  return GATEDRV_WriteREG(GATEDRV_REG_BUCKCTL0_ADDR, u8Val);
}




/****************************************************************************//**
 * @brief      Enable Buck DC-DC
 *
 * @param[in]  none
 *
 * @return     ERROR or SUCCESS
 *
 *******************************************************************************/
ErrorStatus GATEDRV_EnableBuck(void)
{
  ErrorStatus error = SUCCESS;
  uint8_t u8Val = 0;
  
  error = GATEDRV_ReadREG(GATEDRV_REG_BUCKCTL1_ADDR, &u8Val);
  if(error == ERROR) return ERROR;
  
  /* Clear Old Setting */
  u8Val &= (uint8_t)(~BUCKCTL1_ALL_DCDCDISABLE_Msk);
  /* Set New Setting */
  u8Val |= (uint8_t)(BUCKCTL1_ALL_DCDCDISABLE_ENABLE_DCDC);
  
  return GATEDRV_WriteREG(GATEDRV_REG_BUCKCTL1_ADDR, u8Val);
}




/****************************************************************************//**
 * @brief      Disable Buck DC-DC
 *
 * @param[in]  none
 *
 * @return     ERROR or SUCCESS
 *
 *******************************************************************************/
ErrorStatus GATEDRV_DisableBuck(void)
{
  ErrorStatus error = SUCCESS;
  uint8_t u8Val = 0;
  
  error = GATEDRV_ReadREG(GATEDRV_REG_BUCKCTL1_ADDR, &u8Val);
  if(error == ERROR) return ERROR;
  
  /* Clear Old Setting */
  u8Val &= (uint8_t)(~BUCKCTL1_ALL_DCDCDISABLE_Msk);
  /* Set New Setting */
  u8Val |= (uint8_t)(BUCKCTL1_ALL_DCDCDISABLE_DISABLE_DCDC);
  
  return GATEDRV_WriteREG(GATEDRV_REG_BUCKCTL1_ADDR, u8Val);
}




/****************************************************************************//**
 * @brief      Enable Buck Force-into-PWM mode
 *
 * @param[in]  none
 *
 * @return     ERROR or SUCCESS
 *
 *******************************************************************************/
ErrorStatus GATEDRV_EnableBuckForcePWM(void)
{
  ErrorStatus error = SUCCESS;
  uint8_t u8Val = 0;
  
  error = GATEDRV_ReadREG(GATEDRV_REG_BUCKCTL1_ADDR, &u8Val);
  if(error == ERROR) return ERROR;
  
  /* Clear Old Setting */
  u8Val &= (uint8_t)(~BUCKCTL1_ALL_FRCPWM_Msk);
  /* Set New Setting */
  u8Val |= (uint8_t)(BUCKCTL1_ALL_FRCPWM_FORCE);
  
  return GATEDRV_WriteREG(GATEDRV_REG_BUCKCTL1_ADDR, u8Val);
}




/****************************************************************************//**
 * @brief      Disable Buck Force-into-PWM mode
 *
 * @param[in]  none
 *
 * @return     ERROR or SUCCESS
 *
 *******************************************************************************/
ErrorStatus GATEDRV_DisableBuckForcePWM(void)
{
  ErrorStatus error = SUCCESS;
  uint8_t u8Val = 0;
  
  error = GATEDRV_ReadREG(GATEDRV_REG_BUCKCTL1_ADDR, &u8Val);
  if(error == ERROR) return ERROR;
  
  /* Clear Old Setting */
  u8Val &= (uint8_t)(~BUCKCTL1_ALL_FRCPWM_Msk);
  /* Set New Setting */
  u8Val |= (uint8_t)(BUCKCTL1_ALL_FRCPWM_RELEASE);
  
  return GATEDRV_WriteREG(GATEDRV_REG_BUCKCTL1_ADDR, u8Val);
}




/****************************************************************************//**
 * @brief      Enable Pre-Driver output
 *
 * @param[in]  none
 *
 * @return     ERROR or SUCCESS
 *
 *******************************************************************************/
ErrorStatus GATEDRV_EnablePreDriverOutput(void)
{
  ErrorStatus error = SUCCESS;
  uint8_t u8Val = 0;
  
  error = GATEDRV_ReadREG(GATEDRV_REG_PREDRVCTL1_ADDR, &u8Val);
  if(error == ERROR) return ERROR;
  
  /* Clear Old Setting */
  u8Val &= (uint8_t)(~PREDRVCTL1_ALL_OE_Msk);
  /* Set New Setting */
  u8Val |= (uint8_t)(PREDRVCTL1_ALL_OE_OUTPUT_ENABLE);
  
  return GATEDRV_WriteREG(GATEDRV_REG_PREDRVCTL1_ADDR, u8Val);
}




/****************************************************************************//**
 * @brief      Disable Pre-Driver output
 *
 * @param[in]  none
 *
 * @return     ERROR or SUCCESS
 *
 *******************************************************************************/
ErrorStatus GATEDRV_DisablePreDriverOutput(void)
{
  ErrorStatus error = SUCCESS;
  uint8_t u8Val = 0;
  
  error = GATEDRV_ReadREG(GATEDRV_REG_PREDRVCTL1_ADDR, &u8Val);
  if(error == ERROR) return ERROR;
  
  /* Clear Old Setting */
  u8Val &= (uint8_t)(~PREDRVCTL1_ALL_OE_Msk);
  /* Set New Setting */
  u8Val |= (uint8_t)(PREDRVCTL1_ALL_OE_OUTPUT_DISABLE);
  
  return GATEDRV_WriteREG(GATEDRV_REG_PREDRVCTL1_ADDR, u8Val);
}




/****************************************************************************//**
 * @brief      Set Pre-Driver Slew Rate
 *
 * @param[in]  eSlewRate:  Slew Rate value
 *
 * @return     ERROR or SUCCESS
 *
 *******************************************************************************/
ErrorStatus GATEDRV_SetPreDriverSlewRate(GATEDRV_SlewRateEnum eSlewRate)
{
  ErrorStatus error = SUCCESS;
  uint8_t u8Val = 0;
  
  error = GATEDRV_ReadREG(GATEDRV_REG_PREDRVCTL1_ADDR, &u8Val);
  if(error == ERROR) return ERROR;
  
  /* Clear Old Setting */
  u8Val &= (uint8_t)(~PREDRVCTL1_ALL_SLEWRATE_Msk);
  /* Set New Setting */
  u8Val |= (uint8_t)(eSlewRate << PREDRVCTL1_ALL_SLEWRATE_Pos);
  
  return GATEDRV_WriteREG(GATEDRV_REG_PREDRVCTL1_ADDR, u8Val);
}


/******************* Copyright (C) 2022 Spintrol Electronic Technology (Shanghai) Co., Ltd. ***** END OF FILE ****/
