// CPLD使能驱动
// 杭州秒寻科技有限公司
// 周涛
// 2025-03-31

#include "includes.h"

// 模块所用管脚初始化
void driver_cpld_init()
{
    GPIO_InitTypeDef stGpio = {0};

    stGpio.GPIO_Mode = GPIO_Mode_OUT;
    stGpio.GPIO_OType = GPIO_OType_PP;
    stGpio.GPIO_Speed = GPIO_Speed_50MHz;
    stGpio.GPIO_PuPd = GPIO_PuPd_NOPULL;

    // CPLD使能管脚
    RCC_AHB1PeriphClockCmd(DRIVER_CPLD_CS_CLK, ENABLE);
    stGpio.GPIO_Pin = DRIVER_CPLD_CS_PIN;
    GPIO_DeInit(DRIVER_CPLD_CS_PORT);
    GPIO_Init(DRIVER_CPLD_CS_PORT, &stGpio);

    // 北斗GPS使能管脚
    RCC_AHB1PeriphClockCmd(DRIVER_CPLD_BDGPS_CLK, ENABLE);
    stGpio.GPIO_Pin = DRIVER_CPLD_BDGPS_PIN;
    GPIO_DeInit(DRIVER_CPLD_BDGPS_PORT);
    GPIO_Init(DRIVER_CPLD_BDGPS_PORT, &stGpio);

    // 失能
    driver_cpld_cs_enable(Bit_RESET);
    driver_cpld_bdgps_enable(Bit_RESET);
}

// CPLD使能
// byEnable:0：失能，1：使能
void driver_cpld_cs_enable(uint8 byEnable)
{
    if (byEnable > 0)
    {
        GPIO_WriteBit(DRIVER_CPLD_CS_PORT, DRIVER_CPLD_CS_PIN, Bit_SET);
    }
    else
    {
        GPIO_WriteBit(DRIVER_CPLD_CS_PORT, DRIVER_CPLD_CS_PIN, Bit_RESET);
    }
}

// 北斗GPS使能
// byEnable:0：失能，1：使能
void driver_cpld_bdgps_enable(uint8 byEnable)
{
    if (byEnable > 0)
    {
        GPIO_WriteBit(DRIVER_CPLD_BDGPS_PORT, DRIVER_CPLD_BDGPS_PIN, Bit_SET);
    }
    else
    {
        GPIO_WriteBit(DRIVER_CPLD_BDGPS_PORT, DRIVER_CPLD_BDGPS_PIN, Bit_RESET);
    }
}
