2018-12-10 09:48:01 +08:00
|
|
|
/*
|
2021-03-08 22:40:39 +08:00
|
|
|
* Copyright (c) 2006-2021, RT-Thread Development Team
|
2018-12-10 09:48:01 +08:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
* 2018-12-05 zylx first version
|
2018-12-12 16:49:27 +08:00
|
|
|
* 2018-12-12 greedyhao Porting for stm32f7xx
|
2020-06-23 10:43:18 +08:00
|
|
|
* 2019-02-01 yuneizhilin fix the stm32_adc_init function initialization issue
|
|
|
|
* 2020-06-17 thread-liu Porting for stm32mp1xx
|
2020-10-14 15:02:23 +08:00
|
|
|
* 2020-10-14 Dozingfiretruck Porting for stm32wbxx
|
2018-12-10 09:48:01 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <board.h>
|
|
|
|
|
2018-12-12 16:51:39 +08:00
|
|
|
#if defined(BSP_USING_ADC1) || defined(BSP_USING_ADC2) || defined(BSP_USING_ADC3)
|
2018-12-10 09:48:01 +08:00
|
|
|
#include "drv_config.h"
|
|
|
|
|
|
|
|
//#define DRV_DEBUG
|
|
|
|
#define LOG_TAG "drv.adc"
|
|
|
|
#include <drv_log.h>
|
|
|
|
|
|
|
|
static ADC_HandleTypeDef adc_config[] =
|
|
|
|
{
|
|
|
|
#ifdef BSP_USING_ADC1
|
|
|
|
ADC1_CONFIG,
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef BSP_USING_ADC2
|
|
|
|
ADC2_CONFIG,
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef BSP_USING_ADC3
|
|
|
|
ADC3_CONFIG,
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
struct stm32_adc
|
|
|
|
{
|
|
|
|
ADC_HandleTypeDef ADC_Handler;
|
|
|
|
struct rt_adc_device stm32_adc_device;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct stm32_adc stm32_adc_obj[sizeof(adc_config) / sizeof(adc_config[0])];
|
|
|
|
|
|
|
|
static rt_err_t stm32_adc_enabled(struct rt_adc_device *device, rt_uint32_t channel, rt_bool_t enabled)
|
|
|
|
{
|
2019-08-07 14:22:54 +08:00
|
|
|
ADC_HandleTypeDef *stm32_adc_handler;
|
2018-12-10 09:48:01 +08:00
|
|
|
RT_ASSERT(device != RT_NULL);
|
2019-08-07 14:22:54 +08:00
|
|
|
stm32_adc_handler = device->parent.user_data;
|
2018-12-10 09:48:01 +08:00
|
|
|
|
|
|
|
if (enabled)
|
|
|
|
{
|
2020-10-14 15:02:23 +08:00
|
|
|
#if defined(SOC_SERIES_STM32L4) || defined(SOC_SERIES_STM32G0) || defined (SOC_SERIES_STM32MP1) || defined(SOC_SERIES_STM32H7) || defined (SOC_SERIES_STM32WB)
|
2018-12-10 09:48:01 +08:00
|
|
|
ADC_Enable(stm32_adc_handler);
|
|
|
|
#else
|
|
|
|
__HAL_ADC_ENABLE(stm32_adc_handler);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-10-14 15:02:23 +08:00
|
|
|
#if defined(SOC_SERIES_STM32L4) || defined(SOC_SERIES_STM32G0) || defined (SOC_SERIES_STM32MP1) || defined(SOC_SERIES_STM32H7) || defined (SOC_SERIES_STM32WB)
|
2018-12-10 09:48:01 +08:00
|
|
|
ADC_Disable(stm32_adc_handler);
|
|
|
|
#else
|
|
|
|
__HAL_ADC_DISABLE(stm32_adc_handler);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
2022-04-22 22:26:41 +08:00
|
|
|
static rt_uint8_t stm32_adc_get_resolution(struct rt_adc_device *device)
|
|
|
|
{
|
|
|
|
ADC_HandleTypeDef *stm32_adc_handler;
|
|
|
|
|
|
|
|
RT_ASSERT(device != RT_NULL);
|
|
|
|
|
|
|
|
stm32_adc_handler = device->parent.user_data;
|
|
|
|
|
|
|
|
switch(stm32_adc_handler->Init.Resolution)
|
|
|
|
{
|
|
|
|
case ADC_RESOLUTION_12B:
|
|
|
|
return 12;
|
|
|
|
case ADC_RESOLUTION_10B:
|
|
|
|
return 10;
|
|
|
|
case ADC_RESOLUTION_8B:
|
|
|
|
return 8;
|
|
|
|
case ADC_RESOLUTION_6B:
|
|
|
|
return 6;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-10 09:48:01 +08:00
|
|
|
static rt_uint32_t stm32_adc_get_channel(rt_uint32_t channel)
|
|
|
|
{
|
|
|
|
rt_uint32_t stm32_channel = 0;
|
|
|
|
|
|
|
|
switch (channel)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
stm32_channel = ADC_CHANNEL_0;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
stm32_channel = ADC_CHANNEL_1;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
stm32_channel = ADC_CHANNEL_2;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
stm32_channel = ADC_CHANNEL_3;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
stm32_channel = ADC_CHANNEL_4;
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
stm32_channel = ADC_CHANNEL_5;
|
|
|
|
break;
|
|
|
|
case 6:
|
|
|
|
stm32_channel = ADC_CHANNEL_6;
|
|
|
|
break;
|
|
|
|
case 7:
|
|
|
|
stm32_channel = ADC_CHANNEL_7;
|
|
|
|
break;
|
|
|
|
case 8:
|
|
|
|
stm32_channel = ADC_CHANNEL_8;
|
|
|
|
break;
|
|
|
|
case 9:
|
|
|
|
stm32_channel = ADC_CHANNEL_9;
|
|
|
|
break;
|
|
|
|
case 10:
|
|
|
|
stm32_channel = ADC_CHANNEL_10;
|
|
|
|
break;
|
|
|
|
case 11:
|
|
|
|
stm32_channel = ADC_CHANNEL_11;
|
|
|
|
break;
|
|
|
|
case 12:
|
|
|
|
stm32_channel = ADC_CHANNEL_12;
|
|
|
|
break;
|
|
|
|
case 13:
|
|
|
|
stm32_channel = ADC_CHANNEL_13;
|
|
|
|
break;
|
|
|
|
case 14:
|
|
|
|
stm32_channel = ADC_CHANNEL_14;
|
|
|
|
break;
|
|
|
|
case 15:
|
|
|
|
stm32_channel = ADC_CHANNEL_15;
|
|
|
|
break;
|
2020-03-13 11:24:22 +08:00
|
|
|
#ifdef ADC_CHANNEL_16
|
2018-12-10 09:48:01 +08:00
|
|
|
case 16:
|
|
|
|
stm32_channel = ADC_CHANNEL_16;
|
|
|
|
break;
|
2020-03-13 11:24:22 +08:00
|
|
|
#endif
|
2018-12-10 09:48:01 +08:00
|
|
|
case 17:
|
|
|
|
stm32_channel = ADC_CHANNEL_17;
|
|
|
|
break;
|
2020-03-13 11:24:22 +08:00
|
|
|
#ifdef ADC_CHANNEL_18
|
2018-12-10 09:48:01 +08:00
|
|
|
case 18:
|
|
|
|
stm32_channel = ADC_CHANNEL_18;
|
|
|
|
break;
|
2020-03-13 11:24:22 +08:00
|
|
|
#endif
|
|
|
|
#ifdef ADC_CHANNEL_19
|
|
|
|
case 19:
|
|
|
|
stm32_channel = ADC_CHANNEL_19;
|
|
|
|
break;
|
2018-12-10 09:48:01 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
return stm32_channel;
|
|
|
|
}
|
|
|
|
|
2022-04-22 22:26:41 +08:00
|
|
|
static rt_err_t stm32_adc_get_value(struct rt_adc_device *device, rt_uint32_t channel, rt_uint32_t *value)
|
2018-12-10 09:48:01 +08:00
|
|
|
{
|
|
|
|
ADC_ChannelConfTypeDef ADC_ChanConf;
|
2019-08-07 14:22:54 +08:00
|
|
|
ADC_HandleTypeDef *stm32_adc_handler;
|
2018-12-10 09:48:01 +08:00
|
|
|
|
|
|
|
RT_ASSERT(device != RT_NULL);
|
|
|
|
RT_ASSERT(value != RT_NULL);
|
|
|
|
|
2019-08-07 14:22:54 +08:00
|
|
|
stm32_adc_handler = device->parent.user_data;
|
2019-08-06 17:58:36 +08:00
|
|
|
|
2018-12-10 09:48:01 +08:00
|
|
|
rt_memset(&ADC_ChanConf, 0, sizeof(ADC_ChanConf));
|
|
|
|
|
2020-03-13 11:24:22 +08:00
|
|
|
#ifndef ADC_CHANNEL_16
|
|
|
|
if (channel == 16)
|
|
|
|
{
|
|
|
|
LOG_E("ADC channel must not be 16.");
|
|
|
|
return -RT_ERROR;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* ADC channel number is up to 17 */
|
|
|
|
#if !defined(ADC_CHANNEL_18)
|
2018-12-10 09:48:01 +08:00
|
|
|
if (channel <= 17)
|
2020-03-13 11:24:22 +08:00
|
|
|
/* ADC channel number is up to 19 */
|
|
|
|
#elif defined(ADC_CHANNEL_19)
|
|
|
|
if (channel <= 19)
|
|
|
|
/* ADC channel number is up to 18 */
|
|
|
|
#else
|
2018-12-10 09:48:01 +08:00
|
|
|
if (channel <= 18)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
/* set stm32 ADC channel */
|
|
|
|
ADC_ChanConf.Channel = stm32_adc_get_channel(channel);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-03-13 11:24:22 +08:00
|
|
|
#if !defined(ADC_CHANNEL_18)
|
2018-12-10 09:48:01 +08:00
|
|
|
LOG_E("ADC channel must be between 0 and 17.");
|
2020-03-13 11:24:22 +08:00
|
|
|
#elif defined(ADC_CHANNEL_19)
|
|
|
|
LOG_E("ADC channel must be between 0 and 19.");
|
|
|
|
#else
|
2018-12-10 09:48:01 +08:00
|
|
|
LOG_E("ADC channel must be between 0 and 18.");
|
|
|
|
#endif
|
|
|
|
return -RT_ERROR;
|
|
|
|
}
|
2021-03-08 22:40:39 +08:00
|
|
|
|
2020-10-23 18:15:23 +08:00
|
|
|
#if defined(SOC_SERIES_STM32MP1) || defined (SOC_SERIES_STM32H7) || defined (SOC_SERIES_STM32WB)
|
2020-06-23 10:43:18 +08:00
|
|
|
ADC_ChanConf.Rank = ADC_REGULAR_RANK_1;
|
|
|
|
#else
|
2018-12-10 09:48:01 +08:00
|
|
|
ADC_ChanConf.Rank = 1;
|
2020-06-23 10:43:18 +08:00
|
|
|
#endif
|
2021-03-08 22:40:39 +08:00
|
|
|
|
2018-12-26 10:43:16 +08:00
|
|
|
#if defined(SOC_SERIES_STM32F0)
|
|
|
|
ADC_ChanConf.SamplingTime = ADC_SAMPLETIME_71CYCLES_5;
|
|
|
|
#elif defined(SOC_SERIES_STM32F1)
|
2018-12-10 09:48:01 +08:00
|
|
|
ADC_ChanConf.SamplingTime = ADC_SAMPLETIME_55CYCLES_5;
|
2019-06-18 15:54:36 +08:00
|
|
|
#elif defined(SOC_SERIES_STM32F2) || defined(SOC_SERIES_STM32F4) || defined(SOC_SERIES_STM32F7)
|
2018-12-10 09:48:01 +08:00
|
|
|
ADC_ChanConf.SamplingTime = ADC_SAMPLETIME_112CYCLES;
|
2020-10-23 18:15:23 +08:00
|
|
|
#elif defined(SOC_SERIES_STM32L4)
|
2018-12-10 09:48:01 +08:00
|
|
|
ADC_ChanConf.SamplingTime = ADC_SAMPLETIME_247CYCLES_5;
|
2020-06-23 10:43:18 +08:00
|
|
|
#elif defined(SOC_SERIES_STM32MP1)
|
|
|
|
ADC_ChanConf.SamplingTime = ADC_SAMPLETIME_810CYCLES_5;
|
2020-10-09 10:06:47 +08:00
|
|
|
#elif defined(SOC_SERIES_STM32H7)
|
|
|
|
ADC_ChanConf.SamplingTime = ADC_SAMPLETIME_64CYCLES_5;
|
2020-10-23 18:15:23 +08:00
|
|
|
#elif defined (SOC_SERIES_STM32WB)
|
|
|
|
ADC_ChanConf.SamplingTime = ADC_SAMPLETIME_2CYCLES_5;
|
2018-12-10 09:48:01 +08:00
|
|
|
#endif
|
2020-08-12 02:09:52 +08:00
|
|
|
|
2020-10-14 15:02:23 +08:00
|
|
|
#if defined(SOC_SERIES_STM32F2) || defined(SOC_SERIES_STM32F4) || defined(SOC_SERIES_STM32F7) || defined(SOC_SERIES_STM32L4) || defined (SOC_SERIES_STM32WB)
|
2018-12-10 09:48:01 +08:00
|
|
|
ADC_ChanConf.Offset = 0;
|
|
|
|
#endif
|
2020-08-12 02:09:52 +08:00
|
|
|
|
2020-10-23 18:15:23 +08:00
|
|
|
#if defined(SOC_SERIES_STM32L4)
|
2018-12-10 09:48:01 +08:00
|
|
|
ADC_ChanConf.OffsetNumber = ADC_OFFSET_NONE;
|
|
|
|
ADC_ChanConf.SingleDiff = LL_ADC_SINGLE_ENDED;
|
2020-10-23 18:15:23 +08:00
|
|
|
#elif defined(SOC_SERIES_STM32MP1) || defined(SOC_SERIES_STM32H7) || defined (SOC_SERIES_STM32WB)
|
2020-06-23 10:43:18 +08:00
|
|
|
ADC_ChanConf.OffsetNumber = ADC_OFFSET_NONE; /* ADC channel affected to offset number */
|
2021-03-08 22:40:39 +08:00
|
|
|
ADC_ChanConf.Offset = 0;
|
2020-06-23 10:43:18 +08:00
|
|
|
ADC_ChanConf.SingleDiff = ADC_SINGLE_ENDED; /* ADC channel differential mode */
|
2018-12-10 09:48:01 +08:00
|
|
|
#endif
|
|
|
|
HAL_ADC_ConfigChannel(stm32_adc_handler, &ADC_ChanConf);
|
2020-08-12 02:09:52 +08:00
|
|
|
|
|
|
|
/* perform an automatic ADC calibration to improve the conversion accuracy */
|
2020-10-14 15:02:23 +08:00
|
|
|
#if defined(SOC_SERIES_STM32L4) || defined (SOC_SERIES_STM32WB)
|
2020-08-12 02:09:52 +08:00
|
|
|
if (HAL_ADCEx_Calibration_Start(stm32_adc_handler, ADC_ChanConf.SingleDiff) != HAL_OK)
|
|
|
|
{
|
|
|
|
LOG_E("ADC calibration error!\n");
|
|
|
|
return -RT_ERROR;
|
|
|
|
}
|
2020-10-09 10:06:47 +08:00
|
|
|
#elif defined(SOC_SERIES_STM32MP1) || defined(SOC_SERIES_STM32H7)
|
2020-08-12 02:09:52 +08:00
|
|
|
/* Run the ADC linear calibration in single-ended mode */
|
|
|
|
if (HAL_ADCEx_Calibration_Start(stm32_adc_handler, ADC_CALIB_OFFSET_LINEARITY, ADC_ChanConf.SingleDiff) != HAL_OK)
|
2020-06-23 10:43:18 +08:00
|
|
|
{
|
|
|
|
LOG_E("ADC open linear calibration error!\n");
|
|
|
|
/* Calibration Error */
|
|
|
|
return -RT_ERROR;
|
|
|
|
}
|
|
|
|
#endif
|
2018-12-10 09:48:01 +08:00
|
|
|
/* start ADC */
|
|
|
|
HAL_ADC_Start(stm32_adc_handler);
|
|
|
|
|
|
|
|
/* Wait for the ADC to convert */
|
2018-12-26 10:43:16 +08:00
|
|
|
HAL_ADC_PollForConversion(stm32_adc_handler, 100);
|
2018-12-10 09:48:01 +08:00
|
|
|
|
|
|
|
/* get ADC value */
|
|
|
|
*value = (rt_uint32_t)HAL_ADC_GetValue(stm32_adc_handler);
|
|
|
|
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct rt_adc_ops stm_adc_ops =
|
|
|
|
{
|
|
|
|
.enabled = stm32_adc_enabled,
|
2022-04-22 22:26:41 +08:00
|
|
|
.convert = stm32_adc_get_value,
|
|
|
|
.get_resolution = stm32_adc_get_resolution,
|
2018-12-10 09:48:01 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static int stm32_adc_init(void)
|
|
|
|
{
|
|
|
|
int result = RT_EOK;
|
|
|
|
/* save adc name */
|
2019-02-01 12:56:36 +08:00
|
|
|
char name_buf[5] = {'a', 'd', 'c', '0', 0};
|
2018-12-10 09:48:01 +08:00
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < sizeof(adc_config) / sizeof(adc_config[0]); i++)
|
|
|
|
{
|
|
|
|
/* ADC init */
|
2019-02-01 12:56:36 +08:00
|
|
|
name_buf[3] = '0';
|
2018-12-10 09:48:01 +08:00
|
|
|
stm32_adc_obj[i].ADC_Handler = adc_config[i];
|
2019-02-15 10:43:09 +08:00
|
|
|
#if defined(ADC1)
|
2019-02-01 12:56:36 +08:00
|
|
|
if (stm32_adc_obj[i].ADC_Handler.Instance == ADC1)
|
|
|
|
{
|
|
|
|
name_buf[3] = '1';
|
|
|
|
}
|
2019-02-15 10:43:09 +08:00
|
|
|
#endif
|
|
|
|
#if defined(ADC2)
|
2019-02-01 12:56:36 +08:00
|
|
|
if (stm32_adc_obj[i].ADC_Handler.Instance == ADC2)
|
|
|
|
{
|
|
|
|
name_buf[3] = '2';
|
|
|
|
}
|
2019-02-15 10:43:09 +08:00
|
|
|
#endif
|
|
|
|
#if defined(ADC3)
|
2019-02-01 12:56:36 +08:00
|
|
|
if (stm32_adc_obj[i].ADC_Handler.Instance == ADC3)
|
|
|
|
{
|
|
|
|
name_buf[3] = '3';
|
|
|
|
}
|
2019-02-15 10:43:09 +08:00
|
|
|
#endif
|
2018-12-10 09:48:01 +08:00
|
|
|
if (HAL_ADC_Init(&stm32_adc_obj[i].ADC_Handler) != HAL_OK)
|
|
|
|
{
|
2019-02-01 12:56:36 +08:00
|
|
|
LOG_E("%s init failed", name_buf);
|
2018-12-10 09:48:01 +08:00
|
|
|
result = -RT_ERROR;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* register ADC device */
|
|
|
|
if (rt_hw_adc_register(&stm32_adc_obj[i].stm32_adc_device, name_buf, &stm_adc_ops, &stm32_adc_obj[i].ADC_Handler) == RT_EOK)
|
|
|
|
{
|
2019-02-01 12:56:36 +08:00
|
|
|
LOG_D("%s init success", name_buf);
|
2018-12-10 09:48:01 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-02-01 12:56:36 +08:00
|
|
|
LOG_E("%s register failed", name_buf);
|
2018-12-10 09:48:01 +08:00
|
|
|
result = -RT_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
INIT_BOARD_EXPORT(stm32_adc_init);
|
|
|
|
|
|
|
|
#endif /* BSP_USING_ADC */
|