Skip to content
Snippets Groups Projects
Commit ffc293d6 authored by fxk8y's avatar fxk8y :spider:
Browse files

ST: Fixing I²C communication bugs

parent 99151616
No related branches found
No related tags found
No related merge requests found
......@@ -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_stop(cmd) );
i2c_master_start(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 {
......
......@@ -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);
};
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment