Continued i2c driver by implementing read and write reg, did some tests with i2c

This commit is contained in:
anschrammh 2022-12-13 08:20:01 +01:00
parent a94ee14b2f
commit b90d14fb79
4 changed files with 96 additions and 0 deletions

View File

@ -9,3 +9,47 @@ void i2c_init(enum tls_io_name SDAPin, enum tls_io_name SCLPin, uint32_t frequen
wm_i2c_scl_config(SCLPin);
tls_i2c_init(frequency);
}
bool i2c_write(uint8_t address, const uint8_t *data, size_t length, bool sendStop)
{
if(!data)return false;
return true;
}
bool i2c_write_reg(uint8_t address, uint8_t reg, uint8_t data)
{
//First we send the address followed by the register we want to write to:
tls_i2c_write_byte(address << 1, true);
if(tls_i2c_wait_ack() != WM_SUCCESS) return false;
tls_i2c_write_byte(reg, false);
if(tls_i2c_wait_ack() != WM_SUCCESS) return false;
tls_i2c_write_byte(data, false);
if(tls_i2c_wait_ack() != WM_SUCCESS) return false;
tls_i2c_stop();
return true;
}
bool i2c_read(uint8_t address, const uint8_t *data, size_t length, bool sendStop)
{
if(!data)return false;
return true;
}
bool i2c_read_reg(uint8_t address, uint8_t reg, uint8_t * const data)
{
if(!data) return false;
//First we send the address followed by the register we want to read from:
tls_i2c_write_byte(address << 1, true);
if(tls_i2c_wait_ack() != WM_SUCCESS) return false;
tls_i2c_write_byte(reg, false);
if(tls_i2c_wait_ack() != WM_SUCCESS) return false;
tls_i2c_write_byte((address << 1) | 1, true);
if(tls_i2c_wait_ack() != WM_SUCCESS) return false;
*data = tls_i2c_read_byte(false, true);
return true;
}

View File

@ -8,5 +8,11 @@ void i2c_init(enum tls_io_name SDAPin, enum tls_io_name SCLPin, uint32_t frequen
bool i2c_write(uint8_t address, const uint8_t *data, size_t length, bool sendStop);
bool i2c_write_reg(uint8_t address, uint8_t reg, uint8_t data);
bool i2c_read(uint8_t address, const uint8_t *data, size_t length, bool sendStop);
bool i2c_read_reg(uint8_t address, uint8_t reg, uint8_t * const data);
#endif //I2C_H

View File

@ -5,6 +5,7 @@
#include "lcd.h"
#include "watch_face.h"
#include "menu_screen.h"
#include "i2c.h"
#include "lv_port_indev.h"
@ -113,7 +114,20 @@ void gfx_task(void *param)
lv_scr_load(watchFace.display);
/* Let's init the I2C interface */
i2c_init(WM_IO_PA_04, WM_IO_PA_01, 100000);
uint8_t aliveCounter = 0;
uint8_t data = 0;
/*i2c_write_reg(0x0D, 0x09, 0x01|0x0C|0x10|0X00);
i2c_write_reg(0x0D, 0x0B, 0x01);*/
i2c_read_reg(0x76, 0xF4, &data);
APP_LOG_INFO("0xF4 -> 0x%02X", data);
i2c_write_reg(0x76, 0xF4, 0b11111111);
i2c_read_reg(0x76, 0xF4, &data);
APP_LOG_INFO("0xF4 -> 0x%02X", data);
for(;;)
{
@ -122,6 +136,37 @@ void gfx_task(void *param)
if(++aliveCounter % 200 == 0)
{
if(!i2c_read_reg(0x76, 0xD0, &data))
{
APP_LOG_ERROR("Failed to read BMP280 data");
}
else
{
APP_LOG_INFO("Read BMP280 reg 0xD0 -> 0x%02X", data);
}
i2c_read_reg(0x76, 0xFC, &data);
APP_LOG_INFO("0xFC -> 0x%02X", data);
i2c_read_reg(0x76, 0xFB, &data);
APP_LOG_INFO("0xFB -> 0x%02X", data);
i2c_read_reg(0x76, 0xFA, &data);
APP_LOG_INFO("0xFA -> 0x%02X", data);
/*i2c_read_reg(0x0D, 0x00, &data);
APP_LOG_INFO("Read HMC5883L reg 0x00 -> 0x%02X", data);
i2c_read_reg(0x0D, 0x01, &data);
APP_LOG_INFO("Read HMC5883L reg 0x01 -> 0x%02X", data);
i2c_read_reg(0x0D, 0x02, &data);
APP_LOG_INFO("Read HMC5883L reg 0x02 -> 0x%02X", data);
i2c_read_reg(0x0D, 0x03, &data);
APP_LOG_INFO("Read HMC5883L reg 0x03 -> 0x%02X", data);
i2c_read_reg(0x0D, 0x04, &data);
APP_LOG_INFO("Read HMC5883L reg 0x04 -> 0x%02X", data);
i2c_read_reg(0x0D, 0x05, &data);
APP_LOG_INFO("Read HMC5883L reg 0x05 -> 0x%02X", data);*/
APP_LOG_DEBUG("GFX thread");
aliveCounter = 0;
}

View File

@ -46,6 +46,7 @@ INCLUDES += -I $(TOP_DIR)/src/app/bleapp
INCLUDES += -I $(TOP_DIR)/app/app_include
INCLUDES += -I $(TOP_DIR)/app/drivers/lcd
INCLUDES += -I $(TOP_DIR)/app/drivers/mmc_sdio
INCLUDES += -I $(TOP_DIR)/app/drivers/i2c
INCLUDES += -I $(TOP_DIR)/app/gfx
INCLUDES += -I $(TOP_DIR)/app