/*
 * Copyright (c) 2022, Shenzhen CVA Innovation CO.,LTD
 * All rights reserved.
 *
 * Shenzhen CVA Innovation CO.,LTD (CVA chip) is supplying this file for use
 * exclusively with CVA's microcontroller products. This file can be freely
 * distributed within development tools that are supporting such microcontroller
 * products.
 *
 * THIS SOFTWARE IS PROVIDED "AS IS".  NO WARRANTIES, WHETHER EXPRESS, IMPLIED
 * OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
 * CVA SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL,
 * OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
 */

#ifndef _TOUCH_DRV_H_
#define _TOUCH_DRV_H_

/*! \brief Contains public interface to various functions related
 *         to the driver of UART module
 */

/******************************************************************************
 * the includes
 *****************************************************************************/
#include <stdint.h>
#include <stdbool.h>
#include <string.h>

/*! \addtogroup proc_drv
 *  @{
 */

#ifdef __cplusplus
extern "C" {
#endif

/******************************************************************************
 * the defines
 *****************************************************************************/
#define TOUCH_SCAN_PHASE_NUM    30
#define TOUCH_ND_PHASE_NUM      30

#define TOUCHDRV_UNUSED(p)          ((void)(p))

#define BIT_MASK32(n)                   ((uint32_t)1 << (n))

#define TOUCH_DRV_COM_BYTES_TO_WORD(a, b)               (((a) & 0x00FF) | (((b) << 8) & 0xFF00))
#define TOUCH_DRV_COM_BYTES_TO_DWORD(a, b, c, d)        (((a) & 0x000000FF) | (((b) << 8) & 0x0000FF00) | (((c) << 16) & 0x00FF0000)  | (((d) << 24) & 0xFF000000))

#define TOUCH_DRV_GET_BYTE0_FROM_WORD(a)                ((a) & 0xFF)
#define TOUCH_DRV_GET_BYTE1_FROM_WORD(a)                (((a) >> 8) & 0xFF)

#define TOUCH_DRV_GET_BYTE0_FROM_DWORD(a)               ((a) & 0xFF)
#define TOUCH_DRV_GET_BYTE1_FROM_DWORD(a)               (((a) >> 8) & 0xFF)
#define TOUCH_DRV_GET_BYTE2_FROM_DWORD(a)               (((a) >> 16) & 0xFF)
#define TOUCH_DRV_GET_BYTE3_FROM_DWORD(a)               (((a) >> 24) & 0xFF)

/******************************************************************************
 * the typedefs
 *****************************************************************************/

/*! \brief Touch Phase index
 */
typedef enum
{
    TOUCHDRV_Phase0                   = 0,
    TOUCHDRV_Phase1                   = 1,
    TOUCHDRV_Phase2                   = 2,
    TOUCHDRV_Phase3                   = 3,
    TOUCHDRV_Phase4                   = 4,
    TOUCHDRV_Phase5                   = 5,
    TOUCHDRV_Phase6                   = 6,
    TOUCHDRV_Phase7                   = 7,
    TOUCHDRV_Phase8                   = 8,
    TOUCHDRV_Phase9                   = 9,
    TOUCHDRV_Phase10                  = 10,
    TOUCHDRV_Phase11                  = 11,
    TOUCHDRV_Phase12                  = 12,
    TOUCHDRV_Phase13                  = 13,
    TOUCHDRV_Phase14                  = 14,
    TOUCHDRV_Phase15                  = 15,
    TOUCHDRV_Phase16                  = 16,
    TOUCHDRV_Phase17                  = 17,
    TOUCHDRV_Phase18                  = 18,
    TOUCHDRV_Phase19                  = 19,
    TOUCHDRV_Phase20                  = 20,
    TOUCHDRV_Phase21                  = 21,
    TOUCHDRV_Phase22                  = 22,
    TOUCHDRV_Phase23                  = 23,
    TOUCHDRV_Phase24                  = 24,
    TOUCHDRV_Phase25                  = 25,
    TOUCHDRV_Phase26                  = 26,
    TOUCHDRV_Phase27                  = 27,
    TOUCHDRV_Phase28                  = 28,
    TOUCHDRV_Phase29                  = 29,
    TOUCHDRV_Phase30                  = 30,
    TOUCHDRV_Phase31                  = 31,
    TOUCHDRV_Phase32                  = 32,
    TOUCHDRV_Phase33                  = 33,
    TOUCHDRV_Phase34                  = 34,
    TOUCHDRV_Phase35                  = 35,
    TOUCHDRV_Phase36                  = 36,
    TOUCHDRV_Phase37                  = 37,
    TOUCHDRV_Phase38                  = 38,
    TOUCHDRV_Phase39                  = 39,
    TOUCHDRV_Phase40                  = 40,
    TOUCHDRV_Phase41                  = 41,
    TOUCHDRV_Phase42                  = 42,
    TOUCHDRV_Phase43                  = 43,
    TOUCHDRV_Phase44                  = 44,
    TOUCHDRV_Phase45                  = 45,
    TOUCHDRV_Phase46                  = 46,
    TOUCHDRV_Phase47                  = 47,
    TOUCHDRV_Phase48                  = 48,
    TOUCHDRV_Phase49                  = 49,
    TOUCHDRV_Phase50                  = 50,
    TOUCHDRV_Phase51                  = 51,
    TOUCHDRV_Phase52                  = 52,
    TOUCHDRV_Phase53                  = 53,
    TOUCHDRV_Phase54                  = 54,
    TOUCHDRV_Phase55                  = 55,
    TOUCHDRV_Phase56                  = 56,
    TOUCHDRV_Phase57                  = 57,
    TOUCHDRV_Phase58                  = 58,
    TOUCHDRV_Phase59                  = 59,
} TouchDrv_Phase;

/*! \brief Touch Phase mask
 */
typedef enum
{
    TOUCHDRV_PhaseNone_MASK                = 0x00000000,
    TOUCHDRV_Phase0_MASK                   = BIT_MASK32(0),
    TOUCHDRV_Phase1_MASK                   = BIT_MASK32(1),
    TOUCHDRV_Phase2_MASK                   = BIT_MASK32(2),
    TOUCHDRV_Phase3_MASK                   = BIT_MASK32(3),
    TOUCHDRV_Phase4_MASK                   = BIT_MASK32(4),
    TOUCHDRV_Phase5_MASK                   = BIT_MASK32(5),
    TOUCHDRV_Phase6_MASK                   = BIT_MASK32(6),
    TOUCHDRV_Phase7_MASK                   = BIT_MASK32(7),
    TOUCHDRV_Phase8_MASK                   = BIT_MASK32(8),
    TOUCHDRV_Phase9_MASK                   = BIT_MASK32(9),
    TOUCHDRV_Phase10_MASK                  = BIT_MASK32(10),
    TOUCHDRV_Phase11_MASK                  = BIT_MASK32(11),
    TOUCHDRV_Phase12_MASK                  = BIT_MASK32(12),
    TOUCHDRV_Phase13_MASK                  = BIT_MASK32(13),
    TOUCHDRV_Phase14_MASK                  = BIT_MASK32(14),
    TOUCHDRV_Phase15_MASK                  = BIT_MASK32(15),
    TOUCHDRV_Phase16_MASK                  = BIT_MASK32(16),
    TOUCHDRV_Phase17_MASK                  = BIT_MASK32(17),
    TOUCHDRV_Phase18_MASK                  = BIT_MASK32(18),
    TOUCHDRV_Phase19_MASK                  = BIT_MASK32(19),
    TOUCHDRV_Phase20_MASK                  = BIT_MASK32(20),
    TOUCHDRV_Phase21_MASK                  = BIT_MASK32(21),
    TOUCHDRV_Phase22_MASK                  = BIT_MASK32(22),
    TOUCHDRV_Phase23_MASK                  = BIT_MASK32(23),
    TOUCHDRV_Phase24_MASK                  = BIT_MASK32(24),
    TOUCHDRV_Phase25_MASK                  = BIT_MASK32(25),
    TOUCHDRV_Phase26_MASK                  = BIT_MASK32(26),
    TOUCHDRV_Phase27_MASK                  = BIT_MASK32(27),
    TOUCHDRV_Phase28_MASK                  = BIT_MASK32(28),
    TOUCHDRV_Phase29_MASK                  = BIT_MASK32(29),
    TOUCHDRV_PhaseAll_MASK                 = 0x3FFFFFFF,
} TouchDrv_Phase_Mask;

/*! \brief Noise Detection Phase mask
 */
typedef enum
{
    TOUCHDRV_Nd_PhaseNone_MASK              = 0x00000000,
    TOUCHDRV_Phase30_MASK                   = BIT_MASK32(0),
    TOUCHDRV_Phase31_MASK                   = BIT_MASK32(1),
    TOUCHDRV_Phase32_MASK                   = BIT_MASK32(2),
    TOUCHDRV_Phase33_MASK                   = BIT_MASK32(3),
    TOUCHDRV_Phase34_MASK                   = BIT_MASK32(4),
    TOUCHDRV_Phase35_MASK                   = BIT_MASK32(5),
    TOUCHDRV_Phase36_MASK                   = BIT_MASK32(6),
    TOUCHDRV_Phase37_MASK                   = BIT_MASK32(7),
    TOUCHDRV_Phase38_MASK                   = BIT_MASK32(8),
    TOUCHDRV_Phase39_MASK                   = BIT_MASK32(9),
    TOUCHDRV_Phase40_MASK                  = BIT_MASK32(10),
    TOUCHDRV_Phase41_MASK                  = BIT_MASK32(11),
    TOUCHDRV_Phase42_MASK                  = BIT_MASK32(12),
    TOUCHDRV_Phase43_MASK                  = BIT_MASK32(13),
    TOUCHDRV_Phase44_MASK                  = BIT_MASK32(14),
    TOUCHDRV_Phase45_MASK                  = BIT_MASK32(15),
    TOUCHDRV_Phase46_MASK                  = BIT_MASK32(16),
    TOUCHDRV_Phase47_MASK                  = BIT_MASK32(17),
    TOUCHDRV_Phase48_MASK                  = BIT_MASK32(18),
    TOUCHDRV_Phase49_MASK                  = BIT_MASK32(19),
    TOUCHDRV_Phase50_MASK                  = BIT_MASK32(20),
    TOUCHDRV_Phase51_MASK                  = BIT_MASK32(21),
    TOUCHDRV_Phase52_MASK                  = BIT_MASK32(22),
    TOUCHDRV_Phase53_MASK                  = BIT_MASK32(23),
    TOUCHDRV_Phase54_MASK                  = BIT_MASK32(24),
    TOUCHDRV_Phase55_MASK                  = BIT_MASK32(25),
    TOUCHDRV_Phase56_MASK                  = BIT_MASK32(26),
    TOUCHDRV_Phase57_MASK                  = BIT_MASK32(27),
    TOUCHDRV_Phase58_MASK                  = BIT_MASK32(28),
    TOUCHDRV_Phase59_MASK                  = BIT_MASK32(29),
    TOUCHDRV_Nd_PhaseAll_MASK                 = 0x3FFFFFFF,
} TouchDrv_Nd_Phase_Mask;

/*! \brief Touch driver status.
 */
typedef enum
{
    /* Generic error codes */
    TOUCHDRV_STATUS_SUCCESS     = 0x000U, /*!< Generic operation success status */
    TOUCHDRV_STATUS_ERROR       = 0x001U, /*!< Generic operation failure status */
    TOUCHDRV_STATUS_BUSY        = 0x002U, /*!< Generic operation busy status */
    TOUCHDRV_STATUS_TIMEOUT     = 0x003U, /*!< Generic operation timeout status */
    TOUCHDRV_STATUS_UNSUPPORTED = 0x004U, /*!< Generic operation unsupported status */
} TouchDrv_Status;

/*! \brief Touch sensing mode.
 */
typedef enum
{
    TOUCHDRV_SELF_LIGHT = 0x00u,    /*!< Sensing method for self-light. */
    TOUCHDRV_SELF_HEAVY,            /*!< Sensing method for self-heavy. */
    TOUCHDRV_ND,                    /*!< Sensing method for noise detection. */
    TOUCHDRV_MUTUAL,                /*!< Sensing method for mutual. */
} TouchDrv_Sense_Method;

/*! \brief Touch CSIO pad function
 */
typedef enum
{
    TOUCHDRV_CSIO_FUNC_NONE  = 0x00u,   /*!< CSIO function for none.*/
    TOUCHDRV_CSIO_FUNC_INPUT = 0x01u,   /*!< CSIO function for Input.*/
    TOUCHDRV_CSIO_FUNC_SHD   = 0x02u,   /*!< CSIO function for Shielding.*/
    TOUCHDRV_CSIO_FUNC_TX    = 0x03u,   /*!< CSIO function for mutual tx.*/
    TOUCHDRV_CSIO_FUNC_GND   = 0x04u,   /*!< CSIO function for GND.*/
    TOUCHDRV_CSIO_FUNC_VCM   = 0x05u,   /*!< CSIO function for VCM.*/
} TouchDrv_CsioFuncType;

/*! \brief Touch Sampling number
 */
typedef enum
{
    TOUCHDRV_NSMP_64   = 0x02u,         /*!< Sampling number for 64.*/
    TOUCHDRV_NSMP_128  = 0x03u,         /*!< Sampling number for 128.*/
    TOUCHDRV_NSMP_256  = 0x04u,         /*!< Sampling number for 256.*/
    TOUCHDRV_NSMP_512  = 0x05u,         /*!< Sampling number for 512.*/
    TOUCHDRV_NSMP_1024 = 0x06u,         /*!< Sampling number for 1024.*/
    TOUCHDRV_NSMP_2048 = 0x07u,         /*!< Sampling number for 2048.*/
} TouchDrv_Sense_Nsmp;

/*! \brief Touch Auto-Detection filter coefficient
 */
typedef enum
{
    TOUCHDRV_FLT_COEF_BYPASS = 0x00u,   /*!< Auto-Detection filter bypass.*/
    TOUCHDRV_FLT_COEF_1,                /*!< Auto-Detection filter intensity 1.*/
    TOUCHDRV_FLT_COEF_2,                /*!< Auto-Detection filter intensity 2.*/
    TOUCHDRV_FLT_COEF_3,                /*!< Auto-Detection filter intensity 3.*/
    TOUCHDRV_FLT_COEF_4,                /*!< Auto-Detection filter intensity 4.*/
    TOUCHDRV_FLT_COEF_5,                /*!< Auto-Detection filter intensity 5.*/
    TOUCHDRV_FLT_COEF_6,                /*!< Auto-Detection filter intensity 6.*/
    TOUCHDRV_FLT_COEF_7,                /*!< Auto-Detection filter intensity 7.*/
    TOUCHDRV_FLT_COEF_8,                /*!< Auto-Detection filter intensity 8.*/
    TOUCHDRV_FLT_COEF_9,                /*!< Auto-Detection filter intensity 9.*/
    TOUCHDRV_FLT_COEF_10,               /*!< Auto-Detection filter intensity 10.*/
    TOUCHDRV_FLT_COEF_11,               /*!< Auto-Detection filter intensity 11.*/
} TouchDrv_FltCoefType;

/*! \brief Touch Auto-Detection debounce number
 */
typedef enum
{
    TOUCHDRV_DEB_NUM_1 = 0x00u,         /*!< Auto-Detection debounce bypass.*/
    TOUCHDRV_DEB_NUM_2,                 /*!< Auto-Detection debounce number: 2.*/
    TOUCHDRV_DEB_NUM_4,                 /*!< Auto-Detection debounce number: 4.*/
    TOUCHDRV_DEB_NUM_8,                 /*!< Auto-Detection debounce number: 8.*/
} TouchDrv_DebNumType;

/*! \brief Touch Auto-Detection hysteresis intensity
 */
typedef enum
{
    TOUCHDRV_HYST_BYPASS = 0x00u,       /*!< Auto-Detection hysteresis bypass.*/
    TOUCHDRV_HYST_WEAK,                 /*!< Auto-Detection hysteresis intensity: weak.*/
    TOUCHDRV_HYST_MEDIUM,               /*!< Auto-Detection hysteresis intensity: medium.*/
    TOUCHDRV_HYST_STRONG,               /*!< Auto-Detection hysteresis intensity: strong.*/
} TouchDrv_HystCoefType;

/*! \brief Touch Auto-Detection doze mode scan period multiple
 */
typedef enum
{
    TOUCHDRV_DOZE_MUL_BYPASS = 0x00u,   /*!< Auto-Detection doze mode bypass.*/
    TOUCHDRV_DOZE_MUL_2,                /*!< Auto-Detection doze mode scan period multiple equal 2.*/
    TOUCHDRV_DOZE_MUL_4,                /*!< Auto-Detection doze mode scan period multiple equal 4.*/
    TOUCHDRV_DOZE_MUL_8,                /*!< Auto-Detection doze mode scan period multiple equal 8.*/
    TOUCHDRV_DOZE_MUL_16,               /*!< Auto-Detection doze mode scan period multiple equal 16.*/
    TOUCHDRV_DOZE_MUL_32,               /*!< Auto-Detection doze mode scan period multiple equal 32.*/
    TOUCHDRV_DOZE_MUL_64,               /*!< Auto-Detection doze mode scan period multiple equal 64.*/
} TouchDrv_DozeMulType;

/*! \brief Touch sampling mode.
 */
typedef enum
{
    TOUCHDRV_MANUAL_MODE = 0x00u,   /*!< Manual mode Scan */
    TOUCHDRV_AUTO_MODE,             /*!< Auto mode Scan */
    TOUCHDRV_INIT_MODE
} TouchDrv_Mode;

/*! \brief Touch command.
 */
typedef enum
{
    FSMDRV_HOST_SCAN = 0x0Au,     /*!< Start Scan */
    FSMDRV_HOST_IDLE = 0x0Bu,     /*!< go idle */
    FSMDRV_HOST_COMP = 0x0Eu,     /*!< go compensation */
} FsmDrvHostCmd;

/*! \brief Touch state.
 */
typedef enum
{
    TOUCHDRV_FSM_STATE_INIT = 0x00u,    /*!< Touch FSM state: inited. */
    TOUCHDRV_FSM_STATE_IDLE,            /*!< Touch FSM state: idle. */
    TOUCHDRV_FSM_STATE_BRANCH,          /*!< Touch FSM state: branch. */
    TOUCHDRV_FSM_STATE_COMPENSATION,    /*!< Touch FSM state: auto-calibrating. */
    TOUCHDRV_FSM_STATE_SENSING,         /*!< Touch FSM state: sensing. */
    TOUCHDRV_FSM_STATE_SUSPEND,         /*!< Touch FSM state: suspending. */
    TOUCHDRV_FSM_STATE_WAIT,            /*!< Touch FSM state: waitting. */
} TouchDrv_Fsm_State;

/*! \brief Touch IRQ enable.
 */
typedef enum
{
    TOUCHDRV_IRQ_DISABLE       = 0x0000,    /*!< Touch interrupt request: none. */
    TOUCHDRV_IRQ_TOUCH_EN      = 0x0002,    /*!< Touch interrupt request: any phase change to touch state from no-touch state. */
    TOUCHDRV_IRQ_NOTOUCH_EN    = 0x0004,    /*!< Touch interrupt request: any phase change to no-touch state from touch state. */
    TOUCHDRV_IRQ_CALIDONE_EN   = 0x0008,    /*!< Touch interrupt request: calibration done. */
    TOUCHDRV_IRQ_CONVDONE_EN   = 0x0010,    /*!< Touch interrupt request: sampling done. */
    TOUCHDRV_IRQ_ND_EN         = 0x0040,    /*!< Touch interrupt request: noise detection. */
    TOUCHDRV_IRQ_AUTOCALI_EN   = 0x0080,    /*!< Touch interrupt request: auto-calibration. */
    TOUCHDRV_IRQ_TOUCHAND_EN   = 0x0100,    /*!< Touch interrupt request: all phase change to touch state from no-touch state. */
    TOUCHDRV_IRQ_NOTOUCHAND_EN = 0x0200,    /*!< Touch interrupt request: all phase change to no-touch state from touch state. */
} TouchDrv_IrqEnType;

/*! \brief touch instance pointer */
typedef struct _TouchDrvType_
{
    void *afecReg;
    void *procReg;
    void *fsmReg;
} TouchDrvType;

/*! \brief Definition of touch phase configuration
 */
typedef struct
{
    uint8_t  phaseIndex;        /*!< phase index.*/

    uint32_t tk00Func: 3;       /*!< CSIO0 function, refer TouchDrv_CsioFuncType.*/
    uint32_t tk01Func: 3;       /*!< CSIO1 function, refer TouchDrv_CsioFuncType.*/
    uint32_t tk02Func: 3;       /*!< CSIO2 function, refer TouchDrv_CsioFuncType.*/
    uint32_t tk03Func: 3;       /*!< CSIO3 function, refer TouchDrv_CsioFuncType.*/
    uint32_t tk04Func: 3;       /*!< CSIO4 function, refer TouchDrv_CsioFuncType.*/
    uint32_t tk05Func: 3;       /*!< CSIO5 function, refer TouchDrv_CsioFuncType.*/
    uint32_t tk06Func: 3;       /*!< CSIO6 function, refer TouchDrv_CsioFuncType.*/
    uint32_t tk07Func: 3;       /*!< CSIO7 function, refer TouchDrv_CsioFuncType.*/
    uint32_t : 0;

    uint32_t tk08Func: 3;       /*!< CSIO0 function, refer TouchDrv_CsioFuncType.*/
    uint32_t tk09Func: 3;       /*!< CSIO1 function, refer TouchDrv_CsioFuncType.*/
    uint32_t tk10Func: 3;       /*!< CSIO2 function, refer TouchDrv_CsioFuncType.*/
    uint32_t tk11Func: 3;       /*!< CSIO3 function, refer TouchDrv_CsioFuncType.*/
    uint32_t tk12Func: 3;       /*!< CSIO4 function, refer TouchDrv_CsioFuncType.*/
    uint32_t tk13Func: 3;       /*!< CSIO5 function, refer TouchDrv_CsioFuncType.*/
    uint32_t tk14Func: 3;       /*!< CSIO6 function, refer TouchDrv_CsioFuncType.*/
    uint32_t tk15Func: 3;       /*!< CSIO7 function, refer TouchDrv_CsioFuncType.*/
    uint32_t : 0;

    uint32_t tk16Func: 3;       /*!< CSIO0 function, refer TouchDrv_CsioFuncType.*/
    uint32_t tk17Func: 3;       /*!< CSIO1 function, refer TouchDrv_CsioFuncType.*/
    uint32_t tk18Func: 3;       /*!< CSIO2 function, refer TouchDrv_CsioFuncType.*/
    uint32_t tk19Func: 3;       /*!< CSIO3 function, refer TouchDrv_CsioFuncType.*/
    uint32_t tk20Func: 3;       /*!< CSIO4 function, refer TouchDrv_CsioFuncType.*/
    uint32_t tk21Func: 3;       /*!< CSIO5 function, refer TouchDrv_CsioFuncType.*/
    uint32_t tk22Func: 3;       /*!< CSIO6 function, refer TouchDrv_CsioFuncType.*/
    uint32_t tk23Func: 3;       /*!< CSIO7 function, refer TouchDrv_CsioFuncType.*/
    uint32_t : 0;

    uint32_t tk24Func: 3;       /*!< CSIO0 function, refer TouchDrv_CsioFuncType.*/
    uint32_t tk25Func: 3;       /*!< CSIO1 function, refer TouchDrv_CsioFuncType.*/
    uint32_t tk26Func: 3;       /*!< CSIO2 function, refer TouchDrv_CsioFuncType.*/
    uint32_t tk27Func: 3;       /*!< CSIO3 function, refer TouchDrv_CsioFuncType.*/
    uint32_t tk28Func: 3;       /*!< CSIO4 function, refer TouchDrv_CsioFuncType.*/
    uint32_t tk29Func: 3;       /*!< CSIO5 function, refer TouchDrv_CsioFuncType.*/
    uint32_t trst: 3;
    uint32_t td: 4;
    uint32_t : 0;

    uint32_t afeMode: 2;        /*!< Sampling number, refer TouchDrv_SampleNumType.*/
    uint32_t bitSel: 5;         /*!< Sampling frequency, refer TouchDrv_FreqType.*/
    uint32_t cf: 4;             /*!< Sampling number, refer TouchDrv_SampleNumType.*/
    uint32_t rf: 4;             /*!< Sampling number, refer TouchDrv_SampleNumType.*/
    uint32_t rfEn: 1;           /*!< Sampling number, refer TouchDrv_SampleNumType.*/
    uint32_t freq: 7;           /*!< Sampling number, refer TouchDrv_SampleNumType.*/
    uint32_t num: 3;            /*!< Sampling number, refer TouchDrv_SampleNumType.*/
    uint32_t : 0;

    uint16_t adcFltEn: 1;       /*!< adc filter enable.*/
    uint16_t adcFltWin: 2;      /*!< adc filter window.*/
    uint16_t adcFltInCoef: 2;   /*!< adc filter in-coef.*/
    uint16_t adcFltUnderCoef: 2;/*!< adc filter under-coef*/
    uint16_t adcFltOverCoef: 2; /*!< adc filter over-coef*/
    uint16_t : 0;

    uint16_t rawFltCoef: 3;     /*!< Raw data filter coefficient, refer TouchDrv_FltCoefType, range in [TOUCHDRV_FLT_COEF_BYPASS, TOUCHDRV_FLT_COEF_7].*/
    uint16_t avgFltPosCoef: 4;  /*!< Avg filter positive coefficient, refer TouchDrv_FltCoefType, range in [TOUCHDRV_FLT_COEF_BYPASS, TOUCHDRV_FLT_COEF_11].*/
    uint16_t avgFltNegCoef: 3;  /*!< Avg filter negative coefficient, refer TouchDrv_FltCoefType, range in [TOUCHDRV_FLT_COEF_BYPASS, TOUCHDRV_FLT_COEF_7].*/
    uint16_t autoCaliDebNum: 2; /*!< Auto-calibration debounce coefficient, refer TouchDrv_DebNumType.*/
    uint16_t : 0;

    uint32_t autoCaliPosThr: 16;/*!< auto-calibration positive threshold, range in [0, 65535].*/
    uint32_t autoCaliNegThr: 16;/*!< auto-calibration negative threshold, range in [0, 65535].*/

    uint32_t thr: 16;           /*!< Touch threshold, range in [0, 65535].*/
    uint32_t thrHyst: 3;        /*!< Touch threshold hysteresis coefficient, refer TouchDrv_HystCoefType.*/

    uint16_t touchDebNum: 8;    /*!< touch debouncer, range in [0, 255].*/
    uint16_t noTouchDebNum: 8;  /*!< no-touch debouncer, range in [0, 255].*/
} TouchDrv_PhaseCfgType;

/*! \brief Definition of touch driver configuration
 */
typedef struct
{
    const TouchDrv_PhaseCfgType *phaseCfg;  /*!< Phase configuration, refer TouchDrv_PhaseCfgType.*/
    uint8_t phaseCfgNum;                    /*!< Total phase number.*/

    uint16_t irqEn;                         /*!< Interrupt requst enable, refer TouchDrv_IrqEnType.*/
    uint16_t scanPeriod;                    /*!< Scan period, range in [1ms, 4000ms] by 1ms step.*/
    TouchDrv_DozeMulType dozeMul;           /*!< Doze mode scan period multiple, refer TouchDrv_DozeMulType.*/
    uint32_t sensingPhaseSel;               /*!< Sensing phase selection.*/
    uint32_t autoDetectionPhaseSel;         /*!< Auto detection phase selection.*/
    uint32_t ndPhaseSel;                    /*!< Noise detection phase selection.*/
    uint32_t touchAndSel;                   /*!< Touch phase selection when touch-and function enabled.*/
    uint32_t noTouchAndSel;                 /*!< No-Touch phase selection when no-touch-and function enabled.*/
    uint8_t  senScen;
} TouchDrv_CfgType;

/*! \brief Definition of touch driver information
 */
typedef struct _TouchDrv_Info_
{
    uint32_t     irqState;  /*!< Touch interrupt state.*/
    uint32_t     proxState; /*!< Phase state by Auot-Detecion.*/
    int16_t     *raw;       /*!< Raw data.*/
    int16_t     *base;      /*!< Basedata.*/
    int16_t     *diff;      /*!< Diff data.*/
} TouchDrv_InfoType;

/******************************************************************************
 * the globals
 ******************************************************************************/

/******************************************************************************
 * the function prototypes
 ******************************************************************************/

/*! \brief Initializes the Touch driver module
 *
 * This function initializes Touch driver by setting the register
 * instance to it.
 *
 * \param[in] obj : pointer to Touch driver instance
 */
extern void TouchDrv_Init(TouchDrvType *obj);

/*! \brief Configures the Touch module from a user configuration structure
 *
 * This function configures the Touch module from a user configuration structure
 *
 * \param[in] obj : pointer to Touch driver instance
 * \param[in] cfg : pointer to configuration structure
 */
extern TouchDrv_Status TouchDrv_Configure(TouchDrvType *obj, TouchDrv_CfgType *cfg);

/*! \brief Calibration the Touch sensor
 *
 * This function calibarte the touch sensor
 *
 * \param[in] obj : pointer to Touch driver instance
 */
extern TouchDrv_Status TouchDrv_Calibration(TouchDrvType *obj, int32_t target);

/*! \brief Start touch sampling and detection
 *
 * This function start touch module to sample capacitance and detect proxmity state
 *
 * \param[in] obj : pointer to Touch driver instance
 */
extern TouchDrv_Status TouchDrv_Start(TouchDrvType *obj, TouchDrv_Mode mode);

/*! \brief Stop touch sampling and detection
 *
 * This function stop touch module to sample capacitance and detect proxmity state
 *
 * \param[in] obj : pointer to Touch driver instance
 */
extern TouchDrv_Status TouchDrv_Stop(TouchDrvType *obj);

/*! \brief Get touch raw data
 *
 * This function for getting touch raw data
 *
 * \param[in] obj      : pointer to Touch driver instance
 * \param[in] phStart : the start phase to be read
 * \param[in] len      : the len to be read
 * \param[out] data    : pointer to raw data buffer
 *
 */
extern TouchDrv_Status TouchDrv_GetRawdata(TouchDrvType *obj, uint8_t ph_start, uint8_t len, int16_t data[]);

/*! \brief Get touch base data
 *
 * This function for getting touch base data
 *
 * \param[in] obj      : pointer to Touch driver instance
 * \param[in] phStart : the start phase to be read
 * \param[in] len      : the len to be read
 * \param[out] data    : pointer to base data buffer
 *
 */
extern TouchDrv_Status TouchDrv_GetBasedata(TouchDrvType *obj, uint8_t ph_start, uint8_t len, int16_t data[]);

/*! \brief Get touch diff data
 *
 * This function for getting touch diff data
 *
 * \param[in] obj      : pointer to Touch driver instance
 * \param[in] phStart : the start phase to be read
 * \param[in] len      : the len to be read
 * \param[out] data    : pointer to diff data buffer
 *
 */
extern TouchDrv_Status TouchDrv_GetDiffdata(TouchDrvType *obj, uint8_t ph_start, uint8_t len, int16_t data[]);

// extern TouchDrv_Status TouchDrv_GetNddata(TouchDrvType *obj, uint8_t ph_start, uint8_t len, int16_t data[]);
// extern TouchDrv_Status TouchDrv_GetSmoothdata(TouchDrvType *obj, uint8_t ph_start, uint8_t len, int16_t data[]);
// extern TouchDrv_Status TouchDrv_GetAvgdata(TouchDrvType *obj, uint8_t ph_start, uint8_t len, int16_t data[]);
// extern TouchDrv_Status TouchDrv_GetUsefuldata(TouchDrvType *obj, uint8_t ph_start, uint8_t len, int16_t data[]);

/*! \brief Get touch capacitance load
 *
 * This function for getting touch capacitance load
 *
 * \param[in] obj      : pointer to Touch driver instance
 * \param[in] phStart : the start phase to be read
 * \param[in] len      : the len to be read
 * \param[out] data    : pointer to cc data buffer
 *
 */
extern TouchDrv_Status TouchDrv_GetCc(TouchDrvType *obj, uint8_t ph_start, uint8_t len, uint16_t data[]);

/*! \brief Get touch lib version
 *
 * This function for getting touch lib version
 *
 * \param[in] obj      : pointer to Touch driver instance
 *
 */
extern char* TouchDrv_GetLibVersion(TouchDrvType *obj);

/*! \brief Get touch irq state
 *
 * This function for getting irq state
 *
 * \param[in] obj : pointer to Touch driver instance
 * \return touch irq state
 *
 */
extern uint32_t TouchDrv_GetIrqState(TouchDrvType *obj);

/*! \brief Get touch proxmity state
 *
 * This function for getting proxmity state
 *
 * \param[in] obj : pointer to Touch driver instance
 * \return touch proxmity state
 *
 */
extern uint32_t TouchDrv_GetProxState(TouchDrvType *obj);

// extern uint32_t TouchDrv_GetNdState(TouchDrvType *obj);
// extern uint32_t TouchDrv_GetCompState(TouchDrvType *obj);
// extern uint32_t TouchDrv_GetAutoCompState(TouchDrvType *obj);

/*! \brief Get touch fsm state
 *
 * This function for getting fsm state
 *
 * \param[in] obj : pointer to Touch driver instance
 * \return touch fsm state
 *
 */
extern uint8_t TouchDrv_GetDebugState(TouchDrvType *obj);

/*! \brief Clear touch irq state
 *
 * This function for clearing touch irq state
 *
 * \param[in] obj : pointer to Touch driver instance
 *
 */
extern void TouchDrv_CleanIrqState(TouchDrvType *obj);

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

/*! @}*/

#endif
