Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
weixin_43676597
arduino-esp32
提交
cc3c55bb
A
arduino-esp32
项目概览
weixin_43676597
/
arduino-esp32
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
1
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,发现更多精彩内容 >>
提交
cc3c55bb
编写于
10月 08, 2016
作者:
M
me-no-dev
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix i2c hal
上级
917286ac
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
86 addition
and
26 deletion
+86
-26
cores/esp32/esp32-hal-i2c.c
cores/esp32/esp32-hal-i2c.c
+86
-26
未找到文件。
cores/esp32/esp32-hal-i2c.c
浏览文件 @
cc3c55bb
...
...
@@ -63,10 +63,10 @@ enum {
/*
* index - command index (0 to 15)
* op_code - is the command
* byte_num - This register is to store the amounts of data that is read and written. byte_num in RSTART, STOP, END is null.
* ack_val - Each data byte is terminated by an ACK bit used to set the bit level.
* ack_exp - This bit is to set an expected ACK value for the transmitter.
* ack_check - This bit is to decide whether the transmitter checks ACK bit. 1 means yes and 0 means no.
* byte_num - This register is to store the amounts of data that is read and written. byte_num in RSTART, STOP, END is null.
* */
void
i2cSetCmd
(
i2c_t
*
i2c
,
uint8_t
index
,
uint8_t
op_code
,
uint8_t
byte_num
,
bool
ack_val
,
bool
ack_exp
,
bool
ack_check
)
{
...
...
@@ -80,6 +80,7 @@ void i2cSetCmd(i2c_t * i2c, uint8_t index, uint8_t op_code, uint8_t byte_num, bo
int
i2cWrite
(
i2c_t
*
i2c
,
uint16_t
address
,
bool
addr_10bit
,
uint8_t
*
data
,
uint8_t
len
,
bool
sendStop
)
{
int
i
;
uint8_t
index
=
0
;
uint8_t
dataLen
=
len
+
(
addr_10bit
?
2
:
1
);
address
=
(
address
<<
1
);
...
...
@@ -87,8 +88,11 @@ int i2cWrite(i2c_t * i2c, uint16_t address, bool addr_10bit, uint8_t * data, uin
while
(
dataLen
)
{
uint8_t
willSend
=
(
dataLen
>
32
)
?
32
:
dataLen
;
uint8_t
dataSend
=
willSend
;
i2cSetCmd
(
i2c
,
0
,
I2C_CMD_RSTART
,
0
,
false
,
false
,
false
);
//START
i2cSetCmd
(
i2c
,
1
,
I2C_CMD_WRITE
,
willSend
,
false
,
false
,
true
);
//CMD START
i2cSetCmd
(
i2c
,
0
,
I2C_CMD_RSTART
,
0
,
false
,
false
,
false
);
//CMD WRITE(ADDRESS + DATA)
if
(
!
index
)
{
i2c
->
dev
->
fifo_data
.
data
=
address
&
0xFF
;
dataSend
--
;
...
...
@@ -97,27 +101,56 @@ int i2cWrite(i2c_t * i2c, uint16_t address, bool addr_10bit, uint8_t * data, uin
dataSend
--
;
}
}
i
nt
i
=
0
;
i
=
0
;
while
(
i
<
dataSend
)
{
i
++
;
i2c
->
dev
->
fifo_data
.
data
=
data
[
index
++
];
}
i2cSetCmd
(
i2c
,
1
,
I2C_CMD_WRITE
,
willSend
,
false
,
false
,
true
);
dataLen
-=
willSend
;
//CMD STOP or CMD END if there is more data
if
(
dataLen
)
{
i2cSetCmd
(
i2c
,
2
,
I2C_CMD_END
,
0
,
false
,
false
,
false
);
}
else
if
(
sendStop
)
{
i2cSetCmd
(
i2c
,
2
,
I2C_CMD_STOP
,
0
,
false
,
false
,
tru
e
);
i2cSetCmd
(
i2c
,
2
,
I2C_CMD_STOP
,
0
,
false
,
false
,
fals
e
);
}
//Clear Interrupts
i2c
->
dev
->
int_clr
.
val
=
0xFFFFFFFF
;
//START Transmission
i2c
->
dev
->
ctr
.
trans_start
=
1
;
while
(
i2c
->
dev
->
ctr
.
trans_start
||
i2c
->
dev
->
status_reg
.
bus_busy
||
(
!
i2c
->
dev
->
int_raw
.
ack_err
&&
!
i2c
->
dev
->
command
[
2
].
done
));
if
(
!
i2c
->
dev
->
command
[
2
].
done
)
{
log_e
(
"Ack Error"
);
return
-
1
;
//WAIT Transmission
while
(
1
)
{
if
(
i2c
->
dev
->
ctr
.
trans_start
||
i2c
->
dev
->
status_reg
.
bus_busy
||
!
(
i2c
->
dev
->
int_raw
.
trans_complete
)
||
!
(
i2c
->
dev
->
command
[
2
].
done
))
{
continue
;
}
else
if
(
i2c
->
dev
->
int_raw
.
ack_err
||
i2c
->
dev
->
int_raw
.
time_out
||
i2c
->
dev
->
int_raw
.
arbitration_lost
)
{
break
;
}
else
if
(
i2c
->
dev
->
command
[
2
].
done
)
{
break
;
}
}
if
(
i2c
->
dev
->
status_reg
.
arb_lost
||
i2c
->
dev
->
status_reg
.
time_out
)
{
log_e
(
"Bus Fail"
);
return
-
1
;
//Bus failed (maybe check for this while waiting?
if
(
i2c
->
dev
->
int_raw
.
arbitration_lost
)
{
//log_e("Bus Fail! Addr: %x", address >> 1);
return
4
;
}
//Bus timeout
if
(
i2c
->
dev
->
int_raw
.
time_out
)
{
//log_e("Bus Timeout! Addr: %x", address >> 1);
return
3
;
}
//Transmission did not finish and ACK_ERR is set
if
(
i2c
->
dev
->
int_raw
.
ack_err
)
{
//log_e("Ack Error! Addr: %x", address >> 1);
return
1
;
}
}
return
0
;
}
...
...
@@ -130,12 +163,16 @@ int i2cRead(i2c_t * i2c, uint16_t address, bool addr_10bit, uint8_t * data, uint
uint8_t
cmdIdx
;
uint8_t
willRead
;
i2cSetCmd
(
i2c
,
0
,
I2C_CMD_RSTART
,
0
,
false
,
false
,
false
);
//START
i2cSetCmd
(
i2c
,
1
,
I2C_CMD_WRITE
,
addrLen
,
false
,
false
,
true
);
//CMD START
i2cSetCmd
(
i2c
,
0
,
I2C_CMD_RSTART
,
0
,
false
,
false
,
false
);
//CMD WRITE ADDRESS
i2c
->
dev
->
fifo_data
.
data
=
address
&
0xFF
;
if
(
addr_10bit
)
{
i2c
->
dev
->
fifo_data
.
data
=
(
address
>>
8
)
&
0xFF
;
}
i2cSetCmd
(
i2c
,
1
,
I2C_CMD_WRITE
,
addrLen
,
false
,
false
,
true
);
while
(
len
)
{
cmdIdx
=
(
index
)
?
0
:
2
;
willRead
=
(
len
>
32
)
?
32
:
(
len
-
1
);
...
...
@@ -150,14 +187,38 @@ int i2cRead(i2c_t * i2c, uint16_t address, bool addr_10bit, uint8_t * data, uint
}
}
//Clear Interrupts
i2c
->
dev
->
int_clr
.
val
=
0xFFFFFFFF
;
//START Transmission
i2c
->
dev
->
ctr
.
trans_start
=
1
;
while
(
i2c
->
dev
->
ctr
.
trans_start
||
i2c
->
dev
->
status_reg
.
bus_busy
||
(
!
i2c
->
dev
->
int_raw
.
ack_err
&&
!
i2c
->
dev
->
command
[
cmdIdx
-
1
].
done
));
if
(
!
i2c
->
dev
->
command
[
cmdIdx
-
1
].
done
)
{
log_e
(
"Ack Error"
);
return
-
1
;
//WAIT Transmission
while
(
1
)
{
if
(
i2c
->
dev
->
ctr
.
trans_start
||
i2c
->
dev
->
status_reg
.
bus_busy
||
!
(
i2c
->
dev
->
int_raw
.
trans_complete
)
||
!
(
i2c
->
dev
->
command
[
cmdIdx
-
1
].
done
))
{
continue
;
}
else
if
(
i2c
->
dev
->
int_raw
.
ack_err
||
i2c
->
dev
->
int_raw
.
time_out
||
i2c
->
dev
->
int_raw
.
arbitration_lost
)
{
break
;
}
else
if
(
i2c
->
dev
->
command
[
cmdIdx
-
1
].
done
)
{
break
;
}
}
//Bus failed (maybe check for this while waiting?
if
(
i2c
->
dev
->
int_raw
.
arbitration_lost
)
{
//log_e("Bus Fail! Addr: %x", address >> 1);
return
-
4
;
}
//Bus timeout
if
(
i2c
->
dev
->
int_raw
.
time_out
)
{
//log_e("Bus Timeout! Addr: %x", address >> 1);
return
-
3
;
}
if
(
i2c
->
dev
->
status_reg
.
arb_lost
||
i2c
->
dev
->
status_reg
.
time_out
)
{
log_e
(
"Bus Fail"
);
//Transmission did not finish and ACK_ERR is set
if
(
i2c
->
dev
->
int_raw
.
ack_err
)
{
//log_e("Ack Error! Addr: %x", address >> 1);
return
-
1
;
}
int
i
=
0
;
...
...
@@ -192,8 +253,8 @@ void i2cSetFrequency(i2c_t * i2c, uint32_t clk_speed)
i2c
->
dev
->
scl_stop_hold
.
time
=
50
;
i2c
->
dev
->
scl_stop_setup
.
time
=
50
;
i2c
->
dev
->
sda_hold
.
time
=
40
;
i2c
->
dev
->
sda_sample
.
time
=
40
;
i2c
->
dev
->
sda_hold
.
time
=
25
;
i2c
->
dev
->
sda_sample
.
time
=
25
;
}
uint32_t
i2cGetFrequency
(
i2c_t
*
i2c
)
...
...
@@ -205,7 +266,6 @@ uint32_t i2cGetFrequency(i2c_t * i2c)
* mode - 0 = Slave, 1 = Master
* slave_addr - I2C Address
* addr_10bit_en - enable slave 10bit address mode.
* clk_speed - SCL Frequency
* */
i2c_t
*
i2cInit
(
uint8_t
i2c_num
,
uint16_t
slave_addr
,
bool
addr_10bit_en
)
...
...
@@ -226,16 +286,16 @@ i2c_t * i2cInit(uint8_t i2c_num, uint16_t slave_addr, bool addr_10bit_en)
CLEAR_PERI_REG_MASK
(
DPORT_PERIP_RST_EN_REG
,
DPORT_I2C_EXT1_RST
);
}
i2c
->
dev
->
ctr
.
rx_lsb_first
=
0
;
i2c
->
dev
->
ctr
.
tx_lsb_first
=
0
;
i2c
->
dev
->
ctr
.
val
=
0
;
i2c
->
dev
->
ctr
.
ms_mode
=
(
slave_addr
==
0
);
i2c
->
dev
->
ctr
.
sda_force_out
=
1
;
i2c
->
dev
->
ctr
.
scl_force_out
=
1
;
i2c
->
dev
->
ctr
.
sample_scl_level
=
0
;
i2c
->
dev
->
ctr
.
clk_en
=
1
;
i2c
->
dev
->
timeout
.
tout
=
2000
;
i2c
->
dev
->
fifo_conf
.
nonfifo_en
=
0
;
i2c
->
dev
->
slave_addr
.
val
=
0
;
if
(
slave_addr
)
{
i2c
->
dev
->
slave_addr
.
addr
=
slave_addr
;
i2c
->
dev
->
slave_addr
.
en_10bit
=
addr_10bit_en
;
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录