ra6m3之adc踩坑日志(基于rt-thread )
1.已经用fsp工具正确配置Pins
2.已经用fsp工具正确配置Stacks
3.问题是根本没有在RT-Thread Studio看到HAL相关的ADC代码,也没看到Settings里边的ADC配置选项
问题分析
RT-Thread跟Linux内核一样,采用Kconfig来配置驱动和组件,既然没有看到有ADC选项,可以考虑自己添加一个;再者,HAL_Drivers目录下的SConscript代码是这样的 :
Import('RTT_ROOT')
Import('rtconfig')
from building import *
cwd = GetCurrentDir()
# add the general drivers.
src = Split("""
drv_common.c
""")
if GetDepend(['BSP_USING_UART']):
if GetDepend(['RT_USING_SERIAL_V2']):
src += ['drv_usart_v2.c']
else:
print("\nThe current project does not support serial-v1\n")
Return('group')
if GetDepend(['BSP_USING_GPIO']):
src += ['drv_gpio.c']
if GetDepend(['BSP_USING_WDT']):
src += ['drv_wdt.c']
if GetDepend(['BSP_USING_ONCHIP_RTC']):
src += ['drv_rtc.c']
if GetDepend(['BSP_USING_I2C', 'RT_USING_I2C_BITOPS']):
if GetDepend('BSP_USING_I2C0') or GetDepend('BSP_USING_I2C1'):
src += ['drv_soft_i2c.c']
if GetDepend(['BSP_USING_I2C', 'BSP_USING_HW_I2C']):
src += ['drv_i2c.c']
if GetDepend(['BSP_USING_SPI']):
src += ['drv_spi.c']
if GetDepend(['BSP_USING_SCI_SPI']):
src += ['drv_sci_spi.c']
if GetDepend(['BSP_USING_ADC']):
src += ['drv_adc.c']
if GetDepend(['BSP_USING_DAC']):
src += ['drv_dac.c']
if GetDepend(['BSP_USING_ONCHIP_FLASH']):
src += ['drv_flash.c']
if GetDepend(['BSP_USING_PWM']):
src += ['drv_pwm.c']
if GetDepend(['BSP_USING_ETH']):
src += ['drv_eth.c']
if GetDepend(['BSP_USING_CAN']):
src += ['drv_can.c']
if GetDepend(['BSP_USING_SDHI']):
src += ['drv_sdhi.c']
if GetDepend(['BSP_USING_LCD']):
src += ['drv_lcd.c']
path = [cwd]
path += [cwd + '/config']
group = DefineGroup('Drivers', src, depend = [''], CPPPATH = path)
Return('group')
可以了解到drv_adc.c
在HAL_Drivers目录下是存在的,只是它没有添加到整个RT-Thread工程里边来,只要开启BSP_USING_ADC
宏即可;于是乎,编写Kconfig开启ADC支持,RT_USING_ADC
是RT-Thread支持ADC的宏开关,代码如下:
config BSP_USING_ADC
bool "Enable ADC"
select BSP_USING_ADC0
select RT_USING_ADC
default y
重新编译,但是编译报错
很明显,需要开启BSP_USING_ADC0这个宏,于是乎,又在Kconfig加上一下代码:
config BSP_USING_ADC0 bool "Enable ADC0" default y
重新打开Settings,终于看到两个开关,重新编译工程也没有问题了
测试
示例程序,在hal_entry.c加入以下代码:
/*
* Copyright (c) 2006-2023, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2021-10-10 Sherman first version
*/
/* Onboard LED pins */
/* ADC 设备名称 */
/* ADC 通道 */
/* 参考电压 3.3V,数据精度乘以100保留2位小数*/
/* 转换位数为12位 */
static int adc_vol_sample(int argc, char *argv[])
{
rt_adc_device_t adc_dev;
rt_uint32_t value, vol;
rt_err_t ret = RT_EOK;
/* 查找设备 */
adc_dev = (rt_adc_device_t)rt_device_find(ADC_DEV_NAME);
if (adc_dev == RT_NULL)
{
rt_kprintf("adc sample run failed! can't find %s device!\n", ADC_DEV_NAME);
return RT_ERROR;
}
/* 使能设备 */
ret = rt_adc_enable(adc_dev, ADC_DEV_CHANNEL);
/* 读取采样值 */
value = rt_adc_read(adc_dev, ADC_DEV_CHANNEL);
rt_kprintf("the value is :%d \n", value);
/* 转换为对应电压值 */
vol = value * REFER_VOLTAGE / CONVERT_BITS;
rt_kprintf("the voltage is :%d.%02d \n", vol / 100, vol % 100);
/* 关闭通道 */
ret = rt_adc_disable(adc_dev, ADC_DEV_CHANNEL);
return ret;
}
/* 导出到 msh 命令列表中 */
MSH_CMD_EXPORT(adc_vol_sample, adc voltage convert sample);
void hal_entry(void)
{
rt_kprintf("\nHello RT-Thread!\n");
while (1)
{
rt_pin_write(LED_PIN, PIN_HIGH);
rt_thread_mdelay(500);
rt_pin_write(LED_PIN, PIN_LOW);
rt_thread_mdelay(500);
}
}
烧写运行demo,P001接板子电源口,运行结果如下,电压还是算合理的
阅读剩余
版权声明:
作者:hywing
链接:https://iotstuff.cn/ra6m3-adc-problem/
文章版权归作者所有,未经允许请勿转载。
THE END