mirror of https://github.com/ARMmbed/mbed-os.git
STM32F0 ADC: remove adc_inited flag
parent
b025ea16d6
commit
2e2744ccbe
|
@ -33,12 +33,12 @@
|
|||
#include "mbed_wait_api.h"
|
||||
#include "cmsis.h"
|
||||
#include "pinmap.h"
|
||||
#include "PeripheralPins.h"
|
||||
#include "mbed_error.h"
|
||||
#include "PeripheralPins.h"
|
||||
|
||||
int adc_inited = 0;
|
||||
|
||||
void analogin_init(analogin_t *obj, PinName pin) {
|
||||
void analogin_init(analogin_t *obj, PinName pin)
|
||||
{
|
||||
static int adc_calibrated = 0;
|
||||
uint32_t function = (uint32_t)NC;
|
||||
|
||||
// ADC Internal Channels "pins" (Temperature, Vref, Vbat, ...)
|
||||
|
@ -47,14 +47,14 @@ void analogin_init(analogin_t *obj, PinName pin) {
|
|||
if ((pin < 0xF0) || (pin >= 0x100)) {
|
||||
// Normal channels
|
||||
// Get the peripheral name from the pin and assign it to the object
|
||||
obj->handle.Instance = (ADC_TypeDef *) pinmap_peripheral(pin, PinMap_ADC);
|
||||
obj->handle.Instance = (ADC_TypeDef *)pinmap_peripheral(pin, PinMap_ADC);
|
||||
// Get the functions (adc channel) from the pin and assign it to the object
|
||||
function = pinmap_function(pin, PinMap_ADC);
|
||||
// Configure GPIO
|
||||
pinmap_pinout(pin, PinMap_ADC);
|
||||
} else {
|
||||
// Internal channels
|
||||
obj->handle.Instance = (ADC_TypeDef *) pinmap_peripheral(pin, PinMap_ADC_Internal);
|
||||
obj->handle.Instance = (ADC_TypeDef *)pinmap_peripheral(pin, PinMap_ADC_Internal);
|
||||
function = pinmap_function(pin, PinMap_ADC_Internal);
|
||||
// No GPIO configuration for internal channels
|
||||
}
|
||||
|
@ -66,17 +66,10 @@ void analogin_init(analogin_t *obj, PinName pin) {
|
|||
// Save pin number for the read function
|
||||
obj->pin = pin;
|
||||
|
||||
// The ADC initialization is done once
|
||||
if (adc_inited == 0) {
|
||||
adc_inited = 1;
|
||||
|
||||
// Enable ADC clock
|
||||
__ADC1_CLK_ENABLE();
|
||||
|
||||
// Configure ADC
|
||||
// Configure ADC object structures
|
||||
obj->handle.State = HAL_ADC_STATE_RESET;
|
||||
obj->handle.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
|
||||
obj->handle.Init.Resolution = ADC_RESOLUTION12b;
|
||||
obj->handle.Init.Resolution = ADC_RESOLUTION_12B;
|
||||
obj->handle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
|
||||
obj->handle.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD;
|
||||
obj->handle.Init.EOCSelection = EOC_SINGLE_CONV;
|
||||
|
@ -88,18 +81,23 @@ void analogin_init(analogin_t *obj, PinName pin) {
|
|||
obj->handle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
|
||||
obj->handle.Init.DMAContinuousRequests = DISABLE;
|
||||
obj->handle.Init.Overrun = OVR_DATA_OVERWRITTEN;
|
||||
|
||||
__HAL_RCC_ADC1_CLK_ENABLE();
|
||||
|
||||
if (HAL_ADC_Init(&obj->handle) != HAL_OK) {
|
||||
error("Cannot initialize ADC");
|
||||
}
|
||||
// Run the ADC calibration
|
||||
if (HAL_ADCEx_Calibration_Start(&obj->handle) != HAL_OK) {
|
||||
error("Cannot Start ADC_Calibration");
|
||||
}
|
||||
|
||||
// ADC calibration is done only once
|
||||
if (adc_calibrated == 0) {
|
||||
adc_calibrated = 1;
|
||||
HAL_ADCEx_Calibration_Start(&obj->handle);
|
||||
}
|
||||
}
|
||||
|
||||
static inline uint16_t adc_read(analogin_t *obj) {
|
||||
ADC_ChannelConfTypeDef sConfig;
|
||||
static inline uint16_t adc_read(analogin_t *obj)
|
||||
{
|
||||
ADC_ChannelConfTypeDef sConfig = {0};
|
||||
|
||||
// Configure ADC channel
|
||||
sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
|
||||
|
@ -182,20 +180,22 @@ static inline uint16_t adc_read(analogin_t *obj) {
|
|||
|
||||
// Wait end of conversion and get value
|
||||
if (HAL_ADC_PollForConversion(&obj->handle, 10) == HAL_OK) {
|
||||
return (HAL_ADC_GetValue(&obj->handle));
|
||||
return (uint16_t)HAL_ADC_GetValue(&obj->handle);
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
uint16_t analogin_read_u16(analogin_t *obj) {
|
||||
uint16_t analogin_read_u16(analogin_t *obj)
|
||||
{
|
||||
uint16_t value = adc_read(obj);
|
||||
// 12-bit to 16-bit conversion
|
||||
value = ((value << 4) & (uint16_t)0xFFF0) | ((value >> 8) & (uint16_t)0x000F);
|
||||
return value;
|
||||
}
|
||||
|
||||
float analogin_read(analogin_t *obj) {
|
||||
float analogin_read(analogin_t *obj)
|
||||
{
|
||||
uint16_t value = adc_read(obj);
|
||||
return (float)value * (1.0f / (float)0xFFF); // 12 bits range
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue