#ifndef __HAL_LKSMCU_CMP_H__
#define __HAL_LKSMCU_CMP_H__
#include "stdint.h"
#include "hal_lksmcu_gpio.h"
typedef enum
{
    HAL_CMP_0, ///< CMP模块0
    HAL_CMP_1, ///< CMP模块1
    HAL_CMP_2, ///< CMP模块2
    HAL_CMP_3, ///< CMP模块3
    HAL_CMP_4, ///< CMP模块4
    HAL_CMP_5, ///< CMP模块5
} HAL_CMP_x;

typedef enum
{
    // 比较器1正端信号选择
    // 1001: DAC1_OUT
    // 1010: OPA_OUT_CMP
    // CMP0
    // 1001: DAC0_OUT
    // 1010: OUT_OPA_CMP
    HAL_CMP_CHANNEL_IP0             = 4,  ///< CMP模块外部正端输入0
    HAL_CMP_CHANNEL_IP1             = 5,  ///< CMP模块外部正端输入1
    HAL_CMP_CHANNEL_IP2             = 6,  ///< CMP模块外部正端输入2
    HAL_CMP_CHANNEL_IP3             = 7,  ///< CMP模块外部正端输入3
    HAL_CMP_CHANNEL_IP4             = 8,  ///< 仅CMP0 CMP模块外部正端输入4
    HAL_CMP_CHANNEL_IP5             = 11, ///< CMP模块外部正端输入5
    HAL_CMP_CHANNEL_IP6             = 12, ///< CMP模块外部正端输入6
    HAL_CMP_CHANNEL_IP7             = 13, ///< CMP模块外部正端输入6
    HAL_CMP_CHANNEL_IP_OPA0_IP      = 0,  ///< 仅CMP0 运放0正端输入
    HAL_CMP_CHANNEL_IP_OPA1_IP      = 1,  ///< 仅CMP0 运放1正端输入
    HAL_CMP_CHANNEL_IP_OPA2_IP      = 0,  ///< 仅CMP1 运放2正端输入
    HAL_CMP_CHANNEL_IP_OPA3_IP      = 1,  ///< 仅CMP1 运放3正端输入
    HAL_CMP_CHANNEL_IP_OPA0_OUTP    = 2,  ///< 仅CMP0 运放0正端输出
    HAL_CMP_CHANNEL_IP_OPA1_OUTP    = 3,  ///< 仅CMP0 运放1正端输出
    HAL_CMP_CHANNEL_IP_OPA2_OUTP    = 2,  ///< 仅CMP1 运放2正端输出
    HAL_CMP_CHANNEL_IP_OPA3_OUTP    = 3,  ///< 仅CMP1 运放3正端输出
    HAL_CMP_CHANNEL_IP_DAC0         = 9,  ///< 仅CMP0 DAC0输出
    HAL_CMP_CHANNEL_IP_DAC1         = 9,  ///< 仅CMP1 DAC1输出
    HAL_CMP_CHANNEL_IP_OPA_OUT_CMP0 = 10, ///< 可以软件配置的运放输出0

    HAL_CMP_CHANNEL_IP_AVSS,      ///< 模拟地 不支持
    HAL_CMP_CHANNEL_IP_OPA0_IN,   ///< 运放0负端输入 不支持
    HAL_CMP_CHANNEL_IP_OPA0_OUTN, ///< 运放0负端输出 不支持
    HAL_CMP_CHANNEL_IP_OPA1_IN,   ///< 运放1负端输入 不支持
    HAL_CMP_CHANNEL_IP_OPA1_OUTN, ///< 运放1负端输出 不支持
    HAL_CMP_CHANNEL_IP_OPA2_IN,   ///< 运放2负端输入 不支持
    HAL_CMP_CHANNEL_IP_OPA2_OUTN, ///< 运放2负端输出 不支持
    HAL_CMP_CHANNEL_IP_OPA3_IN,   ///< 运放3负端输入 不支持
    HAL_CMP_CHANNEL_IP_OPA3_OUTN, ///< 运放3负端输出 不支持
    HAL_CMP_CHANNEL_IP_OPA4_IP,   ///< 运放4正端输入 不支持
    HAL_CMP_CHANNEL_IP_OPA4_IN,   ///< 运放4负端输入 不支持
    HAL_CMP_CHANNEL_IP_OPA4_OUTP, ///< 运放4正端输出 不支持
    HAL_CMP_CHANNEL_IP_OPA4_OUTN, ///< 运放4负端输出 不支持
    HAL_CMP_CHANNEL_IP_OPA5_IP,   ///< 运放4正端输入 不支持
    HAL_CMP_CHANNEL_IP_OPA5_IN,   ///< 运放5负端输入 不支持
    HAL_CMP_CHANNEL_IP_OPA5_OUTP, ///< 运放5正端输出 不支持
    HAL_CMP_CHANNEL_IP_OPA5_OUTN, ///< 运放5负端输出 不支持

} HAL_CMP_CHANNEL_IPx;
typedef enum
{
    HAL_CMP_CHANNEL_IN_0,   ///< CMPN选择为外部负端输入
    HAL_CMP_CHANNEL_IN_REF, ///< CMPN选择为内部基准源
    HAL_CMP_CHANNEL_IN_DAC, ///< CMPN选择为DAC输出
    HAL_CMP_CHANNEL_IN_MID, ///< CMPN选择为反电动势虚拟中心点
} HAL_CMP_CHANNEL_INx;
// 初始化
void HAL_CMP_Reset(HAL_CMP_x);   // 模块复位
void HAL_CMP_Enable(HAL_CMP_x);  // 模块打开
void HAL_CMP_Disable(HAL_CMP_x); // 模块关闭

void HAL_CMP_SetChannel(HAL_CMP_x, HAL_CMP_CHANNEL_IPx, HAL_CMP_CHANNEL_INx); // 设置CMP输入信号通道
uint32_t HAL_CMP_SetDigitalFilter(HAL_CMP_x, uint32_t filter);                // 设置数字滤波宽度 0-0xffff 单位MCU时钟周期,根据芯片不同,实际滤波宽度可能会有差异,返回值,实际滤波宽度
void HAL_CMP_SetGpioOut(HAL_CMP_x, HAL_GPIO_Px i2cgpio);
void HAL_CMP_SetGpioIn(HAL_CMP_x, HAL_GPIO_Px cmpgpio);
uint8_t HAL_CMP_GetData(HAL_CMP_x);

#endif
