new twi0 (i2c0)

This commit is contained in:
Roy_01
2023-12-27 14:50:14 +08:00
parent c4bb2fec1f
commit 2bf247c103
4 changed files with 68 additions and 1 deletions
+5
View File
@@ -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
+3 -1
View File
@@ -174,12 +174,14 @@
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|VisualGDB'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="..\bmd380_sdk\integration\nrfx\legacy\nrf_drv_twi.c">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|VisualGDB'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|VisualGDB'">
</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="..\bmd380_sdk\integration\nrfx\legacy\nrf_drv_uart.c">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|VisualGDB'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="edc_regular_data.c" />
<ClCompile Include="i2c.c" />
<ClCompile Include="led.c" />
<ClCompile Include="le_adv.c" />
<ClCompile Include="le_dfu.c" />
+58
View File
@@ -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, &reg_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, &reg_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);
}
+2
View File
@@ -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();