/*
 * @Description: 
 * @Author: yangsw
 * @Date: 2022-06-23 13:53:11
 * @LastEditTime: 2023-10-31 11:26:08
 * @LastEditors: LiDa
 * @Reference: 
 */

#include "sr_bsp_pulse.h"
#include "com/svc/svc_com.h"
//#include "log/svc/svc_log.h"
#include "timer/svc/svc_timer.h"

#include "sr_osal.h"
#include "sr_gpio.h"

static void UserPulseInCb(void *args)
{


}
static SR_GPIO_IRQ_CB_T pulseInCb = {.func=UserPulseInCb,.args=SR_NULL};

/**
 * @brief: 计量模块初始化
 * @param  {void}
 * @return {void}
 * @note: 
 * @see: 
 */
void Bsp_Pulse_Init(void)
{
    SR_GPIO_CFG_T config = {0};

    //关总中断
    SR_DisableIrq();

    //PULSE_IN 
    config.gpioMode = SR_GPIO_IRQ;       // 中断输入
    config.pull = SR_GPIO_NP;            // 无上下拉
    config.irqMode = BSP_GPIO_IRQ_DOUBLE; //双边沿
    config.irqCb = &pulseInCb;           //中断回调
    SR_HalGpioInit(PULSE_IN_GPIO_ID, &config);  // 初始化

    //PULSE_CTRL 
    SR_HalGpioInit(PULSE_CTRL_GPIO_ID, &config);  // 初始化

    //PULSE_EN
    config.gpioMode = SR_GPIO_OUTPUT;     // 推挽输出
    config.initLvl = SR_GPIO_LOW;         // 初始低电平
    SR_HalGpioInit(PULSE_EN_GPIO_ID, &config);  // 初始化

    //开总中断
    SR_EnableIrq();
}


/**
 * @brief: Pulse电源开
 * @param  {void}
 * @return {void}
 * @note: 
 * @see: 
 */
void Bsp_Pulse_Power_On(void)
{
    SR_HalGpioWrite(PULSE_EN_GPIO_ID,SR_GPIO_LOW);  //置低
}

/**
 * @brief: Pulse电源关
 * @param  {void}
 * @return {void}
 * @note: 
 * @see: 
 */
void Bsp_Pulse_Power_Off(void)
{
    SR_HalGpioWrite(PULSE_EN_GPIO_ID,SR_GPIO_HIGH);  //置高
}



/**
 * @brief: Pulse获取协议数据开
 * @param  {void}
 * @return {void}
 * @note: 
 * @see: 
 */
void Bsp_Pulse_GetN_On(void){
    
    GPIO_InitType GPIO_InitStructure;
    GPIO_InitStructure.Pin        = PULSE_CTRL_GPIO_PIN;
    GPIO_InitStructure.GPIO_Pull  = GPIO_No_Pull;
    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_Out_PP;
    GPIO_InitPeripheral(PULSE_CTRL_GPIO_PORT, &GPIO_InitStructure);
    
	GPIO_SetBits(PULSE_CTRL_GPIO_PORT, PULSE_CTRL_GPIO_PIN);
	Svc_Timer_Delay_Ms(500);//500			
	GPIO_ResetBits(PULSE_CTRL_GPIO_PORT, PULSE_CTRL_GPIO_PIN); 
    //开中断
    GPIO_InitStructure.Pin        = PULSE_CTRL_GPIO_PIN;    
    GPIO_InitStructure.GPIO_Pull  = GPIO_No_Pull;//GPIO_Pull_Up
    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_Input;
    GPIO_InitPeripheral(PULSE_CTRL_GPIO_PORT, &GPIO_InitStructure);  
}

/**
 * @brief: Pulse获取协议数据开
 * @param  {void}
 * @return {void}
 * @note: 
 * @see: 
 */
void Bsp_Pulse_Clear_Run_State(void){
    
    GPIO_InitType GPIO_InitStructure;
    GPIO_InitStructure.Pin        = PULSE_CTRL_GPIO_PIN;
    GPIO_InitStructure.GPIO_Pull  = GPIO_No_Pull;
    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_Out_PP;
    GPIO_InitPeripheral(PULSE_CTRL_GPIO_PORT, &GPIO_InitStructure);
    
	GPIO_SetBits(PULSE_CTRL_GPIO_PORT, PULSE_CTRL_GPIO_PIN);
	Svc_Timer_Delay_Ms(1000);			
	GPIO_ResetBits(PULSE_CTRL_GPIO_PORT, PULSE_CTRL_GPIO_PIN); 
    //开中断
    GPIO_InitStructure.Pin        = PULSE_CTRL_GPIO_PIN;    
    GPIO_InitStructure.GPIO_Pull  = GPIO_No_Pull;//GPIO_Pull_Up
    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_Input;
    GPIO_InitPeripheral(PULSE_CTRL_GPIO_PORT, &GPIO_InitStructure);  
}

/**
 * @brief: Pulse获取协议数据开
 * @param  {void}
 * @return {void}
 * @note: 
 * @see: 
 */
void Bsp_Pulse_Clear_Calibrate_State(void){
    
    GPIO_InitType GPIO_InitStructure;
    GPIO_InitStructure.Pin        = PULSE_CTRL_GPIO_PIN;
    GPIO_InitStructure.GPIO_Pull  = GPIO_No_Pull;
    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_Out_PP;
    GPIO_InitPeripheral(PULSE_CTRL_GPIO_PORT, &GPIO_InitStructure);
    
	GPIO_SetBits(PULSE_CTRL_GPIO_PORT, PULSE_CTRL_GPIO_PIN);
	Svc_Timer_Delay_Ms(750);			
	GPIO_ResetBits(PULSE_CTRL_GPIO_PORT, PULSE_CTRL_GPIO_PIN); 
    //开中断
    GPIO_InitStructure.Pin        = PULSE_CTRL_GPIO_PIN;    
    GPIO_InitStructure.GPIO_Pull  = GPIO_No_Pull;//GPIO_Pull_Up
    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_Input;
    GPIO_InitPeripheral(PULSE_CTRL_GPIO_PORT, &GPIO_InitStructure);  
}

/**
 * @brief  Configures the different GPIO ports.
 */
static void GPIO_USART1_Configuration(void)
{
    GPIO_InitType GPIO_InitStructure;
    /* Initialize GPIO_InitStructure */
    GPIO_InitStruct(&GPIO_InitStructure);
    /* Configure USARTx Tx as alternate function push-pull */
    // GPIO_InitStructure.Pin            = PULSE_UART_TxPin;
    // GPIO_InitStructure.GPIO_Mode      = GPIO_Mode_AF_PP;
    // GPIO_InitStructure.GPIO_Alternate = PULSE_USART_Tx_GPIO_AF;
    // GPIO_InitPeripheral(PULSE_UART_TX_GPIO, &GPIO_InitStructure);
    /* Configure USARTx Rx as alternate function push-pull and pull-up */
    GPIO_InitStructure.Pin            = PULSE_UART_RxPin;
    GPIO_InitStructure.GPIO_Alternate = PULSE_USART_Rx_GPIO_AF;
    GPIO_InitPeripheral(PULSE_UART_RX_GPIO, &GPIO_InitStructure);

}


static void NVIC_USART1_Configuration(void)
{
    NVIC_InitType NVIC_InitStructure;

    /* Configure the NVIC Preemption Priority Bits */
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
    /* Enable the USARTy Interrupt */
    NVIC_InitStructure.NVIC_IRQChannel            = USART1_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd         = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
}

/**
 * @brief: 串口初始化
 * @param  {void}
 * @return {void}
 * @note: 
 * @see: 
 */
void Pulse_Uart_Init(void)
{

    USART_InitType USART_InitStructure;
    /* System Clocks Configuration */
    // RCC_Configuration();
        /* Enable GPIO clock */
    GPIO_APBxClkCmd(RCC_APB2_PERIPH_GPIOA, ENABLE);
    /* Enable USARTx Clock */
    USART_APBxClkCmd(RCC_APB2_PERIPH_USART1, ENABLE);
    /* Configure the GPIO ports */
    GPIO_USART1_Configuration();
    NVIC_USART1_Configuration();
    /* USARTy and USARTz configuration ------------------------------------------------------*/
    USART_StructInit(&USART_InitStructure);
    USART_InitStructure.BaudRate            = 2400;//2400
    USART_InitStructure.WordLength          = USART_WL_8B;
    USART_InitStructure.StopBits            = USART_STPB_1;
    USART_InitStructure.Parity              = USART_PE_NO;
    USART_InitStructure.HardwareFlowControl = USART_HFCTRL_NONE;
    USART_InitStructure.Mode                = USART_MODE_RX;
    /* Configure USARTx */
    USART_Init(PULSE_UART, &USART_InitStructure);
    USART_ConfigInt(PULSE_UART, USART_INT_RXDNE, ENABLE);
    /* Enable the USARTx */
    USART_Enable(PULSE_UART, ENABLE); 
}

/**
 * @name: 
 * @return {*}
 * @note: 
 */
void Bsp_Pulse_Uart_DeInit(void){
    USART_DeInit(PULSE_UART);
    USART_Enable(PULSE_UART, DISABLE);

    // GPIO_InitType GPIO_InitStructure;
    // GPIO_InitStruct(&GPIO_InitStructure);
    // GPIO_InitStructure.Pin        = PULSE_UART_TxPin;
    // GPIO_InitStructure.GPIO_Pull  = GPIO_No_Pull;
    // GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_Out_PP;
    // GPIO_InitPeripheral(GPIOA, &GPIO_InitStructure);

}