Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
wwsmg1
rt-thread
提交
cbd062ca
R
rt-thread
项目概览
wwsmg1
/
rt-thread
与 Fork 源项目一致
Fork自
RT-Thread / rt-thread
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rt-thread
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
cbd062ca
编写于
4月 20, 2022
作者:
W
wolfJane
提交者:
GitHub
4月 20, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[bsp/n32g45x] drv_spi.c中添加超时检测避免卡死 (#5820)
* 为spi添加超时检测,避免卡死。提取公共部份,避免代码复制。
上级
a0f8d437
变更
1
显示空白变更内容
内联
并排
Showing
1 changed file
with
76 addition
and
19 deletion
+76
-19
bsp/n32g452xx/Libraries/rt_drivers/drv_spi.c
bsp/n32g452xx/Libraries/rt_drivers/drv_spi.c
+76
-19
未找到文件。
bsp/n32g452xx/Libraries/rt_drivers/drv_spi.c
浏览文件 @
cbd062ca
...
...
@@ -6,6 +6,7 @@
* Change Logs:
* Date Author Notes
* 2022-03-06 BalanceTWK first version
* 2022-04-16 wolfJane fix spixfer, add time out check
*/
#include <board.h>
...
...
@@ -22,6 +23,8 @@
#define LOG_TAG "drv.spi"
#include <drv_log.h>
#define SPI_TIME_OUT (1000)
enum
{
#ifdef BSP_USING_SPI1
...
...
@@ -235,13 +238,57 @@ static rt_err_t spi_configure(struct rt_spi_device *device,
return
n32_spi_init
(
spi_drv
,
configuration
);
}
static
int
_spi_recv
(
SPI_Module
*
hspi
,
uint8_t
*
tx_buff
,
uint8_t
*
rx_buff
,
uint32_t
length
,
uint32_t
timeout
)
{
/* Init tickstart for timeout management*/
uint32_t
tickstart
=
rt_tick_get
();
uint8_t
dat
=
0
;
if
((
tx_buff
==
RT_NULL
)
&&
(
rx_buff
==
RT_NULL
)
||
(
length
==
0
))
{
return
RT_EIO
;
}
while
(
length
--
)
{
while
(
SPI_I2S_GetStatus
(
hspi
,
SPI_I2S_TE_FLAG
)
==
RESET
)
{
if
((
rt_tick_get
()
-
tickstart
)
>
timeout
)
{
return
RT_ETIMEOUT
;
}
}
SPI_I2S_TransmitData
(
hspi
,
*
tx_buff
++
);
while
(
SPI_I2S_GetStatus
(
hspi
,
SPI_I2S_RNE_FLAG
)
==
RESET
)
{
if
((
rt_tick_get
()
-
tickstart
)
>
timeout
)
{
return
RT_ETIMEOUT
;
}
}
dat
=
SPI_I2S_ReceiveData
(
hspi
);
if
(
rx_buff
)
{
*
rx_buff
++
=
dat
;
}
}
return
RT_EOK
;
}
static
rt_uint32_t
spixfer
(
struct
rt_spi_device
*
device
,
struct
rt_spi_message
*
message
)
{
rt_size_t
message_length
;
rt_size_t
i
=
0
;
rt_size_t
send_length
;
rt_uint8_t
*
recv_buf
;
const
rt_uint8_t
*
send_buf
;
rt_err_t
stat
=
RT_EOK
;
/* Check Direction parameter */
RT_ASSERT
(
device
!=
RT_NULL
);
RT_ASSERT
(
device
->
bus
!=
RT_NULL
);
RT_ASSERT
(
device
->
bus
->
parent
.
user_data
!=
RT_NULL
);
...
...
@@ -254,12 +301,16 @@ static rt_uint32_t spixfer(struct rt_spi_device *device, struct rt_spi_message *
if
(
message
->
cs_take
&&
!
(
device
->
config
.
mode
&
RT_SPI_NO_CS
))
{
if
(
device
->
config
.
mode
&
RT_SPI_CS_HIGH
)
{
GPIO_SetBits
(
cs
->
module
,
cs
->
pin
);
}
else
{
GPIO_ResetBits
(
cs
->
module
,
cs
->
pin
);
}
}
message
_length
=
message
->
length
;
send
_length
=
message
->
length
;
recv_buf
=
message
->
recv_buf
;
send_buf
=
message
->
send_buf
;
...
...
@@ -267,38 +318,44 @@ static rt_uint32_t spixfer(struct rt_spi_device *device, struct rt_spi_message *
if
(
message
->
send_buf
&&
message
->
recv_buf
)
{
LOG_D
(
"%s:%d"
,
__FUNCTION__
,
__LINE__
);
stat
=
RT_EIO
;
}
else
if
(
message
->
send_buf
)
{
while
(
message_length
--
)
{
while
(
SPI_I2S_GetStatus
(
spi_handle
,
SPI_I2S_TE_FLAG
)
==
RESET
);
SPI_I2S_TransmitData
(
spi_handle
,
send_buf
[
i
++
]);
while
(
SPI_I2S_GetStatus
(
spi_handle
,
SPI_I2S_RNE_FLAG
)
==
RESET
);
SPI_I2S_ReceiveData
(
spi_handle
);
}
stat
=
_spi_recv
(
spi_handle
,
(
uint8_t
*
)
send_buf
,
RT_NULL
,
send_length
,
SPI_TIME_OUT
);
}
else
{
while
(
message_length
--
)
{
while
(
SPI_I2S_GetStatus
(
spi_handle
,
SPI_I2S_TE_FLAG
)
==
RESET
);
SPI_I2S_TransmitData
(
spi_handle
,
0xff
);
while
(
SPI_I2S_GetStatus
(
spi_handle
,
SPI_I2S_RNE_FLAG
)
==
RESET
);
recv_buf
[
i
++
]
=
(
rt_uint8_t
)
SPI_I2S_ReceiveData
(
spi_handle
);
}
rt_memset
(
recv_buf
,
0xff
,
send_length
);
stat
=
_spi_recv
(
spi_handle
,
(
uint8_t
*
)
recv_buf
,
(
uint8_t
*
)
recv_buf
,
send_length
,
SPI_TIME_OUT
);
}
if
(
message
->
cs_release
&&
!
(
device
->
config
.
mode
&
RT_SPI_NO_CS
))
{
if
(
device
->
config
.
mode
&
RT_SPI_CS_HIGH
)
{
GPIO_ResetBits
(
cs
->
module
,
cs
->
pin
);
}
else
{
GPIO_SetBits
(
cs
->
module
,
cs
->
pin
);
}
}
if
(
stat
!=
RT_EOK
)
{
send_length
=
0
;
}
return
message
->
length
;
return
send_
length
;
}
static
const
struct
rt_spi_ops
n32_spi_ops
=
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录