/** function loads calibration table from EEPROM, validate it and if OK uses it */ static void touch_LoadCalibration(void) { I2C_Init_TypeDef i2cInit = I2C_INIT_DEFAULT; uint32_t temp, checksum; int count; MATRIX new_matrix; /* Initialize I2C driver, using standard rate. Devices on DK itself */ /* supports fast mode, but in case some slower devices are added on */ /* prototype board, we use standard mode. */ I2CDRV_Init(&i2cInit); count = EEPROM_Read(I2C0, EEPROM_DVK_ADDR, CALIBRATION_EEPROM_OFFSET, (uint8_t*) &temp, sizeof(temp)); count += EEPROM_Read(I2C0, EEPROM_DVK_ADDR, CALIBRATION_EEPROM_OFFSET + 4, (uint8_t*) &new_matrix, sizeof(new_matrix)); if (count == sizeof(new_matrix) + 4) { if (temp == CALIBRATION_MAGIC_NUMBER) { checksum = touch_CountChecksum(temp, (uint32_t*) &new_matrix, sizeof(new_matrix) / 4); count = EEPROM_Read(I2C0, EEPROM_DVK_ADDR, CALIBRATION_EEPROM_OFFSET + 4 + sizeof(new_matrix), (uint8_t*) &temp, sizeof(temp)); if (temp == checksum) { /* looks like calibration table is valid */ ADC_IntDisable(ADC0, ADC_IF_SINGLE); /* we need to disable ADC interrupt to avoid current_pos structure update for a while */ memcpy(&calibrationMatrix, &new_matrix, sizeof(calibrationMatrix)); ADC_IntEnable(ADC0, ADC_IF_SINGLE); } } } }
/***************************************************************************//** * @brief * Returns current touch position and state * * @return * Current touch position and state ******************************************************************************/ TOUCH_Pos_TypeDef *TOUCH_GetPos(void) { ADC_IntDisable(ADC0, ADC_IF_SINGLE); /* we need to disable ADC interrupt to avoid current_pos structure update for a while */ pos.pen = current_pos.pen; pos.x = current_pos.x; pos.y = current_pos.y; pos.adcx = current_pos.adcx; pos.adcy = current_pos.adcy; ADC_IntEnable(ADC0, ADC_IF_SINGLE); return &pos; }
/** * Initialize ADC Module */ void adc_init(void) { ADC_Init_TypeDef init = ADC_INIT_DEFAULT; /* Enable ADC Clock */ CMU_ClockEnable(cmuClock_ADC0, true); /* Initialize timebases */ init.timebase = ADC_TimebaseCalc(0); init.prescale = ADC_PrescaleCalc(400000,0); ADC_Init(ADC0, &init); /* Setup interrupt generation on completed conversion. */ ADC_IntEnable(ADC0, ADC_IF_SINGLE); NVIC_EnableIRQ(ADC0_IRQn); }
/**************************************************************************//** * @brief Initialize ADC for temperature sensor readings in single poin *****************************************************************************/ void setupSensor(void) { /* Base the ADC configuration on the default setup. */ ADC_Init_TypeDef init = ADC_INIT_DEFAULT; ADC_InitSingle_TypeDef sInit = ADC_INITSINGLE_DEFAULT; /* Initialize timebases */ init.timebase = ADC_TimebaseCalc(0); init.prescale = ADC_PrescaleCalc(400000,0); ADC_Init(ADC0, &init); /* Set input to temperature sensor. Reference must be 1.25V */ sInit.reference = adcRef1V25; sInit.input = adcSingleInpTemp; ADC_InitSingle(ADC0, &sInit); /* Setup interrupt generation on completed conversion. */ ADC_IntEnable(ADC0, ADC_IF_SINGLE); NVIC_EnableIRQ(ADC0_IRQn); }
/***************************************************************************//** * @brief * Initialize touch panel driver * * @param config * Driver configuration data. ******************************************************************************/ void TOUCH_Init(TOUCH_Config_TypeDef *config) { ADC_Init_TypeDef init = ADC_INIT_DEFAULT; #ifndef TOUCH_WITHOUT_STORE touch_LoadCalibration(); #endif CMU_ClockEnable(cmuClock_ADC0, true); ADC_IntDisable(ADC0, _ADC_IF_MASK); init.prescale = ADC_PrescaleCalc(config->frequency, 0); touch_ignore_move = config->ignore; init.ovsRateSel = config->oversampling; ADC_Init(ADC0, &init); BSP_PeripheralAccess(BSP_TOUCH, true); sInit.input = ADC_Y; sInit.reference = adcRefVDD; sInit.resolution = adcResOVS; ADC_InitSingle(ADC0, &sInit); ADC_IntClear(ADC0, _ADC_IF_MASK); touch_state = TOUCH_INIT; NVIC_ClearPendingIRQ(ADC0_IRQn); NVIC_EnableIRQ(ADC0_IRQn); ADC_IntEnable(ADC0, ADC_IF_SINGLE); ADC_Start(ADC0, adcStartSingle); }
void adc_init(ADC_Reference reference, ADC_Input input, uint32_t adc_frequency) { // Initialises clocks CMU_ClockEnable(cmuClock_HFPER, true); CMU_ClockEnable(cmuClock_ADC0, true); /* Base the ADC configuration on the default setup. */ ADC_Init_TypeDef init = ADC_INIT_DEFAULT; ADC_InitSingle_TypeDef sInit = ADC_INITSINGLE_DEFAULT; /* Initialize timebases */ init.timebase = ADC_TimebaseCalc(0); init.prescale = ADC_PrescaleCalc(adc_frequency,0); ADC_Init(ADC0, &init); switch (reference) { /** Internal 1.25V reference. */ case adcReference1V25: sInit.reference = adcRef1V25; break; /** Internal 2.5V reference. */ case adcReference2V5: sInit.reference = adcRef2V5; break; /** Buffered VDD. */ case adcReferenceVDD: sInit.reference = adcRefVDD; break; /** Internal differential 5V reference. */ case adcReference5VDIFF: sInit.reference = adcRef5VDIFF; break; /** Single ended ext. ref. from 1 pin. */ case adcReferenceExtSingle: sInit.reference = adcRefExtSingle; break; /** Differential ext. ref. from 2 pins */ case adcReference2xExtDiff: sInit.reference = adcRef2xExtDiff; break; /** Unbuffered 2xVDD. */ case adcReference2xVDD: sInit.reference = adcRef2xVDD; break; } switch (input) { /** Temperature reference. */ case adcInputSingleTemp: sInit.input = adcSingleInpTemp; break; /** VDD / 3. */ case adcInputSingleVDDDiv3: sInit.input = adcSingleInpVDDDiv3; break; /** Positive Ch4, negative Ch5. */ case adcInputSingleCh4Ch5: sInit.input = adcSingleInpCh4Ch5; sInit.diff = true; break; } ADC_InitSingle(ADC0, &sInit); /* Setup interrupt generation on completed conversion. */ ADC_IntEnable(ADC0, ADC_IF_SINGLE); }
/*****************************************************************************//** * * @brief initialize ADC module. * * @param[in] pADC point to ADC module type. * @param[in] pADC_Config point to ADC configuration structure. * * @return none * * @ Pass/ Fail criteria: none *****************************************************************************/ void ADC_Init(ADC_Type *pADC, ADC_ConfigTypePtr pADC_Config) { if( pADC == ADC) { SIM->SCGC |= SIM_SCGC_ADC_MASK; } /* set clock cource for ADC */ ADC_SelectClock(pADC,pADC_Config->u8ClockSource); /* set clock divide */ ADC_SelectClockDivide(pADC,pADC_Config->u8ClockDiv); /* set ADC mode */ ADC_SetMode(pADC,pADC_Config->u8Mode); /* set FIFO level */ ADC_SetFifoLevel(pADC,pADC_Config->u8FiFoLevel); /* set pin control */ pADC->APCTL1 = pADC_Config->u16PinControl; if( pADC_Config->sSetting.bCompareEn ) { ADC_CompareEnable(pADC); } if( pADC_Config->sSetting.bCompareGreaterEn ) { ADC_CompareGreaterFunction(pADC); } if( pADC_Config->sSetting.bContinuousEn ) { ADC_ContinuousConversion(pADC); } if( pADC_Config->sSetting.bCompareAndEn ) { ADC_CompareFifoAnd(pADC); } if( pADC_Config->sSetting.bFiFoScanModeEn ) { ADC_FifoScanModeEnable(pADC); } if( pADC_Config->sSetting.bHardwareTriggerEn ) { ADC_SetHardwareTrigger(pADC); } if( pADC_Config->sSetting.bIntEn ) { ADC_IntEnable(pADC); NVIC_EnableIRQ( ADC0_IRQn ); } if( pADC_Config->sSetting.bLongSampleEn ) { ADC_SetLongSample(pADC); } if( pADC_Config->sSetting.bLowPowerEn ) { ADC_SetLowPower(pADC); } #if !defined(CPU_KE02) if( pADC_Config->sSetting.bHTRGMEn ) { ADC_HardwareTriggerMultiple(pADC); } else { ADC_HardwareTriggerSingle(pADC); } if( pADC_Config->sSetting.bHTRGMASKEn ) { ADC_HardwareTriggerMaskEnable(pADC); } else { ADC_HardwareTriggerMaskDisable(pADC); } if( pADC_Config->sSetting.bHTRGMASKSEL ) { ADC_HardwareTriggerMaskAuto(pADC); } else { ADC_HardwareTriggerMaskNonAuto(pADC); } #endif }