stm8_tsl_multichannelkey.c

来自「STM8s」· C语言 代码 · 共 1,875 行 · 第 1/4 页

C
1,875
字号
}

/**
  ******************************************************************************
  * @brief Select I/Os to burst and call IO driver for burst sequence.
  * @par Parameters:
  * None
  * @retval void None
  * @par Required preconditions:
  * None
  ******************************************************************************
  */
void TSL_MCKey1_Acquisition(void)
{

  KeyIndex = 0;
  TSL_MCKey_SetStructPointer();

#ifdef CHARGE_TRANSFER
  if (!((pMCKeyStruct->State.whole == ERROR_STATE) || (pMCKeyStruct->State.whole == DISABLED_STATE)))
  {
    for (ChannelIndex = 0; ChannelIndex < CHANNEL_PER_MCKEY; ChannelIndex++)
    {
      sTouchIO.PORT_ADDR = (GPIO_TypeDef *)(Table_MCKEY_PORTS[ChannelIndex]);
      sTouchIO.AcqMask = Table_MCKEY_BITS[ChannelIndex];
      sTouchIO.Measurement = &sMCKeyInfo[0].Channel[ChannelIndex].LastMeas;
      sTouchIO.Type = MCKEY_TYPE;
      sMCKeyInfo[0].Channel[ChannelIndex].LastMeas = Channel_address_tab[MCKEY1_LOOK_TABLE[ChannelIndex][0]]->Measure[MCKEY1_LOOK_TABLE[ChannelIndex][1]];
    }
  }

#else /*RC is used*/
  if (!((pMCKeyStruct->State.whole == ERROR_STATE) || (pMCKeyStruct->State.whole == DISABLED_STATE)))
  {
    for (ChannelIndex = 0; ChannelIndex < CHANNEL_PER_MCKEY; ChannelIndex++)
    {
      sTouchIO.PORT_ADDR = (GPIO_TypeDef *)(Table_MCKEY_PORTS[ChannelIndex]);
      sTouchIO.AcqMask = Table_MCKEY_BITS[ChannelIndex];
      sTouchIO.DriveMask = (u8)(sTouchIO.AcqMask | MCKEY1_DRIVEN_SHIELD_MASK);
      sTouchIO.Measurement = &sMCKeyInfo[0].Channel[ChannelIndex].LastMeas;
      sTouchIO.RejectedNb = &sMCKeyInfo[0].Channel[ChannelIndex].LastMeasRejectNb;
      sTouchIO.Type = MCKEY_TYPE;
      TSL_IO_Acquisition(MCKEY_ACQ_NUM, MCKEY_ADJUST_LEVEL);
    }
  }
#endif

}


#if NUMBER_OF_MULTI_CHANNEL_KEYS > 1
/**
  ******************************************************************************
  * @brief Select I/Os to burst and call IO driver for burst sequence.
  * @par Parameters:
  * None
  * @retval void None
  * @par Required preconditions:
  * None
  ******************************************************************************
  */
void TSL_MCKey2_Acquisition(void)
{

  KeyIndex = 1;
  TSL_MCKey_SetStructPointer();

#ifdef CHARGE_TRANSFER
  if (!((pMCKeyStruct->State.whole == ERROR_STATE) || (pMCKeyStruct->State.whole == DISABLED_STATE)))
  {
    for (ChannelIndex = 0; ChannelIndex < CHANNEL_PER_MCKEY; ChannelIndex++)
    {
      sTouchIO.PORT_ADDR = (GPIO_TypeDef *)(Table_MCKEY_PORTS[CHANNEL_PER_MCKEY + ChannelIndex]);
      sTouchIO.AcqMask = Table_MCKEY_BITS[CHANNEL_PER_MCKEY + ChannelIndex];
      sTouchIO.Measurement = &sMCKeyInfo[1].Channel[ChannelIndex].LastMeas;
      sTouchIO.Type = MCKEY_TYPE;
      sMCKeyInfo[1].Channel[ChannelIndex].LastMeas = Channel_address_tab[MCKEY2_LOOK_TABLE[ChannelIndex][0]]->Measure[MCKEY2_LOOK_TABLE[ChannelIndex][1]];
    }
  }
#else /*RC is used*/
  if (!((pMCKeyStruct->State.whole == ERROR_STATE) || (pMCKeyStruct->State.whole == DISABLED_STATE)))
  {
    for (ChannelIndex = 0; ChannelIndex < CHANNEL_PER_MCKEY; ChannelIndex++)
    {
      sTouchIO.PORT_ADDR = (GPIO_TypeDef *)(Table_MCKEY_PORTS[CHANNEL_PER_MCKEY + ChannelIndex]);
      sTouchIO.AcqMask = Table_MCKEY_BITS[CHANNEL_PER_MCKEY + ChannelIndex];
      sTouchIO.DriveMask = (u8)(sTouchIO.AcqMask | MCKEY2_DRIVEN_SHIELD_MASK);
      sTouchIO.Measurement = &sMCKeyInfo[1].Channel[ChannelIndex].LastMeas;
      sTouchIO.RejectedNb = &sMCKeyInfo[1].Channel[ChannelIndex].LastMeasRejectNb;
      sTouchIO.Type = MCKEY_TYPE;
      TSL_IO_Acquisition(MCKEY_ACQ_NUM, MCKEY_ADJUST_LEVEL);
    }
  }
#endif

}
#endif


/**
  ******************************************************************************
  * @brief After Touch Sensing acquisition, data processing.
  * @par Parameters:
  * None
  * @retval void None
  * @par Required preconditions:
  * None
  ******************************************************************************
  */
void TSL_MCKey_Process(void)
{

  TSL_MCKey_SetStructPointer();

  switch (pMCKeyStruct->State.whole)
  {
    case IDLE_STATE:
      if (TSL_MCKey_CheckErrorCondition())
      {
        TSL_MCKey_SetErrorState();
        break;
      }
      TSL_MCKey_IdleTreatment();
      TSL_MCKey_CheckDisabled();
      break;

    case PRE_DETECTED_STATE:
      TSL_MCKey_PreDetectTreatment();
      break;

    case DETECTED_STATE:
      if (TSL_MCKey_CheckErrorCondition())
      {
        TSL_MCKey_SetErrorState();
        break;
      }
      TSL_MCKey_DetectedTreatment();
      TSL_MCKey_CheckDisabled();
      break;

    case POST_DETECTED_STATE:
      TSL_MCKey_PostDetectTreatment();
      break;

    case PRE_CALIBRATION_STATE:
      TSL_MCKey_PreRecalibrationTreatment();
      break;

    case CALIBRATION_STATE:
      if (TSL_MCKey_CheckErrorCondition())
      {
        TSL_MCKey_SetErrorState();
        break;
      }
      TSL_MCKey_CalibrationTreatment();
      TSL_MCKey_CheckDisabled();
      break;

    case ERROR_STATE:
      TSL_MCKey_CheckDisabled();
      break;

    case DISABLED_STATE:
      TSL_MCKey_CheckEnabled();
      break;

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

  TSL_TempGlobalSetting.whole |= pMCKeyStruct->Setting.whole;
  TSL_TempGlobalState.whole |= pMCKeyStruct->State.whole;
  pMCKeyStruct->Setting.b.CHANGED = 0;
  pMCKeyStruct->Setting.b.POSCHANGED = 0;

}


/**
  ******************************************************************************
  * @brief Check for MCKey info during Idle state: Verify detection and recalibration.
  * @par Parameters:
  * None
  * @retval void None
  * @par Required preconditions:
  * None
  ******************************************************************************
  */
void TSL_MCKey_IdleTreatment(void)
{

  /* Check all channels to enter in Calibration state if needed */
  for (ChannelIndex = 0; ChannelIndex < CHANNEL_PER_MCKEY; ChannelIndex++)
  {

#if !defined(CHARGE_TRANSFER)
    /* Ignore channel if too much noise is detected */
    if (pMCKeyStruct->Channel[ChannelIndex].LastMeasRejectNb > MAX_REJECTED_MEASUREMENTS)
    {
      break;
    }
#endif

    TSL_MCKey_DeltaCalculation(ChannelIndex);

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

  }

  /* Check all channels to enter in Pre-detect state if needed */
  for (ChannelIndex = 0; ChannelIndex < CHANNEL_PER_MCKEY; ChannelIndex++)
  {

#if !defined(CHARGE_TRANSFER)
    /* Ignore channel if too much noise is detected */
    if (pMCKeyStruct->Channel[ChannelIndex].LastMeasRejectNb > MAX_REJECTED_MEASUREMENTS)
    {
      break;
    }
#endif

    TSL_MCKey_DeltaCalculation(ChannelIndex);

#if NEGDETECT_AUTOCAL == 1
    if (Delta >= pMCKeyStruct->DetectThreshold)
#else
    if ((Delta >= pMCKeyStruct->DetectThreshold) || (Delta <= pMCKeyStruct->RecalibrationThreshold))
#endif
    {
      TSL_MCKey_SetPreDetectState();
      if (!DetectionIntegrator)
      {
        pMCKeyStruct->Channel[0].IntegratorCounter++;
        TSL_MCKey_PreDetectTreatment();
      }
      return;
    }

  }

}


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

  u8 ConditionLeaveDetect = 1; /* Per default we go back to idle state */

  /* Go back in idle state if ALL channels are below the detect threshold */
  for (ChannelIndex = 0; ChannelIndex < CHANNEL_PER_MCKEY; ChannelIndex++)
  {
    TSL_MCKey_DeltaCalculation(ChannelIndex);
#if NEGDETECT_AUTOCAL == 1
#if !defined(CHARGE_TRANSFER)
    if ((pMCKeyStruct->Channel[ChannelIndex].LastMeasRejectNb <= MAX_REJECTED_MEASUREMENTS) &&
        (Delta >= pMCKeyStruct->DetectThreshold))
#else
    if (Delta >= pMCKeyStruct->DetectThreshold)
#endif
#else
#if !defined(CHARGE_TRANSFER)
    if ((pMCKeyStruct->Channel[ChannelIndex].LastMeasRejectNb <= MAX_REJECTED_MEASUREMENTS) &&
        ((Delta >= pMCKeyStruct->DetectThreshold) || (Delta <= pMCKeyStruct->RecalibrationThreshold)))
#else
      if ((Delta >= pMCKeyStruct->DetectThreshold) || (Delta <= pMCKeyStruct->RecalibrationThreshold))
#endif
#endif
    {
      ConditionLeaveDetect = 0;
      break;
    }
  }

  if (ConditionLeaveDetect)
  {
    TSL_MCKey_BackToIdleState();
    return;
  }
  else
  {
    TSL_MCKey_DxS();
    pMCKeyStruct->Channel[0].IntegratorCounter--;
    if (!pMCKeyStruct->Channel[0].IntegratorCounter)
    {
      TSL_MCKey_Position();
      TSL_MCKey_SetDetectedState();
    }
  }

}


/**
  ******************************************************************************
  * @brief Check MCKey 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_MCKey_DetectedTreatment(void)
{

  u8 ConditionLeaveDetect = 1;

  /* Go in post-detect state if ALL channels are below or equal the end-detect threshold */
  for (ChannelIndex = 0; ChannelIndex < CHANNEL_PER_MCKEY; ChannelIndex++)
  {
    TSL_MCKey_DeltaCalculation(ChannelIndex);
#if NEGDETECT_AUTOCAL == 1
#if !defined(CHARGE_TRANSFER)
    if ((pMCKeyStruct->Channel[ChannelIndex].LastMeasRejectNb <= MAX_REJECTED_MEASUREMENTS) &&
        (Delta > pMCKeyStruct->EndDetectThreshold))
#else
    if (Delta > pMCKeyStruct->EndDetectThreshold)
#endif
#else
#if !defined(CHARGE_TRANSFER)
    if ((pMCKeyStruct->Channel[ChannelIndex].LastMeasRejectNb <= MAX_REJECTED_MEASUREMENTS) &&
        (((Delta > pMCKeyStruct->EndDetectThreshold) && (Delta > 0)) ||
         ((Delta < pMCKeyStruct->RecalibrationThreshold) && (Delta < 0))))
#else
      if (((Delta > pMCKeyStruct->EndDetectThreshold) && (Delta > 0)) ||
          ((Delta < pMCKeyStruct->RecalibrationThreshold) && (Delta < 0)))
#endif
#endif
    {
      ConditionLeaveDetect = 0;
      break;
    }
  }

  if (ConditionLeaveDetect)
  {
    TSL_MCKey_SetPostDetectState();
    if (!EndDetectionIntegrator)
    {
      pMCKeyStruct->Channel[0].IntegratorCounter++;
      TSL_MCKey_PostDetectTreatment();
    }
    return;
  }

  /* Detection TimeOut only if there is no change in the slider/wheel position */
  if (TSL_MCKey_Position() == 0x00)
  {
    TSL_MCKey_DetectionTimeout();
  }

}


/**
  ******************************************************************************
  * @brief Check MCKey info during POST DETECT state: Verify end of detection.
  * @par Parameters:
  * None
  * @retval void None
  * @par Required preconditions:
  * None
  ******************************************************************************
  */
void TSL_MCKey_PostDetectTreatment(void)
{

  u8 ConditionLeaveDetect = 1;

  /* Go in idle state if ALL channels are below or equal the end-detect threshold */
  for (ChannelIndex = 0; ChannelIndex < CHANNEL_PER_MCKEY; ChannelIndex++)
  {
    TSL_MCKey_DeltaCalculation(ChannelIndex);
#if NEGDETECT_AUTOCAL == 1
#if !defined(CHARGE_TRANSFER)
    if ((pMCKeyStruct->Channel[ChannelIndex].LastMeasRejectNb <= MAX_REJECTED_MEASUREMENTS) &&
        (Delta > pMCKeyStruct->EndDetectThreshold))
#else
    if (Delta > pMCKeyStruct->EndDetectThreshold)
#endif
#else
#if !defined(CHARGE_TRANSFER)
    if ((pMCKeyStruct->Channel[ChannelIndex].LastMeasRejectNb <= MAX_REJECTED_MEASUREMENTS) &&
        (((Delta > pMCKeyStruct->EndDetectThreshold) && (Delta > 0)) ||
         ((Delta < pMCKeyStruct->RecalibrationThreshold) && (Delta < 0))))
#else
      if (((Delta > pMCKeyStruct->EndDetectThreshold) && (Delta > 0)) ||
          ((Delta < pMCKeyStruct->RecalibrationThreshold) && (Delta < 0)))
#endif
#endif
    {
      ConditionLeaveDetect = 0;
      break;
    }
  }

  if (ConditionLeaveDetect)
  {
    pMCKeyStruct->Channel[0].IntegratorCounter--;
    if (!pMCKeyStruct->Channel[0].IntegratorCounter)
    {
      TSL_MCKey_SetIdleState();
    }
  }
  else
  {
    // No reset of DTO counter.
    TSL_MCKey_BackToDetectedState();
  }
}


/**
  ******************************************************************************
  * @brief Check MCKey info during PRE RECALIBRATION state: Verify condition for recalibration.
  * @par Parameters:
  * None
  * @retval void None
  * @par Required preconditions:
  * None
  ******************************************************************************
  */
void TSL_MCKey_PreRecalibrationTreatment(void)
{

  u8 ConditionLeaveCalib = 1;

  /* Go in calibration state if at least ONE channel is below the calibration threshold */
  for (ChannelIndex = 0; ChannelIndex < CHANNEL_PER_MCKEY; ChannelIndex++)
  {
    TSL_MCKey_DeltaCalculation(ChannelIndex);
#if !defined(CHARGE_TRANSFER)
    if ((pMCKeyStruct->Channel[ChannelIndex].LastMeasRejectNb <= MAX_REJECTED_MEASUREMENTS) &&
        (Delta <= pMCKeyStruct->RecalibrationThreshold))
#else
    if (Delta <= pMCKeyStruct->RecalibrationThreshold)
#endif
    {
      ConditionLeaveCalib = 0;
      break;
    }
  }

  if (!ConditionLeaveCalib)
  {
    pMCKeyStruct->Channel[0].IntegratorCounter--;
    if (!pMCKeyStruct->Channel[0].IntegratorCounter)
    {
      TSL_MCKey_SetCalibrationState();

⌨️ 快捷键说明

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