int adc_init(adc_t line) { uint32_t clk_div = 2; /* check if the line is valid */ if (line >= ADC_NUMOF) { return -1; } /* lock and power-on the device */ prep(line); /* configure the pin */ gpio_init_analog(adc_config[line].pin); /* set sequence length to 1 conversion and enable the ADC device */ dev(line)->SQR1 = 0; dev(line)->CR2 = ADC_CR2_ADON; /* set clock prescaler to get the maximal possible ADC clock value */ for (clk_div = 2; clk_div < 8; clk_div += 2) { if ((CLOCK_CORECLOCK / clk_div) <= MAX_ADC_SPEED) { break; } } ADC->CCR = ((clk_div / 2) - 1) << 16; /* free the device again */ done(line); return 0; }
int adc_init(adc_t line) { uint32_t clk_div = 2; /* check if the line is valid */ if (line >= ADC_NUMOF) { return -1; } /* lock and power-on the device */ prep(line); /* configure the pin */ gpio_init_analog(adc_config[line].pin); /* set clock prescaler to get the maximal possible ADC clock value */ for (clk_div = 2; clk_div < 8; clk_div += 2) { if ((CLOCK_CORECLOCK / clk_div) <= MAX_ADC_SPEED) { break; } } RCC->CFGR &= ~(RCC_CFGR_ADCPRE); RCC->CFGR |= ((clk_div / 2) - 1) << 14; /* enable the ADC module */ dev(line)->CR2 |= ADC_CR2_ADON; /* resets the selected ADC calibration registers */ dev(line)->CR2 |= ADC_CR2_RSTCAL; /* check the status of RSTCAL bit */ while (dev(line)->CR2 & ADC_CR2_RSTCAL) {} /* enable the selected ADC calibration process */ dev(line)->CR2 |= ADC_CR2_CAL; /* wait for the calibration to have finished */ while (dev(line)->CR2 & ADC_CR2_CAL) {} /* set all channels to maximum (239.5) cycles for best accuracy */ dev(line)->SMPR1 |= 0x00ffffff; dev(line)->SMPR2 |= 0x3fffffff; /* we want to sample one channel */ dev(line)->SQR1 = ADC_SQR1_L_0; /* start sampling from software */ dev(line)->CR2 |= ADC_CR2_EXTTRIG | ADC_CR2_EXTSEL; /* check if this channel is an internal ADC channel, if so * enable the internal temperature and Vref */ if (adc_config[line].chan == 16 || adc_config[line].chan == 17) { /* check if the internal channels are configured to use ADC1 */ if (dev(line) != ADC1) { return -3; } dev(line)->CR2 |= ADC_CR2_TSVREFE; } /* free the device again */ done(line); return 0; }
int adc_init(adc_t line) { uint32_t clk_div = 2; /* check if the line is valid */ if (line >= ADC_NUMOF) { return -1; } /* lock and power-on the device */ prep(line); /* configure the pin */ gpio_init_analog(adc_config[line].pin); /* set clock prescaler to get the maximal possible ADC clock value */ for (clk_div = 2; clk_div < 8; clk_div += 2) { if ((CLOCK_CORECLOCK / clk_div) <= MAX_ADC_SPEED) { break; } } ADC->CCR = ((clk_div / 2) - 1) << 16; /* enable the ADC module */ dev(line)->CR2 = ADC_CR2_ADON; /* check if this channel is an internal ADC channel, if so * enable the internal temperature and Vref */ if (adc_config[line].chan == 16 || adc_config[line].chan == 17) { /* check if the internal channels are configured to use ADC1 */ if (dev(line) != ADC1) { return -3; } ADC->CCR |= ADC_CCR_TSVREFE; } /* free the device again */ done(line); return 0; }