2022-05-06 09:28:21 +08:00
|
|
|
/*
|
2022-05-31 11:53:56 +08:00
|
|
|
* Copyright (c) 2006-2022, RT-Thread Development Team
|
|
|
|
* Copyright (c) 2022, Xiaohua Semiconductor Co., Ltd.
|
2022-05-06 09:28:21 +08:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Change Logs:
|
2022-06-13 21:13:51 +08:00
|
|
|
* Date Author Notes
|
|
|
|
* 2022-04-28 CDT first version
|
|
|
|
* 2022-06-08 xiaoxiaolisunny add hc32f460 series
|
2022-05-06 09:28:21 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <board.h>
|
|
|
|
#include <drivers/adc.h>
|
|
|
|
#include <drv_adc.h>
|
|
|
|
#include <drv_config.h>
|
|
|
|
|
|
|
|
#define DBG_TAG "drv.adc"
|
|
|
|
#define DBG_LVL DBG_INFO
|
|
|
|
|
|
|
|
#include <rtdbg.h>
|
|
|
|
#ifdef RT_USING_ADC
|
|
|
|
typedef struct
|
|
|
|
{
|
2022-05-15 20:57:35 +08:00
|
|
|
struct rt_adc_device rt_adc;
|
|
|
|
CM_ADC_TypeDef *instance;
|
|
|
|
struct adc_dev_init_params init;
|
|
|
|
} adc_device;
|
2022-05-06 09:28:21 +08:00
|
|
|
|
|
|
|
#if !defined(BSP_USING_ADC1) && !defined(BSP_USING_ADC2) && !defined(BSP_USING_ADC3)
|
|
|
|
#error "Please define at least one BSP_USING_ADCx"
|
|
|
|
#endif
|
|
|
|
|
2022-05-15 20:57:35 +08:00
|
|
|
static adc_device g_adc_dev_array[] =
|
2022-05-06 09:28:21 +08:00
|
|
|
{
|
|
|
|
#ifdef BSP_USING_ADC1
|
|
|
|
{
|
|
|
|
{0},
|
|
|
|
CM_ADC1,
|
2022-05-15 20:57:35 +08:00
|
|
|
ADC1_INIT_PARAMS,
|
2022-05-06 09:28:21 +08:00
|
|
|
},
|
|
|
|
#endif
|
|
|
|
#ifdef BSP_USING_ADC2
|
|
|
|
{
|
|
|
|
{0},
|
|
|
|
CM_ADC2,
|
2022-05-15 20:57:35 +08:00
|
|
|
ADC2_INIT_PARAMS,
|
2022-05-06 09:28:21 +08:00
|
|
|
},
|
|
|
|
#endif
|
|
|
|
#ifdef BSP_USING_ADC3
|
|
|
|
{
|
|
|
|
{0},
|
|
|
|
CM_ADC3,
|
2022-05-15 20:57:35 +08:00
|
|
|
ADC3_INIT_PARAMS,
|
2022-05-06 09:28:21 +08:00
|
|
|
},
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2022-05-15 20:57:35 +08:00
|
|
|
static void _adc_internal_trigger0_set(adc_device *p_adc_dev)
|
2022-05-06 09:28:21 +08:00
|
|
|
{
|
|
|
|
uint32_t u32TriggerSel;
|
2022-05-15 20:57:35 +08:00
|
|
|
rt_bool_t is_internal_trig0_enabled = (p_adc_dev->init.hard_trig_src == ADC_HARDTRIG_EVT0 || p_adc_dev->init.hard_trig_src == ADC_HARDTRIG_EVT0_EVT1);
|
2022-05-06 09:28:21 +08:00
|
|
|
|
|
|
|
if (is_internal_trig0_enabled == RT_FALSE)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(HC32F4A0)
|
2022-05-15 20:57:35 +08:00
|
|
|
switch ((rt_uint32_t)p_adc_dev->instance)
|
2022-05-06 09:28:21 +08:00
|
|
|
{
|
|
|
|
case (rt_uint32_t)CM_ADC1:
|
|
|
|
u32TriggerSel = AOS_ADC1_0;
|
|
|
|
break;
|
|
|
|
case (rt_uint32_t)CM_ADC2:
|
|
|
|
u32TriggerSel = AOS_ADC2_0;
|
|
|
|
break;
|
|
|
|
case (rt_uint32_t)CM_ADC3:
|
|
|
|
u32TriggerSel = AOS_ADC3_0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2022-05-15 20:57:35 +08:00
|
|
|
AOS_CommonTriggerCmd(u32TriggerSel, AOS_COMM_TRIG1, (en_functional_state_t)p_adc_dev->init.internal_trig0_comtrg0_enable);
|
|
|
|
AOS_CommonTriggerCmd(u32TriggerSel, AOS_COMM_TRIG2, (en_functional_state_t)p_adc_dev->init.internal_trig0_comtrg1_enable);
|
2022-05-06 09:28:21 +08:00
|
|
|
#endif
|
2022-06-13 21:13:51 +08:00
|
|
|
|
|
|
|
#if defined(HC32F460)
|
|
|
|
switch ((rt_uint32_t)p_adc_dev->instance)
|
|
|
|
{
|
|
|
|
case (rt_uint32_t)CM_ADC1:
|
|
|
|
u32TriggerSel = AOS_ADC1_0;
|
|
|
|
break;
|
|
|
|
case (rt_uint32_t)CM_ADC2:
|
|
|
|
u32TriggerSel = AOS_ADC2_0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
AOS_CommonTriggerCmd(u32TriggerSel, AOS_COMM_TRIG1, (en_functional_state_t)p_adc_dev->init.internal_trig0_comtrg0_enable);
|
|
|
|
AOS_CommonTriggerCmd(u32TriggerSel, AOS_COMM_TRIG2, (en_functional_state_t)p_adc_dev->init.internal_trig0_comtrg1_enable);
|
|
|
|
#endif
|
2022-05-15 20:57:35 +08:00
|
|
|
AOS_SetTriggerEventSrc(u32TriggerSel, p_adc_dev->init.internal_trig0_sel);
|
2022-05-06 09:28:21 +08:00
|
|
|
}
|
|
|
|
|
2022-05-15 20:57:35 +08:00
|
|
|
static void _adc_internal_trigger1_set(adc_device *p_adc_dev)
|
2022-05-06 09:28:21 +08:00
|
|
|
{
|
|
|
|
uint32_t u32TriggerSel;
|
2022-05-15 20:57:35 +08:00
|
|
|
rt_bool_t is_internal_trig1_enabled = (p_adc_dev->init.hard_trig_src == ADC_HARDTRIG_EVT1 || p_adc_dev->init.hard_trig_src == ADC_HARDTRIG_EVT0_EVT1);
|
2022-05-06 09:28:21 +08:00
|
|
|
|
|
|
|
if (is_internal_trig1_enabled == RT_FALSE)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(HC32F4A0)
|
2022-05-15 20:57:35 +08:00
|
|
|
switch ((rt_uint32_t)p_adc_dev->instance)
|
2022-05-06 09:28:21 +08:00
|
|
|
{
|
|
|
|
case (rt_uint32_t)CM_ADC1:
|
|
|
|
u32TriggerSel = AOS_ADC1_1;
|
|
|
|
break;
|
|
|
|
case (rt_uint32_t)CM_ADC2:
|
|
|
|
u32TriggerSel = AOS_ADC2_1;
|
|
|
|
break;
|
|
|
|
case (rt_uint32_t)CM_ADC3:
|
|
|
|
u32TriggerSel = AOS_ADC3_1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2022-05-15 20:57:35 +08:00
|
|
|
AOS_CommonTriggerCmd(u32TriggerSel, AOS_COMM_TRIG1, (en_functional_state_t)p_adc_dev->init.internal_trig0_comtrg0_enable);
|
|
|
|
AOS_CommonTriggerCmd(u32TriggerSel, AOS_COMM_TRIG2, (en_functional_state_t)p_adc_dev->init.internal_trig0_comtrg1_enable);
|
2022-05-06 09:28:21 +08:00
|
|
|
#endif
|
2022-06-13 21:13:51 +08:00
|
|
|
|
|
|
|
#if defined(HC32F460)
|
|
|
|
switch ((rt_uint32_t)p_adc_dev->instance)
|
|
|
|
{
|
|
|
|
case (rt_uint32_t)CM_ADC1:
|
|
|
|
u32TriggerSel = AOS_ADC1_1;
|
|
|
|
break;
|
|
|
|
case (rt_uint32_t)CM_ADC2:
|
|
|
|
u32TriggerSel = AOS_ADC2_1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
AOS_CommonTriggerCmd(u32TriggerSel, AOS_COMM_TRIG1, (en_functional_state_t)p_adc_dev->init.internal_trig0_comtrg0_enable);
|
|
|
|
AOS_CommonTriggerCmd(u32TriggerSel, AOS_COMM_TRIG2, (en_functional_state_t)p_adc_dev->init.internal_trig0_comtrg1_enable);
|
|
|
|
#endif
|
2022-05-15 20:57:35 +08:00
|
|
|
AOS_SetTriggerEventSrc(u32TriggerSel, p_adc_dev->init.internal_trig1_sel);
|
2022-05-06 09:28:21 +08:00
|
|
|
}
|
|
|
|
|
2022-05-15 20:57:35 +08:00
|
|
|
static rt_err_t _adc_enable(struct rt_adc_device *device, rt_uint32_t channel, rt_bool_t enabled)
|
2022-05-06 09:28:21 +08:00
|
|
|
{
|
2022-05-15 20:57:35 +08:00
|
|
|
adc_device *p_adc_dev = rt_container_of(device, adc_device, rt_adc);
|
|
|
|
ADC_ChCmd(p_adc_dev->instance, ADC_SEQ_A, channel, (en_functional_state_t)enabled);
|
2022-05-06 09:28:21 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-05-15 20:57:35 +08:00
|
|
|
static rt_err_t _adc_convert(struct rt_adc_device *device, rt_uint32_t channel, rt_uint32_t *value)
|
2022-05-06 09:28:21 +08:00
|
|
|
{
|
|
|
|
rt_err_t rt_ret = RT_ERROR;
|
|
|
|
|
|
|
|
if (!value)
|
|
|
|
{
|
|
|
|
return -RT_EINVAL;
|
|
|
|
}
|
|
|
|
|
2022-05-15 20:57:35 +08:00
|
|
|
adc_device *p_adc_dev = rt_container_of(device, adc_device, rt_adc);
|
|
|
|
if (p_adc_dev->init.hard_trig_enable == RT_FALSE && p_adc_dev->instance->STR == 0)
|
2022-05-06 09:28:21 +08:00
|
|
|
{
|
2022-05-15 20:57:35 +08:00
|
|
|
ADC_Start(p_adc_dev->instance);
|
2022-05-06 09:28:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t start_time = rt_tick_get();
|
|
|
|
do
|
|
|
|
{
|
2022-05-15 20:57:35 +08:00
|
|
|
if (ADC_GetStatus(p_adc_dev->instance, ADC_FLAG_EOCA) == SET)
|
2022-05-06 09:28:21 +08:00
|
|
|
{
|
2022-05-15 20:57:35 +08:00
|
|
|
ADC_ClearStatus(p_adc_dev->instance, ADC_FLAG_EOCA);
|
2022-05-06 09:28:21 +08:00
|
|
|
rt_ret = LL_OK;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-05-15 20:57:35 +08:00
|
|
|
while ((rt_tick_get() - start_time) < p_adc_dev->init.eoc_poll_time_max);
|
2022-05-06 09:28:21 +08:00
|
|
|
|
|
|
|
if (rt_ret == LL_OK)
|
|
|
|
{
|
|
|
|
/* Get any ADC value of sequence A channel that needed. */
|
2022-05-15 20:57:35 +08:00
|
|
|
*value = ADC_GetValue(p_adc_dev->instance, channel);
|
2022-05-06 09:28:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return rt_ret;
|
|
|
|
}
|
|
|
|
|
2022-05-15 20:57:35 +08:00
|
|
|
static struct rt_adc_ops g_adc_ops =
|
2022-05-06 09:28:21 +08:00
|
|
|
{
|
2022-05-15 20:57:35 +08:00
|
|
|
_adc_enable,
|
|
|
|
_adc_convert,
|
2022-05-06 09:28:21 +08:00
|
|
|
};
|
|
|
|
|
2022-05-15 20:57:35 +08:00
|
|
|
static void _adc_clock_enable(void)
|
|
|
|
{
|
|
|
|
#if defined(HC32F4A0)
|
|
|
|
#if defined(BSP_USING_ADC1)
|
|
|
|
FCG_Fcg3PeriphClockCmd(FCG3_PERIPH_ADC1, ENABLE);
|
|
|
|
#endif
|
|
|
|
#if defined(BSP_USING_ADC2)
|
|
|
|
FCG_Fcg3PeriphClockCmd(FCG3_PERIPH_ADC2, ENABLE);
|
|
|
|
#endif
|
|
|
|
#if defined(BSP_USING_ADC3)
|
|
|
|
FCG_Fcg3PeriphClockCmd(FCG3_PERIPH_ADC3, ENABLE);
|
|
|
|
#endif
|
|
|
|
#endif
|
2022-06-13 21:13:51 +08:00
|
|
|
|
|
|
|
#if defined(HC32F460)
|
|
|
|
#if defined(BSP_USING_ADC1)
|
|
|
|
FCG_Fcg3PeriphClockCmd(FCG3_PERIPH_ADC1, ENABLE);
|
|
|
|
#endif
|
|
|
|
#if defined(BSP_USING_ADC2)
|
|
|
|
FCG_Fcg3PeriphClockCmd(FCG3_PERIPH_ADC2, ENABLE);
|
|
|
|
#endif
|
|
|
|
#endif
|
2022-05-15 20:57:35 +08:00
|
|
|
}
|
|
|
|
|
2022-05-06 09:28:21 +08:00
|
|
|
extern rt_err_t rt_hw_board_adc_init(CM_ADC_TypeDef *ADCx);
|
|
|
|
static int rt_hw_adc_init(void)
|
|
|
|
{
|
|
|
|
int ret, i = 0;
|
|
|
|
stc_adc_init_t stcAdcInit = {0};
|
|
|
|
int32_t ll_ret = 0;
|
|
|
|
|
2022-05-15 20:57:35 +08:00
|
|
|
_adc_clock_enable();
|
|
|
|
uint32_t dev_cnt = sizeof(g_adc_dev_array) / sizeof(g_adc_dev_array[0]);
|
2022-05-06 09:28:21 +08:00
|
|
|
for (; i < dev_cnt; i++)
|
|
|
|
{
|
2022-05-15 20:57:35 +08:00
|
|
|
ADC_DeInit(g_adc_dev_array[i].instance);
|
2022-05-06 09:28:21 +08:00
|
|
|
/* Initializes ADC. */
|
2022-05-15 20:57:35 +08:00
|
|
|
stcAdcInit.u16Resolution = g_adc_dev_array[i].init.resolution;
|
|
|
|
stcAdcInit.u16DataAlign = g_adc_dev_array[i].init.data_align;
|
|
|
|
stcAdcInit.u16ScanMode = (g_adc_dev_array[i].init.continue_conv_mode_enable) ? ADC_MD_SEQA_CONT : ADC_MD_SEQA_SINGLESHOT;
|
|
|
|
ll_ret = ADC_Init((void *)g_adc_dev_array[i].instance, &stcAdcInit);
|
2022-05-06 09:28:21 +08:00
|
|
|
if (ll_ret != LL_OK)
|
|
|
|
{
|
|
|
|
ret = -RT_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-05-15 20:57:35 +08:00
|
|
|
ADC_TriggerCmd(g_adc_dev_array[i].instance, ADC_SEQ_A, (en_functional_state_t)g_adc_dev_array[i].init.hard_trig_enable);
|
|
|
|
ADC_TriggerConfig(g_adc_dev_array[i].instance, ADC_SEQ_A, g_adc_dev_array[i].init.hard_trig_src);
|
|
|
|
if (g_adc_dev_array[i].init.hard_trig_enable && g_adc_dev_array[i].init.hard_trig_src != ADC_HARDTRIG_ADTRG_PIN)
|
2022-05-06 09:28:21 +08:00
|
|
|
{
|
2022-05-15 20:57:35 +08:00
|
|
|
_adc_internal_trigger0_set(&g_adc_dev_array[i]);
|
|
|
|
_adc_internal_trigger1_set(&g_adc_dev_array[i]);
|
2022-05-06 09:28:21 +08:00
|
|
|
}
|
|
|
|
|
2022-05-15 20:57:35 +08:00
|
|
|
rt_hw_board_adc_init((void *)g_adc_dev_array[i].instance);
|
|
|
|
ret = rt_hw_adc_register(&g_adc_dev_array[i].rt_adc, \
|
|
|
|
(const char *)g_adc_dev_array[i].init.name, \
|
|
|
|
&g_adc_ops, (void *)g_adc_dev_array[i].instance);
|
2022-05-06 09:28:21 +08:00
|
|
|
if (ret != RT_EOK)
|
|
|
|
{
|
|
|
|
/* TODO err handler */
|
2022-05-15 20:57:35 +08:00
|
|
|
// LOG_E("failed register %s, err=%d", g_adc_dev_array[i].name, ret);
|
2022-05-06 09:28:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
INIT_DEVICE_EXPORT(rt_hw_adc_init);
|
|
|
|
#endif
|