Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
CyanLight
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
fxk8y
CyanLight
Commits
ffc293d6
Commit
ffc293d6
authored
2 years ago
by
fxk8y
Browse files
Options
Downloads
Patches
Plain Diff
ST: Fixing I²C communication bugs
parent
99151616
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
CLC-qthing/SiliconTorch/Hardware/AS3935.cpp
+17
-57
17 additions, 57 deletions
CLC-qthing/SiliconTorch/Hardware/AS3935.cpp
CLC-qthing/SiliconTorch/Hardware/AS3935.hpp
+4
-4
4 additions, 4 deletions
CLC-qthing/SiliconTorch/Hardware/AS3935.hpp
with
21 additions
and
61 deletions
CLC-qthing/SiliconTorch/Hardware/AS3935.cpp
+
17
−
57
View file @
ffc293d6
...
...
@@ -28,13 +28,8 @@ namespace AS3935 {
AS3935
::
AS3935
(
u8
SDA
,
u8
SCL
,
u8
IRQ
,
u8
i2cAddr
,
u8
i2cChannel
,
u32
i2cFrequency
)
:
i2cAddr
(
i2cAddr
&
I2C_ADDR_MASK
),
i2cChannel
(
i2cChannel
)
{
// TODO: write ctor
ESP_LOGI
(
TAG
,
"AS3935 test init…"
);
i2c_config_t
cfg
;
cfg
.
mode
=
I2C_MODE_MASTER
;
...
...
@@ -50,11 +45,7 @@ namespace AS3935 {
ESP_LOGI
(
TAG
,
"I²C_driver_install(…): %s"
,
result
==
ESP_OK
?
"OK"
:
"FAILED!!"
);
// TODO: what to do next…?
}
...
...
@@ -64,77 +55,46 @@ namespace AS3935 {
bool
AS3935
::
i2cWriteByte
(
u8
regAddr
,
u8
value
)
{
i2c_cmd_handle_t
cmd
=
i2c_cmd_link_create
();
i2c_master_start
(
cmd
);
i2c_master_write_byte
(
cmd
,
writeAddr
(),
true
);
i2c_master_write_byte
(
cmd
,
regAddr
,
true
);
i2c_master_write_byte
(
cmd
,
value
,
true
);
i2c_master_stop
(
cmd
);
esp_err_t
err
=
i2c_master_cmd_begin
((
i2c_port_t
)
i2cChannel
,
cmd
,
1000
/
portTICK_PERIOD_MS
);
i2c_cmd_link_delete
(
cmd
);
return
err
==
ESP_OK
;
}
bool
AS3935
::
i2cReadByte
(
u8
regAddr
,
u8
*
value
)
{
if
(
true
)
{
// "normal" read routine
i2c_cmd_handle_t
cmd
=
i2c_cmd_link_create
();
ESP_ERROR_CHECK_WITHOUT_ABORT
(
i2c_master_start
(
cmd
)
);
ESP_ERROR_CHECK_WITHOUT_ABORT
(
i2c_master_write_byte
(
cmd
,
readAddr
(),
true
)
);
ESP_ERROR_CHECK_WITHOUT_ABORT
(
i2c_master_write_byte
(
cmd
,
regAddr
,
true
)
);
ESP_ERROR_CHECK_WITHOUT_ABORT
(
i2c_master_read_byte
(
cmd
,
value
,
I2C_MASTER_LAST_NACK
)
);
// I2C_MASTER_ACK
//i2c_master_write_byte(cmd, value, true);
ESP_ERROR_CHECK_WITHOUT_ABORT
(
i2c_master_stop
(
cmd
)
);
//ESP_ERROR_CHECK( i2c_master_cmd_begin((i2c_port_t)i2cChannel, cmd, 1000 / portTICK_PERIOD_MS) );
esp_err_t
err
=
i2c_master_cmd_begin
((
i2c_port_t
)
i2cChannel
,
cmd
,
1000
/
portTICK_PERIOD_MS
);
i2c_cmd_link_delete
(
cmd
);
ESP_ERROR_CHECK_WITHOUT_ABORT
(
err
);
return
err
==
ESP_OK
;
}
else
{
// "0-byte-write-then-direction-change" read routine
i2c_cmd_handle_t
cmd
=
i2c_cmd_link_create
();
ESP_ERROR_CHECK_WITHOUT_ABORT
(
i2c_master_start
(
cmd
)
);
ESP_ERROR_CHECK_WITHOUT_ABORT
(
i2c_master_write_byte
(
cmd
,
writeAddr
(),
true
)
);
ESP_ERROR_CHECK_WITHOUT_ABORT
(
i2c_master_write_byte
(
cmd
,
regAddr
,
true
)
);
ESP_ERROR_CHECK_WITHOUT_ABORT
(
i2c_master_start
(
cmd
)
);
// repeated start condition
i2c_cmd_handle_t
cmd
=
i2c_cmd_link_create
();
ESP_ERROR_CHECK_WITHOUT_ABORT
(
i2c_master_write_byte
(
cmd
,
readAddr
(),
true
)
);
i2c_master_start
(
cmd
);
ESP_ERROR_CHECK_WITHOUT_ABORT
(
i2c_master_read_byte
(
cmd
,
value
,
I2C_MASTER_LAST_NACK
)
);
// I2C_MASTER_ACK / or NACK ? (manual says we have to NACK, but who's gonna sue us we have our data lol)
i2c_master_write_byte
(
cmd
,
writeAddr
(),
true
);
i2c_master_write_byte
(
cmd
,
regAddr
,
true
);
ESP_ERROR_CHECK_WITHOUT_ABORT
(
i2c_master_st
op
(
cmd
)
);
i2c_master_st
art
(
cmd
)
;
// repeated start condition ==> direction change
//ESP_ERROR_CHECK( i2c_master_cmd_begin((i2c_port_t)i2cChannel, cmd, 1000 / portTICK_PERIOD_MS) );
esp_err_t
err
=
i2c_master_cmd_begin
((
i2c_port_t
)
i2cChannel
,
cmd
,
1000
/
portTICK_PERIOD_MS
);
i2c_cmd_link_delete
(
cmd
);
i2c_master_write_byte
(
cmd
,
readAddr
(),
true
);
i2c_master_read_byte
(
cmd
,
value
,
I2C_MASTER_LAST_NACK
);
ESP_ERROR_CHECK_WITHOUT_ABORT
(
err
);
i2c_master_stop
(
cmd
);
return
err
==
ESP_OK
;
esp_err_t
err
=
i2c_master_cmd_begin
((
i2c_port_t
)
i2cChannel
,
cmd
,
1000
/
portTICK_PERIOD_MS
);
i2c_cmd_link_delete
(
cmd
);
}
return
err
==
ESP_OK
;
}
u8
AS3935
::
readAddr
()
const
{
return
(
i2cAddr
<<
1
)
&
1
;
return
(
i2cAddr
<<
1
)
|
1
;
}
u8
AS3935
::
writeAddr
()
const
{
...
...
This diff is collapsed.
Click to expand it.
CLC-qthing/SiliconTorch/Hardware/AS3935.hpp
+
4
−
4
View file @
ffc293d6
...
...
@@ -179,16 +179,16 @@ namespace AS3935 {
u8
writeAddr
()
const
;
bool
i2cReadByte
(
u8
regAddr
,
u8
*
value
);
bool
i2cWriteByte
(
u8
regAddr
,
u8
value
);
bool
i2cReadByte
(
u8
regAddr
,
u8
*
value
);
// TMP public for debug
bool
i2cWriteByte
(
u8
regAddr
,
u8
value
);
// TMP public for debug
protected:
const
u8
i2cAddr
;
const
u8
i2cChannel
;
//bool i2cReadByte (u8 regAddr, u8* value);
//bool i2cWriteByte(u8 regAddr, u8 value);
//
bool i2cReadByte (u8 regAddr, u8* value);
//
bool i2cWriteByte(u8 regAddr, u8 value);
};
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment