Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
2301_76519099
rt-thread
提交
8c12df98
R
rt-thread
项目概览
2301_76519099
/
rt-thread
与 Fork 源项目一致
Fork自
RT-Thread / rt-thread
通知
3
Star
1
Fork
1
代码
文件
提交
分支
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,发现更多精彩内容 >>
提交
8c12df98
编写于
3月 20, 2019
作者:
lymzzyh
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[BSP][k210] fix spi
上级
5a765d80
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
92 addition
and
21 deletion
+92
-21
bsp/k210/driver/drv_spi.c
bsp/k210/driver/drv_spi.c
+92
-21
未找到文件。
bsp/k210/driver/drv_spi.c
浏览文件 @
8c12df98
...
...
@@ -19,9 +19,12 @@
#include <sysctl.h>
#include <gpiohs.h>
#include <string.h>
#include "utils.h"
#define DRV_SPI_DEVICE(spi_bus) (struct drv_spi_bus *)(spi_bus)
#define MAX_CLOCK (40000000UL)
struct
drv_spi_bus
{
struct
rt_spi_bus
parent
;
...
...
@@ -48,6 +51,7 @@ static rt_err_t drv_spi_configure(struct rt_spi_device *device,
struct
rt_spi_configuration
*
configuration
)
{
rt_err_t
ret
=
RT_EOK
;
int
freq
=
0
;
struct
drv_spi_bus
*
bus
=
DRV_SPI_DEVICE
(
device
->
bus
);
struct
drv_cs
*
cs
=
(
struct
drv_cs
*
)
device
->
parent
.
user_data
;
RT_ASSERT
(
bus
!=
RT_NULL
);
...
...
@@ -60,31 +64,43 @@ static rt_err_t drv_spi_configure(struct rt_spi_device *device,
#else
spi_init
(
bus
->
spi_instance
,
configuration
->
mode
&
RT_SPI_MODE_3
,
SPI_FF_STANDARD
,
configuration
->
data_width
,
0
);
#endif
spi_set_clk_rate
(
bus
->
spi_instance
,
configuration
->
max_hz
);
freq
=
spi_set_clk_rate
(
bus
->
spi_instance
,
configuration
->
max_hz
>
MAX_CLOCK
?
MAX_CLOCK
:
configuration
->
max_hz
);
rt_kprintf
(
"set spi freq %d
\n
"
,
freq
);
return
ret
;
}
extern
void
spi_receive_data_normal_dma
(
dmac_channel_number_t
dma_send_channel_num
,
dmac_channel_number_t
dma_receive_channel_num
,
spi_device_num_t
spi_num
,
spi_chip_select_t
chip_select
,
const
void
*
cmd_buff
,
size_t
cmd_len
,
void
*
rx_buff
,
size_t
rx_len
);
void
__spi_set_tmod
(
uint8_t
spi_num
,
uint32_t
tmod
)
{
RT_ASSERT
(
spi_num
<
SPI_DEVICE_MAX
);
volatile
spi_t
*
spi_handle
=
spi
[
spi_num
];
uint8_t
tmod_offset
=
0
;
switch
(
spi_num
)
{
case
0
:
case
1
:
case
2
:
tmod_offset
=
8
;
break
;
case
3
:
default:
tmod_offset
=
10
;
break
;
}
set_bit
(
&
spi_handle
->
ctrlr0
,
3
<<
tmod_offset
,
tmod
<<
tmod_offset
);
}
static
rt_uint32_t
drv_spi_xfer
(
struct
rt_spi_device
*
device
,
struct
rt_spi_message
*
message
)
{
struct
drv_spi_bus
*
bus
=
DRV_SPI_DEVICE
(
device
->
bus
);
struct
drv_cs
*
cs
=
(
struct
drv_cs
*
)
device
->
parent
.
user_data
;
struct
rt_spi_configuration
*
cfg
=
&
device
->
config
;
const
uint8_t
*
tx_buff
=
message
->
send_buf
;
uint
8_t
*
rx_buff
=
message
->
recv_buf
;
uint32_t
dummy
[
1024
]
;
size_t
send_size
,
recv_size
;
uint32_t
*
tx_buff
=
RT_NULL
;
uint
32_t
*
rx_buff
=
RT_NULL
;
int
i
;
rt_ubase_t
dummy
=
0xFFFFFFFFU
;
send_size
=
message
->
length
;
recv_size
=
message
->
length
;
__spi_set_tmod
(
bus
->
spi_instance
,
SPI_TMOD_TRANS_RECV
);
RT_ASSERT
(
bus
!=
RT_NULL
);
...
...
@@ -94,18 +110,73 @@ static rt_uint32_t drv_spi_xfer(struct rt_spi_device *device, struct rt_spi_mess
}
if
(
message
->
length
)
{
if
(
!
tx_buff
)
spi_instance
[
bus
->
spi_instance
]
->
dmacr
=
0x3
;
spi_instance
[
bus
->
spi_instance
]
->
ssienr
=
0x01
;
sysctl_dma_select
(
bus
->
dma_send_channel
,
SYSCTL_DMA_SELECT_SSI0_TX_REQ
+
bus
->
spi_instance
*
2
);
sysctl_dma_select
(
bus
->
dma_recv_channel
,
SYSCTL_DMA_SELECT_SSI0_RX_REQ
+
bus
->
spi_instance
*
2
);
if
(
!
message
->
recv_buf
)
{
tx_buff
=
(
uint8_t
*
)
&
dummy
;
send_size
=
1
;
dmac_set_single_mode
(
bus
->
dma_recv_channel
,
(
void
*
)(
&
spi_instance
[
bus
->
spi_instance
]
->
dr
[
0
]),
&
dummy
,
DMAC_ADDR_NOCHANGE
,
DMAC_ADDR_NOCHANGE
,
DMAC_MSIZE_1
,
DMAC_TRANS_WIDTH_32
,
message
->
length
);
}
else
{
rx_buff
=
rt_calloc
(
message
->
length
*
4
,
1
);
if
(
!
rx_buff
)
{
goto
transfer_done
;
}
dmac_set_single_mode
(
bus
->
dma_recv_channel
,
(
void
*
)(
&
spi_instance
[
bus
->
spi_instance
]
->
dr
[
0
]),
rx_buff
,
DMAC_ADDR_NOCHANGE
,
DMAC_ADDR_INCREMENT
,
DMAC_MSIZE_1
,
DMAC_TRANS_WIDTH_32
,
message
->
length
);
}
if
(
!
rx_buff
)
if
(
!
message
->
send_buf
)
{
dmac_set_single_mode
(
bus
->
dma_send_channel
,
&
dummy
,
(
void
*
)(
&
spi_instance
[
bus
->
spi_instance
]
->
dr
[
0
]),
DMAC_ADDR_NOCHANGE
,
DMAC_ADDR_NOCHANGE
,
DMAC_MSIZE_4
,
DMAC_TRANS_WIDTH_32
,
message
->
length
);
}
else
{
tx_buff
=
rt_malloc
(
message
->
length
*
4
);
if
(
!
tx_buff
)
{
goto
transfer_done
;
}
for
(
i
=
0
;
i
<
message
->
length
;
i
++
)
{
tx_buff
[
i
]
=
((
uint8_t
*
)
message
->
send_buf
)[
i
];
}
dmac_set_single_mode
(
bus
->
dma_send_channel
,
tx_buff
,
(
void
*
)(
&
spi_instance
[
bus
->
spi_instance
]
->
dr
[
0
]),
DMAC_ADDR_INCREMENT
,
DMAC_ADDR_NOCHANGE
,
DMAC_MSIZE_4
,
DMAC_TRANS_WIDTH_32
,
message
->
length
);
}
spi_instance
[
bus
->
spi_instance
]
->
ser
=
1U
<<
cs
->
cs_index
;
dmac_wait_done
(
bus
->
dma_send_channel
);
dmac_wait_done
(
bus
->
dma_recv_channel
);
spi_instance
[
bus
->
spi_instance
]
->
ser
=
0x00
;
spi_instance
[
bus
->
spi_instance
]
->
ssienr
=
0x00
;
if
(
message
->
recv_buf
)
{
for
(
i
=
0
;
i
<
message
->
length
;
i
++
)
{
((
uint8_t
*
)
message
->
recv_buf
)[
i
]
=
(
uint8_t
)
rx_buff
[
i
];
}
}
transfer_done:
if
(
tx_buff
)
{
rt_free
(
tx_buff
);
}
if
(
rx_buff
)
{
rx_buff
=
(
uint8_t
*
)
&
dummy
;
recv_size
=
1
;
rt_free
(
rx_buff
);
}
spi_dup_send_receive_data_dma
(
bus
->
dma_send_channel
,
bus
->
dma_recv_channel
,
bus
->
spi_instance
,
cs
->
cs_index
,
tx_buff
,
send_size
,
rx_buff
,
recv_size
);
}
if
(
message
->
cs_release
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录