/*!
 * @brief main function
 */
int main(void)
{
#if FSL_FEATURE_ADC16_HAS_CALIBRATION
    adc16_calibration_param_t tempSnseCalibraitionParam;
#endif

    hardware_init();
    GPIO_DRV_Init(NULL, ledPins);

    // Configure the power mode protection
    SMC_HAL_SetProtection(SMC_BASE_PTR, kAllowPowerModeVlp);

    ADC16_DRV_StructInitUserConfigDefault(&tempSnseAdcConfig);
#if (FSL_FEATURE_ADC16_MAX_RESOLUTION >= 16)
    tempSnseAdcConfig.resolution = kAdc16ResolutionBitOf16;
#endif

#if BOARD_ADC_USE_ALT_VREF
    tempSnseAdcConfig.refVoltSrc = kAdc16RefVoltSrcOfValt;
#endif
    // Init ADC
    ADC16_DRV_Init(ADC_INSTANCE, &tempSnseAdcConfig);
    // Calibrate VDD and ADCR_TEMP25
#if FSL_FEATURE_ADC16_HAS_CALIBRATION
    // Auto calibraion
    ADC16_DRV_GetAutoCalibrationParam(ADC_INSTANCE, &tempSnseCalibraitionParam);
    ADC16_DRV_SetCalibrationParam(ADC_INSTANCE, &tempSnseCalibraitionParam);
#endif // FSL_FEATURE_ADC16_HAS_CALIBRATION
    calibrateParams();

    // get cpu uid low value for slave
    gSlaveId = SIM_UIDL_UID(SIM_BASE_PTR);

    PRINTF("i2c_rtos_slave_bm demo\r\n");

    // task list initialize
    OSA_Init();

    // create task(in BM: only the first registered task can be executed)
    OSA_TaskCreate(task_slave,
                   (uint8_t *)"slave",
                   512,
                   task_slave_stack,
                   0,
                   (void *)0,
                   false,
                   &task_slave_task_handler);

    OSA_Start();

    return 0;
}
Esempio n. 2
0
/*!
 * @brief Initialize the ADCx for HW trigger.
 * @return Error code.
 */
int adc16Init(adc16_converter_config_t *adcUserConfig, adc16_chn_config_t *adcChnConfig, adc16_calibration_param_t *adcCalibraitionParam)
{
    if(!adcUserConfig || !adcChnConfig)
    {
        return LPM_DEMO_RETURN_NULL;
    }

    memset(adcUserConfig, 0, sizeof(adc16_converter_config_t));
    memset(adcChnConfig, 0, sizeof(adc16_chn_config_t));
    memset(adcCalibraitionParam, 0, sizeof(adc16_calibration_param_t));

    adc16CalibrateParams();

    // Auto calibration
    if(adcCalibraitionParam)
    {
        ADC16_DRV_GetAutoCalibrationParam(HWADC_INSTANCE, adcCalibraitionParam);
        ADC16_DRV_SetCalibrationParam(HWADC_INSTANCE, adcCalibraitionParam);
    }

    // Initialization ADC for
    // 16bit resolution, interrupt mode, hw trigger enabled.
    // normal convert speed, VREFH/L as reference,
    // disable continuous convert mode.
    ADC16_DRV_StructInitUserConfigDefault(adcUserConfig);
    adcUserConfig->resolution = kAdc16ResolutionBitOf16;
    adcUserConfig->hwTriggerEnable = true;
    adcUserConfig->continuousConvEnable = false;
    adcUserConfig->clkSrc = kAdc16ClkSrcOfAsynClk;
#if BOARD_ADC_USE_ALT_VREF
    adcUserConfig->refVoltSrc = kAdc16RefVoltSrcOfValt;
#endif
    ADC16_DRV_Init(HWADC_INSTANCE, adcUserConfig);

    // Install Callback function into ISR
    ADC_InstallCallback(HWADC_INSTANCE, CHANNEL_0, adc1IrqHandler);

    adcChnConfig->chnIdx = ADC16_TEMPERATURE_CHN;
#if FSL_FEATURE_ADC16_HAS_DIFF_MODE
    adcChnConfig->diffConvEnable = false;
#endif
    adcChnConfig->convCompletedIntEnable = true;

    // Configure channel0
    ADC16_DRV_ConfigConvChn(HWADC_INSTANCE, CHANNEL_0, adcChnConfig);

    adc16InitPitTriggerSource(HWADC_INSTANCE);

    gConversionCompleteFlag = 0;

    return LPM_DEMO_RETURN_OK;
}
Esempio n. 3
0
/*!
 * @brief Initialize the ADCx for HW trigger.
 *
 * @param instance The ADC instance number
 */
static int32_t init_adc(uint32_t instance)
{
    adc16_converter_config_t adcUserConfig;
    adc16_chn_config_t adcChnConfig;
#if FSL_FEATURE_ADC16_HAS_CALIBRATION
    // Initialization ADC for calibration purposes
    adc16_calibration_param_t adcCalibrationParam;
    adc16_chn_config_t adcCalibrationChnConfig;
    const adc16_hw_average_config_t adcAverageConfig = {
      .hwAverageEnable = true,
      .hwAverageCountMode = kAdc16HwAverageCountOf32
    };
    
    ADC16_DRV_StructInitUserConfigDefault(&adcUserConfig);
#if BOARD_ADC_USE_ALT_VREF
    adcUserConfig.refVoltSrc = kAdc16RefVoltSrcOfValt;
#endif
    ADC16_DRV_Init(instance, &adcUserConfig);
    ADC16_DRV_ConfigHwAverage(instance, &adcAverageConfig);

    adcChnConfig.chnIdx = kAdc16Chn31;
#if FSL_FEATURE_ADC16_HAS_DIFF_MODE
    adcCalibrationChnConfig.diffConvEnable = false;
#endif /* FSL_FEATURE_ADC16_HAS_DIFF_MODE */
    adcCalibrationChnConfig.convCompletedIntEnable = false;

    // Configure channel0
    ADC16_DRV_ConfigConvChn(instance, 0U, &adcCalibrationChnConfig);

    // Configure channel1, which is used in PDB trigger case
    ADC16_DRV_ConfigConvChn(instance, 1U, &adcCalibrationChnConfig);
    
    // Auto calibration.
    ADC16_DRV_GetAutoCalibrationParam(instance, &adcCalibrationParam);
    ADC16_DRV_SetCalibrationParam(instance, &adcCalibrationParam);
#endif

    // Initialization ADC for
    // 12bit resolution, interrrupt mode, hw trigger enabled.
    // normal convert speed, VREFH/L as reference,
    // disable continuouse convert mode.
    ADC16_DRV_StructInitUserConfigDefault(&adcUserConfig);
    adcUserConfig.hwTriggerEnable = true;
    adcUserConfig.continuousConvEnable = false;
#if BOARD_ADC_USE_ALT_VREF
    adcUserConfig.refVoltSrc = kAdc16RefVoltSrcOfValt;
#endif
    ADC16_DRV_Init(instance, &adcUserConfig);

    // Install Callback function into ISR
    ADC_TEST_InstallCallback(instance, 0U, adc_chn0_isr_callback);
    ADC_TEST_InstallCallback(instance, 1U, adc_chn1_isr_callback);

    adcChnConfig.chnIdx = (adc16_chn_t)ADC_INPUT_CHAN;
#if FSL_FEATURE_ADC16_HAS_DIFF_MODE
    adcChnConfig.diffConvEnable = false;
#endif /* FSL_FEATURE_ADC16_HAS_DIFF_MODE */
    adcChnConfig.convCompletedIntEnable = true;

    // Configure channel0
    ADC16_DRV_ConfigConvChn(instance, 0U, &adcChnConfig);

    // Configure channel1, which is used in PDB trigger case
    ADC16_DRV_ConfigConvChn(instance, 1U, &adcChnConfig);

    return 0;
}

/*!
 * @brief Reset the sparse matrix
 */
void sparse_reset(void)
{
    memset(gChartHead, 0, sizeof(gChartHead));
    memset(gChartNodes, 0, sizeof(gChartNodes));
    gFreeNode = 0;
}
Esempio n. 4
0
/*
** ###################################################################
**
**     This file was created by Processor Expert 10.5 [05.21]
**     for the Freescale Kinetis series of microcontrollers.
**
** ###################################################################
*/
void calibrateParams(void)
{
    adc16_chn_config_t adcChnConfig;
#if FSL_FEATURE_ADC16_HAS_HW_AVERAGE
    adc16_hw_average_config_t userHwAverageConfig;
#endif
    pmc_bandgap_buffer_config_t pmcBandgapConfig = {
        .enable = true,
#if FSL_FEATURE_PMC_HAS_BGEN
        .enableInLowPower = false,
#endif
#if FSL_FEATURE_PMC_HAS_BGBDS
        .drive = kPmcBandgapBufferDriveLow,
#endif
    };

    uint32_t bandgapValue = 0;  // ADC value of BANDGAP
    uint32_t vdd = 0;           // VDD in mV

#if FSL_FEATURE_ADC16_HAS_CALIBRATION
    // Auto calibration
    adc16_calibration_param_t adcCalibraitionParam;
    ADC16_DRV_GetAutoCalibrationParam(ADC16_INSTANCE, &adcCalibraitionParam);
    ADC16_DRV_SetCalibrationParam(ADC16_INSTANCE, &adcCalibraitionParam);
#endif // FSL_FEATURE_ADC16_HAS_CALIBRATION.

    // Enable BANDGAP reference voltage
    PMC_HAL_BandgapBufferConfig(PMC_BASE_PTR, &pmcBandgapConfig);

#if FSL_FEATURE_ADC16_HAS_HW_AVERAGE
    // Use hardware average to increase stability of the measurement.
    userHwAverageConfig.hwAverageEnable = true;
    userHwAverageConfig.hwAverageCountMode = kAdc16HwAverageCountOf32;
    ADC16_DRV_ConfigHwAverage(ADC16_INSTANCE, &userHwAverageConfig);
#endif // FSL_FEATURE_ADC16_HAS_HW_AVERAGE

    // Configure the conversion channel
    // differential and interrupt mode disable.
    adcChnConfig.chnIdx                  = (adc16_chn_t)ADC16_BANDGAP_CHN;
#if FSL_FEATURE_ADC16_HAS_DIFF_MODE
    adcChnConfig.diffConvEnable          = false;
#endif
    adcChnConfig.convCompletedIntEnable  = false;
    ADC16_DRV_ConfigConvChn(ADC16_INSTANCE, ADC16_CHN_GROUP, &adcChnConfig);

    // Wait for the conversion to be done
    ADC16_DRV_WaitConvDone(ADC16_INSTANCE, ADC16_CHN_GROUP);

    // Get current ADC BANDGAP value and format it.
    bandgapValue = ADC16_DRV_GetConvValueSigned(ADC16_INSTANCE, ADC16_CHN_GROUP);
    // Calculates bandgapValue in 16bit resolution
    // from 12bit resolution to calibrate.
#if (FSL_FEATURE_ADC16_MAX_RESOLUTION < 16)
    bandgapValue = bandgapValue << 4;
#endif
    // ADC stop conversion
    ADC16_DRV_PauseConv(ADC16_INSTANCE, ADC16_CHN_GROUP);

    // Get VDD value measured in mV
    // VDD = (ADCR_VDD x V_BG) / ADCR_BG
    vdd = ADCR_VDD * V_BG / bandgapValue;
    // Calibrate ADCR_TEMP25
    // ADCR_TEMP25 = ADCR_VDD x V_TEMP25 / VDD
    adcrTemp25 = ADCR_VDD * V_TEMP25 / vdd;
    // Calculate conversion value of 100mV.
    // ADCR_100M = ADCR_VDD x 100 / VDD
    adcr100m = ADCR_VDD*100/ vdd;

    // Disable BANDGAP reference voltage
    pmcBandgapConfig.enable = false;
    PMC_HAL_BandgapBufferConfig(PMC_BASE_PTR, &pmcBandgapConfig);
}



/*!
 * @brief Gets current temperature of chip.
 *
 * This function gets convertion value, converted temperature and print them to terminal.
 */
void ADC16_Measure(void)
{
    adc16_chn_config_t chnConfig;

    // Configure the conversion channel
    // differential and interrupt mode disable.
    chnConfig.chnIdx     = (adc16_chn_t)ADC16_TEMPERATURE_CHN;
#if FSL_FEATURE_ADC16_HAS_DIFF_MODE
    chnConfig.diffConvEnable = false;
#endif
    chnConfig.convCompletedIntEnable  = false;

    // Software trigger the conversion.
    ADC16_DRV_ConfigConvChn(ADC16_INSTANCE, ADC16_CHN_GROUP, &chnConfig);

    //ADC16_DRV_ConfigConvChn(FSL_ADCONV1, 0U, &adConv1_ChnConfig0);
    // Wait for the conversion to be done.
    ADC16_DRV_WaitConvDone(ADC16_INSTANCE, ADC16_CHN_GROUP);

    // Fetch the conversion value.
    adcValue = ADC16_DRV_GetConvValueSigned(ADC16_INSTANCE, ADC16_CHN_GROUP);

    // Show the current temperature value.
    PRINTF("\r\n ADC converted value: %ld\t", adcValue );
    // Calculates adcValue in 16bit resolution
    // from 12bit resolution in order to convert to temperature.
#if (FSL_FEATURE_ADC16_MAX_RESOLUTION < 16)
    adcValue = adcValue << 4;
#endif
    PRINTF("\r\n Temperature %ld\r\n", get_current_temperature(adcValue));
    // Pause the conversion.
    ADC16_DRV_PauseConv(ADC16_INSTANCE, ADC16_CHN_GROUP);

}
Esempio n. 5
0
/*!
 * @brief calibrate param for adc.
 */
void adc16CalibrateParams(void)
{
#if FSL_FEATURE_ADC16_HAS_CALIBRATION
    adc16_calibration_param_t adcCalibraitionParam;
#endif
    adc16_converter_config_t adcUserConfig;
#if FSL_FEATURE_ADC16_HAS_HW_AVERAGE
    adc16_hw_average_config_t hwAverageConfig;
#endif
    adc16_chn_config_t adcChnConfig;
    pmc_bandgap_buffer_config_t pmcBandgapConfig = {
        .enable = true,
#if FSL_FEATURE_PMC_HAS_BGEN
        .enableInLowPower = false,
#endif
#if FSL_FEATURE_PMC_HAS_BGBDS
        .drive = kPmcBandgapBufferDriveLow,
#endif
    };
    uint32_t bandgapValue = 0;  /*! ADC value of BANDGAP */
    uint32_t vdd = 0;           /*! VDD in mV */

    // Enable BANDGAP reference voltage
    PMC_HAL_BandgapBufferConfig(PMC_BASE_PTR, &pmcBandgapConfig);

    // Initialization ADC for
    // 16bit resolution, interrupt mode, hw trigger disabled.
    // normal convert speed, VREFH/L as reference,
    // disable continuous convert mode.
    ADC16_DRV_StructInitUserConfigDefault(&adcUserConfig);
    adcUserConfig.resolution = kAdc16ResolutionBitOf16;
    adcUserConfig.continuousConvEnable = false;
    adcUserConfig.clkSrc = kAdc16ClkSrcOfAsynClk;
#if BOARD_ADC_USE_ALT_VREF
    adcUserConfig.refVoltSrc = kAdc16RefVoltSrcOfValt;
#endif
    ADC16_DRV_Init(HWADC_INSTANCE, &adcUserConfig);

#if FSL_FEATURE_ADC16_HAS_HW_AVERAGE
    hwAverageConfig.hwAverageEnable = true;
    hwAverageConfig.hwAverageCountMode = kAdc16HwAverageCountOf32;
    ADC16_DRV_ConfigHwAverage(HWADC_INSTANCE, &hwAverageConfig);
#endif // FSL_FEATURE_ADC16_HAS_HW_AVERAGE

#if FSL_FEATURE_ADC16_HAS_CALIBRATION
    // Auto calibration
    ADC16_DRV_GetAutoCalibrationParam(HWADC_INSTANCE, &adcCalibraitionParam);
    ADC16_DRV_SetCalibrationParam(HWADC_INSTANCE, &adcCalibraitionParam);
#endif

    adcChnConfig.chnIdx = ADC16_BANDGAP_CHN;
#if FSL_FEATURE_ADC16_HAS_DIFF_MODE
    adcChnConfig.diffConvEnable = false;
#endif
    adcChnConfig.convCompletedIntEnable = false;
    ADC16_DRV_ConfigConvChn(HWADC_INSTANCE, CHANNEL_0, &adcChnConfig);

    // Wait for the conversion to be done
    ADC16_DRV_WaitConvDone(HWADC_INSTANCE, CHANNEL_0);

    // Get current ADC BANDGAP value
    bandgapValue = ADC16_DRV_GetConvValueSigned(HWADC_INSTANCE, CHANNEL_0);

    // ADC stop conversion
    ADC16_DRV_PauseConv(HWADC_INSTANCE, CHANNEL_0);

    // Get VDD value measured in mV: VDD = (ADCR_VDD x V_BG) / ADCR_BG
    vdd = ADCR_VDD * V_BG / bandgapValue;
    // Calibrate ADCR_TEMP25: ADCR_TEMP25 = ADCR_VDD x V_TEMP25 / VDD
    sAdcrTemp25 = ADCR_VDD * V_TEMP25 / vdd;
    // ADCR_100M = ADCR_VDD x M x 100 / VDD
    sAdcr100m = (ADCR_VDD * M) / (vdd * 10);

#if FSL_FEATURE_ADC16_HAS_HW_AVERAGE
    hwAverageConfig.hwAverageEnable = false;
    ADC16_DRV_ConfigHwAverage(HWADC_INSTANCE, &hwAverageConfig);
#endif // FSL_FEATURE_ADC16_HAS_HW_AVERAGE

    // Disable BANDGAP reference voltage
    pmcBandgapConfig.enable = false;
    PMC_HAL_BandgapBufferConfig(PMC_BASE_PTR, &pmcBandgapConfig);
}

/*!
 * @brief Getting current temperature value.
 * @return Current temperature.
 */
int32_t adc16GetCurrentTempValue(void)
{
    int32_t currentTemperature = 0;

    currentTemperature = (int32_t)(STANDARD_TEMP - ((int32_t)sAdcValue - (int32_t)sAdcrTemp25) * 100 / (int32_t)sAdcr100m);
    return currentTemperature;
}
Esempio n. 6
0
/*!
 * @brief Initialize the ADCx for HW trigger.
 *
 * @param instance The ADC instance number
 */
int32_t init_adc(uint32_t instance)
{
#if FSL_FEATURE_ADC16_HAS_CALIBRATION
    adc16_calibration_param_t adcCalibraitionParam;
#endif
    adc16_converter_config_t adcUserConfig;
    adc16_chn_config_t adcChnConfig;
    adc16_hw_average_config_t adchwaverageConfig;

    //ADC16_DRV_StructInitUserConfigDefault(&adcUserConfig);    
    /* Special configuration for highest accuracy. */
    adcUserConfig.lowPowerEnable = false;
    adcUserConfig.clkDividerMode = kAdc16ClkDividerOf1;
    adcUserConfig.longSampleTimeEnable = false;
    //yanglliang
    adcUserConfig.resolution = kAdc16ResolutionBitOfSingleEndAs16;
    //adcUserConfig.resolution = kAdc16ResolutionBitOfSingleEndAs12;
    adcUserConfig.clkSrc = kAdc16ClkSrcOfBusClk;   //kAdc16ClkSrcOfAsynClk;
    adcUserConfig.asyncClkEnable = true;
    adcUserConfig.highSpeedEnable = true;
    adcUserConfig.longSampleCycleMode = kAdc16LongSampleCycleOf24;
    adcUserConfig.hwTriggerEnable = false;
    adcUserConfig.refVoltSrc = kAdc16RefVoltSrcOfVref;
    adcUserConfig.continuousConvEnable = false;
    
#if (  defined(FRDM_KL43Z)   /* CPU_MKL43Z256VLH4 */ \
    || defined(TWR_KL43Z48M) /* CPU_MKL43Z256VLH4 */ \
    || defined(FRDM_KL27Z)   /* CPU_MKL27Z64VLH4  */ \
    )
    adcUserConfig.refVoltSrc = kAdc16RefVoltSrcOfValt;
#endif
    ADC16_DRV_Init(instance, &adcUserConfig);

#if FSL_FEATURE_ADC16_HAS_CALIBRATION
    // Auto calibraion.
    ADC16_DRV_GetAutoCalibrationParam(instance, &adcCalibraitionParam);
    ADC16_DRV_SetCalibrationParam(instance, &adcCalibraitionParam);
#endif

    // Initialization ADC for
    // 12bit resolution, interrrupt mode, hw trigger enabled.
    // normal convert speed, VREFH/L as reference,
    // disable continuouse convert mode.
    //ADC16_DRV_StructInitUserConfigDefault(&adcUserConfig);
    adcUserConfig.hwTriggerEnable = true;
    adcUserConfig.continuousConvEnable = false;
#if (  defined(FRDM_KL43Z)   /* CPU_MKL43Z256VLH4 */ \
    || defined(TWR_KL43Z48M) /* CPU_MKL43Z256VLH4 */ \
    || defined(FRDM_KL27Z)   /* CPU_MKL27Z64VLH4  */ \
    )
    adcUserConfig.refVoltSrc = kAdc16RefVoltSrcOfValt;
#endif
    
    
    ADC16_DRV_Init(instance, &adcUserConfig);

    // Install Callback function into ISR
    ADC_TEST_InstallCallback(instance, 0U, adc_chn0_isr_callback);
    //ADC_TEST_InstallCallback(instance, 1U, adc_chn1_isr_callback);

    adcChnConfig.chnIdx = (adc16_chn_t)ADC_AI1;
#if FSL_FEATURE_ADC16_HAS_DIFF_MODE
    adcChnConfig.diffConvEnable = false;
#endif /* FSL_FEATURE_ADC16_HAS_DIFF_MODE */
    adcChnConfig.convCompletedIntEnable = true;
    
    ADC0_CFG2 |= ADC_CFG2_MUXSEL_MASK;

    // Configure channel0
    ADC16_DRV_ConfigConvChn(instance, 0U, &adcChnConfig);
    
    adchwaverageConfig.hwAverageEnable=false;
    //yangliang for hardware average
    adchwaverageConfig.hwAverageCountMode=kAdc16HwAverageCountOf4;
    //adchwaverageConfig.hwAverageCountMode=kAdc16HwAverageCountOf16;
    ADC16_DRV_ConfigHwAverage(0, & adchwaverageConfig);                  //Set Hardware Average numbers

    // Configure channel1, which is used in PDB trigger case
    //ADC16_DRV_ConfigConvChn(instance, 1U, &adcChnConfig);

    return 0;
}