stm8_tsl_singlechannelkey.c

来自「STM8s」· C语言 代码 · 共 666 行 · 第 1/2 页

C
666
字号
      TSL_SCKey_PreRecalibrationTreatment();
      break;

    case CALIBRATION_STATE:
      if (TSL_SCKey_CheckErrorCondition())
      {
        TSL_SCKey_SetErrorState();
        break;
      }
      TSL_SCKey_CalibrationTreatment();
      TSL_SCKey_CheckDisabled();
      break;

    case ERROR_STATE:
      TSL_SCKey_CheckDisabled();
      break;

    case DISABLED_STATE:
      TSL_SCKey_CheckEnabled();
      break;

    default:
      for (;;)
      {
        // Infinite loop.
      }

  }

  TSL_TempGlobalSetting.whole |= pKeyStruct->Setting.whole;
  TSL_TempGlobalState.whole |= pKeyStruct->State.whole;
  pKeyStruct->Setting.b.CHANGED = 0;
}


/**
  ******************************************************************************
  * @brief Check SCKey info during Idle state: Verify detection and recalibration.
  * @par Parameters:
  * None
  * @retval void None
  * @par Required preconditions:
  * None
  ******************************************************************************
  */
void TSL_SCKey_IdleTreatment(void)
{
#if !defined(CHARGE_TRANSFER)
  /* Ignore channel if too much noise is detected */
  if (pKeyStruct->Channel.LastMeasRejectNb > MAX_REJECTED_MEASUREMENTS)
  {
    return;
  }
#endif

#if NEGDETECT_AUTOCAL == 1
  if (Delta <= pKeyStruct->RecalibrationThreshold)
  {
    TSL_SCKey_SetPreRecalibrationState();
    return;
  }
#endif

#if NEGDETECT_AUTOCAL == 1
  if (Delta >= pKeyStruct->DetectThreshold)
#else
  if ((Delta >= pKeyStruct->DetectThreshold) || (Delta <= pKeyStruct->RecalibrationThreshold))
#endif
  {
    TSL_SCKey_SetPreDetectState();
    if (!DetectionIntegrator)
    {
      pKeyStruct->Channel.IntegratorCounter++;
      TSL_SCKey_PreDetectTreatment();
    }
  }
}


/**
  ******************************************************************************
  * @brief Check SCKey info during PRE DETECT state: Verify detection integrator and detection exclusion.
  * @par Parameters:
  * None
  * @retval void None
  * @par Required preconditions:
  * None
  ******************************************************************************
  */
void TSL_SCKey_PreDetectTreatment(void)
{

#if NEGDETECT_AUTOCAL == 1
#if !defined(CHARGE_TRANSFER)
  if ((pKeyStruct->Channel.LastMeasRejectNb <= MAX_REJECTED_MEASUREMENTS) &&
      (Delta >= pKeyStruct->DetectThreshold))
#else
  if (Delta >= pKeyStruct->DetectThreshold)
#endif
#else
#if !defined(CHARGE_TRANSFER)
  if ((pKeyStruct->Channel.LastMeasRejectNb <= MAX_REJECTED_MEASUREMENTS) &&
      ((Delta >= pKeyStruct->DetectThreshold) || (Delta <= pKeyStruct->RecalibrationThreshold)))
#else
    if ((Delta >= pKeyStruct->DetectThreshold) || (Delta <= pKeyStruct->RecalibrationThreshold))
#endif
#endif
  {
    TSL_SCKey_DxS();
    pKeyStruct->Channel.IntegratorCounter--;
    if (!pKeyStruct->Channel.IntegratorCounter)
    {
      TSL_SCKey_SetDetectedState();
    }
  }
  else
  {
    TSL_SCKey_BackToIdleState();
    return;
  }
}


/**
  ******************************************************************************
  * @brief Check SCKey info during DETECTED state: Verify detection timeout, end of detection and detection exclusion.
  * @par Parameters:
  * None
  * @retval void None
  * @par Required preconditions:
  * None
  ******************************************************************************
  */
void TSL_SCKey_DetectedTreatment(void)
{
#if NEGDETECT_AUTOCAL == 1
#if !defined(CHARGE_TRANSFER)
  if ((pKeyStruct->Channel.LastMeasRejectNb <= MAX_REJECTED_MEASUREMENTS) &&
      (Delta <= pKeyStruct->EndDetectThreshold))
#else
  if (Delta <= pKeyStruct->EndDetectThreshold)
#endif
#else
#if !defined(CHARGE_TRANSFER)
  if ((pKeyStruct->Channel.LastMeasRejectNb <= MAX_REJECTED_MEASUREMENTS) &&
      (((Delta <= pKeyStruct->EndDetectThreshold) && (Delta > 0)) ||
       ((Delta >= pKeyStruct->RecalibrationThreshold) && (Delta < 0))))
#else
    if (((Delta <= pKeyStruct->EndDetectThreshold) && (Delta > 0)) ||
        ((Delta >= pKeyStruct->RecalibrationThreshold) && (Delta < 0)))
#endif
#endif
  {
    TSL_SCKey_SetPostDetectState();
    if (!EndDetectionIntegrator)
    {
      pKeyStruct->Channel.IntegratorCounter++;
      TSL_SCKey_PostDetectTreatment();
    }
    return;
  }

  TSL_SCKey_DetectionTimeout();
}


/**
  ******************************************************************************
  * @brief Check SCKey info during POST DETECT state: Verify end of detection.
  * @par Parameters:
  * None
  * @retval void None
  * @par Required preconditions:
  * None
  ******************************************************************************
  */
void TSL_SCKey_PostDetectTreatment(void)
{
#if NEGDETECT_AUTOCAL == 1
#if !defined(CHARGE_TRANSFER)
  if ((pKeyStruct->Channel.LastMeasRejectNb <= MAX_REJECTED_MEASUREMENTS) &&
      (Delta <= pKeyStruct->EndDetectThreshold))
#else
  if (Delta <= pKeyStruct->EndDetectThreshold)
#endif
#else
#if !defined(CHARGE_TRANSFER)
  if ((pKeyStruct->Channel.LastMeasRejectNb <= MAX_REJECTED_MEASUREMENTS) &&
      (((Delta <= pKeyStruct->EndDetectThreshold) && (Delta > 0)) ||
       ((Delta >= pKeyStruct->RecalibrationThreshold) && (Delta < 0))))
#else
    if (((Delta <= pKeyStruct->EndDetectThreshold) && (Delta > 0)) ||
        ((Delta >= pKeyStruct->RecalibrationThreshold) && (Delta < 0)))
#endif
#endif
  {
    pKeyStruct->Channel.IntegratorCounter--;
    if (!pKeyStruct->Channel.IntegratorCounter)
    {
      TSL_SCKey_SetIdleState();
    }
  }
  else
  {
    // No reset of DTO counter.
    TSL_SCKey_BackToDetectedState();
  }
}


/**
  ******************************************************************************
  * @brief Check SCKey info during PRE RECALIBRATION state: Verify condition for recalibration.
  * @par Parameters:
  * None
  * @retval void None
  * @par Required preconditions:
  * None
  ******************************************************************************
  */
void TSL_SCKey_PreRecalibrationTreatment(void)
{
#if !defined(CHARGE_TRANSFER)
  if ((pKeyStruct->Channel.LastMeasRejectNb <= MAX_REJECTED_MEASUREMENTS) &&
      (Delta <= pKeyStruct->RecalibrationThreshold))
#else
  if (Delta <= pKeyStruct->RecalibrationThreshold)
#endif
  {
    pKeyStruct->Channel.IntegratorCounter--;
    if (!pKeyStruct->Channel.IntegratorCounter)
    {
      TSL_SCKey_SetCalibrationState();
    }
  }
  else
  {
    TSL_SCKey_BackToIdleState();
  }
}


/**
  ******************************************************************************
  * @brief During calibration, calculates the new reference.
  * @par Parameters:
  * None
  * @retval void None
  * @par Required preconditions:
  * None
  ******************************************************************************
  */
void TSL_SCKey_CalibrationTreatment(void)
{
#if !defined(CHARGE_TRANSFER)
  if (pKeyStruct->Channel.LastMeasRejectNb <= MAX_REJECTED_MEASUREMENTS)
#endif
  {
    pKeyStruct->Channel.Reference += pKeyStruct->Channel.LastMeas;
    pKeyStruct->Counter--;
    if (!pKeyStruct->Counter)
    {
      // Warning: Must be divided by SCKEY_CALIBRATION_COUNT_DEFAULT !!!
      pKeyStruct->Channel.Reference = (pKeyStruct->Channel.Reference >> 3);
      TSL_SCKey_SetIdleState();
    }
  }
}


/**
  ******************************************************************************
  * @brief Takes into account the customer code settings in memory to disable the key.
  * @par Parameters:
  * None
  * @retval void None
  * @par Required preconditions:
  * None
  ******************************************************************************
  */
void TSL_SCKey_CheckDisabled(void)
{
  if (!pKeyStruct->Setting.b.ENABLED)
  {
    TSL_SCKey_SetDisabledState();
  }
}


/**
  ******************************************************************************
  * @brief Takes into account the customer code settings in memory to enable the key.
  * @par Parameters:
  * None
  * @retval void None
  * @par Required preconditions:
  * None
  ******************************************************************************
  */
void TSL_SCKey_CheckEnabled(void)
{
  if (pKeyStruct->Setting.b.ENABLED && pKeyStruct->Setting.b.IMPLEMENTED)
  {
    TSL_SCKey_SetCalibrationState();
  }
}


/**
  ******************************************************************************
  * @brief Verification of the last acquisition result to be within the authorized range.
  * @par Parameters:
  * None
  * @retval u8 Returns 0xFF if burst count out of range, 0 if OK.
  * @par Required preconditions:
  * None
  ******************************************************************************
  */
u8 TSL_SCKey_CheckErrorCondition(void)
{
  if ((pKeyStruct->Channel.LastMeas < SCKEY_MIN_ACQUISITION)
      || (pKeyStruct->Channel.LastMeas > SCKEY_MAX_ACQUISITION))
  {
    return 0xFF;  // Error case !
  }

  return 0;
}
#endif
/* Public functions ----------------------------------------------------------*/

/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?