/******************************************************************************
*Copyright(C),2024-2024,万众一芯生
*van_BSP_Com_Pwm.c
*stm32 pwm控制的
*修改历史
author				范义东			新建				2024/01/27
******************************************************************************/

#include "onechip_include_header.h"
#include "van_BSP_Com_Pwm.h"

	
VAN_COM_PWM_Def gcom_pwm_MINI_V2_V1[] = {
    {MINI7_SUB_FUN_MOTOR, NAME(MOTOR), PWM_BUMP_ST_IDLE, COM_PWM_8870, (void *)&htim3, TIM_CHANNEL_2}, 
    {MINI7_SUB_FUN_HEATER1, NAME(HEATER1), PWM_BUMP_ST_IDLE, COM_PWM_8870, (void *)&htim3, TIM_CHANNEL_1},			 
    {MINI7_SUB_FUN_HEATER2, NAME(HEATER2), PWM_BUMP_ST_IDLE, COM_PWM_8870, (void *)&htim1, TIM_CHANNEL_3}
};

unsigned int gcom_pwm_MINI_V2_Num = ARR_LEN(gcom_pwm_MINI_V2_V1);


P_VAN_COM_PWM_Def gcom_pwm_V1;
unsigned int gcom_pwm_V1_Num;

/****************************************************************
 函数名:    loadPidCfg
 创建时间:  2024/01/08
 创建人:    范义东
 函数说明：加载配置文件
 参数:

 返回值 :  加载成功
****************************************************************/
unsigned int load_com_pwm_Cfg(pDeviceBasicInfdef pDevice) {

	gcom_pwm_V1 = gcom_pwm_MINI_V2_V1;
  gcom_pwm_V1_Num = gcom_pwm_MINI_V2_Num;
  P_VAN_COM_PWM_Def pdev = gcom_pwm_V1;
  for (int i = 0; i < gcom_pwm_V1_Num; i++) {
    pdev++;
  }
  return true;
}
/****************************************************************
 * 函数名:    com_pwm_FindChanle
 * 创建时间:    2024/01/08
 * 创建人:    范义东
 * 函数说明：通过ID，查找PWM模块
 * 输入参数: devId
 * 输出参数: pdev 比例阀配置
 * 返回值 :   有配置文件返回true，返回失败
 ****************************************************************/
bool com_pwm_FindChanle(unsigned int devId, P_VAN_COM_PWM_Def *pdev) {
  unsigned char id;
  /*寻找配置文件*/
  for (id = 0; id < gcom_pwm_V1_Num; id++) {
    if (devId == gcom_pwm_V1[id].ID) {
      *pdev = &gcom_pwm_V1[id];
      return true;
    }
  }
  Van_Device_Printf(VAN_LOG_NOTICE, "pwm devId:%d no exit\r\n", devId);
  *pdev = NULL;
  return false;
}

/****************************************************************
 函数名:   com_pwm_Set
 创建时间:  2024/01/08
 创建人:    范义东
 函数说明：设置pwm
 参数: devId 通道
       value pwm值，负值为反向
 返回值 :  加载成功
****************************************************************/
void com_pwm_Set(unsigned int devId, float value) {
  P_VAN_COM_PWM_Def pdev;
//  value ;
  int DutyCycle = value;
  if (DutyCycle > MAX_PWM_VALUE) {
      DutyCycle = MAX_PWM_VALUE;
  }else if(DutyCycle < -MAX_PWM_VALUE) {
      DutyCycle = -MAX_PWM_VALUE;
  }
  if (com_pwm_FindChanle(devId, &pdev)) {
    //    Van_Device_Printf(VAN_LOG_NOTICE, "before pwn %s pluse:%d\n", pdev->Name, DutyCycle);
    if (pdev->dirPort && (pdev->type == COM_PWM_8870 || pdev->type == COM_PWM_8873)) {
			GPIO_PinState PinState =   ((DutyCycle < 0) ^ (pdev->dirState == GPIO_PIN_SET))?GPIO_PIN_SET:GPIO_PIN_RESET;
			HAL_GPIO_WritePin(pdev->dirPort, pdev->dirPin, PinState);
			if (DutyCycle < 0 && pdev->type == COM_PWM_8870) {
				DutyCycle = MAX_PWM_VALUE+DutyCycle;	
			}		
			if (DutyCycle < 0 && pdev->type == COM_PWM_8873) {
				DutyCycle = -DutyCycle;	
			}
    }
    if (DutyCycle < 0) {
      DutyCycle = 0;
    } else if (DutyCycle > MAX_PWM_VALUE) {
      DutyCycle = MAX_PWM_VALUE;
    }
    __HAL_TIM_SET_COMPARE((TIM_HandleTypeDef *)pdev->timerHandle, pdev->pwmChanle, DutyCycle);
    pdev->state = PWM_BUMP_ST_RUN;
    //    Van_Device_Printf(VAN_LOG_NOTICE, "pwn %s pluse:%d\n", pdev->Name, DutyCycle);
  } else {
    Van_Device_Printf(VAN_LOG_ERROR, "pwn %d not support:%d\n", devId);
  }
}

/****************************************************************
 函数名:   com_pwm_Getconter
 创建时间:  2024/01/08
 创建人:    范义东
 函数说明：获取当前脉冲计算器
 参数: devId 通道
       value pwm值，负值为反向
 返回值 :  加载成功
****************************************************************/
unsigned int com_pwm_Getconter(unsigned int devId) {
  P_VAN_COM_PWM_Def pdev;
  if (com_pwm_FindChanle(devId, &pdev)) {
    Van_Device_Printf(VAN_LOG_DEBUG, "time:%d  conter:%d\n", devId, pdev->feedbackconter);
    return pdev->feedbackconter;
  } else {
    Van_Device_Printf(VAN_LOG_ERROR, "pwn %d not support:%d\n", devId);
  }
  return 0;
}

/****************************************************************
 * 函数名:    com_pwm_test
 * 创建时间:    2024/01/08
 * 创建人:    范义东
 * 函数说明：设置PWM输出
 * 输入参数:     runMtsId 通道
 * 输出参数: pTemp 当前温度
 * 返回值 :   成功rue，失败返回false
 ****************************************************************/
bool com_pwm_test(unsigned int Id, int value) {
  P_VAN_COM_PWM_Def pdev;
  float fvalue = value;
  if (com_pwm_FindChanle(Id, &pdev)) {
    com_pwm_Set(Id, fvalue);
    Van_Device_Printf(VAN_LOG_NOTICE, "%s: ID:%d set out:%f\r\n", pdev->Name, Id, fvalue);
  } else {
    Van_Device_Printf(VAN_LOG_NOTICE, "no support  ID:%d\r\n", Id);
  }
  return False;
}

/****************************************************************
 * 函数名:    showIVT
 * 创建时间:    2024/01/08
 * 创建人:    范义东
 * 函数说明：IVT模块查看
 * 输入参数: 无
 * 输出参数: 无
 * 返回值 :   无
 ****************************************************************/
void show_com_pwm(void) {
  P_VAN_COM_PWM_Def pdev;
  char TimeName[16] = {0};
  char FeedTimeCName[16] = {0};
  char DirPortName[16] = {0};
  Van_Device_Printf(VAN_LOG_NOTICE, "devtype:%d strtype:%d hdtype:%d PWM Num:%d\r\n", sysinf.devdesc.deveice_type, sysinf.devdesc.structure_type,
                    sysinf.devdesc.hardware_type, gcom_pwm_V1_Num);
  if (gcom_pwm_V1_Num == 0) {
    Van_Device_Printf(VAN_LOG_NOTICE, "***************no Pwm***************\r\n");
  }

  Van_Device_Printf(VAN_LOG_NOTICE, "%2s %16s %2s %2s %6s %2s %6s %2s %4s %6s %2s %4s\r\n ", " ID ", " name ", "St", "Type", "tim ", "ch ", "ftim", "fch",
                    "con ", "Dport", "pin", "value");
  HAL_Delay(10); 
  pdev = gcom_pwm_V1;
  for (int i = 0; i < gcom_pwm_V1_Num; i++) {
    find_stm32_Peripherals(pdev->timerHandle, TimeName);
    find_stm32_Peripherals(pdev->feedbackTimer, FeedTimeCName);
    find_stm32_Peripherals(pdev->dirPort, DirPortName);

    Van_Device_Printf(VAN_LOG_NOTICE, "%2d %16s %2d  %2x %6s  %2x %6s %2d %4d %6s %2x %4d\r\n", pdev->ID, pdev->Name, pdev->state, pdev->type, TimeName,
                      pdev->pwmChanle, FeedTimeCName, pdev->feedbackChanlen, pdev->feedbackconter, DirPortName, pdev->dirPin, pdev->setValue);
    HAL_Delay(10);
    pdev++;
  }
  return;
}


/****************************************************************
 * 函数名:    PWM_Find_By_CHl
 * 创建时间:    2024/01/08
 * 创建人:    范义东
 * 函数说明：通过ID，查找反馈模块
 * 输入参数: devId
 * 输出参数: pdev 比例阀配置
 * 返回值 :   有配置文件返回true，返回失败
 ****************************************************************/
bool PWM_Find_feed_By_ch(unsigned int devId, P_VAN_COM_PWM_Def *pdev) {
  unsigned char id;
  /*寻找配置文件*/
  for (id = 0; id < gcom_pwm_V1_Num; id++) {
    if (devId == gcom_pwm_V1[id].ID || gcom_pwm_V1[id].feedbackChanlen) {
      *pdev = &gcom_pwm_V1[id];
      return true;
    }
  }
  Van_Device_Printf(VAN_LOG_NOTICE, "pwm feed devId:%d no exit\r\n", devId);
  *pdev = NULL;
  return false;
}

/****************************************************************
 * 函数名:    com_feed_Add_Pulse
 * 创建时间:    2024/01/08
 * 创建人:    范义东
 * 函数说明：通过GPIO_Pin，跟新转到脉冲计数
 * 输入参数: devId
 * 输出参数: pdev 比例阀配置
 * 返回值 :   有配置文件返回true，返回失败
 ****************************************************************/
void com_feed_Add_Pulse(uint16_t GPIO_Pin) {
  P_VAN_COM_PWM_Def pdev = gcom_pwm_V1;
  for (int i = 0; i < gcom_pwm_V1_Num; i++) {
    if (pdev->state == PWM_BUMP_ST_RUN && pdev->feedbackChanlen == GPIO_Pin) {
      pdev->plusconter++;
      //	   		Van_Device_Printf(VAN_LOG_NOTICE, "ID:%d plus:%d\n",pdev->ID,pdev->plusconter);
    }
    pdev++;
  }
}

/****************************************************************
 * 函数名:    check_Pwm_Plus_feedOK
 * 创建时间:    2024/01/08
 * 创建人:    范义东
 * 函数说明：通过plusconter，检查是否转到
 * 输入参数: 无
 * 输出参数: 无
 * 返回值 :   有配置文件返回true，返回失败
 ****************************************************************/
void check_Pwm_Plus_feedOK(void) {
  P_VAN_COM_PWM_Def pdev = gcom_pwm_V1;
  for (int i = 0; i < gcom_pwm_V1_Num; i++) {
    if (pdev->state == PWM_BUMP_ST_RUN && pdev->feedbackChanlen != VAN_CONFIG_NOCARE) {
     
      pdev->plusconter = 0;
    }
    pdev++;
  }
}


/**********************************************************************************
Func    Name: Paltform_Set_PWM_Pause
Descriptions: 平台设置暂停PWM
Input   para: 无
In&Out  Para: 无
Output  para: 无
Return value: 无
***********************************************************************************/
void Paltform_Set_PWM_Pause(unsigned int id) {
  P_VAN_COM_PWM_Def dev;
  // float goal = 0; /*安1000配置占空比*/

  if (com_pwm_FindChanle(id, &dev) == True) {
    com_pwm_Set(id, 0);
    dev->state = PWM_BUMP_ST_IDLE;
  }
}

/**********************************************************************************
Func    Name: Paltform_Set_PWM_Resume
Descriptions: 平台设置恢复PWM
Input   para: 无
In&Out  Para: 无
Output  para: 无
Return value: 无
***********************************************************************************/
void Paltform_Set_PWM_Resume(unsigned int id) {
  P_VAN_COM_PWM_Def dev;

  if (com_pwm_FindChanle(id, &dev) == True) {
    com_pwm_Set(id, dev->setValue);
    dev->state = PWM_BUMP_ST_RUN;
  }
}
