void adc_calibrate() { // Initialises clocks CMU_ClockEnable(cmuClock_HFPER, true); CMU_ClockEnable(cmuClock_ADC0, true); //todo: use new calibration values //todo: make adaptable for other ref voltages uint32_t old_gain_calibration_value = (DEVINFO->ADC0CAL0 & _DEVINFO_ADC0CAL0_1V25_GAIN_MASK) >> _DEVINFO_ADC0CAL0_1V25_GAIN_SHIFT; uint32_t old_offset_calibration_value = (DEVINFO->ADC0CAL0 & _DEVINFO_ADC0CAL0_1V25_OFFSET_MASK) >> _DEVINFO_ADC0CAL0_1V25_OFFSET_SHIFT; //RTCDRV_Trigger(100, NULL); uint32_t calibration_value = ADC_Calibration(ADC0, adcRef1V25); uint32_t offset_calibration_value = (calibration_value & _ADC_CAL_SINGLEOFFSET_MASK) >> _ADC_CAL_SINGLEOFFSET_SHIFT; uint32_t gain_calibration_value = (calibration_value & _ADC_CAL_SINGLEGAIN_MASK) >> _ADC_CAL_SINGLEGAIN_SHIFT; log_print_stack_string(LOG_STACK_FWK, "ADC Calibration offset %d -> %d", old_offset_calibration_value, offset_calibration_value); log_print_stack_string(LOG_STACK_FWK, "ADC Calibration gain %d -> %d", old_gain_calibration_value, gain_calibration_value); }
/** * @brief Configure ADC Configuration. * @param None * @retval None */ void ADC_OVERConfiguration(void) { /* Configure ADC ----------------------------------------------------------*/ ADC_InitPara ADC_InitStructure; ADC_InitStructure.ADC_Mode_Scan = DISABLE ; ADC_InitStructure.ADC_Mode_Continuous = DISABLE; ADC_InitStructure.ADC_Trig_External = ADC_EXTERNAL_TRIGGER_MODE_NONE; ADC_InitStructure.ADC_Data_Align = ADC_DATAALIGN_RIGHT; ADC_InitStructure.ADC_Channel_Number = 1; ADC_Init(&ADC_InitStructure); /* ADC regular channels configuration */ ADC_RegularChannel_Config(ADC_CHANNEL_10, 1,ADC_SAMPLETIME_55POINT5); ADC_OversamplingRatio_Config(ADC_OVERSAMPLING_RATIO_4X); ADC_OversamplingShift_Config(ADC_OVERSAMPLING_SHIFT_NONE); ADC_Oversampler_Enable(ENABLE); /* Enable ADC */ ADC_Enable(ENABLE); ADC_Calibration(); /* Start ADC Software Conversion */ ADC_SoftwareStartConv_Enable(ENABLE); }
void adc_init(void) { DMA_InitPara DMA_InitStructure; ADC_InitPara ADC_InitStructure; RCC_APB2PeriphClock_Enable(RCC_APB2PERIPH_ADC1, ENABLE); RCC_ADCCLKConfig(RCC_ADCCLK_APB2_DIV6); RCC_AHBPeriphClock_Enable(RCC_AHBPERIPH_DMA1, ENABLE); DMA_InitStructure.DMA_PeripheralBaseAddr = 0x4001244C; DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)adcarray; DMA_InitStructure.DMA_DIR = DMA_DIR_PERIPHERALSRC; DMA_InitStructure.DMA_BufferSize = 4; DMA_InitStructure.DMA_PeripheralInc = DMA_PERIPHERALINC_DISABLE; DMA_InitStructure.DMA_MemoryInc = DMA_MEMORYINC_ENABLE; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PERIPHERALDATASIZE_HALFWORD; DMA_InitStructure.DMA_MemoryDataSize = DMA_MEMORYDATASIZE_HALFWORD; DMA_InitStructure.DMA_Mode = DMA_MODE_CIRCULAR; DMA_InitStructure.DMA_Priority = DMA_PRIORITY_HIGH; DMA_InitStructure.DMA_MTOM = DMA_MEMTOMEM_DISABLE; DMA_Init(DMA1_CHANNEL1, &DMA_InitStructure); DMA_Enable(DMA1_CHANNEL1, ENABLE); // ADC_DeInit(&ADC_InitStructure); ADC_InitStructure.ADC_Mode_Scan = ENABLE; ADC_InitStructure.ADC_Mode_Continuous = ENABLE; ADC_InitStructure.ADC_Trig_External = ADC_EXTERNAL_TRIGGER_MODE_NONE; ADC_InitStructure.ADC_Data_Align = ADC_DATAALIGN_RIGHT; ADC_InitStructure.ADC_Channel_Number = 4; ADC_Init(&ADC_InitStructure); // current? ADC_RegularChannel_Config(ADC_CHANNEL_7, 1, ADC_SAMPLETIME_239POINT5); // battery channel ADC_RegularChannel_Config(ADC_CHANNEL_5, 2, ADC_SAMPLETIME_239POINT5); ADC_RegularChannel_Config(ADC_CHANNEL_4, 3, ADC_SAMPLETIME_239POINT5); ADC_RegularChannel_Config(ADC_CHANNEL_1, 4, ADC_SAMPLETIME_239POINT5); ADC_DMA_Enable(ENABLE); ADC_Enable(ENABLE); ADC_Calibration(); ADC_SoftwareStartConv_Enable(ENABLE); }
/** * @brief Main program. * @param None * @retval None */ int main(void) { /* Configure System clocks -----------------------------------------------*/ RCC_Configuration(); /* Configure GPIO ports --------------------------------------------------*/ GPIO_Configuration(); /* Configure DMA1 channel1 -----------------------------------------------*/ DMA_DeInit(DMA1_CHANNEL1); DMA_InitStructure.DMA_PeripheralBaseAddr = ADC_RDTR_Address; DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&ADCConvertedValue; DMA_InitStructure.DMA_DIR = DMA_DIR_PERIPHERALSRC; DMA_InitStructure.DMA_BufferSize = 1; DMA_InitStructure.DMA_PeripheralInc = DMA_PERIPHERALINC_DISABLE; DMA_InitStructure.DMA_MemoryInc = DMA_MEMORYINC_DISABLE; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PERIPHERALDATASIZE_HALFWORD; DMA_InitStructure.DMA_MemoryDataSize = DMA_MEMORYDATASIZE_HALFWORD; DMA_InitStructure.DMA_Mode = DMA_MODE_CIRCULAR; DMA_InitStructure.DMA_Priority = DMA_PRIORITY_HIGH; DMA_InitStructure.DMA_MTOM = DMA_MEMTOMEM_DISABLE; DMA_Init(DMA1_CHANNEL1, &DMA_InitStructure); /* Enable DMA1 channel1 */ DMA_Enable(DMA1_CHANNEL1, ENABLE); /* Configure ADC ---------------------------------------------------------*/ ADC_InitStructure.ADC_Mode_Scan = DISABLE; ADC_InitStructure.ADC_Mode_Continuous = ENABLE; ADC_InitStructure.ADC_Trig_External = ADC_EXTERNAL_TRIGGER_MODE_NONE; ADC_InitStructure.ADC_Data_Align = ADC_DATAALIGN_RIGHT; ADC_InitStructure.ADC_Channel_Number = 1; ADC_Init(&ADC_InitStructure); /* Configure ADC regular channelx */ ADC_RegularChannel_Config( BOARD_ADC_CHANNEL, 1, ADC_SAMPLETIME_239POINT5); /* Enable ADC DMA */ ADC_DMA_Enable(ENABLE); /* Enable ADC */ ADC_Enable(ENABLE); ADC_Calibration(); /* Start ADC Software Conversion */ ADC_SoftwareStartConv_Enable(ENABLE); while (1) { } }
/** * @brief Configure ADC_12B resolution. * @param None * @retval None */ void ADC_12BConfiguration(void) { /* Configure ADC ----------------------------------------------------------*/ ADC_InitStructure.ADC_Mode_Scan = DISABLE; ADC_InitStructure.ADC_Mode_Continuous = DISABLE; ADC_InitStructure.ADC_Trig_External = ADC_EXTERNAL_TRIGGER_MODE_NONE; ADC_InitStructure.ADC_Data_Align = ADC_DATAALIGN_RIGHT; ADC_InitStructure.ADC_Channel_Number = 1; ADC_Init(&ADC_InitStructure); /* Configure ADC regular channel10 */ ADC_RegularChannel_Config(ADC_CHANNEL_10, 1, ADC_SAMPLETIME_55POINT5); /* Enable ADC DMA */ ADC_DMA_Enable(ENABLE); ADC_Resolution_Config(ADC_RESOLUTION_12B); /* Enable ADC */ ADC_Enable(ENABLE); ADC_Calibration(); /* Start ADC Software Conversion */ ADC_SoftwareStartConv_Enable(ENABLE); }
/** * @brief Main program. * @param None * @retval None */ int main(void) { /* Configure System clocks -----------------------------------------------*/ RCC_Configuration(); /* Configure NVIC --------------------------------------------------------*/ NVIC_Configuration(); /* Configure GPIO ports --------------------------------------------------*/ GPIO_Configuration(); /* Configure TIM2 --------------------------------------------------------*/ /* Configure Time Base */ TIMER_BaseStructInit(&TIM_TimeBaseStructure); TIM_TimeBaseStructure.TIMER_Period = 256; TIM_TimeBaseStructure.TIMER_Prescaler = 6; TIM_TimeBaseStructure.TIMER_ClockDivision = 0x0; TIM_TimeBaseStructure.TIMER_CounterMode = TIMER_COUNTER_UP; TIMER_BaseInit(TIMER2, &TIM_TimeBaseStructure); /* Configure TIM2 channel2 in PWM mode */ TIM_OCInitStructure.TIMER_OCMode = TIMER_OC_MODE_PWM1; TIM_OCInitStructure.TIMER_OutputState = TIMER_OUTPUT_STATE_ENABLE; TIM_OCInitStructure.TIMER_Pulse = 128; TIM_OCInitStructure.TIMER_OCPolarity = TIMER_OC_POLARITY_LOW; TIMER_OC2_Init(TIMER2, &TIM_OCInitStructure); /* Configure DMA1 Channel1 -----------------------------------------------*/ DMA_DeInit(DMA1_CHANNEL1); DMA_InitStructure.DMA_PeripheralBaseAddr = ADC_RDTR_Address; DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)ADC_RegularConvertedValueTab; DMA_InitStructure.DMA_DIR = DMA_DIR_PERIPHERALSRC; DMA_InitStructure.DMA_BufferSize = 64; DMA_InitStructure.DMA_PeripheralInc = DMA_PERIPHERALINC_DISABLE; DMA_InitStructure.DMA_MemoryInc = DMA_MEMORYINC_ENABLE; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PERIPHERALDATASIZE_HALFWORD; DMA_InitStructure.DMA_MemoryDataSize = DMA_MEMORYDATASIZE_HALFWORD; DMA_InitStructure.DMA_Mode = DMA_MODE_NORMAL; DMA_InitStructure.DMA_Priority = DMA_PRIORITY_HIGH; DMA_InitStructure.DMA_MTOM = DMA_MEMTOMEM_DISABLE; DMA_Init(DMA1_CHANNEL1, &DMA_InitStructure); /* Enable DMA1 channel1 */ DMA_Enable(DMA1_CHANNEL1, ENABLE); /* Configure ADC ---------------------------------------------------------*/ ADC_InitStructure.ADC_Mode_Scan = ENABLE; ADC_InitStructure.ADC_Mode_Continuous = DISABLE; ADC_InitStructure.ADC_Trig_External = ADC_EXTERNAL_TRIGGER_MODE_T2_CC2; ADC_InitStructure.ADC_Data_Align = ADC_DATAALIGN_RIGHT; ADC_InitStructure.ADC_Channel_Number = 1; ADC_Init(&ADC_InitStructure); /* Configure ADC regular channel14 */ ADC_RegularChannel_Config(ADC_CHANNEL_14, 1, ADC_SAMPLETIME_55POINT5); /* Set inserted sequencer length */ ADC_InsertedSequencerLength_Config(1); /* Configure ADC inserted channel */ ADC_InsertedChannel_Config(ADC_CHANNEL_11, 1, ADC_SAMPLETIME_55POINT5); /* Configure ADC inserted external trigger */ ADC_ExternalTrigInsertedConv_Config(ADC_EXTERNAL_TRIG_INSERTCONV_NONE); /* Enable automatic inserted conversion start after regular one */ ADC_AutoInsertedConv_Enable(ENABLE); /* Enable ADC DMA */ ADC_DMA_Enable(ENABLE); /* Enable ADC external trigger */ ADC_ExternalTrigConv_Enable( ENABLE); /* Enable EOIC interrupt */ ADC_INTConfig(ADC_INT_EOIC, ENABLE); /* Enable ADC */ ADC_Enable(ENABLE); ADC_Calibration(); /* TIM2 counter enable */ TIMER_Enable(TIMER2, ENABLE); /* TIM2 main Output enable */ TIMER_CtrlPWMOutputs(TIMER2, ENABLE); /* Test on channel1 transfer complete flag */ while(!DMA_GetBitState(DMA1_FLAG_TC1)); /* Clear channel1 transfer complete flag */ DMA_ClearBitState(DMA1_FLAG_TC1); /* TIM2 counter disable */ TIMER_Enable(TIMER2, DISABLE); /* Turn on the Led1 */ GD_EVAL_LEDOn(LED1); while (1) { } }