/*******************************************************************************
 * @file     hv.h
 * @brief    High voltage module header file.
 * @version  V4.0.3
 * @date     30-July-2026
 *
 * @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.
 *
 ******************************************************************************/

#ifndef HV_H
#define HV_H

#ifdef __cplusplus
extern "C" {
#endif


#include "epwr.h"
#include "spd1178_hv_bitfield.h"




/**
 *  @brief  High Voltage Module Register Address Definition
 */
typedef enum
{
  HV_REG_GLBSTS                   = 0x00,   /*!< Global Status Register               */
  HV_REG_GLBSTSCLR                = 0x01,   /*!< Global Status Clear Register         */
  HV_REG_PDRVSTS                  = 0x02,   /*!< Pre-Driver Status Register           */
  HV_REG_PDRVSTSCLR               = 0x03,   /*!< Pre-Driver Status Clear Register     */
  HV_REG_PDRV0TZEN                = 0x04,   /*!< PDRV0 Trip-Zone Enable Register      */
  HV_REG_PDRV1TZEN                = 0x05,   /*!< PDRV1 Trip-Zone Enable Register      */
  HV_REG_LDOGCTL                  = 0x06,   /*!< VDDG LDO Control Register            */
  HV_REG_BODCTL                   = 0x07,   /*!< BOD Control Register                 */
  HV_REG_BODVDDGLVTH              = 0x08,   /*!< VDDG BOD Too-Low Threshold Register  */
  HV_REG_BODVDDGHVTH              = 0x09,   /*!< VDDG BOD Too-High Threshold Register */
  HV_REG_BODVBATLVTH              = 0x0A,   /*!< VBAT BOD Too-Low Threshold Register  */
  HV_REG_BODVBATHVTH              = 0x0B,   /*!< VBAT BOD Too-High Threshold Register */
  HV_REG_PDRV0CTL0                = 0x0C,   /*!< Pre-Driver 0 Control Register 0      */
  HV_REG_PDRV0CTL1                = 0x0D,   /*!< Pre-Driver 0 Control Register 1      */
  HV_REG_PDRV1CTL0                = 0x0E,   /*!< Pre-Driver 1 Control Register 0      */
  HV_REG_PDRV1CTL1                = 0x0F,   /*!< Pre-Driver 1 Control Register 1      */
  HV_REG_PWRUPKEY                 = 0x10,   /*!< Power-Up Key Register                */
  HV_REG_CTLKEY                   = 0x11,   /*!< Control Key Register                 */

  /* Registers For Engineering Test */
  HV_REG_ENGRKEY                  = 0x16,   /*!< Engineering Key Register             */
  HV_REG_BGENGR                   = 0x17,   /*!< Bandgap Engineering Register         */
  HV_REG_CPENGR                   = 0x18,   /*!< CP Engineering Register              */
  HV_REG_LDOGENGR                 = 0x19,   /*!< VDDG LDO Engineering Register        */
  HV_REG_BODENGR                  = 0x1A,   /*!< BOD Engineering Register             */
  HV_REG_RCOENGR                  = 0x1B,   /*!< RCO Engineering Register             */
  HV_REG_PDRVENGR                 = 0x1C,   /*!< Pre-Driver Engineering Register      */
  HV_REG_ATESTEN                  = 0x1E,   /*!< ATEST Enable Register                */
  HV_REG_TESTSEL                  = 0x1F,   /*!< ATEST Test Point Select Register     */
} HV_REGS;




/**
 *  @brief  High Voltage Module Chage-pump clock dividing value definition
 */
typedef enum
{
  HV_CHARGEPUMP_CLKDIV_4          = 0x00,    /*!< Divide by 4  */
  HV_CHARGEPUMP_CLKDIV_8          = 0x01,    /*!< Divide by 8  */
  HV_CHARGEPUMP_CLKDIV_16         = 0x02,    /*!< Divide by 16 */
  HV_CHARGEPUMP_CLKDIV_32         = 0x03,    /*!< Divide by 32 */
} HV_ChargePumpClockDivEnum;




/**
 *  @brief  High Voltage Module Pin Interface Definition
 */
#define GPIO_HV_PWM_U0_L          GPIO_22   /*!< GPIO for PWM_U0_L           */
#define GPIO_HV_PWM_U0_H          GPIO_23   /*!< GPIO for PWM_U0_H           */
#define GPIO_HV_PWM_V0_L          GPIO_20   /*!< GPIO for PWM_V0_L           */
#define GPIO_HV_PWM_V0_H          GPIO_21   /*!< GPIO for PWM_V0_H           */
#define GPIO_HV_PWM_W0_L          GPIO_18   /*!< GPIO for PWM_W0_L           */
#define GPIO_HV_PWM_W0_H          GPIO_19   /*!< GPIO for PWM_W0_H           */
#define GPIO_HV_PWM_U1_L          GPIO_28   /*!< GPIO for PWM_U1_L           */
#define GPIO_HV_PWM_U1_H          GPIO_29   /*!< GPIO for PWM_U1_H           */
#define GPIO_HV_PWM_V1_L          GPIO_26   /*!< GPIO for PWM_V1_L           */
#define GPIO_HV_PWM_V1_H          GPIO_27   /*!< GPIO for PWM_V1_H           */
#define GPIO_HV_PWM_W1_L          GPIO_24   /*!< GPIO for PWM_W1_L           */
#define GPIO_HV_PWM_W1_H          GPIO_25   /*!< GPIO for PWM_W1_H           */




/**
 *  @brief  High Voltage Module Communication Command
 */
#define HV_CMD_READ_ID            (0x514E4944UL)
#define HV_CMD_WRITE              (0x5740005AUL)
#define HV_CMD_EXECUTE            (0x45786563UL)
#define HV_CMD_READ               (0x5240445AUL)




/**
 *  @brief  High Voltage Module Masked Response
 */
#define HV_RESP_READ_ID           (0x49444A00UL)
#define HV_RESP_WRITE             (0x57A0005AUL)
#define HV_RESP_EXECUTE           (0x446F6E65UL)
#define HV_RESP_READ              (0x52A00000UL)




/**
 *  @brief  High Voltage Module timeout count number
 */
#define HV_TIMEOUT_MAX            (100000U)




/**
 *  @brief  High Voltage Module SPI Communication Maximum Clock Frequency
 */
#define HV_SPI_MAX_CLK            (40000000)  /*!< 40 MHz */




/**
 *  @brief  High Voltage Module Register Write Access KEY
 */
#define HV_WRITE_ACCESS_KEY       (0xAC)




/**
 *  @brief  High Voltage Module Register Address Check
 */
#define IS_HV_REG(u8Addr)         (((u8Addr) <= 0x11) || (((u8Addr) >= 0x16) && ((u8Addr) <= 0x1C) ) || (((u8Addr) >= 0x1E) && ((u8Addr) <= 0x1F)))




/**
 *  @brief  High Voltage Module Public Function Declaration
 */
ErrorStatus HV_ComInit(uint32_t u32Baudrate);
ErrorStatus HV_Init(uint32_t u32Baudrate, uint8_t *pu8ID, uint32_t u32Timeout);

ErrorStatus HV_Handshake(uint8_t *pu8ID, uint32_t u32Timeout);
ErrorStatus HV_Command(uint32_t u32Req, uint32_t *pu32Resp);

ErrorStatus HV_WriteReg(uint8_t u8Addr, uint8_t u8Data);
ErrorStatus HV_ReadReg(uint8_t u8Addr, uint8_t *pu8Data);
ErrorStatus HV_WriteRegField(uint8_t u8Addr, uint8_t u8Mask, uint8_t u8FieldData);

ErrorStatus HV_WALLOW(void);
ErrorStatus HV_WDIS(void);

ErrorStatus HV_InitReg(void);

void HV_ConnectAnalogTestToADC(void);
ErrorStatus HV_ConnectTSensorToAnalogTest(void);
ErrorStatus HV_EnableLowVoltageOperation(void);
ErrorStatus HV_SetChargePumpClockDiv(HV_ChargePumpClockDivEnum eClockDiv);
ErrorStatus HV_GetChargePumpClockDiv(uint8_t *pu8Data);

#ifdef __cplusplus
}
#endif /* extern "C" */

#endif /* HV_H */


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