Toggle navigation
首页
问答
文章
积分商城
专家
专区
更多专区...
文档中心
返回主站
搜索
提问
会员
中心
登录
注册
ADC
NXP 微控制器
【NXP-MCXA153】RTT ADC驱动添加
发布于 2024-07-12 22:33:34 浏览:292
订阅该版
[tocm] ![1.png](https://oss-club.rt-thread.org/uploads/20240712/c0caa9d7a901c6fabc448caf7016ab5d.png.webp) ## 层级结构 ### 应用层:开发者利用驱动框架API操作底层 ### ADC设备驱动架构: 与具体硬件不相干,源码在adc.c中 1. 为应用层提供接口:rt_adc_read rt_adc_enable rt_adc_disable 等 2. 底层提供操作方法接口:struct rt_adc_ops ```cpp struct rt_adc_ops { rt_err_t (*enabled)(struct rt_adc_device *device, rt_int8_t channel, rt_bool_t enabled); rt_err_t (*convert)(struct rt_adc_device *device, rt_int8_t channel, rt_uint32_t *value); rt_uint8_t (*get_resolution)(struct rt_adc_device *device);// rt_int16_t (*get_vref) (struct rt_adc_device *device);// }; ``` 3、提供注册接口 ```c rt_err_t rt_hw_adc_register(rt_adc_device_t adc,const char *name, const struct rt_adc_ops *ops, const void *user_data); ``` ### ADC设备驱动 实现驱动框架中 struct rt_adc_ops 的各个接口 ## 代码实现 - 首先在rtconfig.h中添加宏 ```c #define RT_USING_ADC ``` - 其次根据MCXA153的ADC外设配置流程写结构体 - struct mcx_adc 其成员包括 - 通道 - 指令ID - triggerID - ADC_Type * adc_base - 还有最重要的 rt_adc_device_t (adc.c中) ```c #include "drv_lpadc.h" #include "fsl_common.h" #include "fsl_gpio.h" #include "fsl_port.h" #include "fsl_inputmux.h" #include "fsl_lpadc.h" #include
struct mcx_adc { //IRQn_Type irqn; //中断向量 已经默认 //clock_attach_id_t clock_attach_id; 已经默认 //clock_div_name_t clock_div_name; 已经默认 uint32_t channelNumber; //通道ID uint32_t commandID; //指令ID command number are 1-15 uint32_t triggerId; // 0 1 2 3 char *device_name; ADC_Type * adc_base; // rt_adc_device_t mcx_adc_device; }; static struct mcx_adc adcs[] = { { 0U, 1U, 0U, "adc1", ADC0, NULL, }, }; rt_err_t mcx_enabled(struct rt_adc_device *device, rt_int8_t channel, rt_bool_t enabled) { lpadc_config_t mLpadcConfigStruct; lpadc_conv_trigger_config_t mLpadcTriggerConfigStruct; lpadc_conv_command_config_t mLpadcCommandConfigStruct; if(enabled) { CLOCK_SetClockDiv(kCLOCK_DivADC0, 1u); CLOCK_AttachClk(kFRO12M_to_ADC0); LPADC_GetDefaultConfig(&mLpadcConfigStruct); mLpadcConfigStruct.enableAnalogPreliminary = true; mLpadcConfigStruct.referenceVoltageSource = kLPADC_ReferenceVoltageAlt3;//vdda #if defined(FSL_FEATURE_LPADC_HAS_CTRL_CAL_AVGS) && FSL_FEATURE_LPADC_HAS_CTRL_CAL_AVGS mLpadcConfigStruct.conversionAverageMode = kLPADC_ConversionAverage128; #endif LPADC_Init(ADC0, &mLpadcConfigStruct); #if defined(FSL_FEATURE_LPADC_HAS_CTRL_CALOFS) && FSL_FEATURE_LPADC_HAS_CTRL_CALOFS #if defined(FSL_FEATURE_LPADC_HAS_OFSTRIM) && FSL_FEATURE_LPADC_HAS_OFSTRIM /* Request offset calibration. */ #if defined(DEMO_LPADC_DO_OFFSET_CALIBRATION) && DEMO_LPADC_DO_OFFSET_CALIBRATION LPADC_DoOffsetCalibration(ADC0); #else //LPADC_SetOffsetValue(ADC0, DEMO_LPADC_OFFSET_VALUE_A, DEMO_LPADC_OFFSET_VALUE_B); #endif /* DEMO_LPADC_DO_OFFSET_CALIBRATION */ #endif /* FSL_FEATURE_LPADC_HAS_OFSTRIM */ #if defined(FSL_FEATURE_LPADC_HAS_CTRL_CALOFSMODE) && FSL_FEATURE_LPADC_HAS_CTRL_CALOFSMODE LPADC_SetOffsetCalibrationMode(ADC0, DEMO_LPADC_OFFSET_CALIBRATION_MODE); LPADC_DoOffsetCalibration(ADC0); #endif /* FSL_FEATURE_LPADC_HAS_CTRL_CALOFSMODE */ /* Request gain calibration. */ LPADC_DoAutoCalibration(ADC0); #endif /* FSL_FEATURE_LPADC_HAS_CTRL_CALOFS */ LPADC_GetDefaultConvCommandConfig(&mLpadcCommandConfigStruct); mLpadcCommandConfigStruct.channelNumber = 0U; #if defined(DEMO_LPADC_USE_HIGH_RESOLUTION) && DEMO_LPADC_USE_HIGH_RESOLUTION mLpadcCommandConfigStruct.conversionResolutionMode = kLPADC_ConversionResolutionHigh; #endif /* DEMO_LPADC_USE_HIGH_RESOLUTION */ LPADC_SetConvCommandConfig(ADC0, 1U, &mLpadcCommandConfigStruct); /* Set trigger configuration. */ LPADC_GetDefaultConvTriggerConfig(&mLpadcTriggerConfigStruct); mLpadcTriggerConfigStruct.targetCommandId = 1U; mLpadcTriggerConfigStruct.enableHardwareTrigger = false; LPADC_SetConvTriggerConfig(ADC0, 0U, &mLpadcTriggerConfigStruct); } else { LPADC_Deinit(ADC0); } return RT_EOK; } rt_err_t mcx_convert(struct rt_adc_device *device, rt_int8_t channel, rt_uint32_t *value) { lpadc_conv_result_t mLpadcResultConfigStruct; LPADC_DoSoftwareTrigger(ADC0, 1U); /* 1U is trigger0 mask. */ while (!LPADC_GetConvResult(ADC0, &mLpadcResultConfigStruct)) { } *value = mLpadcResultConfigStruct.convValue; return RT_EOK; } rt_uint8_t mcx_get_resolution(struct rt_adc_device *device) { return 1; } rt_int16_t mcx_get_vref (struct rt_adc_device *device) { return 1; } struct rt_adc_ops mcx_adc_ops = { mcx_enabled, mcx_convert, mcx_get_resolution, mcx_get_vref, }; int rt_hw_adc_init(void) { int i; for (i = 0; i < 1; i++) { adcs[i].mcx_adc_device = (rt_adc_device_t)rt_malloc(sizeof(struct rt_adc_device)); rt_memset(adcs[i].mcx_adc_device , 0, sizeof(struct rt_adc_device)); rt_hw_adc_register(adcs[i].mcx_adc_device, "adc1", &mcx_adc_ops, adcs[i].adc_base); } return 0; } //INIT_BOARD_EXPORT(rt_hw_adc_init); ``` ```c //注册 rt_hw_adc_init(); rt_adc_device_t adc_dev; rt_uint32_t value; //查找 adc_dev = (rt_adc_device_t) rt_device_find("adc1"); if(adc_dev == RT_NULL) rt_kprintf("adc device find error\r\n"); //使能 rt_adc_enable(adc_dev, 0); //读取 value = rt_adc_read(adc_dev, 0); rt_kprintf("adc value = %d\r\n",value); ``` ## 总结 - 代码中很多都是直接写成固定值,而不是通过参数去传递,后面再优化 - INIT_BOARD_EXPORT(rt_hw_adc_init); 不能用此方式进行提前注册,会有硬件错误 - 如果熟悉A153的ADC外设会更好的去写驱动 ![2.png](https://oss-club.rt-thread.org/uploads/20240712/388a238b829efdf5952b667bf8475032.png.webp)
0
条评论
默认排序
按发布时间排序
登录
注册新账号
关于作者
LTBOHAPPY
加油努力
文章
6
回答
1
被采纳
0
关注TA
发私信
相关文章
1
试贴-消灭0主题
2
LPC M4的一些资料
3
LPC4088的临时分支
4
lpc1788 ad 不稳定
5
1788 LCD控制器缓冲区字节问题
6
一起来学习LPC4088吧
7
上传LPC4088的realtouch主工程
8
RealBoard 4088预定帖 [第一批板子不多,预定提前结束]
9
晒RealBoard LPC4088开箱照啦,速带小板凳前来围观
10
4088主程序需要的SD卡资源
推荐文章
1
RT-Thread应用项目汇总
2
玩转RT-Thread系列教程
3
国产MCU移植系列教程汇总,欢迎查看!
4
机器人操作系统 (ROS2) 和 RT-Thread 通信
5
五分钟玩转RT-Thread新社区
6
【技术三千问】之《玩转ART-Pi》,看这篇就够了!干货汇总
7
关于STM32H7开发板上使用SDIO接口驱动SD卡挂载文件系统的问题总结
8
STM32的“GPU”——DMA2D实例详解
9
RT-Thread隐藏的宝藏之completion
10
【ART-PI】RT-Thread 开启RTC 与 Alarm组件
热门标签
RT-Thread Studio
串口
Env
LWIP
SPI
AT
Bootloader
Hardfault
CAN总线
FinSH
ART-Pi
USB
DMA
文件系统
RT-Thread
SCons
RT-Thread Nano
线程
MQTT
STM32
RTC
FAL
rt-smart
I2C_IIC
ESP8266
UART
WIZnet_W5500
ota在线升级
cubemx
PWM
flash
freemodbus
BSP
packages_软件包
潘多拉开发板_Pandora
定时器
ADC
flashDB
GD32
socket
编译报错
中断
Debug
rt_mq_消息队列_msg_queue
SFUD
msh
keil_MDK
ulog
C++_cpp
MicroPython
本月问答贡献
xusiwei1236
8
个答案
2
次被采纳
踩姑娘的小蘑菇
1
个答案
2
次被采纳
用户名由3_15位
9
个答案
1
次被采纳
bernard
4
个答案
1
次被采纳
RTT_逍遥
3
个答案
1
次被采纳
本月文章贡献
聚散无由
2
篇文章
15
次点赞
catcatbing
2
篇文章
5
次点赞
Wade
2
篇文章
4
次点赞
Ghost_Girls
1
篇文章
7
次点赞
YZRD
1
篇文章
2
次点赞
回到
顶部
发布
问题
投诉
建议
回到
底部