Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
yunke120
rt-thread
提交
f1fe6479
R
rt-thread
项目概览
yunke120
/
rt-thread
与 Fork 源项目一致
Fork自
RT-Thread / rt-thread
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rt-thread
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
f1fe6479
编写于
9月 24, 2019
作者:
T
tangweikang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[bsp][stm32][driver][drv_pulse_encoder.c] remove pulse_encoder->parent.user_data
上级
7c455e67
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
154 addition
and
33 deletion
+154
-33
bsp/stm32/libraries/HAL_Drivers/config/f4/pulse_encoder_config.h
...32/libraries/HAL_Drivers/config/f4/pulse_encoder_config.h
+20
-16
bsp/stm32/libraries/HAL_Drivers/drv_pulse_encoder.c
bsp/stm32/libraries/HAL_Drivers/drv_pulse_encoder.c
+134
-17
未找到文件。
bsp/stm32/libraries/HAL_Drivers/config/f4/pulse_encoder_config.h
浏览文件 @
f1fe6479
...
...
@@ -19,40 +19,44 @@ extern "C" {
#ifdef BSP_USING_PULSE_ENCODER1
#ifndef PULSE_ENCODER1_CONFIG
#define PULSE_ENCODER1_CONFIG \
{ \
.tim_handler.Instance = TIM1, \
.name = "pulse1" \
#define PULSE_ENCODER1_CONFIG \
{ \
.tim_handler.Instance = TIM1, \
.encoder_irqn = TIM1_UP_TIM10_IRQn, \
.name = "pulse1" \
}
#endif
/* PULSE_ENCODER1_CONFIG */
#endif
/* BSP_USING_PULSE_ENCODER1 */
#ifdef BSP_USING_PULSE_ENCODER2
#ifndef PULSE_ENCODER2_CONFIG
#define PULSE_ENCODER2_CONFIG \
{ \
.tim_handler.Instance = TIM2, \
.name = "pulse2" \
#define PULSE_ENCODER2_CONFIG \
{ \
.tim_handler.Instance = TIM2, \
.encoder_irqn = TIM2_IRQn, \
.name = "pulse2" \
}
#endif
/* PULSE_ENCODER2_CONFIG */
#endif
/* BSP_USING_PULSE_ENCODER2 */
#ifdef BSP_USING_PULSE_ENCODER3
#ifndef PULSE_ENCODER3_CONFIG
#define PULSE_ENCODER3_CONFIG \
{ \
.tim_handler.Instance = TIM3, \
.name = "pulse3" \
#define PULSE_ENCODER3_CONFIG \
{ \
.tim_handler.Instance = TIM3, \
.encoder_irqn = TIM3_IRQn, \
.name = "pulse3" \
}
#endif
/* PULSE_ENCODER3_CONFIG */
#endif
/* BSP_USING_PULSE_ENCODER3 */
#ifdef BSP_USING_PULSE_ENCODER4
#ifndef PULSE_ENCODER4_CONFIG
#define PULSE_ENCODER4_CONFIG \
{ \
.tim_handler.Instance = TIM4, \
.name = "pulse4" \
#define PULSE_ENCODER4_CONFIG \
{ \
.tim_handler.Instance = TIM4, \
.encoder_irqn = TIM4_IRQn, \
.name = "pulse4" \
}
#endif
/* PULSE_ENCODER4_CONFIG */
#endif
/* BSP_USING_PULSE_ENCODER4 */
...
...
bsp/stm32/libraries/HAL_Drivers/drv_pulse_encoder.c
浏览文件 @
f1fe6479
...
...
@@ -22,6 +22,8 @@
/* this driver can be disabled at menuconfig → RT-Thread Components → Device Drivers */
#endif
#define AUTO_RELOAD_VALUE 0x7FFF
enum
{
#ifdef BSP_USING_PULSE_ENCODER1
...
...
@@ -48,6 +50,8 @@ struct stm32_pulse_encoder_device
{
struct
rt_pulse_encoder_device
pulse_encoder
;
TIM_HandleTypeDef
tim_handler
;
IRQn_Type
encoder_irqn
;
volatile
rt_int32_t
over_under_flowcount
;
char
*
name
;
};
...
...
@@ -77,24 +81,26 @@ 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
;
struct
stm32_pulse_encoder_device
*
stm32_device
;
stm32_device
=
rt_container_of
(
pulse_encoder
,
struct
stm32_pulse_encoder_device
,
pulse_encoder
);
tim_handler
->
Init
.
Prescaler
=
0
;
tim_handler
->
Init
.
CounterMode
=
TIM_COUNTERMODE_UP
;
tim_handler
->
Init
.
Period
=
0xffff
;
tim_handler
->
Init
.
ClockDivision
=
TIM_CLOCKDIVISION_DIV1
;
stm32_device
->
tim_handler
.
Init
.
Prescaler
=
0
;
stm32_device
->
tim_handler
.
Init
.
CounterMode
=
TIM_COUNTERMODE_UP
;
stm32_device
->
tim_handler
.
Init
.
Period
=
AUTO_RELOAD_VALUE
;
stm32_device
->
tim_handler
.
Init
.
ClockDivision
=
TIM_CLOCKDIVISION_DIV1
;
stm32_device
->
tim_handler
.
Init
.
AutoReloadPreload
=
TIM_AUTORELOAD_PRELOAD_ENABLE
;
sConfig
.
EncoderMode
=
TIM_ENCODERMODE_TI12
;
sConfig
.
IC1Polarity
=
TIM_ICPOLARITY_RISING
;
sConfig
.
IC1Selection
=
TIM_ICSELECTION_DIRECTTI
;
sConfig
.
IC1Prescaler
=
TIM_ICPSC_DIV
4
;
sConfig
.
IC1Prescaler
=
TIM_ICPSC_DIV
1
;
sConfig
.
IC1Filter
=
3
;
sConfig
.
IC2Polarity
=
TIM_ICPOLARITY_RISING
;
sConfig
.
IC2Selection
=
TIM_ICSELECTION_DIRECTTI
;
sConfig
.
IC2Prescaler
=
TIM_ICPSC_DIV
4
;
sConfig
.
IC2Prescaler
=
TIM_ICPSC_DIV
1
;
sConfig
.
IC2Filter
=
3
;
if
(
HAL_TIM_Encoder_Init
(
tim_handler
,
&
sConfig
)
!=
HAL_OK
)
if
(
HAL_TIM_Encoder_Init
(
&
stm32_device
->
tim_handler
,
&
sConfig
)
!=
HAL_OK
)
{
LOG_E
(
"pulse_encoder init failed"
);
return
-
RT_ERROR
;
...
...
@@ -103,42 +109,60 @@ rt_err_t pulse_encoder_init(struct rt_pulse_encoder_device *pulse_encoder)
sMasterConfig
.
MasterOutputTrigger
=
TIM_TRGO_RESET
;
sMasterConfig
.
MasterSlaveMode
=
TIM_MASTERSLAVEMODE_DISABLE
;
if
(
HAL_TIMEx_MasterConfigSynchronization
(
tim_handler
,
&
sMasterConfig
))
if
(
HAL_TIMEx_MasterConfigSynchronization
(
&
stm32_device
->
tim_handler
,
&
sMasterConfig
))
{
LOG_E
(
"TIMx master config failed"
);
return
-
RT_ERROR
;
}
else
{
HAL_NVIC_SetPriority
(
stm32_device
->
encoder_irqn
,
3
,
0
);
/* enable the TIMx global Interrupt */
HAL_NVIC_EnableIRQ
(
stm32_device
->
encoder_irqn
);
/* clear update flag */
__HAL_TIM_CLEAR_FLAG
(
&
stm32_device
->
tim_handler
,
TIM_FLAG_UPDATE
);
/* enable update request source */
__HAL_TIM_URS_ENABLE
(
&
stm32_device
->
tim_handler
);
}
return
RT_EOK
;
}
rt_err_t
pulse_encoder_clear_count
(
struct
rt_pulse_encoder_device
*
pulse_encoder
)
{
TIM_HandleTypeDef
*
tim_handler
=
(
TIM_HandleTypeDef
*
)
pulse_encoder
->
parent
.
user_data
;
__HAL_TIM_SET_COUNTER
(
tim_handler
,
0
);
struct
stm32_pulse_encoder_device
*
stm32_device
;
stm32_device
=
rt_container_of
(
pulse_encoder
,
struct
stm32_pulse_encoder_device
,
pulse_encoder
);
stm32_device
->
over_under_flowcount
=
0
;
__HAL_TIM_SET_COUNTER
(
&
stm32_device
->
tim_handler
,
0
);
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
);
struct
stm32_pulse_encoder_device
*
stm32_device
;
stm32_device
=
rt_container_of
(
pulse_encoder
,
struct
stm32_pulse_encoder_device
,
pulse_encoder
);
return
(
rt_int32_t
)((
rt_int16_t
)
__HAL_TIM_GET_COUNTER
(
&
stm32_device
->
tim_handler
)
+
stm32_device
->
over_under_flowcount
*
AUTO_RELOAD_VALUE
);
}
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
;
struct
stm32_pulse_encoder_device
*
stm32_device
;
stm32_device
=
rt_container_of
(
pulse_encoder
,
struct
stm32_pulse_encoder_device
,
pulse_encoder
);
result
=
RT_EOK
;
switch
(
cmd
)
{
case
PULSE_ENCODER_CMD_ENABLE
:
HAL_TIM_Encoder_Start
(
tim_handler
,
TIM_CHANNEL_ALL
);
HAL_TIM_Encoder_Start
(
&
stm32_device
->
tim_handler
,
TIM_CHANNEL_ALL
);
HAL_TIM_Encoder_Start_IT
(
&
stm32_device
->
tim_handler
,
TIM_CHANNEL_ALL
);
break
;
case
PULSE_ENCODER_CMD_DISABLE
:
HAL_TIM_Encoder_Stop
(
tim_handler
,
TIM_CHANNEL_ALL
);
HAL_TIM_Encoder_Stop
(
&
stm32_device
->
tim_handler
,
TIM_CHANNEL_ALL
);
HAL_TIM_Encoder_Stop_IT
(
&
stm32_device
->
tim_handler
,
TIM_CHANNEL_ALL
);
break
;
default:
result
=
-
RT_ENOSYS
;
...
...
@@ -148,6 +172,99 @@ rt_err_t pulse_encoder_control(struct rt_pulse_encoder_device *pulse_encoder, rt
return
result
;
}
void
pulse_encoder_update_isr
(
struct
stm32_pulse_encoder_device
*
device
)
{
/* TIM Update event */
if
(
__HAL_TIM_GET_FLAG
(
&
device
->
tim_handler
,
TIM_FLAG_UPDATE
)
!=
RESET
)
{
__HAL_TIM_CLEAR_IT
(
&
device
->
tim_handler
,
TIM_IT_UPDATE
);
if
(
__HAL_TIM_IS_TIM_COUNTING_DOWN
(
&
device
->
tim_handler
))
{
device
->
over_under_flowcount
--
;
}
else
{
device
->
over_under_flowcount
++
;
}
}
/* Capture compare 1 event */
if
(
__HAL_TIM_GET_FLAG
(
&
device
->
tim_handler
,
TIM_FLAG_CC1
)
!=
RESET
)
{
__HAL_TIM_CLEAR_IT
(
&
device
->
tim_handler
,
TIM_IT_CC1
);
}
/* Capture compare 2 event */
if
(
__HAL_TIM_GET_FLAG
(
&
device
->
tim_handler
,
TIM_FLAG_CC2
)
!=
RESET
)
{
__HAL_TIM_CLEAR_IT
(
&
device
->
tim_handler
,
TIM_IT_CC2
);
}
/* Capture compare 3 event */
if
(
__HAL_TIM_GET_FLAG
(
&
device
->
tim_handler
,
TIM_FLAG_CC3
)
!=
RESET
)
{
__HAL_TIM_CLEAR_IT
(
&
device
->
tim_handler
,
TIM_IT_CC3
);
}
/* Capture compare 4 event */
if
(
__HAL_TIM_GET_FLAG
(
&
device
->
tim_handler
,
TIM_FLAG_CC4
)
!=
RESET
)
{
__HAL_TIM_CLEAR_IT
(
&
device
->
tim_handler
,
TIM_IT_CC4
);
}
/* TIM Break input event */
if
(
__HAL_TIM_GET_FLAG
(
&
device
->
tim_handler
,
TIM_FLAG_BREAK
)
!=
RESET
)
{
__HAL_TIM_CLEAR_IT
(
&
device
->
tim_handler
,
TIM_IT_BREAK
);
}
/* TIM Trigger detection event */
if
(
__HAL_TIM_GET_FLAG
(
&
device
->
tim_handler
,
TIM_FLAG_TRIGGER
)
!=
RESET
)
{
__HAL_TIM_CLEAR_IT
(
&
device
->
tim_handler
,
TIM_IT_TRIGGER
);
}
/* TIM commutation event */
if
(
__HAL_TIM_GET_FLAG
(
&
device
->
tim_handler
,
TIM_FLAG_COM
)
!=
RESET
)
{
__HAL_TIM_CLEAR_IT
(
&
device
->
tim_handler
,
TIM_FLAG_COM
);
}
}
#ifdef BSP_USING_PULSE_ENCODER1
void
TIM1_UP_TIM10_IRQHandler
(
void
)
{
/* enter interrupt */
rt_interrupt_enter
();
pulse_encoder_update_isr
(
&
stm32_pulse_encoder_obj
[
PULSE_ENCODER1_INDEX
]);
/* leave interrupt */
rt_interrupt_leave
();
}
#endif
#ifdef BSP_USING_PULSE_ENCODER2
void
TIM2_IRQHandler
(
void
)
{
/* enter interrupt */
rt_interrupt_enter
();
pulse_encoder_update_isr
(
&
stm32_pulse_encoder_obj
[
PULSE_ENCODER2_INDEX
]);
/* leave interrupt */
rt_interrupt_leave
();
}
#endif
#ifdef BSP_USING_PULSE_ENCODER3
void
TIM3_IRQHandler
(
void
)
{
/* enter interrupt */
rt_interrupt_enter
();
pulse_encoder_update_isr
(
&
stm32_pulse_encoder_obj
[
PULSE_ENCODER3_INDEX
]);
/* leave interrupt */
rt_interrupt_leave
();
}
#endif
#ifdef BSP_USING_PULSE_ENCODER4
void
TIM4_IRQHandler
(
void
)
{
/* enter interrupt */
rt_interrupt_enter
();
pulse_encoder_update_isr
(
&
stm32_pulse_encoder_obj
[
PULSE_ENCODER4_INDEX
]);
/* leave interrupt */
rt_interrupt_leave
();
}
#endif
static
const
struct
rt_pulse_encoder_ops
_ops
=
{
.
init
=
pulse_encoder_init
,
...
...
@@ -167,7 +284,7 @@ int hw_pulse_encoder_init(void)
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
)
if
(
rt_device_pulse_encoder_register
(
&
stm32_pulse_encoder_obj
[
i
].
pulse_encoder
,
stm32_pulse_encoder_obj
[
i
].
name
,
RT_NULL
)
!=
RT_EOK
)
{
LOG_E
(
"%s register failed"
,
stm32_pulse_encoder_obj
[
i
].
name
);
result
=
-
RT_ERROR
;
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录