The f0, f30x and l0 have a very similar "v2" adc peripheral. Start extracting out some of the common code, and fix the glaring bug in adc_power_down that was affecting them both. This is not intended to be a fully comprehensive extraction, just the first easy steps.
116 lines
3.0 KiB
C
116 lines
3.0 KiB
C
/** @addtogroup adc_file
|
|
|
|
@author @htmlonly © @endhtmlonly
|
|
2015 Karl Palsson <karlp@tweak.net.au>
|
|
|
|
This library supports one style of the Analog to Digital Conversion System in
|
|
the STM32 series of ARM Cortex Microcontrollers by ST Microelectronics.
|
|
|
|
The style of ADC Peripheral supported by this code is found in the F0, L0 and
|
|
F30x series devices (at the time of writing)
|
|
|
|
LGPL License Terms @ref lgpl_license
|
|
*/
|
|
|
|
/*
|
|
* This file is part of the libopencm3 project.
|
|
*
|
|
* Copyright (C) 2015 Karl Palsson <karlp@tweak.net.au>
|
|
*
|
|
* This library is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU Lesser General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This library is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU Lesser General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
* along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
/**@{*/
|
|
|
|
#include <libopencm3/stm32/adc.h>
|
|
|
|
/**
|
|
* Turn on the ADC (async)
|
|
* @sa adc_wait_power_on
|
|
* @param adc ADC Block register address base @ref adc_reg_base
|
|
*/
|
|
void adc_power_on_async(uint32_t adc)
|
|
{
|
|
ADC_CR(adc) |= ADC_CR_ADEN;
|
|
}
|
|
|
|
/**
|
|
* Is the ADC powered up and ready?
|
|
* @sa adc_power_on_async
|
|
* @param adc ADC Block register address base @ref adc_reg_base
|
|
* @return true if adc is ready for use
|
|
*/
|
|
bool adc_is_power_on(uint32_t adc)
|
|
{
|
|
return (ADC_ISR(adc) & ADC_ISR_ADRDY);
|
|
}
|
|
|
|
/**
|
|
* Turn on the ADC
|
|
* @sa adc_power_on_async
|
|
* @param adc ADC Block register address base @ref adc_reg_base
|
|
*/
|
|
void adc_power_on(uint32_t adc)
|
|
{
|
|
adc_power_on_async(adc);
|
|
while (!adc_is_power_on(adc));
|
|
}
|
|
|
|
/**
|
|
* Turn off the ADC (async)
|
|
* This will actually block if it needs to turn off a currently running
|
|
* conversion, as per ref man. (Handles injected on hardware that supports
|
|
* injected conversions.
|
|
* @sa adc_wait_power_off
|
|
* @param adc ADC Block register address base @ref adc_reg_base
|
|
*/
|
|
void adc_power_off_async(uint32_t adc)
|
|
{
|
|
uint32_t checks = ADC_CR_ADSTART;
|
|
uint32_t stops = ADC_CR_ADSTP;
|
|
#if defined (ADC_CR_JADSTART)
|
|
checks |= ADC_CR_JADSTART;
|
|
stops |= ADC_CR_JADSTP;
|
|
#endif
|
|
if (ADC_CR(adc) & checks) {
|
|
ADC_CR(adc) |= stops;
|
|
while (ADC_CR(adc) & checks);
|
|
}
|
|
ADC_CR(adc) |= ADC_CR_ADDIS;
|
|
}
|
|
|
|
/**
|
|
* Is the ADC powered down?
|
|
* @sa adc_power_off_async
|
|
* @param adc ADC Block register address base @ref adc_reg_base
|
|
*/
|
|
bool adc_is_power_off(uint32_t adc)
|
|
{
|
|
return (!(ADC_CR(adc) & ADC_CR_ADEN));
|
|
}
|
|
|
|
/**
|
|
* Turn off the ADC
|
|
* This will actually block if it needs to turn off a currently running
|
|
* conversion, as per ref man.
|
|
* @sa adc_power_off_async
|
|
* @param adc ADC Block register address base @ref adc_reg_base
|
|
*/
|
|
void adc_power_off(uint32_t adc)
|
|
{
|
|
adc_power_off_async(adc);
|
|
while (!adc_is_power_off(adc));
|
|
}
|
|
|