59 lines
1.8 KiB
C
59 lines
1.8 KiB
C
#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);
|
|
}
|