analogRead: Use all ADC instances when searching for the pin / channel

This commit is contained in:
Daniel Fekete 2017-07-15 18:21:53 +02:00
parent a4869dcc00
commit 304267d95e
3 changed files with 46 additions and 22 deletions

View File

@ -52,12 +52,34 @@ void stm32_adc_init(ADC_HandleTypeDef *handle);
static int readResolution = 10;
static ADC_HandleTypeDef handle[3];
void analogReadResolution(int resolution) {
readResolution = resolution;
}
int analogRead(uint8_t pin) {
static ADC_HandleTypeDef handle = {};
stm32_chip_adc1_channel_type config = stm32ADC1GetChannel(variant_pin_list[pin].port, variant_pin_list[pin].pin_mask);
if (config.instance == NULL) {
return 0;
}
int instanceIndex = 0;
#ifdef ADC2
if (config.instance == ADC2) {
instanceIndex = 1;
__HAL_RCC_ADC2_CLK_ENABLE();
}
#endif
#ifdef ADC3
if (config.instance == ADC3) {
instanceIndex = 2;
__HAL_RCC_ADC3_CLK_ENABLE();
}
#endif
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.Pin = variant_pin_list[pin].pin_mask;
@ -65,7 +87,7 @@ int analogRead(uint8_t pin) {
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(variant_pin_list[pin].port, &GPIO_InitStruct);
if (handle.Instance == NULL) {
if (handle[instanceIndex].Instance == NULL) {
#ifdef __HAL_RCC_ADC1_CLK_ENABLE
__HAL_RCC_ADC1_CLK_ENABLE();
#endif
@ -73,29 +95,29 @@ int analogRead(uint8_t pin) {
__HAL_RCC_ADC_CLK_ENABLE();
#endif
handle.Instance = ADC1;
handle.Init.ScanConvMode = DISABLE;
handle.Init.ContinuousConvMode = ENABLE;
handle.Init.DiscontinuousConvMode = DISABLE;
handle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
handle[instanceIndex].Instance = config.instance;
handle[instanceIndex].Init.ScanConvMode = DISABLE;
handle[instanceIndex].Init.ContinuousConvMode = ENABLE;
handle[instanceIndex].Init.DiscontinuousConvMode = DISABLE;
handle[instanceIndex].Init.DataAlign = ADC_DATAALIGN_RIGHT;
#ifdef STM32L0
handle.Init.SamplingTime = ADC_SAMPLETIME_13CYCLES_5;
handle[instanceIndex].Init.SamplingTime = ADC_SAMPLETIME_13CYCLES_5;
#endif
#if !defined(STM32L0) && !defined(STM32F0)
handle.Init.NbrOfConversion = 1;
handle[instanceIndex].Init.NbrOfConversion = 1;
#endif
#ifdef ADC_EOC_SINGLE_CONV
handle.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
handle[instanceIndex].Init.EOCSelection = ADC_EOC_SINGLE_CONV;
#endif
#ifdef STM32F1
handle.Init.ExternalTrigConv = ADC_SOFTWARE_START;
handle[instanceIndex].Init.ExternalTrigConv = ADC_SOFTWARE_START;
#else
handle.Init.ClockPrescaler = ADC_CLOCK_DIV;
handle.Init.Resolution = ADC_RESOLUTION_12B;
handle[instanceIndex].Init.ClockPrescaler = ADC_CLOCK_DIV;
handle[instanceIndex].Init.Resolution = ADC_RESOLUTION_12B;
#endif
HAL_ADC_Init(&handle);
@ -103,7 +125,7 @@ int analogRead(uint8_t pin) {
}
ADC_ChannelConfTypeDef sConfig;
sConfig.Channel = stm32ADC1GetChannel(variant_pin_list[pin].port, variant_pin_list[pin].pin_mask);
sConfig.Channel = config.channel;
sConfig.Rank = 1;
#if STM32L0
@ -122,13 +144,13 @@ int analogRead(uint8_t pin) {
#error "unknown sampleing time"
#endif
HAL_ADC_ConfigChannel(&handle, &sConfig);
HAL_ADC_ConfigChannel(&handle[instanceIndex], &sConfig);
HAL_ADC_Start(&handle);
HAL_ADC_Start(&handle[instanceIndex]);
if (HAL_ADC_PollForConversion(&handle, 1000) != HAL_OK) {
if (HAL_ADC_PollForConversion(&handle[instanceIndex], 1000) != HAL_OK) {
return 0;
}
return (HAL_ADC_GetValue(&handle) << readResolution) >> 12;
return (HAL_ADC_GetValue(&handle[instanceIndex]) << readResolution) >> 12;
}

View File

@ -54,14 +54,15 @@ uint32_t stm32GetClockFrequency(void *instance) {
return 0;
}
uint8_t stm32ADC1GetChannel(GPIO_TypeDef *port, uint32_t pin_mask) {
stm32_chip_adc1_channel_type stm32ADC1GetChannel(GPIO_TypeDef *port, uint32_t pin_mask) {
for(size_t i=0; i<sizeof(chip_adc1_channel) / sizeof(chip_adc1_channel[0]); i++) {
if (chip_adc1_channel[i].port == port && chip_adc1_channel[i].pin_mask == pin_mask) {
return chip_adc1_channel[i].channel;
return chip_adc1_channel[i];
}
}
return -1;
stm32_chip_adc1_channel_type notFound = {0};
return notFound;
}
void stm32AfUARTInit(const USART_TypeDef *instance,

View File

@ -54,6 +54,7 @@ typedef struct {
typedef struct {
ADC_TypeDef *instance;
GPIO_TypeDef *port;
uint32_t pin_mask;
uint32_t channel;
@ -130,7 +131,7 @@ uint32_t stm32GetClockFrequency(void *instance);
/**
* Get the ADC1 channel for the specified port / pin
*/
uint8_t stm32ADC1GetChannel(GPIO_TypeDef *port, uint32_t pin_mask);
stm32_chip_adc1_channel_type stm32ADC1GetChannel(GPIO_TypeDef *port, uint32_t pin_mask);
/**
* Internal: set the AF function for the selected peripheral on the selected pin, with GPIO_SPEED_FREQ_VERY_HIGH speed