diff --git a/app_config.h b/app_config.h
index 15f94ce..423c1db 100644
--- a/app_config.h
+++ b/app_config.h
@@ -193,6 +193,11 @@ extern "C"
#define SPI_DEFAULT_CONFIG_IRQ_PRIORITY 7
#define NRFX_SPIM_MISO_PULL_CFG 3
+// Enable twi(i2c)
+#define TWI_ENABLED 1
+#define TWI0_ENABLED 1
+#define TWI0_USE_EASY_DMA 1
+
#ifdef __cplusplus
}
#endif
diff --git a/bmd380_peripheral.vcxproj b/bmd380_peripheral.vcxproj
index 7c462c2..80fda79 100644
--- a/bmd380_peripheral.vcxproj
+++ b/bmd380_peripheral.vcxproj
@@ -174,12 +174,14 @@
true
- true
+
+
true
+
diff --git a/i2c.c b/i2c.c
new file mode 100644
index 0000000..1df8c0a
--- /dev/null
+++ b/i2c.c
@@ -0,0 +1,58 @@
+#include "nrf_gpio.h"
+#include "nrf_log.h"
+#include "nrf_drv_twi.h"
+
+#define I2C_SDA NRF_GPIO_PIN_MAP(0, 11)
+#define I2C_SCL NRF_GPIO_PIN_MAP(0, 22)
+
+static const nrf_drv_twi_t twi0 = NRF_DRV_TWI_INSTANCE(0);
+
+static void virtual_data(uint8_t *p_rx_buf, uint8_t rx_buffer_length)
+{
+ uint8_t virtual_data_buff[20] = {9,8,7,6,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
+ if (rx_buffer_length == 0)
+ return;
+
+ memcpy(p_rx_buf, virtual_data_buff, rx_buffer_length);
+}
+
+void twi0_init(void)
+{
+ ret_code_t err_code;
+
+ const nrf_drv_twi_config_t twi0_config = {
+ .scl = I2C_SCL,
+ .sda = I2C_SDA,
+ .frequency = NRF_DRV_TWI_FREQ_100K,
+ .interrupt_priority = APP_IRQ_PRIORITY_HIGH,
+ .clear_bus_init = false
+ };
+
+ err_code = nrf_drv_twi_init(&twi0, &twi0_config, NULL, NULL);
+ APP_ERROR_CHECK(err_code);
+
+ nrf_drv_twi_enable(&twi0);
+}
+
+void twi0_write_reg(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data, uint8_t data_len)
+{
+ uint8_t i2c_buf[data_len+1]; //reg_addr + data
+
+ memcpy(i2c_buf, ®_addr, 1);
+ memcpy(i2c_buf+1, data, data_len+1);
+ APP_ERROR_CHECK(nrf_drv_twi_tx(&twi0, slave_addr, i2c_buf, sizeof(i2c_buf), false));
+
+ NRF_LOG_INFO("i2c(W): slave_addr=0x%02x", slave_addr);
+ NRF_LOG_HEXDUMP_INFO(i2c_buf, sizeof(i2c_buf));
+}
+
+void twi0_read_reg(uint8_t slave_addr, uint8_t reg_addr, uint8_t *p_rx_buf, uint8_t rx_buffer_length)
+{
+ APP_ERROR_CHECK(nrf_drv_twi_tx(&twi0, slave_addr, ®_addr, 1, false));
+ APP_ERROR_CHECK(nrf_drv_twi_rx(&twi0, slave_addr, p_rx_buf, rx_buffer_length));
+
+ // virtual_data(p_rx_buf, rx_buffer_length);
+
+ NRF_LOG_INFO("i2c(R): slave_addr=0x%02x reg_addr=0x%02x", slave_addr, reg_addr);
+ NRF_LOG_HEXDUMP_INFO(p_rx_buf, rx_buffer_length);
+}
diff --git a/main.c b/main.c
index 150f589..c27f579 100644
--- a/main.c
+++ b/main.c
@@ -190,6 +190,7 @@ static void enable_6994_callback(void *pvParameter)
NRF_LOG_INFO("enable 6994");
}
+extern void twi0_init(void);
extern void spi_init(void);
int main(void)
{
@@ -197,6 +198,7 @@ int main(void)
NRF_LOG_DEFAULT_BACKENDS_INIT();
NRF_LOG_INFO("%s Build: %s %s", ELITE_DEVICE_NAME, __TIME__, __DATE__);
+ twi0_init();
spi_init();
gpio_init();
led_init();