Toggle navigation
首页
问答
文章
积分商城
专家
专区
更多专区...
文档中心
返回主站
搜索
提问
会员
中心
登录
注册
RT-Thread一般讨论
[Pulse Encoder]文档中心的示例程序有问题
发布于 2019-10-22 15:08:33 浏览:2559
订阅该版
* 本帖最后由 霹雳大乌龙 于 2019-10-22 15:09 编辑 * ```c for(rt_uint32 i; i <= 10; i++) { rt_thread_mdelay(500); /* 读取脉冲编码器计数值 */ rt_device_read(pulse_encoder_dev, 0, &count, 1); /* 清空脉冲编码器计数值 */ rt_device_control(pulse_encoder_dev, PULSE_ENCODER_CMD_CLEAR_COUNT, RT_NULL); rt_kprintf("get count %d
",count); } ``` 应该是: ```c for(rt_uint32_t i = 0; i <= 10; i++) { rt_thread_mdelay(500); /* 读取脉冲编码器计数值 */ rt_device_read(pulse_encoder_dev, 0, &count, 1); /* 清空脉冲编码器计数值 */ rt_device_control(pulse_encoder_dev, PULSE_ENCODER_CMD_CLEAR_COUNT, RT_NULL); rt_kprintf("get count %d
",count); } ``` 但是这样改之后,程序一运行就hard fault,发现问题出在这一句: ```c rt_device_control(pulse_encoder_dev, PULSE_ENCODER_CMD_CLEAR_COUNT, RT_NULL); ``` 将该句屏蔽后,程序才能运行,但是计数值就没法清空,有没有大佬帮忙看看。
查看更多
18
个回答
默认排序
按发布时间排序
Ernest
2019-10-22
这家伙很懒,什么也没写!
感觉驱动没对上
来一颗糖
2019-10-22
这家伙很懒,什么也没写!
是自己加的 bsp 嘛?
霹雳大乌龙
2019-10-22
这家伙很懒,什么也没写!
>是自己加的 bsp 嘛? --- 用的是stm32f401-st-nucleo,自己写的kconfig,框架驱动和对应的驱动都有,也能读到计数值,就是清空计数这个有问题。
霹雳大乌龙
2019-10-22
这家伙很懒,什么也没写!
>感觉驱动没对上 --- 驱动官方的,能读到计数值。
来一颗糖
2019-10-23
这家伙很懒,什么也没写!
>驱动官方的,能读到计数值。 --- 驱动 和 驱动框架 那两个 .c 文件发来瞧瞧
霹雳大乌龙
2019-10-23
这家伙很懒,什么也没写!
>驱动 和 驱动框架 那两个 .c 文件发来瞧瞧 --- ``` /* * Copyright (c) 2006-2018, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes * 2019-08-23 balanceTWK first version */ #include "board.h" #include "drv_config.h" #ifdef RT_USING_PULSE_ENCODER //#define DRV_DEBUG #define LOG_TAG "drv.pulse_encoder" #include
#if !defined(BSP_USING_PULSE_ENCODER1) && !defined(BSP_USING_PULSE_ENCODER2) && !defined(BSP_USING_PULSE_ENCODER3) \ && !defined(BSP_USING_PULSE_ENCODER4) && !defined(BSP_USING_PULSE_ENCODER5) && !defined(BSP_USING_PULSE_ENCODER6) #error "Please define at least one BSP_USING_PULSE_ENCODERx" /* this driver can be disabled at menuconfig → RT-Thread Components → Device Drivers */ #endif enum { #ifdef BSP_USING_PULSE_ENCODER1 PULSE_ENCODER1_INDEX, #endif #ifdef BSP_USING_PULSE_ENCODER2 PULSE_ENCODER2_INDEX, #endif #ifdef BSP_USING_PULSE_ENCODER3 PULSE_ENCODER3_INDEX, #endif #ifdef BSP_USING_PULSE_ENCODER4 PULSE_ENCODER4_INDEX, #endif #ifdef BSP_USING_PULSE_ENCODER5 PULSE_ENCODER5_INDEX, #endif #ifdef BSP_USING_PULSE_ENCODER6 PULSE_ENCODER6_INDEX, #endif }; struct stm32_pulse_encoder_device { struct rt_pulse_encoder_device pulse_encoder; TIM_HandleTypeDef tim_handler; char *name; }; static struct stm32_pulse_encoder_device stm32_pulse_encoder_obj[] = { #ifdef BSP_USING_PULSE_ENCODER1 PULSE_ENCODER1_CONFIG, #endif #ifdef BSP_USING_PULSE_ENCODER2 PULSE_ENCODER2_CONFIG, #endif #ifdef BSP_USING_PULSE_ENCODER3 PULSE_ENCODER3_CONFIG, #endif #ifdef BSP_USING_PULSE_ENCODER4 PULSE_ENCODER4_CONFIG, #endif #ifdef BSP_USING_PULSE_ENCODER5 PULSE_ENCODER5_CONFIG, #endif #ifdef BSP_USING_PULSE_ENCODER6 PULSE_ENCODER6_CONFIG, #endif }; rt_err_t pulse_encoder_init(struct rt_pulse_encoder_device *pulse_encoder) { TIM_Encoder_InitTypeDef sConfig; TIM_MasterConfigTypeDef sMasterConfig; TIM_HandleTypeDef *tim_handler = (TIM_HandleTypeDef *)pulse_encoder->parent.user_data; tim_handler->Init.Prescaler = 0; tim_handler->Init.CounterMode = TIM_COUNTERMODE_UP; tim_handler->Init.Period = 0xffff; tim_handler->Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; sConfig.EncoderMode = TIM_ENCODERMODE_TI12; sConfig.IC1Polarity = TIM_ICPOLARITY_RISING; sConfig.IC1Selection = TIM_ICSELECTION_DIRECTTI; sConfig.IC1Prescaler = TIM_ICPSC_DIV1; sConfig.IC1Filter = 3; sConfig.IC2Polarity = TIM_ICPOLARITY_RISING; sConfig.IC2Selection = TIM_ICSELECTION_DIRECTTI; sConfig.IC2Prescaler = TIM_ICPSC_DIV1; sConfig.IC2Filter = 3; if (HAL_TIM_Encoder_Init(tim_handler, &sConfig) != HAL_OK) { LOG_E("pulse_encoder init failed"); return -RT_ERROR; } sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; if (HAL_TIMEx_MasterConfigSynchronization(tim_handler, &sMasterConfig)) { LOG_E("TIMx master config failed"); return -RT_ERROR; } return RT_EOK; } rt_int32_t pulse_encoder_get_count(struct rt_pulse_encoder_device *pulse_encoder) { TIM_HandleTypeDef *tim_handler = (TIM_HandleTypeDef *)pulse_encoder->parent.user_data; return (rt_int16_t)__HAL_TIM_GET_COUNTER(tim_handler); } rt_err_t pulse_encoder_control(struct rt_pulse_encoder_device *pulse_encoder, rt_uint32_t cmd, void *args) { rt_err_t result; TIM_HandleTypeDef *tim_handler = (TIM_HandleTypeDef *)pulse_encoder->parent.user_data; result = RT_EOK; switch (cmd) { case PULSE_ENCODER_CMD_ENABLE: HAL_TIM_Encoder_Start(tim_handler, TIM_CHANNEL_ALL); break; case PULSE_ENCODER_CMD_DISABLE: HAL_TIM_Encoder_Stop(tim_handler, TIM_CHANNEL_ALL); break; case PULSE_ENCODER_CMD_CLEAR_COUNT: __HAL_TIM_SET_COUNTER(tim_handler, 0); break; default: result = -RT_ENOSYS; break; } return result; } static const struct rt_pulse_encoder_ops _ops = { .init = pulse_encoder_init, .get_count = pulse_encoder_get_count, .control = pulse_encoder_control, }; int hw_pulse_encoder_init(void) { int i; int result; result = RT_EOK; for (i = 0; i < sizeof(stm32_pulse_encoder_obj) / sizeof(stm32_pulse_encoder_obj[0]); i++) { stm32_pulse_encoder_obj[i].pulse_encoder.type = AB_PHASE_PULSE_ENCODER; stm32_pulse_encoder_obj[i].pulse_encoder.ops = &_ops; if (rt_device_pulse_encoder_register(&stm32_pulse_encoder_obj[i].pulse_encoder, stm32_pulse_encoder_obj[i].name, &stm32_pulse_encoder_obj[i].tim_handler) != RT_EOK) { LOG_E("%s register failed", stm32_pulse_encoder_obj[i].name); result = -RT_ERROR; } } return result; } INIT_BOARD_EXPORT(hw_pulse_encoder_init); #endif ``` drv_pulse_encoder.c
霹雳大乌龙
2019-10-23
这家伙很懒,什么也没写!
>驱动 和 驱动框架 那两个 .c 文件发来瞧瞧 --- ``` /* * Copyright (c) 2006-2018, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes * 2019-08-08 balanceTWK the first version */ #include
#include
static rt_err_t rt_pulse_encoder_init(struct rt_device *dev) { struct rt_pulse_encoder_device *pulse_encoder; pulse_encoder = (struct rt_pulse_encoder_device *)dev; if (pulse_encoder->ops->init) { return pulse_encoder->ops->init(pulse_encoder); } else { return -RT_ENOSYS; } } static rt_err_t rt_pulse_encoder_open(struct rt_device *dev, rt_uint16_t oflag) { struct rt_pulse_encoder_device *pulse_encoder; pulse_encoder = (struct rt_pulse_encoder_device *)dev; if (pulse_encoder->ops->control) { return pulse_encoder->ops->control(pulse_encoder, PULSE_ENCODER_CMD_ENABLE, RT_NULL); } else { return -RT_ENOSYS; } } static rt_err_t rt_pulse_encoder_close(struct rt_device *dev) { struct rt_pulse_encoder_device *pulse_encoder; pulse_encoder = (struct rt_pulse_encoder_device *)dev; if (pulse_encoder->ops->control) { return pulse_encoder->ops->control(pulse_encoder, PULSE_ENCODER_CMD_DISABLE, RT_NULL); } else { return -RT_ENOSYS; } } static rt_size_t rt_pulse_encoder_read(struct rt_device *dev, rt_off_t pos, void *buffer, rt_size_t size) { struct rt_pulse_encoder_device *pulse_encoder; pulse_encoder = (struct rt_pulse_encoder_device *)dev; if (pulse_encoder->ops->get_count) { *(rt_int32_t *)buffer = pulse_encoder->ops->get_count(pulse_encoder); } return 1; } static rt_err_t rt_pulse_encoder_control(struct rt_device *dev, int cmd, void *args) { rt_err_t result; struct rt_pulse_encoder_device *pulse_encoder; result = RT_EOK; pulse_encoder = (struct rt_pulse_encoder_device *)dev; switch (cmd) { case PULSE_ENCODER_CMD_CLEAR_COUNT: result = pulse_encoder->ops->clear_count(pulse_encoder); break; case PULSE_ENCODER_CMD_GET_TYPE: *(enum rt_pulse_encoder_type *)args = pulse_encoder->type; break; case PULSE_ENCODER_CMD_ENABLE: case PULSE_ENCODER_CMD_DISABLE: result = pulse_encoder->ops->control(pulse_encoder, cmd, args); break; default: result = -RT_ENOSYS; break; } return result; } #ifdef RT_USING_DEVICE_OPS const static struct rt_device_ops pulse_encoder_ops = { rt_pulse_encoder_init, rt_pulse_encoder_open, rt_pulse_encoder_close, rt_pulse_encoder_read, RT_NULL, rt_pulse_encoder_control }; #endif rt_err_t rt_device_pulse_encoder_register(struct rt_pulse_encoder_device *pulse_encoder, const char *name, void *user_data) { struct rt_device *device; RT_ASSERT(pulse_encoder != RT_NULL); RT_ASSERT(pulse_encoder->ops != RT_NULL); device = &(pulse_encoder->parent); device->type = RT_Device_Class_Miscellaneous; device->rx_indicate = RT_NULL; device->tx_complete = RT_NULL; #ifdef RT_USING_DEVICE_OPS device->ops = &pulse_encoder_ops; #else device->init = rt_pulse_encoder_init; device->open = rt_pulse_encoder_open; device->close = rt_pulse_encoder_close; device->read = rt_pulse_encoder_read; device->write = RT_NULL; device->control = rt_pulse_encoder_control; #endif device->user_data = user_data; return rt_device_register(device, name, RT_DEVICE_FLAG_RDONLY | RT_DEVICE_FLAG_STANDALONE); } ``` pulse_encoder.c
来一颗糖
2019-10-23
这家伙很懒,什么也没写!
把这两个文件以及对应的 .h 文件 去 GitHub 更新成最新的吧。
霹雳大乌龙
2019-10-23
这家伙很懒,什么也没写!
>把这两个文件以及对应的 .h 文件 去 GitHub 更新成最新的吧。 --- 更新后问题解决:lol
Ernest
2019-10-24
这家伙很懒,什么也没写!
>更新后问题解决 --- “来一颗糖”,他写的
撰写答案
登录
注册新账号
关注者
0
被浏览
2.6k
关于作者
霹雳大乌龙
这家伙很懒,什么也没写!
提问
11
回答
53
被采纳
0
关注TA
发私信
相关问题
1
有关动态模块加载的一篇论文
2
最近的调程序总结
3
晕掉了,这么久都不见layer2的踪影啊
4
继续K9ii的历程
5
[GUI相关] FreeType 2
6
[GUI相关]嵌入式系统中文输入法的设计
7
20081101 RT-Thread开发者聚会总结
8
嵌入式系统基础
9
linux2.4.19在at91rm9200 上的寄存器设置
10
[转]基于嵌入式Linux的通用触摸屏校准程序
推荐文章
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组件
最新文章
1
【24嵌入式设计大赛】基于RT-Thread星火一号的智慧家居系统
2
RT-Thread EtherKit开源以太网硬件正式发布
3
如何在master上的BSP中添加配置yml文件
4
使用百度AI助手辅助编写一个rt-thread下的ONVIF设备发现功能的功能代码
5
RT-Thread 发布 EtherKit开源以太网硬件!
热门标签
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
ESP8266
I2C_IIC
WIZnet_W5500
UART
ota在线升级
PWM
cubemx
freemodbus
flash
packages_软件包
BSP
潘多拉开发板_Pandora
定时器
ADC
GD32
flashDB
socket
中断
Debug
编译报错
msh
SFUD
keil_MDK
rt_mq_消息队列_msg_queue
MicroPython
ulog
C++_cpp
本月问答贡献
踩姑娘的小蘑菇
7
个答案
3
次被采纳
a1012112796
15
个答案
2
次被采纳
张世争
9
个答案
2
次被采纳
rv666
5
个答案
2
次被采纳
用户名由3_15位
13
个答案
1
次被采纳
本月文章贡献
程序员阿伟
9
篇文章
2
次点赞
hhart
3
篇文章
4
次点赞
大龄码农
1
篇文章
5
次点赞
RTT_逍遥
1
篇文章
2
次点赞
ThinkCode
1
篇文章
1
次点赞
回到
顶部
发布
问题
分享
好友
手机
浏览
扫码手机浏览
投诉
建议
回到
底部