Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
小毕学习代码
arduino-esp32
提交
d0232d12
A
arduino-esp32
项目概览
小毕学习代码
/
arduino-esp32
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
A
arduino-esp32
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
d0232d12
编写于
2月 14, 2017
作者:
M
me-no-dev
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
SPI implement transaction locking
Use transactions to skip mutex locks for reads and writes
上级
9bd5de11
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
449 addition
and
109 deletion
+449
-109
cores/esp32/esp32-hal-spi.c
cores/esp32/esp32-hal-spi.c
+352
-81
cores/esp32/esp32-hal-spi.h
cores/esp32/esp32-hal-spi.h
+31
-6
libraries/SPI/src/SPI.cpp
libraries/SPI/src/SPI.cpp
+51
-11
libraries/SPI/src/SPI.h
libraries/SPI/src/SPI.h
+15
-11
未找到文件。
cores/esp32/esp32-hal-spi.c
浏览文件 @
d0232d12
...
...
@@ -517,41 +517,24 @@ uint8_t spiTransferByte(spi_t * spi, uint8_t data)
return
data
;
}
uint32_t
__spiTranslate
16
(
uint16_t
data
,
bool
msb
)
uint32_t
__spiTranslate
24
(
uint32_t
data
)
{
if
(
msb
)
{
return
(
data
>>
8
)
|
(
data
<<
8
);
}
else
{
return
data
;
}
}
uint32_t
__spiTranslate24
(
uint32_t
data
,
bool
msb
)
{
if
(
msb
)
{
union
{
uint32_t
l
;
uint8_t
b
[
4
];
}
out
;
out
.
l
=
data
;
return
out
.
b
[
2
]
|
(
out
.
b
[
1
]
<<
8
)
|
(
out
.
b
[
0
]
<<
16
);
}
else
{
return
data
;
}
union
{
uint32_t
l
;
uint8_t
b
[
4
];
}
out
;
out
.
l
=
data
;
return
out
.
b
[
2
]
|
(
out
.
b
[
1
]
<<
8
)
|
(
out
.
b
[
0
]
<<
16
);
}
uint32_t
__spiTranslate32
(
uint32_t
data
,
bool
msb
)
uint32_t
__spiTranslate32
(
uint32_t
data
)
{
if
(
msb
)
{
union
{
uint32_t
l
;
uint8_t
b
[
4
];
}
out
;
out
.
l
=
data
;
return
out
.
b
[
3
]
|
(
out
.
b
[
2
]
<<
8
)
|
(
out
.
b
[
1
]
<<
16
)
|
(
out
.
b
[
0
]
<<
24
);
}
else
{
return
data
;
}
union
{
uint32_t
l
;
uint8_t
b
[
4
];
}
out
;
out
.
l
=
data
;
return
out
.
b
[
3
]
|
(
out
.
b
[
2
]
<<
8
)
|
(
out
.
b
[
1
]
<<
16
)
|
(
out
.
b
[
0
]
<<
24
);
}
void
spiWriteWord
(
spi_t
*
spi
,
uint16_t
data
)
...
...
@@ -559,10 +542,13 @@ void spiWriteWord(spi_t * spi, uint16_t data)
if
(
!
spi
)
{
return
;
}
if
(
!
spi
->
dev
->
ctrl
.
wr_bit_order
){
data
=
(
data
>>
8
)
|
(
data
<<
8
);
}
SPI_MUTEX_LOCK
();
spi
->
dev
->
mosi_dlen
.
usr_mosi_dbitlen
=
15
;
spi
->
dev
->
miso_dlen
.
usr_miso_dbitlen
=
0
;
spi
->
dev
->
data_buf
[
0
]
=
__spiTranslate16
(
data
,
!
spi
->
dev
->
ctrl
.
wr_bit_order
)
;
spi
->
dev
->
data_buf
[
0
]
=
data
;
spi
->
dev
->
cmd
.
usr
=
1
;
while
(
spi
->
dev
->
cmd
.
usr
);
SPI_MUTEX_UNLOCK
();
...
...
@@ -573,14 +559,20 @@ uint16_t spiTransferWord(spi_t * spi, uint16_t data)
if
(
!
spi
)
{
return
0
;
}
if
(
!
spi
->
dev
->
ctrl
.
wr_bit_order
){
data
=
(
data
>>
8
)
|
(
data
<<
8
);
}
SPI_MUTEX_LOCK
();
spi
->
dev
->
mosi_dlen
.
usr_mosi_dbitlen
=
15
;
spi
->
dev
->
miso_dlen
.
usr_miso_dbitlen
=
15
;
spi
->
dev
->
data_buf
[
0
]
=
__spiTranslate16
(
data
,
!
spi
->
dev
->
ctrl
.
wr_bit_order
)
;
spi
->
dev
->
data_buf
[
0
]
=
data
;
spi
->
dev
->
cmd
.
usr
=
1
;
while
(
spi
->
dev
->
cmd
.
usr
);
data
=
__spiTranslate16
(
spi
->
dev
->
data_buf
[
0
]
&
0xFFFF
,
!
spi
->
dev
->
ctrl
.
rd_bit_order
)
;
data
=
spi
->
dev
->
data_buf
[
0
]
;
SPI_MUTEX_UNLOCK
();
if
(
!
spi
->
dev
->
ctrl
.
rd_bit_order
){
data
=
(
data
>>
8
)
|
(
data
<<
8
);
}
return
data
;
}
...
...
@@ -589,10 +581,13 @@ void spiWriteLong(spi_t * spi, uint32_t data)
if
(
!
spi
)
{
return
;
}
if
(
!
spi
->
dev
->
ctrl
.
wr_bit_order
){
data
=
__spiTranslate32
(
data
);
}
SPI_MUTEX_LOCK
();
spi
->
dev
->
mosi_dlen
.
usr_mosi_dbitlen
=
31
;
spi
->
dev
->
miso_dlen
.
usr_miso_dbitlen
=
0
;
spi
->
dev
->
data_buf
[
0
]
=
__spiTranslate32
(
data
,
!
spi
->
dev
->
ctrl
.
wr_bit_order
)
;
spi
->
dev
->
data_buf
[
0
]
=
data
;
spi
->
dev
->
cmd
.
usr
=
1
;
while
(
spi
->
dev
->
cmd
.
usr
);
SPI_MUTEX_UNLOCK
();
...
...
@@ -603,57 +598,21 @@ uint32_t spiTransferLong(spi_t * spi, uint32_t data)
if
(
!
spi
)
{
return
0
;
}
if
(
!
spi
->
dev
->
ctrl
.
wr_bit_order
){
data
=
__spiTranslate32
(
data
);
}
SPI_MUTEX_LOCK
();
spi
->
dev
->
mosi_dlen
.
usr_mosi_dbitlen
=
31
;
spi
->
dev
->
miso_dlen
.
usr_miso_dbitlen
=
31
;
spi
->
dev
->
data_buf
[
0
]
=
__spiTranslate32
(
data
,
!
spi
->
dev
->
ctrl
.
wr_bit_order
)
;
spi
->
dev
->
data_buf
[
0
]
=
data
;
spi
->
dev
->
cmd
.
usr
=
1
;
while
(
spi
->
dev
->
cmd
.
usr
);
data
=
__spiTranslate32
(
spi
->
dev
->
data_buf
[
0
],
!
spi
->
dev
->
ctrl
.
rd_bit_order
)
;
data
=
spi
->
dev
->
data_buf
[
0
]
;
SPI_MUTEX_UNLOCK
();
return
data
;
}
void
spiTransferBits
(
spi_t
*
spi
,
uint32_t
data
,
uint32_t
*
out
,
uint8_t
bits
)
{
if
(
!
spi
)
{
return
;
}
if
(
bits
>
32
)
{
bits
=
32
;
if
(
!
spi
->
dev
->
ctrl
.
rd_bit_order
){
data
=
__spiTranslate32
(
data
);
}
uint32_t
bytes
=
(
bits
+
7
)
/
8
;
//64 max
uint32_t
mask
=
(((
uint64_t
)
1
<<
bits
)
-
1
)
&
0xFFFFFFFF
;
SPI_MUTEX_LOCK
();
spi
->
dev
->
mosi_dlen
.
usr_mosi_dbitlen
=
(
bits
-
1
);
spi
->
dev
->
miso_dlen
.
usr_miso_dbitlen
=
(
bits
-
1
);
if
(
bytes
==
1
)
{
spi
->
dev
->
data_buf
[
0
]
=
data
&
mask
;
}
else
if
(
bytes
==
2
)
{
spi
->
dev
->
data_buf
[
0
]
=
__spiTranslate16
(
data
&
mask
,
!
spi
->
dev
->
ctrl
.
wr_bit_order
);
}
else
if
(
bytes
==
3
)
{
spi
->
dev
->
data_buf
[
0
]
=
__spiTranslate24
(
data
&
mask
,
!
spi
->
dev
->
ctrl
.
wr_bit_order
);
}
else
{
spi
->
dev
->
data_buf
[
0
]
=
__spiTranslate32
(
data
&
mask
,
!
spi
->
dev
->
ctrl
.
wr_bit_order
);
}
spi
->
dev
->
cmd
.
usr
=
1
;
while
(
spi
->
dev
->
cmd
.
usr
);
if
(
out
)
{
if
(
bytes
==
1
)
{
*
out
=
spi
->
dev
->
data_buf
[
0
]
&
mask
;
}
else
if
(
bytes
==
2
)
{
*
out
=
__spiTranslate16
(
spi
->
dev
->
data_buf
[
0
]
&
mask
,
!
spi
->
dev
->
ctrl
.
wr_bit_order
);
}
else
if
(
bytes
==
3
)
{
*
out
=
__spiTranslate24
(
spi
->
dev
->
data_buf
[
0
]
&
mask
,
!
spi
->
dev
->
ctrl
.
wr_bit_order
);
}
else
{
*
out
=
__spiTranslate32
(
spi
->
dev
->
data_buf
[
0
]
&
mask
,
!
spi
->
dev
->
ctrl
.
wr_bit_order
);
}
}
SPI_MUTEX_UNLOCK
();
return
data
;
}
void
__spiTransferBytes
(
spi_t
*
spi
,
uint8_t
*
data
,
uint8_t
*
out
,
uint32_t
bytes
)
...
...
@@ -721,6 +680,320 @@ void spiTransferBytes(spi_t * spi, uint8_t * data, uint8_t * out, uint32_t size)
SPI_MUTEX_UNLOCK
();
}
/*
* Manual Lock Management
* */
#define MSB_32_SET(var, val) { uint8_t * d = (uint8_t *)&(val); (var) = d[3] | (d[2] << 8) | (d[1] << 16) | (d[0] << 24); }
#define MSB_24_SET(var, val) { uint8_t * d = (uint8_t *)&(val); (var) = d[2] | (d[1] << 8) | (d[0] << 16); }
#define MSB_16_SET(var, val) { (var) = (((val) & 0xFF00) >> 8) | (((val) & 0xFF) << 8); }
void
spiTransaction
(
spi_t
*
spi
,
uint32_t
clockDiv
,
uint8_t
dataMode
,
uint8_t
bitOrder
)
{
if
(
!
spi
)
{
return
;
}
SPI_MUTEX_LOCK
();
spi
->
dev
->
clock
.
val
=
clockDiv
;
switch
(
dataMode
)
{
case
SPI_MODE1
:
spi
->
dev
->
pin
.
ck_idle_edge
=
0
;
spi
->
dev
->
user
.
ck_out_edge
=
1
;
break
;
case
SPI_MODE2
:
spi
->
dev
->
pin
.
ck_idle_edge
=
1
;
spi
->
dev
->
user
.
ck_out_edge
=
0
;
break
;
case
SPI_MODE3
:
spi
->
dev
->
pin
.
ck_idle_edge
=
1
;
spi
->
dev
->
user
.
ck_out_edge
=
1
;
break
;
case
SPI_MODE0
:
default:
spi
->
dev
->
pin
.
ck_idle_edge
=
0
;
spi
->
dev
->
user
.
ck_out_edge
=
0
;
break
;
}
if
(
SPI_MSBFIRST
==
bitOrder
)
{
spi
->
dev
->
ctrl
.
wr_bit_order
=
0
;
spi
->
dev
->
ctrl
.
rd_bit_order
=
0
;
}
else
if
(
SPI_LSBFIRST
==
bitOrder
)
{
spi
->
dev
->
ctrl
.
wr_bit_order
=
1
;
spi
->
dev
->
ctrl
.
rd_bit_order
=
1
;
}
}
void
spiSimpleTransaction
(
spi_t
*
spi
)
{
if
(
!
spi
)
{
return
;
}
SPI_MUTEX_LOCK
();
}
void
spiEndTransaction
(
spi_t
*
spi
)
{
if
(
!
spi
)
{
return
;
}
SPI_MUTEX_UNLOCK
();
}
void
spiWriteByteNL
(
spi_t
*
spi
,
uint8_t
data
)
{
if
(
!
spi
)
{
return
;
}
spi
->
dev
->
mosi_dlen
.
usr_mosi_dbitlen
=
7
;
spi
->
dev
->
miso_dlen
.
usr_miso_dbitlen
=
0
;
spi
->
dev
->
data_buf
[
0
]
=
data
;
spi
->
dev
->
cmd
.
usr
=
1
;
while
(
spi
->
dev
->
cmd
.
usr
);
}
uint8_t
spiTransferByteNL
(
spi_t
*
spi
,
uint8_t
data
)
{
if
(
!
spi
)
{
return
0
;
}
spi
->
dev
->
mosi_dlen
.
usr_mosi_dbitlen
=
7
;
spi
->
dev
->
miso_dlen
.
usr_miso_dbitlen
=
7
;
spi
->
dev
->
data_buf
[
0
]
=
data
;
spi
->
dev
->
cmd
.
usr
=
1
;
while
(
spi
->
dev
->
cmd
.
usr
);
data
=
spi
->
dev
->
data_buf
[
0
]
&
0xFF
;
return
data
;
}
void
spiWriteShortNL
(
spi_t
*
spi
,
uint16_t
data
)
{
if
(
!
spi
)
{
return
;
}
if
(
!
spi
->
dev
->
ctrl
.
wr_bit_order
){
MSB_16_SET
(
data
,
data
);
}
spi
->
dev
->
mosi_dlen
.
usr_mosi_dbitlen
=
15
;
spi
->
dev
->
miso_dlen
.
usr_miso_dbitlen
=
0
;
spi
->
dev
->
data_buf
[
0
]
=
data
;
spi
->
dev
->
cmd
.
usr
=
1
;
while
(
spi
->
dev
->
cmd
.
usr
);
}
uint16_t
spiTransferShortNL
(
spi_t
*
spi
,
uint16_t
data
)
{
if
(
!
spi
)
{
return
0
;
}
if
(
!
spi
->
dev
->
ctrl
.
wr_bit_order
){
MSB_16_SET
(
data
,
data
);
}
spi
->
dev
->
mosi_dlen
.
usr_mosi_dbitlen
=
15
;
spi
->
dev
->
miso_dlen
.
usr_miso_dbitlen
=
15
;
spi
->
dev
->
data_buf
[
0
]
=
data
;
spi
->
dev
->
cmd
.
usr
=
1
;
while
(
spi
->
dev
->
cmd
.
usr
);
data
=
spi
->
dev
->
data_buf
[
0
]
&
0xFFFF
;
if
(
!
spi
->
dev
->
ctrl
.
rd_bit_order
){
MSB_16_SET
(
data
,
data
);
}
return
data
;
}
void
spiWriteLongNL
(
spi_t
*
spi
,
uint32_t
data
)
{
if
(
!
spi
)
{
return
;
}
if
(
!
spi
->
dev
->
ctrl
.
wr_bit_order
){
MSB_32_SET
(
data
,
data
);
}
spi
->
dev
->
mosi_dlen
.
usr_mosi_dbitlen
=
31
;
spi
->
dev
->
miso_dlen
.
usr_miso_dbitlen
=
0
;
spi
->
dev
->
data_buf
[
0
]
=
data
;
spi
->
dev
->
cmd
.
usr
=
1
;
while
(
spi
->
dev
->
cmd
.
usr
);
}
uint32_t
spiTransferLongNL
(
spi_t
*
spi
,
uint32_t
data
)
{
if
(
!
spi
)
{
return
0
;
}
if
(
!
spi
->
dev
->
ctrl
.
wr_bit_order
){
MSB_32_SET
(
data
,
data
);
}
spi
->
dev
->
mosi_dlen
.
usr_mosi_dbitlen
=
31
;
spi
->
dev
->
miso_dlen
.
usr_miso_dbitlen
=
31
;
spi
->
dev
->
data_buf
[
0
]
=
data
;
spi
->
dev
->
cmd
.
usr
=
1
;
while
(
spi
->
dev
->
cmd
.
usr
);
data
=
spi
->
dev
->
data_buf
[
0
];
if
(
!
spi
->
dev
->
ctrl
.
rd_bit_order
){
MSB_32_SET
(
data
,
data
);
}
return
data
;
}
void
spiWriteNL
(
spi_t
*
spi
,
const
void
*
data_in
,
size_t
len
){
size_t
longs
=
len
>>
2
;
if
(
len
&
3
){
longs
++
;
}
uint32_t
*
data
=
(
uint32_t
*
)
data_in
;
size_t
c_len
=
0
,
c_longs
=
0
;
while
(
len
){
c_len
=
(
len
>
64
)
?
64
:
len
;
c_longs
=
(
longs
>
16
)
?
16
:
longs
;
spi
->
dev
->
mosi_dlen
.
usr_mosi_dbitlen
=
(
c_len
*
8
)
-
1
;
spi
->
dev
->
miso_dlen
.
usr_miso_dbitlen
=
0
;
for
(
int
i
=
0
;
i
<
c_longs
;
i
++
)
{
spi
->
dev
->
data_buf
[
i
]
=
data
[
i
];
}
spi
->
dev
->
cmd
.
usr
=
1
;
while
(
spi
->
dev
->
cmd
.
usr
);
data
+=
c_longs
;
longs
-=
c_longs
;
len
-=
c_len
;
}
}
void
spiTransferBytesNL
(
spi_t
*
spi
,
const
void
*
data_in
,
uint8_t
*
data_out
,
size_t
len
){
if
(
!
spi
)
{
return
;
}
size_t
longs
=
len
>>
2
;
if
(
len
&
3
){
longs
++
;
}
uint32_t
*
data
=
(
uint32_t
*
)
data_in
;
uint32_t
*
result
=
(
uint32_t
*
)
data_out
;
size_t
c_len
=
0
,
c_longs
=
0
;
while
(
len
){
c_len
=
(
len
>
64
)
?
64
:
len
;
c_longs
=
(
longs
>
16
)
?
16
:
longs
;
spi
->
dev
->
mosi_dlen
.
usr_mosi_dbitlen
=
(
c_len
*
8
)
-
1
;
spi
->
dev
->
miso_dlen
.
usr_miso_dbitlen
=
(
c_len
*
8
)
-
1
;
if
(
data
){
for
(
int
i
=
0
;
i
<
c_longs
;
i
++
)
{
spi
->
dev
->
data_buf
[
i
]
=
data
[
i
];
}
}
else
{
for
(
int
i
=
0
;
i
<
c_longs
;
i
++
)
{
spi
->
dev
->
data_buf
[
i
]
=
0xFFFFFFFF
;
}
}
spi
->
dev
->
cmd
.
usr
=
1
;
while
(
spi
->
dev
->
cmd
.
usr
);
if
(
result
){
for
(
int
i
=
0
;
i
<
c_longs
;
i
++
)
{
result
[
i
]
=
spi
->
dev
->
data_buf
[
i
];
}
}
if
(
data
){
data
+=
c_longs
;
}
if
(
result
){
result
+=
c_longs
;
}
longs
-=
c_longs
;
len
-=
c_len
;
}
}
void
spiTransferBitsNL
(
spi_t
*
spi
,
uint32_t
data
,
uint32_t
*
out
,
uint8_t
bits
)
{
if
(
!
spi
)
{
return
;
}
if
(
bits
>
32
)
{
bits
=
32
;
}
uint32_t
bytes
=
(
bits
+
7
)
/
8
;
//64 max
uint32_t
mask
=
(((
uint64_t
)
1
<<
bits
)
-
1
)
&
0xFFFFFFFF
;
data
=
data
&
mask
;
if
(
!
spi
->
dev
->
ctrl
.
wr_bit_order
){
if
(
bytes
==
2
)
{
MSB_16_SET
(
data
,
data
);
}
else
if
(
bytes
==
3
)
{
MSB_24_SET
(
data
,
data
);
}
else
{
MSB_32_SET
(
data
,
data
);
}
}
spi
->
dev
->
mosi_dlen
.
usr_mosi_dbitlen
=
(
bits
-
1
);
spi
->
dev
->
miso_dlen
.
usr_miso_dbitlen
=
(
bits
-
1
);
spi
->
dev
->
data_buf
[
0
]
=
data
;
spi
->
dev
->
cmd
.
usr
=
1
;
while
(
spi
->
dev
->
cmd
.
usr
);
data
=
spi
->
dev
->
data_buf
[
0
];
if
(
out
)
{
*
out
=
data
;
if
(
!
spi
->
dev
->
ctrl
.
rd_bit_order
){
if
(
bytes
==
2
)
{
MSB_16_SET
(
*
out
,
data
);
}
else
if
(
bytes
==
3
)
{
MSB_24_SET
(
*
out
,
data
);
}
else
{
MSB_32_SET
(
*
out
,
data
);
}
}
}
}
void
spiWritePixelsNL
(
spi_t
*
spi
,
const
void
*
data_in
,
size_t
len
){
size_t
longs
=
len
>>
2
;
if
(
len
&
3
){
longs
++
;
}
bool
msb
=
!
spi
->
dev
->
ctrl
.
wr_bit_order
;
uint32_t
*
data
=
(
uint32_t
*
)
data_in
;
size_t
c_len
=
0
,
c_longs
=
0
,
l_bytes
=
0
;
while
(
len
){
c_len
=
(
len
>
64
)
?
64
:
len
;
c_longs
=
(
longs
>
16
)
?
16
:
longs
;
l_bytes
=
(
c_len
&
3
);
spi
->
dev
->
mosi_dlen
.
usr_mosi_dbitlen
=
(
c_len
*
8
)
-
1
;
spi
->
dev
->
miso_dlen
.
usr_miso_dbitlen
=
0
;
for
(
int
i
=
0
;
i
<
c_longs
;
i
++
)
{
if
(
msb
){
if
(
l_bytes
&&
i
==
(
c_longs
-
1
)){
if
(
l_bytes
==
2
){
MSB_16_SET
(
spi
->
dev
->
data_buf
[
i
],
data
[
i
]);
}
else
{
spi
->
dev
->
data_buf
[
i
]
=
data
[
i
]
&
0xFF
;
}
}
else
{
MSB_32_SET
(
spi
->
dev
->
data_buf
[
i
],
data
[
i
]);
}
}
else
{
spi
->
dev
->
data_buf
[
i
]
=
data
[
i
];
}
}
spi
->
dev
->
cmd
.
usr
=
1
;
while
(
spi
->
dev
->
cmd
.
usr
);
data
+=
c_longs
;
longs
-=
c_longs
;
len
-=
c_len
;
}
}
/*
* Clock Calculators
*
* */
typedef
union
{
uint32_t
regValue
;
...
...
@@ -795,5 +1068,3 @@ uint32_t spiFrequencyToClockDiv(uint32_t freq)
return
bestReg
.
regValue
;
}
cores/esp32/esp32-hal-spi.h
浏览文件 @
d0232d12
...
...
@@ -85,12 +85,15 @@ void spiSSClear(spi_t * spi);
void
spiWaitReady
(
spi_t
*
spi
);
uint32_t
spiGetClockDiv
(
spi_t
*
spi
);
void
spiSetClockDiv
(
spi_t
*
spi
,
uint32_t
clockDiv
);
uint8_t
spiGetDataMode
(
spi_t
*
spi
);
void
spiSetDataMode
(
spi_t
*
spi
,
uint8_t
dataMode
);
uint8_t
spiGetBitOrder
(
spi_t
*
spi
);
/*
* Non transaction based lock methods (each locks and unlocks when called)
* */
void
spiSetClockDiv
(
spi_t
*
spi
,
uint32_t
clockDiv
);
void
spiSetDataMode
(
spi_t
*
spi
,
uint8_t
dataMode
);
void
spiSetBitOrder
(
spi_t
*
spi
,
uint8_t
bitOrder
);
void
spiWrite
(
spi_t
*
spi
,
uint32_t
*
data
,
uint8_t
len
);
...
...
@@ -102,10 +105,32 @@ void spiTransfer(spi_t * spi, uint32_t *out, uint8_t len);
uint8_t
spiTransferByte
(
spi_t
*
spi
,
uint8_t
data
);
uint16_t
spiTransferWord
(
spi_t
*
spi
,
uint16_t
data
);
uint32_t
spiTransferLong
(
spi_t
*
spi
,
uint32_t
data
);
void
spiTransferBits
(
spi_t
*
spi
,
uint32_t
data
,
uint32_t
*
out
,
uint8_t
bits
);
void
spiTransferBytes
(
spi_t
*
spi
,
uint8_t
*
data
,
uint8_t
*
out
,
uint32_t
size
);
void
spiTransferBits
(
spi_t
*
spi
,
uint32_t
data
,
uint32_t
*
out
,
uint8_t
bits
);
/*
* New (EXPERIMENTAL) Transaction lock based API (lock once until endTransaction)
* */
void
spiTransaction
(
spi_t
*
spi
,
uint32_t
clockDiv
,
uint8_t
dataMode
,
uint8_t
bitOrder
);
void
spiSimpleTransaction
(
spi_t
*
spi
);
void
spiEndTransaction
(
spi_t
*
spi
);
void
spiWriteNL
(
spi_t
*
spi
,
const
void
*
data
,
uint32_t
len
);
void
spiWriteByteNL
(
spi_t
*
spi
,
uint8_t
data
);
void
spiWriteShortNL
(
spi_t
*
spi
,
uint16_t
data
);
void
spiWriteLongNL
(
spi_t
*
spi
,
uint32_t
data
);
void
spiWritePixelsNL
(
spi_t
*
spi
,
const
void
*
data
,
uint32_t
len
);
#define spiTransferNL(spi, data, len) spiTransferBytesNL(spi, data, data, len)
uint8_t
spiTransferByteNL
(
spi_t
*
spi
,
uint8_t
data
);
uint16_t
spiTransferShortNL
(
spi_t
*
spi
,
uint16_t
data
);
uint32_t
spiTransferLongNL
(
spi_t
*
spi
,
uint32_t
data
);
void
spiTransferBytesNL
(
spi_t
*
spi
,
const
void
*
data_in
,
uint8_t
*
data_out
,
uint32_t
len
);
void
spiTransferBitsNL
(
spi_t
*
spi
,
uint32_t
data_in
,
uint32_t
*
data_out
,
uint8_t
bits
);
/*
* Helper functions to translate frequency to clock divider and back
* */
uint32_t
spiFrequencyToClockDiv
(
uint32_t
freq
);
uint32_t
spiClockDivToFrequency
(
uint32_t
freq
);
...
...
libraries/SPI/src/SPI.cpp
浏览文件 @
d0232d12
...
...
@@ -31,6 +31,7 @@ SPIClass::SPIClass(uint8_t spi_bus)
,
_ss
(
-
1
)
,
_div
(
0
)
,
_freq
(
1000000
)
,
_inTransaction
(
false
)
{}
void
SPIClass
::
begin
(
int8_t
sck
,
int8_t
miso
,
int8_t
mosi
,
int8_t
ss
)
...
...
@@ -84,16 +85,6 @@ void SPIClass::setHwCs(bool use)
_use_hw_ss
=
use
;
}
void
SPIClass
::
beginTransaction
(
SPISettings
settings
)
{
spiWaitReady
(
_spi
);
setFrequency
(
settings
.
_clock
);
setBitOrder
(
settings
.
_bitOrder
);
setDataMode
(
settings
.
_dataMode
);
}
void
SPIClass
::
endTransaction
()
{}
void
SPIClass
::
setFrequency
(
uint32_t
freq
)
{
//check if last freq changed
...
...
@@ -121,38 +112,79 @@ void SPIClass::setBitOrder(uint8_t bitOrder)
spiSetBitOrder
(
_spi
,
bitOrder
);
}
void
SPIClass
::
beginTransaction
(
SPISettings
settings
)
{
//check if last freq changed
uint32_t
cdiv
=
spiGetClockDiv
(
_spi
);
if
(
_freq
!=
settings
.
_clock
||
_div
!=
cdiv
)
{
_freq
=
settings
.
_clock
;
_div
=
spiFrequencyToClockDiv
(
_freq
);
}
spiTransaction
(
_spi
,
_div
,
settings
.
_dataMode
,
settings
.
_bitOrder
);
_inTransaction
=
true
;
}
void
SPIClass
::
endTransaction
()
{
if
(
_inTransaction
){
spiEndTransaction
(
_spi
);
_inTransaction
=
false
;
}
}
void
SPIClass
::
write
(
uint8_t
data
)
{
if
(
_inTransaction
){
return
spiWriteByteNL
(
_spi
,
data
);
}
spiWriteByte
(
_spi
,
data
);
}
uint8_t
SPIClass
::
transfer
(
uint8_t
data
)
{
if
(
_inTransaction
){
return
spiTransferByteNL
(
_spi
,
data
);
}
return
spiTransferByte
(
_spi
,
data
);
}
void
SPIClass
::
write16
(
uint16_t
data
)
{
if
(
_inTransaction
){
return
spiWriteShortNL
(
_spi
,
data
);
}
spiWriteWord
(
_spi
,
data
);
}
uint16_t
SPIClass
::
transfer16
(
uint16_t
data
)
{
if
(
_inTransaction
){
return
spiTransferShortNL
(
_spi
,
data
);
}
return
spiTransferWord
(
_spi
,
data
);
}
void
SPIClass
::
write32
(
uint32_t
data
)
{
if
(
_inTransaction
){
return
spiWriteLongNL
(
_spi
,
data
);
}
spiWriteLong
(
_spi
,
data
);
}
uint32_t
SPIClass
::
transfer32
(
uint32_t
data
)
{
if
(
_inTransaction
){
return
spiTransferLongNL
(
_spi
,
data
);
}
return
spiTransferLong
(
_spi
,
data
);
}
void
SPIClass
::
transferBits
(
uint32_t
data
,
uint32_t
*
out
,
uint8_t
bits
)
{
if
(
_inTransaction
){
return
spiTransferBitsNL
(
_spi
,
data
,
out
,
bits
);
}
spiTransferBits
(
_spi
,
data
,
out
,
bits
);
}
...
...
@@ -165,7 +197,12 @@ void SPIClass::transferBits(uint32_t data, uint32_t * out, uint8_t bits)
*/
void
SPIClass
::
writeBytes
(
uint8_t
*
data
,
uint32_t
size
)
{
spiTransferBytes
(
_spi
,
data
,
0
,
size
);
if
(
_inTransaction
){
return
spiWriteNL
(
_spi
,
data
,
size
);
}
spiSimpleTransaction
(
_spi
);
spiWriteNL
(
_spi
,
data
,
size
);
spiEndTransaction
(
_spi
);
}
/**
...
...
@@ -175,6 +212,9 @@ void SPIClass::writeBytes(uint8_t * data, uint32_t size)
*/
void
SPIClass
::
transferBytes
(
uint8_t
*
data
,
uint8_t
*
out
,
uint32_t
size
)
{
if
(
_inTransaction
){
return
spiTransferBytesNL
(
_spi
,
data
,
out
,
size
);
}
spiTransferBytes
(
_spi
,
data
,
out
,
size
);
}
...
...
libraries/SPI/src/SPI.h
浏览文件 @
d0232d12
...
...
@@ -36,6 +36,19 @@ public:
class
SPIClass
{
private:
int8_t
_spi_num
;
spi_t
*
_spi
;
bool
_use_hw_ss
;
int8_t
_sck
;
int8_t
_miso
;
int8_t
_mosi
;
int8_t
_ss
;
uint32_t
_div
;
uint32_t
_freq
;
bool
_inTransaction
;
void
writePattern_
(
uint8_t
*
data
,
uint8_t
size
,
uint8_t
repeat
);
public:
SPIClass
(
uint8_t
spi_bus
=
HSPI
);
void
begin
(
int8_t
sck
=-
1
,
int8_t
miso
=-
1
,
int8_t
mosi
=-
1
,
int8_t
ss
=-
1
);
...
...
@@ -61,17 +74,8 @@ public:
void
write32
(
uint32_t
data
);
void
writeBytes
(
uint8_t
*
data
,
uint32_t
size
);
void
writePattern
(
uint8_t
*
data
,
uint8_t
size
,
uint32_t
repeat
);
private:
int8_t
_spi_num
;
spi_t
*
_spi
;
bool
_use_hw_ss
;
int8_t
_sck
;
int8_t
_miso
;
int8_t
_mosi
;
int8_t
_ss
;
uint32_t
_div
;
uint32_t
_freq
;
void
writePattern_
(
uint8_t
*
data
,
uint8_t
size
,
uint8_t
repeat
);
spi_t
*
bus
(){
return
_spi
;
}
};
extern
SPIClass
SPI
;
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录