uint32_t TSI_HAL_Recalibrate(TSI_Type * base, tsi_config_t *config, const uint32_t electrodes, const tsi_parameter_limits_t *parLimits)
{
    assert(config != NULL);
    
    uint32_t is_enabled = TSI_HAL_IsModuleEnabled(base);
    uint32_t is_int_enabled = TSI_HAL_IsInterruptEnabled(base);
    uint32_t lowest_signal = TSI_RECALIBRATE_MAX_SIGNAL_VAL;
    
    if (is_enabled) {
        TSI_HAL_DisableModule(base);
    }
    if (is_int_enabled) {
        TSI_HAL_DisableInterrupt(base);
    }

    TSI_HAL_SetNumberOfScans(base, config->nscn);
    TSI_HAL_SetPrescaler(base, config->ps);
    TSI_HAL_SetElectrodeChargeCurrent(base, config->extchrg);
    TSI_HAL_SetReferenceChargeCurrent(base, config->refchrg);

    TSI_HAL_EnableModule(base);

    if (TSI_HAL_MeasurementBlocking(base) == 0) {
        for (uint32_t i = 0U; i < 16U; i++) {
            if (TSI_HAL_GetEnabledChannel(base, i)) {
                int32_t counter = TSI_HAL_GetCounter(base, i);
                if (counter < lowest_signal) {
                    lowest_signal = counter;
                }
            }
        }
    }

    if (!is_enabled) {
        TSI_HAL_EnableModule(base);
    }
    if (is_int_enabled) {
        TSI_HAL_EnableInterrupt(base);
    }
    if (lowest_signal == TSI_RECALIBRATE_MAX_SIGNAL_VAL) {
        lowest_signal = 0U;  /* not valid */
    }

    return lowest_signal;
}
/*FUNCTION**********************************************************************
*
* Function Name : TSI_HAL_SetConfiguration
* Description   : Function set the whole TSI peripheral by handled configuration
*
*END**************************************************************************/
void TSI_HAL_SetConfiguration(TSI_Type * base, tsi_config_t *config)
{
    assert(config != NULL);

    uint32_t is_enabled = TSI_HAL_IsModuleEnabled(base);
    uint32_t is_int_enabled = TSI_HAL_IsInterruptEnabled(base);
    
    if (is_enabled) {
        TSI_HAL_DisableModule(base);
    }
    if (is_int_enabled) {
        TSI_HAL_DisableInterrupt(base);
    }
    
    if(config->mode == kTsiAnalogModeSel_AutoNoise)
    {
     TSI_HAL_SetMode(base, config->mode); 
     TSI_HAL_SetPrescaler(base, config->ps);
     TSI_HAL_SetNumberOfScans(base, config->nscn);
     TSI_HAL_SetReferenceChargeCurrent(base, config->refchrg);
     TSI_HAL_SetOscilatorVoltageRails(base, config->dvolt);
     TSI_HAL_SetElectrodeSeriesResistor(base, config->serres);
     TSI_HAL_SetFilterBits(base, config->filter);
    }
    
    else
    {  
     TSI_HAL_SetPrescaler(base, config->ps);
     TSI_HAL_SetNumberOfScans(base, config->nscn);
     TSI_HAL_SetReferenceChargeCurrent(base, config->refchrg);
     TSI_HAL_SetElectrodeChargeCurrent(base, config->extchrg);
     TSI_HAL_SetMode(base, config->mode);
     TSI_HAL_SetOscilatorVoltageRails(base, config->dvolt);
     TSI_HAL_SetLowThreshold(base, config->thresl);
     TSI_HAL_SetHighThreshold(base, config->thresh);
    }
    
    if (is_enabled) {
        TSI_HAL_EnableModule(base);
    }
    if (is_int_enabled) {
        TSI_HAL_EnableInterrupt(base);
    }
}
uint32_t TSI_HAL_Recalibrate(TSI_Type * base, tsi_config_t *config, const uint32_t electrodes, const tsi_parameter_limits_t *parLimits)
{
    assert(config != NULL);

    uint32_t is_enabled = TSI_HAL_IsModuleEnabled(base);
    uint32_t is_int_enabled = TSI_HAL_IsInterruptEnabled(base);
    uint32_t lowest_signal = TSI_RECALIBRATE_MAX_SIGNAL_VAL;

    if(electrodes == 0)
    {
      return 0;
    }
    
    if (is_enabled) {
        TSI_HAL_DisableModule(base);
    }
    if (is_int_enabled) {
        TSI_HAL_DisableInterrupt(base);
    }
    
    if(parLimits == NULL) /* If NOISE mode calibration*/
    {
      /* parLimits are not used in NOISE mode so this is calibration of noise mode. */      
      TSI_HAL_SetConfiguration(base, config);
      
      lowest_signal = 1;      
      
    }else
    {
      // Normal capacitive mode calibration 
      TSI_HAL_SetNumberOfScans(base, config->nscn);
      TSI_HAL_SetPrescaler(base, config->ps);
      TSI_HAL_SetElectrodeChargeCurrent(base, config->extchrg);
      TSI_HAL_SetReferenceChargeCurrent(base, config->refchrg);

       TSI_HAL_EnableModule(base);

      for (uint32_t i = 0U; i < 16U; i++) 
          {
          if ((uint32_t)(1 << i) & electrodes) 
                  {
              int32_t counter = TSI_HAL_MeasurementBlocking(base, i, 0);
              if (counter < lowest_signal) {
                  lowest_signal = counter;
              }
          }
      }
    }
    
    if (!is_enabled) {
        TSI_HAL_EnableModule(base);
    }
    if (is_int_enabled) {
        TSI_HAL_EnableInterrupt(base);
    }
    if (lowest_signal == TSI_RECALIBRATE_MAX_SIGNAL_VAL) {
        lowest_signal = 0U;  /* not valid */
    }

    return lowest_signal;
}