From b90d14fb790708066ab51ef604c97aee7704ce91 Mon Sep 17 00:00:00 2001 From: anschrammh Date: Tue, 13 Dec 2022 08:20:01 +0100 Subject: [PATCH] Continued i2c driver by implementing read and write reg, did some tests with i2c --- src/W800 SDK v1.00.08/app/drivers/i2c/i2c.c | 44 ++++++++++++++++++++ src/W800 SDK v1.00.08/app/drivers/i2c/i2c.h | 6 +++ src/W800 SDK v1.00.08/app/gfx/gfx_task.c | 45 +++++++++++++++++++++ src/W800 SDK v1.00.08/tools/w800/inc.mk | 1 + 4 files changed, 96 insertions(+) diff --git a/src/W800 SDK v1.00.08/app/drivers/i2c/i2c.c b/src/W800 SDK v1.00.08/app/drivers/i2c/i2c.c index 6227488..19783fa 100644 --- a/src/W800 SDK v1.00.08/app/drivers/i2c/i2c.c +++ b/src/W800 SDK v1.00.08/app/drivers/i2c/i2c.c @@ -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; +} diff --git a/src/W800 SDK v1.00.08/app/drivers/i2c/i2c.h b/src/W800 SDK v1.00.08/app/drivers/i2c/i2c.h index f5830cd..2566dff 100644 --- a/src/W800 SDK v1.00.08/app/drivers/i2c/i2c.h +++ b/src/W800 SDK v1.00.08/app/drivers/i2c/i2c.h @@ -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 \ No newline at end of file diff --git a/src/W800 SDK v1.00.08/app/gfx/gfx_task.c b/src/W800 SDK v1.00.08/app/gfx/gfx_task.c index 55f552c..d746d6c 100644 --- a/src/W800 SDK v1.00.08/app/gfx/gfx_task.c +++ b/src/W800 SDK v1.00.08/app/gfx/gfx_task.c @@ -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; } diff --git a/src/W800 SDK v1.00.08/tools/w800/inc.mk b/src/W800 SDK v1.00.08/tools/w800/inc.mk index b80e198..bec1066 100644 --- a/src/W800 SDK v1.00.08/tools/w800/inc.mk +++ b/src/W800 SDK v1.00.08/tools/w800/inc.mk @@ -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