Merge pull request #4623 from LMESTM/analogin_handle

Improve management handling of multiple instances of analogin ojects
pull/4844/head
Jimmy Brisson 2017-07-31 15:23:31 +00:00 committed by GitHub
commit f08d5a496c
66 changed files with 278 additions and 564 deletions

View File

@ -54,12 +54,6 @@ struct port_s {
__IO uint32_t *reg_out;
};
struct analogin_s {
ADCName adc;
PinName pin;
uint32_t channel;
};
#include "common_objects.h"
#ifdef __cplusplus

View File

@ -54,12 +54,6 @@ struct port_s {
__IO uint32_t *reg_out;
};
struct analogin_s {
ADCName adc;
PinName pin;
uint32_t channel;
};
#include "common_objects.h"
#ifdef __cplusplus

View File

@ -54,12 +54,6 @@ struct port_s {
__IO uint32_t *reg_out;
};
struct analogin_s {
ADCName adc;
PinName pin;
uint32_t channel;
};
#include "common_objects.h"
#ifdef __cplusplus

View File

@ -54,12 +54,6 @@ struct port_s {
__IO uint32_t *reg_out;
};
struct analogin_s {
ADCName adc;
PinName pin;
uint32_t channel;
};
struct can_s {
CANName can;
int index;

View File

@ -54,12 +54,6 @@ struct port_s {
__IO uint32_t *reg_out;
};
struct analogin_s {
ADCName adc;
PinName pin;
uint32_t channel;
};
#include "common_objects.h"
#ifdef __cplusplus

View File

@ -54,12 +54,6 @@ struct port_s {
__IO uint32_t *reg_out;
};
struct analogin_s {
ADCName adc;
PinName pin;
uint32_t channel;
};
struct can_s {
CANName can;
int index;

View File

@ -54,12 +54,6 @@ struct port_s {
__IO uint32_t *reg_out;
};
struct analogin_s {
ADCName adc;
PinName pin;
uint32_t channel;
};
struct can_s {
CANName can;
int index;

View File

@ -36,15 +36,13 @@
#include "PeripheralPins.h"
#include "mbed_error.h"
ADC_HandleTypeDef AdcHandle;
int adc_inited = 0;
void analogin_init(analogin_t *obj, PinName pin) {
// Get the peripheral name from the pin and assign it to the object
obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC);
MBED_ASSERT(obj->adc != (ADCName)NC);
obj->handle.Instance = (ADC_TypeDef *) pinmap_peripheral(pin, PinMap_ADC);
MBED_ASSERT(obj->handle.Instance != (ADC_TypeDef *)NC);
// Get the functions (adc channel) from the pin and assign it to the object
uint32_t function = pinmap_function(pin, PinMap_ADC);
MBED_ASSERT(function != (uint32_t)NC);
@ -67,25 +65,25 @@ void analogin_init(analogin_t *obj, PinName pin) {
__ADC1_CLK_ENABLE();
// Configure ADC
AdcHandle.Instance = (ADC_TypeDef *)(obj->adc);
AdcHandle.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
AdcHandle.Init.Resolution = ADC_RESOLUTION12b;
AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
AdcHandle.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD;
AdcHandle.Init.EOCSelection = EOC_SINGLE_CONV;
AdcHandle.Init.LowPowerAutoWait = DISABLE;
AdcHandle.Init.LowPowerAutoPowerOff = DISABLE;
AdcHandle.Init.ContinuousConvMode = DISABLE;
AdcHandle.Init.DiscontinuousConvMode = DISABLE;
AdcHandle.Init.ExternalTrigConv = ADC_SOFTWARE_START;
AdcHandle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
AdcHandle.Init.DMAContinuousRequests = DISABLE;
AdcHandle.Init.Overrun = OVR_DATA_OVERWRITTEN;
if (HAL_ADC_Init(&AdcHandle) != HAL_OK) {
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.DataAlign = ADC_DATAALIGN_RIGHT;
obj->handle.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD;
obj->handle.Init.EOCSelection = EOC_SINGLE_CONV;
obj->handle.Init.LowPowerAutoWait = DISABLE;
obj->handle.Init.LowPowerAutoPowerOff = DISABLE;
obj->handle.Init.ContinuousConvMode = DISABLE;
obj->handle.Init.DiscontinuousConvMode = DISABLE;
obj->handle.Init.ExternalTrigConv = ADC_SOFTWARE_START;
obj->handle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
obj->handle.Init.DMAContinuousRequests = DISABLE;
obj->handle.Init.Overrun = OVR_DATA_OVERWRITTEN;
if (HAL_ADC_Init(&obj->handle) != HAL_OK) {
error("Cannot initialize ADC");
}
// Run the ADC calibration
if (HAL_ADCEx_Calibration_Start(&AdcHandle) != HAL_OK) {
if (HAL_ADCEx_Calibration_Start(&obj->handle) != HAL_OK) {
error("Cannot Start ADC_Calibration");
}
}
@ -94,8 +92,6 @@ void analogin_init(analogin_t *obj, PinName pin) {
static inline uint16_t adc_read(analogin_t *obj) {
ADC_ChannelConfTypeDef sConfig;
AdcHandle.Instance = (ADC_TypeDef *)(obj->adc);
// Configure ADC channel
sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
#if defined (TARGET_STM32F091RC)
@ -169,15 +165,15 @@ static inline uint16_t adc_read(analogin_t *obj) {
}
// Clear all channels as it is not done in HAL_ADC_ConfigChannel()
AdcHandle.Instance->CHSELR = 0;
obj->handle.Instance->CHSELR = 0;
HAL_ADC_ConfigChannel(&AdcHandle, &sConfig);
HAL_ADC_ConfigChannel(&obj->handle, &sConfig);
HAL_ADC_Start(&AdcHandle); // Start conversion
HAL_ADC_Start(&obj->handle); // Start conversion
// Wait end of conversion and get value
if (HAL_ADC_PollForConversion(&AdcHandle, 10) == HAL_OK) {
return (HAL_ADC_GetValue(&AdcHandle));
if (HAL_ADC_PollForConversion(&obj->handle, 10) == HAL_OK) {
return (HAL_ADC_GetValue(&obj->handle));
} else {
return 0;
}

View File

@ -111,6 +111,12 @@ struct i2c_s {
#endif
};
struct analogin_s {
ADC_HandleTypeDef handle;
PinName pin;
uint8_t channel;
};
#include "gpio_object.h"
#if DEVICE_ANALOGOUT

View File

@ -54,12 +54,6 @@ struct port_s {
__IO uint32_t *reg_out;
};
struct analogin_s {
ADCName adc;
PinName pin;
uint8_t channel;
};
struct can_s {
CANName can;
int index;

View File

@ -54,12 +54,6 @@ struct port_s {
__IO uint32_t *reg_out;
};
struct analogin_s {
ADCName adc;
PinName pin;
uint8_t channel;
};
#include "common_objects.h"
#ifdef __cplusplus

View File

@ -54,12 +54,6 @@ struct port_s {
__IO uint32_t *reg_out;
};
struct analogin_s {
ADCName adc;
PinName pin;
uint8_t channel;
};
struct can_s {
CANName can;
int index;

View File

@ -35,8 +35,6 @@
#include "pinmap.h"
#include "PeripheralPins.h"
ADC_HandleTypeDef AdcHandle;
int adc_inited = 0;
void analogin_init(analogin_t *obj, PinName pin)
@ -44,9 +42,9 @@ void analogin_init(analogin_t *obj, PinName pin)
RCC_PeriphCLKInitTypeDef PeriphClkInit;
// Get the peripheral name from the pin and assign it to the object
obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC);
MBED_ASSERT(obj->adc != (ADCName)NC);
obj->handle.Instance = (ADC_TypeDef *) pinmap_peripheral(pin, PinMap_ADC);
MBED_ASSERT(obj->handle.Instance != (ADC_TypeDef *)NC);
// Get the functions (adc channel) from the pin and assign it to the object
uint32_t function = pinmap_function(pin, PinMap_ADC);
MBED_ASSERT(function != (uint32_t)NC);
@ -79,15 +77,15 @@ void analogin_init(analogin_t *obj, PinName pin)
HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit);
// Configure ADC
AdcHandle.Instance = (ADC_TypeDef *)(obj->adc);
AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
AdcHandle.Init.ScanConvMode = DISABLE;
AdcHandle.Init.ContinuousConvMode = DISABLE;
AdcHandle.Init.NbrOfConversion = 1;
AdcHandle.Init.DiscontinuousConvMode = DISABLE;
AdcHandle.Init.NbrOfDiscConversion = 0;
AdcHandle.Init.ExternalTrigConv = ADC_SOFTWARE_START;
HAL_ADC_Init(&AdcHandle);
obj->handle.State = HAL_ADC_STATE_RESET;
obj->handle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
obj->handle.Init.ScanConvMode = DISABLE;
obj->handle.Init.ContinuousConvMode = DISABLE;
obj->handle.Init.NbrOfConversion = 1;
obj->handle.Init.DiscontinuousConvMode = DISABLE;
obj->handle.Init.NbrOfDiscConversion = 0;
obj->handle.Init.ExternalTrigConv = ADC_SOFTWARE_START;
HAL_ADC_Init(&obj->handle);
}
}
@ -95,8 +93,6 @@ static inline uint16_t adc_read(analogin_t *obj)
{
ADC_ChannelConfTypeDef sConfig;
AdcHandle.Instance = (ADC_TypeDef *)(obj->adc);
// Configure ADC channel
sConfig.Rank = 1;
sConfig.SamplingTime = ADC_SAMPLETIME_7CYCLES_5;
@ -160,13 +156,13 @@ static inline uint16_t adc_read(analogin_t *obj)
return 0;
}
HAL_ADC_ConfigChannel(&AdcHandle, &sConfig);
HAL_ADC_ConfigChannel(&obj->handle, &sConfig);
HAL_ADC_Start(&AdcHandle); // Start conversion
HAL_ADC_Start(&obj->handle); // Start conversion
// Wait end of conversion and get value
if (HAL_ADC_PollForConversion(&AdcHandle, 10) == HAL_OK) {
return (HAL_ADC_GetValue(&AdcHandle));
if (HAL_ADC_PollForConversion(&obj->handle, 10) == HAL_OK) {
return (HAL_ADC_GetValue(&obj->handle));
} else {
return 0;
}

View File

@ -110,6 +110,12 @@ struct i2c_s {
#endif
};
struct analogin_s {
ADC_HandleTypeDef handle;
PinName pin;
uint8_t channel;
};
#include "gpio_object.h"
#ifdef __cplusplus

View File

@ -36,12 +36,9 @@
#include "mbed_error.h"
#include "PeripheralPins.h"
ADC_HandleTypeDef AdcHandle;
void analogin_init(analogin_t *obj, PinName pin)
{
uint32_t function = (uint32_t)NC;
obj->adc = (ADCName)NC;
#if defined(ADC1)
static int adc1_inited = 0;
@ -58,18 +55,18 @@ 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->adc = (ADCName)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->adc = (ADCName)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
}
MBED_ASSERT(obj->adc != (ADCName)NC);
MBED_ASSERT(obj->handle.Instance != (ADC_TypeDef *)NC);
MBED_ASSERT(function != (uint32_t)NC);
obj->channel = STM_PIN_CHANNEL(function);
@ -80,41 +77,41 @@ void analogin_init(analogin_t *obj, PinName pin)
// Check if ADC is already initialized
// Enable ADC clock
#if defined(ADC1)
if ((obj->adc == ADC_1) && adc1_inited) return;
if (obj->adc == ADC_1) {
if (((ADCName)obj->handle.Instance == ADC_1) && adc1_inited) return;
if ((ADCName)obj->handle.Instance == ADC_1) {
__ADC1_CLK_ENABLE();
adc1_inited = 1;
}
#endif
#if defined(ADC2)
if ((obj->adc == ADC_2) && adc2_inited) return;
if (obj->adc == ADC_2) {
if (((ADCName)obj->handle.Instance == ADC_2) && adc2_inited) return;
if ((ADCName)obj->handle.Instance == ADC_2) {
__ADC2_CLK_ENABLE();
adc2_inited = 1;
}
#endif
#if defined(ADC3)
if ((obj->adc == ADC_3) && adc3_inited) return;
if (obj->adc == ADC_3) {
if (((ADCName)obj->handle.Instance == ADC_3) && adc3_inited) return;
if ((ADCName)obj->handle.Instance == ADC_3) {
__ADC3_CLK_ENABLE();
adc3_inited = 1;
}
#endif
// Configure ADC
AdcHandle.Instance = (ADC_TypeDef *)(obj->adc);
AdcHandle.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV2;
AdcHandle.Init.Resolution = ADC_RESOLUTION12b;
AdcHandle.Init.ScanConvMode = DISABLE;
AdcHandle.Init.ContinuousConvMode = DISABLE;
AdcHandle.Init.DiscontinuousConvMode = DISABLE;
AdcHandle.Init.NbrOfDiscConversion = 0;
AdcHandle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
AdcHandle.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T1_CC1;
AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
AdcHandle.Init.NbrOfConversion = 1;
AdcHandle.Init.DMAContinuousRequests = DISABLE;
AdcHandle.Init.EOCSelection = DISABLE;
if (HAL_ADC_Init(&AdcHandle) != HAL_OK) {
obj->handle.State = HAL_ADC_STATE_RESET;
obj->handle.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV2;
obj->handle.Init.Resolution = ADC_RESOLUTION12b;
obj->handle.Init.ScanConvMode = DISABLE;
obj->handle.Init.ContinuousConvMode = DISABLE;
obj->handle.Init.DiscontinuousConvMode = DISABLE;
obj->handle.Init.NbrOfDiscConversion = 0;
obj->handle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
obj->handle.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T1_CC1;
obj->handle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
obj->handle.Init.NbrOfConversion = 1;
obj->handle.Init.DMAContinuousRequests = DISABLE;
obj->handle.Init.EOCSelection = DISABLE;
if (HAL_ADC_Init(&obj->handle) != HAL_OK) {
error("Cannot initialize ADC\n");
}
}
@ -123,8 +120,6 @@ static inline uint16_t adc_read(analogin_t *obj)
{
ADC_ChannelConfTypeDef sConfig = {0};
AdcHandle.Instance = (ADC_TypeDef *)(obj->adc);
// Configure ADC channel
sConfig.Rank = 1;
sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
@ -192,13 +187,13 @@ static inline uint16_t adc_read(analogin_t *obj)
return 0;
}
HAL_ADC_ConfigChannel(&AdcHandle, &sConfig);
HAL_ADC_ConfigChannel(&obj->handle, &sConfig);
HAL_ADC_Start(&AdcHandle); // Start conversion
HAL_ADC_Start(&obj->handle); // Start conversion
// Wait end of conversion and get value
if (HAL_ADC_PollForConversion(&AdcHandle, 10) == HAL_OK) {
return (HAL_ADC_GetValue(&AdcHandle));
if (HAL_ADC_PollForConversion(&obj->handle, 10) == HAL_OK) {
return (HAL_ADC_GetValue(&obj->handle));
} else {
return 0;
}

View File

@ -55,7 +55,7 @@ struct port_s {
};
struct analogin_s {
ADCName adc;
ADC_HandleTypeDef handle;
PinName pin;
uint8_t channel;
};

View File

@ -54,12 +54,6 @@ struct port_s {
__IO uint32_t *reg_out;
};
struct analogin_s {
ADCName adc;
PinName pin;
uint32_t channel;
};
struct can_s {
CANName can;
int index;

View File

@ -54,12 +54,6 @@ struct port_s {
__IO uint32_t *reg_out;
};
struct analogin_s {
ADCName adc;
PinName pin;
uint32_t channel;
};
struct can_s {
CANName can;
int index;

View File

@ -54,13 +54,6 @@ struct port_s {
__IO uint32_t *reg_out;
};
struct analogin_s {
ADCName adc;
PinName pin;
uint32_t channel;
DAC_HandleTypeDef handle;
};
struct can_s {
CANName can;
int index;

View File

@ -54,12 +54,6 @@ struct port_s {
__IO uint32_t *reg_out;
};
struct analogin_s {
ADCName adc;
PinName pin;
uint32_t channel;
};
struct can_s {
CANName can;
int index;

View File

@ -54,12 +54,6 @@ struct port_s {
__IO uint32_t *reg_out;
};
struct analogin_s {
ADCName adc;
PinName pin;
uint32_t channel;
};
#if defined (DEVICE_CAN)
struct can_s {
CANName can;

View File

@ -36,7 +36,6 @@
#include "mbed_error.h"
#include "PeripheralPins.h"
ADC_HandleTypeDef AdcHandle;
void analogin_init(analogin_t *obj, PinName pin)
{
@ -54,8 +53,8 @@ void analogin_init(analogin_t *obj, PinName pin)
#endif
// Get the peripheral name from the pin and assign it to the object
obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC);
MBED_ASSERT(obj->adc != (ADCName)NC);
obj->handle.Instance = (ADC_TypeDef *) pinmap_peripheral(pin, PinMap_ADC);
MBED_ASSERT(obj->handle.Instance != (ADC_TypeDef *)NC);
// Get the pin function and assign the used channel to the object
uint32_t function = pinmap_function(pin, PinMap_ADC);
@ -74,56 +73,52 @@ void analogin_init(analogin_t *obj, PinName pin)
// Check if ADC is already initialized
// Enable ADC clock
#if defined(ADC1)
if ((obj->adc == ADC_1) && adc1_inited) return;
if (obj->adc == ADC_1) {
AdcHandle.State = HAL_ADC_STATE_RESET;
if (((ADCName)obj->handle.Instance == ADC_1) && adc1_inited) return;
if ((ADCName)obj->handle.Instance == ADC_1) {
__ADC1_CLK_ENABLE();
adc1_inited = 1;
}
#endif
#if defined(ADC2)
if ((obj->adc == ADC_2) && adc2_inited) return;
if (obj->adc == ADC_2) {
AdcHandle.State = HAL_ADC_STATE_RESET;
if (((ADCName)obj->handle.Instance == ADC_2) && adc2_inited) return;
if ((ADCName)obj->handle.Instance == ADC_2) {
__ADC2_CLK_ENABLE();
adc2_inited = 1;
}
#endif
#if defined(ADC3)
if ((obj->adc == ADC_3) && adc3_inited) return;
if (obj->adc == ADC_3) {
AdcHandle.State = HAL_ADC_STATE_RESET;
if (((ADCName)obj->handle.Instance == ADC_3) && adc3_inited) return;
if ((ADCName)obj->handle.Instance == ADC_3) {
__ADC34_CLK_ENABLE();
adc3_inited = 1;
}
#endif
#if defined(ADC4)
if ((obj->adc == ADC_4) && adc4_inited) return;
if (obj->adc == ADC_4) {
AdcHandle.State = HAL_ADC_STATE_RESET;
if (((ADCName)obj->handle.Instance == ADC_4) && adc4_inited) return;
if ((ADCName)obj->handle.Instance == ADC_4) {
__ADC34_CLK_ENABLE();
adc4_inited = 1;
}
#endif
// Configure ADC
AdcHandle.Instance = (ADC_TypeDef *)(obj->adc);
AdcHandle.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV2;
AdcHandle.Init.Resolution = ADC_RESOLUTION12b;
AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
AdcHandle.Init.ScanConvMode = DISABLE;
AdcHandle.Init.EOCSelection = EOC_SINGLE_CONV;
AdcHandle.Init.LowPowerAutoWait = DISABLE;
AdcHandle.Init.ContinuousConvMode = DISABLE;
AdcHandle.Init.NbrOfConversion = 1;
AdcHandle.Init.DiscontinuousConvMode = DISABLE;
AdcHandle.Init.NbrOfDiscConversion = 0;
AdcHandle.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T1_CC1;
AdcHandle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
AdcHandle.Init.DMAContinuousRequests = DISABLE;
AdcHandle.Init.Overrun = OVR_DATA_OVERWRITTEN;
obj->handle.State = HAL_ADC_STATE_RESET;
obj->handle.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV2;
obj->handle.Init.Resolution = ADC_RESOLUTION12b;
obj->handle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
obj->handle.Init.ScanConvMode = DISABLE;
obj->handle.Init.EOCSelection = EOC_SINGLE_CONV;
obj->handle.Init.LowPowerAutoWait = DISABLE;
obj->handle.Init.ContinuousConvMode = DISABLE;
obj->handle.Init.NbrOfConversion = 1;
obj->handle.Init.DiscontinuousConvMode = DISABLE;
obj->handle.Init.NbrOfDiscConversion = 0;
obj->handle.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T1_CC1;
obj->handle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
obj->handle.Init.DMAContinuousRequests = DISABLE;
obj->handle.Init.Overrun = OVR_DATA_OVERWRITTEN;
if (HAL_ADC_Init(&AdcHandle) != HAL_OK) {
if (HAL_ADC_Init(&obj->handle) != HAL_OK) {
error("Cannot initialize ADC");
}
}
@ -132,8 +127,6 @@ static inline uint16_t adc_read(analogin_t *obj)
{
ADC_ChannelConfTypeDef sConfig = {0};
AdcHandle.Instance = (ADC_TypeDef *)(obj->adc);
// Configure ADC channel
sConfig.Rank = ADC_REGULAR_RANK_1;
sConfig.SamplingTime = ADC_SAMPLETIME_19CYCLES_5;
@ -200,13 +193,13 @@ static inline uint16_t adc_read(analogin_t *obj)
return 0;
}
HAL_ADC_ConfigChannel(&AdcHandle, &sConfig);
HAL_ADC_ConfigChannel(&obj->handle, &sConfig);
HAL_ADC_Start(&AdcHandle); // Start conversion
HAL_ADC_Start(&obj->handle); // Start conversion
// Wait end of conversion and get value
if (HAL_ADC_PollForConversion(&AdcHandle, 10) == HAL_OK) {
return (HAL_ADC_GetValue(&AdcHandle));
if (HAL_ADC_PollForConversion(&obj->handle, 10) == HAL_OK) {
return (HAL_ADC_GetValue(&obj->handle));
} else {
return 0;
}

View File

@ -118,6 +118,12 @@ struct dac_s {
DAC_HandleTypeDef handle;
};
struct analogin_s {
ADC_HandleTypeDef handle;
PinName pin;
uint8_t channel;
};
#include "gpio_object.h"
#ifdef __cplusplus

View File

@ -54,12 +54,6 @@ struct port_s {
__IO uint32_t *reg_out;
};
struct analogin_s {
ADCName adc;
PinName pin;
uint8_t channel;
};
#include "common_objects.h"
#ifdef __cplusplus

View File

@ -54,12 +54,6 @@ struct port_s {
__IO uint32_t *reg_out;
};
struct analogin_s {
ADCName adc;
PinName pin;
uint8_t channel;
};
#include "common_objects.h"
#ifdef __cplusplus

View File

@ -54,12 +54,6 @@ struct port_s {
__IO uint32_t *reg_out;
};
struct analogin_s {
ADCName adc;
PinName pin;
uint8_t channel;
};
#include "common_objects.h"
#ifdef __cplusplus

View File

@ -54,12 +54,6 @@ struct port_s {
__IO uint32_t *reg_out;
};
struct analogin_s {
ADCName adc;
PinName pin;
uint8_t channel;
};
#include "common_objects.h"
#ifdef __cplusplus

View File

@ -54,12 +54,6 @@ struct port_s {
__IO uint32_t *reg_out;
};
struct analogin_s {
ADCName adc;
PinName pin;
uint8_t channel;
};
#include "common_objects.h"
#ifdef __cplusplus

View File

@ -54,12 +54,6 @@ struct port_s {
__IO uint32_t *reg_out;
};
struct analogin_s {
ADCName adc;
PinName pin;
uint8_t channel;
};
#include "common_objects.h"
#ifdef __cplusplus

View File

@ -54,12 +54,6 @@ struct port_s {
__IO uint32_t *reg_out;
};
struct analogin_s {
ADCName adc;
PinName pin;
uint8_t channel;
};
struct trng_s {
RNG_HandleTypeDef handle;
};

View File

@ -54,12 +54,6 @@ struct port_s {
__IO uint32_t *reg_out;
};
struct analogin_s {
ADCName adc;
PinName pin;
uint8_t channel;
};
#include "common_objects.h"
#ifdef __cplusplus

View File

@ -40,12 +40,6 @@ struct port_s {
__IO uint32_t *reg_out;
};
struct analogin_s {
ADCName adc;
PinName pin;
uint8_t channel;
};
struct can_s {
CANName can;
int index;

View File

@ -40,12 +40,6 @@ struct port_s {
__IO uint32_t *reg_out;
};
struct analogin_s {
ADCName adc;
PinName pin;
uint8_t channel;
};
struct can_s {
CANName can;
int index;

View File

@ -54,12 +54,6 @@ struct port_s {
__IO uint32_t *reg_out;
};
struct analogin_s {
ADCName adc;
PinName pin;
uint8_t channel;
};
struct can_s {
CANName can;
int index;

View File

@ -54,12 +54,6 @@ struct port_s {
__IO uint32_t *reg_out;
};
struct analogin_s {
ADCName adc;
PinName pin;
uint8_t channel;
};
struct trng_s {
RNG_HandleTypeDef handle;
};

View File

@ -54,12 +54,6 @@ struct port_s {
__IO uint32_t *reg_out;
};
struct analogin_s {
ADCName adc;
PinName pin;
uint8_t channel;
};
struct can_s {
CANName can;
int index;

View File

@ -54,12 +54,6 @@ struct port_s {
__IO uint32_t *reg_out;
};
struct analogin_s {
ADCName adc;
PinName pin;
uint8_t channel;
};
struct can_s {
CANName can;
int index;

View File

@ -54,12 +54,6 @@ struct port_s {
__IO uint32_t *reg_out;
};
struct analogin_s {
ADCName adc;
PinName pin;
uint8_t channel;
};
struct can_s {
CANName can;
int index;

View File

@ -36,12 +36,9 @@
#include "mbed_error.h"
#include "PeripheralPins.h"
ADC_HandleTypeDef AdcHandle;
void analogin_init(analogin_t *obj, PinName pin)
{
uint32_t function = (uint32_t)NC;
obj->adc = (ADCName)NC;
#if defined(ADC1)
static int adc1_inited = 0;
@ -58,18 +55,18 @@ void analogin_init(analogin_t *obj, PinName pin)
if (pin < 0xF0) {
// Normal channels
// Get the peripheral name from the pin and assign it to the object
obj->adc = (ADCName)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->adc = (ADCName)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
}
MBED_ASSERT(obj->adc != (ADCName)NC);
MBED_ASSERT(obj->handle.Instance != (ADC_TypeDef *)NC);
MBED_ASSERT(function != (uint32_t)NC);
obj->channel = STM_PIN_CHANNEL(function);
@ -80,42 +77,42 @@ void analogin_init(analogin_t *obj, PinName pin)
// Check if ADC is already initialized
// Enable ADC clock
#if defined(ADC1)
if ((obj->adc == ADC_1) && adc1_inited) return;
if (obj->adc == ADC_1) {
if (((ADCName)obj->handle.Instance == ADC_1) && adc1_inited) return;
if ((ADCName)obj->handle.Instance == ADC_1) {
__HAL_RCC_ADC1_CLK_ENABLE();
adc1_inited = 1;
}
#endif
#if defined(ADC2)
if ((obj->adc == ADC_2) && adc2_inited) return;
if (obj->adc == ADC_2) {
if (((ADCName)obj->handle.Instance == ADC_2) && adc2_inited) return;
if ((ADCName)obj->handle.Instance == ADC_2) {
__HAL_RCC_ADC2_CLK_ENABLE();
adc2_inited = 1;
}
#endif
#if defined(ADC3)
if ((obj->adc == ADC_3) && adc3_inited) return;
if (obj->adc == ADC_3) {
if (((ADCName)obj->handle.Instance == ADC_3) && adc3_inited) return;
if ((ADCName)obj->handle.Instance == ADC_3) {
__HAL_RCC_ADC3_CLK_ENABLE();
adc3_inited = 1;
}
#endif
// Configure ADC
AdcHandle.Instance = (ADC_TypeDef *)(obj->adc);
AdcHandle.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;
AdcHandle.Init.Resolution = ADC_RESOLUTION_12B;
AdcHandle.Init.ScanConvMode = DISABLE;
AdcHandle.Init.ContinuousConvMode = DISABLE;
AdcHandle.Init.DiscontinuousConvMode = DISABLE;
AdcHandle.Init.NbrOfDiscConversion = 0;
AdcHandle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
AdcHandle.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T1_CC1;
AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
AdcHandle.Init.NbrOfConversion = 1;
AdcHandle.Init.DMAContinuousRequests = DISABLE;
AdcHandle.Init.EOCSelection = DISABLE;
obj->handle.State = HAL_ADC_STATE_RESET;
obj->handle.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;
obj->handle.Init.Resolution = ADC_RESOLUTION_12B;
obj->handle.Init.ScanConvMode = DISABLE;
obj->handle.Init.ContinuousConvMode = DISABLE;
obj->handle.Init.DiscontinuousConvMode = DISABLE;
obj->handle.Init.NbrOfDiscConversion = 0;
obj->handle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
obj->handle.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T1_CC1;
obj->handle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
obj->handle.Init.NbrOfConversion = 1;
obj->handle.Init.DMAContinuousRequests = DISABLE;
obj->handle.Init.EOCSelection = DISABLE;
if (HAL_ADC_Init(&AdcHandle) != HAL_OK) {
if (HAL_ADC_Init(&obj->handle) != HAL_OK) {
error("Cannot initialize ADC\n");
}
}
@ -124,8 +121,6 @@ static inline uint16_t adc_read(analogin_t *obj)
{
ADC_ChannelConfTypeDef sConfig = {0};
AdcHandle.Instance = (ADC_TypeDef *)(obj->adc);
// Configure ADC channel
sConfig.Rank = 1;
sConfig.SamplingTime = ADC_SAMPLETIME_15CYCLES;
@ -202,13 +197,13 @@ static inline uint16_t adc_read(analogin_t *obj)
// so VBAT readings are returned in place of temperature.
ADC->CCR &= ~(ADC_CCR_VBATE | ADC_CCR_TSVREFE);
HAL_ADC_ConfigChannel(&AdcHandle, &sConfig);
HAL_ADC_ConfigChannel(&obj->handle, &sConfig);
HAL_ADC_Start(&AdcHandle); // Start conversion
HAL_ADC_Start(&obj->handle); // Start conversion
// Wait end of conversion and get value
if (HAL_ADC_PollForConversion(&AdcHandle, 10) == HAL_OK) {
return (uint16_t)HAL_ADC_GetValue(&AdcHandle);
if (HAL_ADC_PollForConversion(&obj->handle, 10) == HAL_OK) {
return (uint16_t)HAL_ADC_GetValue(&obj->handle);
} else {
return 0;
}

View File

@ -109,11 +109,19 @@ struct i2c_s {
uint8_t available_events;
#endif
};
#if DEVICE_FLASH
struct flash_s {
uint32_t dummy;
};
#endif
struct analogin_s {
ADC_HandleTypeDef handle;
PinName pin;
uint8_t channel;
};
#define GPIO_IP_WITHOUT_BRR
#include "gpio_object.h"

View File

@ -54,12 +54,6 @@ struct port_s {
__IO uint32_t *reg_out;
};
struct analogin_s {
ADCName adc;
PinName pin;
uint8_t channel;
};
struct can_s {
CANName can;
int index;

View File

@ -54,12 +54,6 @@ struct port_s {
__IO uint32_t *reg_out;
};
struct analogin_s {
ADCName adc;
PinName pin;
uint8_t channel;
};
struct can_s {
CANName can;
int index;

View File

@ -54,12 +54,6 @@ struct port_s {
__IO uint32_t *reg_out;
};
struct analogin_s {
ADCName adc;
PinName pin;
uint8_t channel;
};
struct can_s {
CANName can;
int index;

View File

@ -54,12 +54,6 @@ struct port_s {
__IO uint32_t *reg_out;
};
struct analogin_s {
ADCName adc;
PinName pin;
uint8_t channel;
};
struct can_s {
CANName can;
int index;

View File

@ -36,12 +36,9 @@
#include "PeripheralPins.h"
#include "mbed_error.h"
ADC_HandleTypeDef AdcHandle;
void analogin_init(analogin_t *obj, PinName pin)
{
uint32_t function = (uint32_t)NC;
obj->adc = (ADCName)NC;
#if defined(ADC1)
static int adc1_inited = 0;
@ -58,18 +55,17 @@ void analogin_init(analogin_t *obj, PinName pin)
if (pin < 0xF0) {
// Normal channels
// Get the peripheral name from the pin and assign it to the object
obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC);
// Get the functions (adc channel) from the pin and assign it to the object
obj->handle.Instance = (ADC_TypeDef *) pinmap_peripheral(pin, PinMap_ADC);
function = pinmap_function(pin, PinMap_ADC);
// Configure GPIO
pinmap_pinout(pin, PinMap_ADC);
} else {
// Internal channels
obj->adc = (ADCName)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
}
MBED_ASSERT(obj->adc != (ADCName)NC);
MBED_ASSERT(obj->handle.Instance != (ADC_TypeDef *)NC);
MBED_ASSERT(function != (uint32_t)NC);
obj->channel = STM_PIN_CHANNEL(function);
@ -80,43 +76,43 @@ void analogin_init(analogin_t *obj, PinName pin)
// Check if ADC is already initialized
// Enable ADC clock
#if defined(ADC1)
if ((obj->adc == ADC_1) && adc1_inited) return;
if (obj->adc == ADC_1) {
if (((ADCName)obj->handle.Instance == ADC_1) && adc1_inited) return;
if ((ADCName)obj->handle.Instance == ADC_1) {
__HAL_RCC_ADC1_CLK_ENABLE();
adc1_inited = 1;
}
#endif
#if defined(ADC2)
if ((obj->adc == ADC_2) && adc2_inited) return;
if (obj->adc == ADC_2) {
if (((ADCName)obj->handle.Instance == ADC_2) && adc2_inited) return;
if ((ADCName)obj->handle.Instance == ADC_2) {
__HAL_RCC_ADC2_CLK_ENABLE();
adc2_inited = 1;
}
#endif
#if defined(ADC3)
if ((obj->adc == ADC_3) && adc3_inited) return;
if (obj->adc == ADC_3) {
if (((ADCName)obj->handle.Instance == ADC_3) && adc3_inited) return;
if ((ADCName)obj->handle.Instance == ADC_3) {
__HAL_RCC_ADC3_CLK_ENABLE();
adc3_inited = 1;
}
#endif
// Configure ADC
AdcHandle.Instance = (ADC_TypeDef *)(obj->adc);
AdcHandle.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV4;
AdcHandle.Init.Resolution = ADC_RESOLUTION_12B;
AdcHandle.Init.ScanConvMode = DISABLE;
AdcHandle.Init.ContinuousConvMode = DISABLE;
AdcHandle.Init.DiscontinuousConvMode = DISABLE;
AdcHandle.Init.NbrOfDiscConversion = 0;
AdcHandle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
AdcHandle.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T1_CC1;
AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
AdcHandle.Init.NbrOfConversion = 1;
AdcHandle.Init.DMAContinuousRequests = DISABLE;
AdcHandle.Init.EOCSelection = DISABLE;
obj->handle.State = HAL_ADC_STATE_RESET;
obj->handle.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV4;
obj->handle.Init.Resolution = ADC_RESOLUTION_12B;
obj->handle.Init.ScanConvMode = DISABLE;
obj->handle.Init.ContinuousConvMode = DISABLE;
obj->handle.Init.DiscontinuousConvMode = DISABLE;
obj->handle.Init.NbrOfDiscConversion = 0;
obj->handle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
obj->handle.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T1_CC1;
obj->handle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
obj->handle.Init.NbrOfConversion = 1;
obj->handle.Init.DMAContinuousRequests = DISABLE;
obj->handle.Init.EOCSelection = DISABLE;
if (HAL_ADC_Init(&AdcHandle) != HAL_OK) {
if (HAL_ADC_Init(&obj->handle) != HAL_OK) {
error("Cannot initialize ADC");
}
}
@ -125,8 +121,6 @@ static inline uint16_t adc_read(analogin_t *obj)
{
ADC_ChannelConfTypeDef sConfig = {0};
AdcHandle.Instance = (ADC_TypeDef *)(obj->adc);
// Configure ADC channel
sConfig.Rank = 1;
sConfig.SamplingTime = ADC_SAMPLETIME_15CYCLES;
@ -194,16 +188,16 @@ static inline uint16_t adc_read(analogin_t *obj)
return 0;
}
if (HAL_ADC_ConfigChannel(&AdcHandle, &sConfig) != HAL_OK) {
if (HAL_ADC_ConfigChannel(&obj->handle, &sConfig) != HAL_OK) {
error("Cannot configure ADC channel");
}
HAL_ADC_Start(&AdcHandle); // Start conversion
HAL_ADC_Start(&obj->handle); // Start conversion
// Wait end of conversion and get value
HAL_ADC_PollForConversion(&AdcHandle, 10);
if (HAL_ADC_GetState(&AdcHandle) & HAL_ADC_STATE_EOC_REG) {
return (HAL_ADC_GetValue(&AdcHandle));
HAL_ADC_PollForConversion(&obj->handle, 10);
if (HAL_ADC_GetState(&obj->handle) & HAL_ADC_STATE_EOC_REG) {
return (HAL_ADC_GetValue(&obj->handle));
} else {
return 0;
}

View File

@ -111,6 +111,12 @@ struct i2c_s {
#endif
};
struct analogin_s {
ADC_HandleTypeDef handle;
PinName pin;
uint8_t channel;
};
#define GPIO_IP_WITHOUT_BRR
#include "gpio_object.h"

View File

@ -54,12 +54,6 @@ struct port_s {
__IO uint32_t *reg_out;
};
struct analogin_s {
ADCName adc;
PinName pin;
uint32_t channel;
};
struct trng_s {
RNG_HandleTypeDef handle;
};

View File

@ -54,12 +54,6 @@ struct port_s {
__IO uint32_t *reg_out;
};
struct analogin_s {
ADCName adc;
PinName pin;
uint32_t channel;
};
struct trng_s {
RNG_HandleTypeDef handle;
};

View File

@ -54,12 +54,6 @@ struct port_s {
__IO uint32_t *reg_out;
};
struct analogin_s {
ADCName adc;
PinName pin;
uint32_t channel;
};
#include "common_objects.h"
#ifdef __cplusplus

View File

@ -54,12 +54,6 @@ struct port_s {
__IO uint32_t *reg_out;
};
struct analogin_s {
ADCName adc;
PinName pin;
uint32_t channel;
};
#include "common_objects.h"
#ifdef __cplusplus

View File

@ -54,12 +54,6 @@ struct port_s {
__IO uint32_t *reg_out;
};
struct analogin_s {
ADCName adc;
PinName pin;
uint32_t channel;
};
#include "common_objects.h"
#ifdef __cplusplus

View File

@ -54,12 +54,6 @@ struct port_s {
__IO uint32_t *reg_out;
};
struct analogin_s {
ADCName adc;
PinName pin;
uint32_t channel;
};
struct trng_s {
RNG_HandleTypeDef handle;
};

View File

@ -36,14 +36,12 @@
#include "mbed_error.h"
#include "PeripheralPins.h"
ADC_HandleTypeDef AdcHandle;
int adc_inited = 0;
void analogin_init(analogin_t *obj, PinName pin)
{
uint32_t function = (uint32_t)NC;
obj->adc = (ADCName)NC;
obj->handle.Instance = (ADC_TypeDef *)NC;
// ADC Internal Channels "pins" (Temperature, Vref, Vbat, ...)
// are described in PinNames.h and PeripheralPins.c
@ -51,18 +49,18 @@ void analogin_init(analogin_t *obj, PinName pin)
if (pin < 0xF0) {
// Normal channels
// Get the peripheral name from the pin and assign it to the object
obj->adc = (ADCName)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->adc = (ADCName)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
}
MBED_ASSERT(obj->adc != (ADCName)NC);
MBED_ASSERT(obj->handle.Instance != (ADC_TypeDef *)NC);
MBED_ASSERT(function != (uint32_t)NC);
obj->channel = STM_PIN_CHANNEL(function);
@ -74,37 +72,36 @@ void analogin_init(analogin_t *obj, PinName pin)
if (adc_inited == 0) {
adc_inited = 1;
AdcHandle.Instance = (ADC_TypeDef *)(obj->adc);
obj->handle.State = HAL_ADC_STATE_RESET;
// Enable ADC clock
__ADC1_CLK_ENABLE();
// Configure ADC
AdcHandle.Init.OversamplingMode = DISABLE;
AdcHandle.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV1;
AdcHandle.Init.Resolution = ADC_RESOLUTION12b;
AdcHandle.Init.SamplingTime = ADC_SAMPLETIME_239CYCLES_5;
AdcHandle.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD;
AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
AdcHandle.Init.ContinuousConvMode = DISABLE;
AdcHandle.Init.DiscontinuousConvMode = DISABLE;
AdcHandle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIG_EDGE_NONE;
AdcHandle.Init.ExternalTrigConv = ADC_EXTERNALTRIG0_T6_TRGO; // Not used here
AdcHandle.Init.DMAContinuousRequests = DISABLE;
AdcHandle.Init.EOCSelection = EOC_SINGLE_CONV;
AdcHandle.Init.Overrun = OVR_DATA_OVERWRITTEN;
AdcHandle.Init.LowPowerAutoWait = ENABLE;
AdcHandle.Init.LowPowerFrequencyMode = DISABLE; // To be enabled only if ADC clock < 2.8 MHz
AdcHandle.Init.LowPowerAutoPowerOff = DISABLE;
obj->handle.Init.OversamplingMode = DISABLE;
obj->handle.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV1;
obj->handle.Init.Resolution = ADC_RESOLUTION12b;
obj->handle.Init.SamplingTime = ADC_SAMPLETIME_239CYCLES_5;
obj->handle.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD;
obj->handle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
obj->handle.Init.ContinuousConvMode = DISABLE;
obj->handle.Init.DiscontinuousConvMode = DISABLE;
obj->handle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIG_EDGE_NONE;
obj->handle.Init.ExternalTrigConv = ADC_EXTERNALTRIG0_T6_TRGO; // Not used here
obj->handle.Init.DMAContinuousRequests = DISABLE;
obj->handle.Init.EOCSelection = EOC_SINGLE_CONV;
obj->handle.Init.Overrun = OVR_DATA_OVERWRITTEN;
obj->handle.Init.LowPowerAutoWait = ENABLE;
obj->handle.Init.LowPowerFrequencyMode = DISABLE; // To be enabled only if ADC clock < 2.8 MHz
obj->handle.Init.LowPowerAutoPowerOff = DISABLE;
if (HAL_ADC_Init(&AdcHandle) != HAL_OK) {
if (HAL_ADC_Init(&obj->handle) != HAL_OK) {
error("Cannot initialize ADC");
}
// Calibration
HAL_ADCEx_Calibration_Start(&AdcHandle, ADC_SINGLE_ENDED);
HAL_ADCEx_Calibration_Start(&obj->handle, ADC_SINGLE_ENDED);
__HAL_ADC_ENABLE(&AdcHandle);
__HAL_ADC_ENABLE(&obj->handle);
}
}
@ -112,8 +109,6 @@ static inline uint16_t adc_read(analogin_t *obj)
{
ADC_ChannelConfTypeDef sConfig = {0};
AdcHandle.Instance = (ADC_TypeDef *)(obj->adc);
// Configure ADC channel
sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
switch (obj->channel) {
@ -181,13 +176,13 @@ static inline uint16_t adc_read(analogin_t *obj)
}
ADC1->CHSELR = 0; // [TODO] Workaround. To be removed after Cube driver is corrected.
HAL_ADC_ConfigChannel(&AdcHandle, &sConfig);
HAL_ADC_ConfigChannel(&obj->handle, &sConfig);
HAL_ADC_Start(&AdcHandle); // Start conversion
HAL_ADC_Start(&obj->handle); // Start conversion
// Wait end of conversion and get value
if (HAL_ADC_PollForConversion(&AdcHandle, 10) == HAL_OK) {
return (HAL_ADC_GetValue(&AdcHandle));
if (HAL_ADC_PollForConversion(&obj->handle, 10) == HAL_OK) {
return (HAL_ADC_GetValue(&obj->handle));
} else {
return 0;
}

View File

@ -116,6 +116,12 @@ struct flash_s {
uint32_t dummy;
};
struct analogin_s {
ADC_HandleTypeDef handle;
PinName pin;
uint8_t channel;
};
#include "gpio_object.h"
#if DEVICE_ANALOGOUT

View File

@ -54,12 +54,6 @@ struct port_s {
__IO uint32_t *reg_out;
};
struct analogin_s {
ADCName adc;
PinName pin;
uint32_t channel;
};
#define GPIO_IP_WITHOUT_BRR
#include "common_objects.h"

View File

@ -54,12 +54,6 @@ struct port_s {
__IO uint32_t *reg_out;
};
struct analogin_s {
ADCName adc;
PinName pin;
uint32_t channel;
};
#include "common_objects.h"
#ifdef __cplusplus

View File

@ -54,12 +54,6 @@ struct port_s {
__IO uint32_t *reg_out;
};
struct analogin_s {
ADCName adc;
PinName pin;
uint32_t channel;
};
#include "common_objects.h"
#ifdef __cplusplus

View File

@ -54,12 +54,6 @@ struct port_s {
__IO uint32_t *reg_out;
};
struct analogin_s {
ADCName adc;
PinName pin;
uint32_t channel;
};
#define GPIO_IP_WITHOUT_BRR
#include "common_objects.h"

View File

@ -36,8 +36,6 @@
#include "mbed_error.h"
#include "PeripheralPins.h"
ADC_HandleTypeDef AdcHandle;
int adc_inited = 0;
void analogin_init(analogin_t *obj, PinName pin)
@ -45,8 +43,8 @@ void analogin_init(analogin_t *obj, PinName pin)
RCC_OscInitTypeDef RCC_OscInitStruct;
// Get the peripheral name from the pin and assign it to the object
obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC);
MBED_ASSERT(obj->adc != (ADCName)NC);
obj->handle.Instance = (ADC_TypeDef *) pinmap_peripheral(pin, PinMap_ADC);
MBED_ASSERT(obj->handle.Instance != (ADC_TypeDef *)NC);
// Get the pin function and assign the used channel to the object
uint32_t function = pinmap_function(pin, PinMap_ADC);
@ -73,29 +71,28 @@ void analogin_init(analogin_t *obj, PinName pin)
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
HAL_RCC_OscConfig(&RCC_OscInitStruct);
AdcHandle.Instance = (ADC_TypeDef *)(obj->adc);
obj->handle.State = HAL_ADC_STATE_RESET;
// Enable ADC clock
__ADC1_CLK_ENABLE();
// Configure ADC
AdcHandle.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV4;
AdcHandle.Init.Resolution = ADC_RESOLUTION12b;
AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
AdcHandle.Init.ScanConvMode = DISABLE; // Sequencer disabled (ADC conversion on only 1 channel: channel set on rank 1)
AdcHandle.Init.EOCSelection = EOC_SINGLE_CONV; // On STM32L1xx ADC, overrun detection is enabled only if EOC selection is set to each conversion (or transfer by DMA enabled, this is not the case in this example).
AdcHandle.Init.LowPowerAutoWait = ADC_AUTOWAIT_UNTIL_DATA_READ; // Enable the dynamic low power Auto Delay: new conversion start only when the previous conversion (for regular group) or previous sequence (for injected group) has been treated by user software.
AdcHandle.Init.LowPowerAutoPowerOff = ADC_AUTOPOWEROFF_IDLE_PHASE; // Enable the auto-off mode: the ADC automatically powers-off after a conversion and automatically wakes-up when a new conversion is triggered (with startup time between trigger and start of sampling).
AdcHandle.Init.ChannelsBank = ADC_CHANNELS_BANK_A;
AdcHandle.Init.ContinuousConvMode = DISABLE; // Continuous mode disabled to have only 1 conversion at each conversion trig
AdcHandle.Init.NbrOfConversion = 1; // Parameter discarded because sequencer is disabled
AdcHandle.Init.DiscontinuousConvMode = DISABLE; // Parameter discarded because sequencer is disabled
AdcHandle.Init.NbrOfDiscConversion = 1; // Parameter discarded because sequencer is disabled
AdcHandle.Init.ExternalTrigConv = 0; // Not used
AdcHandle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
AdcHandle.Init.DMAContinuousRequests = DISABLE;
obj->handle.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV4;
obj->handle.Init.Resolution = ADC_RESOLUTION12b;
obj->handle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
obj->handle.Init.ScanConvMode = DISABLE; // Sequencer disabled (ADC conversion on only 1 channel: channel set on rank 1)
obj->handle.Init.EOCSelection = EOC_SINGLE_CONV; // On STM32L1xx ADC, overrun detection is enabled only if EOC selection is set to each conversion (or transfer by DMA enabled, this is not the case in this example).
obj->handle.Init.LowPowerAutoWait = ADC_AUTOWAIT_UNTIL_DATA_READ; // Enable the dynamic low power Auto Delay: new conversion start only when the previous conversion (for regular group) or previous sequence (for injected group) has been treated by user software.
obj->handle.Init.LowPowerAutoPowerOff = ADC_AUTOPOWEROFF_IDLE_PHASE; // Enable the auto-off mode: the ADC automatically powers-off after a conversion and automatically wakes-up when a new conversion is triggered (with startup time between trigger and start of sampling).
obj->handle.Init.ChannelsBank = ADC_CHANNELS_BANK_A;
obj->handle.Init.ContinuousConvMode = DISABLE; // Continuous mode disabled to have only 1 conversion at each conversion trig
obj->handle.Init.NbrOfConversion = 1; // Parameter discarded because sequencer is disabled
obj->handle.Init.DiscontinuousConvMode = DISABLE; // Parameter discarded because sequencer is disabled
obj->handle.Init.NbrOfDiscConversion = 1; // Parameter discarded because sequencer is disabled
obj->handle.Init.ExternalTrigConv = 0; // Not used
obj->handle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
obj->handle.Init.DMAContinuousRequests = DISABLE;
if (HAL_ADC_Init(&AdcHandle) != HAL_OK) {
if (HAL_ADC_Init(&obj->handle) != HAL_OK) {
error("Cannot initialize ADC");
}
}
@ -105,8 +102,6 @@ static inline uint16_t adc_read(analogin_t *obj)
{
ADC_ChannelConfTypeDef sConfig = {0};
AdcHandle.Instance = (ADC_TypeDef *)(obj->adc);
// Configure ADC channel
switch (obj->channel) {
case 0:
@ -222,13 +217,13 @@ static inline uint16_t adc_read(analogin_t *obj)
sConfig.Rank = ADC_REGULAR_RANK_1;
sConfig.SamplingTime = ADC_SAMPLETIME_16CYCLES;
HAL_ADC_ConfigChannel(&AdcHandle, &sConfig);
HAL_ADC_ConfigChannel(&obj->handle, &sConfig);
HAL_ADC_Start(&AdcHandle); // Start conversion
HAL_ADC_Start(&obj->handle); // Start conversion
// Wait end of conversion and get value
if (HAL_ADC_PollForConversion(&AdcHandle, 10) == HAL_OK) {
return (HAL_ADC_GetValue(&AdcHandle));
if (HAL_ADC_PollForConversion(&obj->handle, 10) == HAL_OK) {
return (HAL_ADC_GetValue(&obj->handle));
} else {
return 0;
}

View File

@ -122,6 +122,12 @@ struct dac_s {
DAC_HandleTypeDef handle;
};
struct analogin_s {
ADC_HandleTypeDef handle;
PinName pin;
uint8_t channel;
};
#include "gpio_object.h"
#ifdef __cplusplus

View File

@ -54,12 +54,6 @@ struct port_s {
__IO uint32_t *reg_out;
};
struct analogin_s {
ADCName adc;
PinName pin;
uint32_t channel;
};
struct can_s {
CANName can;
int index;

View File

@ -54,12 +54,6 @@ struct port_s {
__IO uint32_t *reg_out;
};
struct analogin_s {
ADCName adc;
PinName pin;
uint32_t channel;
};
struct can_s {
CANName can;
int index;

View File

@ -54,12 +54,6 @@ struct port_s {
__IO uint32_t *reg_out;
};
struct analogin_s {
ADCName adc;
PinName pin;
uint32_t channel;
};
struct can_s {
CANName can;
int index;

View File

@ -54,12 +54,6 @@ struct port_s {
__IO uint32_t *reg_out;
};
struct analogin_s {
ADCName adc;
PinName pin;
uint32_t channel;
};
struct can_s {
CANName can;
int index;

View File

@ -36,14 +36,11 @@
#include "mbed_error.h"
#include "PeripheralPins.h"
ADC_HandleTypeDef AdcHandle;
int adc_inited = 0;
void analogin_init(analogin_t *obj, PinName pin)
{
uint32_t function = (uint32_t)NC;
obj->adc = (ADCName)NC;
// ADC Internal Channels "pins" (Temperature, Vref, Vbat, ...)
// are described in PinNames.h and PeripheralPins.c
@ -51,18 +48,18 @@ 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->adc = (ADCName)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->adc = (ADCName)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
}
MBED_ASSERT(obj->adc != (ADCName)NC);
MBED_ASSERT(obj->handle.Instance != (ADC_TypeDef *)NC);
MBED_ASSERT(function != (uint32_t)NC);
obj->channel = STM_PIN_CHANNEL(function);
@ -78,26 +75,25 @@ void analogin_init(analogin_t *obj, PinName pin)
__HAL_RCC_ADC_CLK_ENABLE();
__HAL_RCC_ADC_CONFIG(RCC_ADCCLKSOURCE_SYSCLK);
AdcHandle.Instance = (ADC_TypeDef *)(obj->adc);
obj->handle.State = HAL_ADC_STATE_RESET;
// Configure ADC
AdcHandle.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV2; // Asynchronous clock mode, input ADC clock
AdcHandle.Init.Resolution = ADC_RESOLUTION_12B;
AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
AdcHandle.Init.ScanConvMode = DISABLE; // Sequencer disabled (ADC conversion on only 1 channel: channel set on rank 1)
AdcHandle.Init.EOCSelection = ADC_EOC_SINGLE_CONV; // On STM32L1xx ADC, overrun detection is enabled only if EOC selection is set to each conversion (or transfer by DMA enabled, this is not the case in this example).
AdcHandle.Init.LowPowerAutoWait = DISABLE;
AdcHandle.Init.ContinuousConvMode = DISABLE; // Continuous mode disabled to have only 1 conversion at each conversion trig
AdcHandle.Init.NbrOfConversion = 1; // Parameter discarded because sequencer is disabled
AdcHandle.Init.DiscontinuousConvMode = DISABLE; // Parameter discarded because sequencer is disabled
AdcHandle.Init.NbrOfDiscConversion = 1; // Parameter discarded because sequencer is disabled
AdcHandle.Init.ExternalTrigConv = ADC_SOFTWARE_START; // Software start to trig the 1st conversion manually, without external event
AdcHandle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
AdcHandle.Init.DMAContinuousRequests = DISABLE;
AdcHandle.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN; // DR register is overwritten with the last conversion result in case of overrun
AdcHandle.Init.OversamplingMode = DISABLE; // No oversampling
obj->handle.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV2; // Asynchronous clock mode, input ADC clock
obj->handle.Init.Resolution = ADC_RESOLUTION_12B;
obj->handle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
obj->handle.Init.ScanConvMode = DISABLE; // Sequencer disabled (ADC conversion on only 1 channel: channel set on rank 1)
obj->handle.Init.EOCSelection = ADC_EOC_SINGLE_CONV; // On STM32L1xx ADC, overrun detection is enabled only if EOC selection is set to each conversion (or transfer by DMA enabled, this is not the case in this example).
obj->handle.Init.LowPowerAutoWait = DISABLE;
obj->handle.Init.ContinuousConvMode = DISABLE; // Continuous mode disabled to have only 1 conversion at each conversion trig
obj->handle.Init.NbrOfConversion = 1; // Parameter discarded because sequencer is disabled
obj->handle.Init.DiscontinuousConvMode = DISABLE; // Parameter discarded because sequencer is disabled
obj->handle.Init.NbrOfDiscConversion = 1; // Parameter discarded because sequencer is disabled
obj->handle.Init.ExternalTrigConv = ADC_SOFTWARE_START; // Software start to trig the 1st conversion manually, without external event
obj->handle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
obj->handle.Init.DMAContinuousRequests = DISABLE;
obj->handle.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN; // DR register is overwritten with the last conversion result in case of overrun
obj->handle.Init.OversamplingMode = DISABLE; // No oversampling
if (HAL_ADC_Init(&AdcHandle) != HAL_OK) {
if (HAL_ADC_Init(&obj->handle) != HAL_OK) {
error("Cannot initialize ADC\n");
}
}
@ -107,8 +103,6 @@ static inline uint16_t adc_read(analogin_t *obj)
{
ADC_ChannelConfTypeDef sConfig = {0};
AdcHandle.Instance = (ADC_TypeDef *)(obj->adc);
// Configure ADC channel
switch (obj->channel) {
case 0:
@ -178,13 +172,13 @@ static inline uint16_t adc_read(analogin_t *obj)
sConfig.OffsetNumber = ADC_OFFSET_NONE;
sConfig.Offset = 0;
HAL_ADC_ConfigChannel(&AdcHandle, &sConfig);
HAL_ADC_ConfigChannel(&obj->handle, &sConfig);
HAL_ADC_Start(&AdcHandle); // Start conversion
HAL_ADC_Start(&obj->handle); // Start conversion
// Wait end of conversion and get value
if (HAL_ADC_PollForConversion(&AdcHandle, 10) == HAL_OK) {
return (HAL_ADC_GetValue(&AdcHandle));
if (HAL_ADC_PollForConversion(&obj->handle, 10) == HAL_OK) {
return (HAL_ADC_GetValue(&obj->handle));
} else {
return 0;
}

View File

@ -116,6 +116,12 @@ struct flash_s {
uint32_t dummy;
};
struct analogin_s {
ADC_HandleTypeDef handle;
PinName pin;
uint8_t channel;
};
#include "gpio_object.h"
struct dac_s {