Análisis de TWI desarrollado por nRF52832

TWI de nRF52832 (interfaz de dos hilos, también conocida como hardware i2c):

1. reserva de conocimiento de comunicación i2c

La comunicación 1.i2c requiere dos líneas, sda y scl. Los roles de comunicación I2C se dividen en: maestro y esclavo
2. Cuando el bus de comunicación está inactivo, scl y sda están en estado alto
3. Durante el proceso de comunicación i2c, hay dos conjuntos de señales de control:
1) inicio:
sda cuando scl es alto De nivel alto a nivel bajo
2) parada: cuando
scl es nivel alto, sda cambia de nivel bajo a nivel alto

Segundo, análisis de programación.

Versión del SDK: nRF5_SDK_15.2.0_9412b96

//1.twi初始化配置
//头文件:nrf_drv_twi.h
//sdk_config.h中开启twi功能
#define ARDUINO_SCL_PIN 7
#define ARDUINO_SDA_PIN 8
#define TWI_IMU_INSTANCE_ID 1 //使用TWI1
#define IMU_ADDR (0x78>>1) //IMU地址
static const nrf_drv_twi_t m_twi = NRF_DRV_TWI_INSTANCE(TWI_IMU_INSTANCE_ID) //twi实例
static volatile bool m_xfer_done = false; //指示twi的操作是否结束

//twi回调函数,主要是用来上报twi操作完成功能
static void twi_handler(nrf_drv_twi_evt_t const *p_event,void *p_context)
{
	switch(p_event->type)
	{
		case NRF_DRV_TWI_EVT_DONE:
			if(p_event->xfer_desc.type == NRF_DRV_TWI_XFER_TX)
			{
				NRF_LOG_INFO("twi tx donw!");
			}
			else if(p_event->xfer_desc.type == NRF_DRV_TWI_XFER_RX)
			{
				NRF_LOG_INFO("twi rx donw!");
			}
			m_xfer_done = true;
			break;
		case NRF_DRV_TWI_EVT_ADDRESS_NACK:
			NRF_LOG_INFO("Error event: NACK received after sending the address.");
			break;
		case NRF_DRV_TWI_EVT_DATA_NACK:
			NRF_LOG_INFO(" Error event: NACK received after sending a data byte.");
			break;
		default:
			break;
	}
}

static void twi_config(void)
{
	uint32_t err_code;
	nrf_drv_twi_config_t const config = {
	.scl = ARDUINO_SCL_PIN, //时钟gpio引脚
	.sda = ARDUINO_SDA_PIN, //数据gpio引脚
	.frequency = NRF_TWI_FREQ_100K, //twi时钟频率
	.interrupt_priority = APP_IRQ_PRIORITY_LOWEST, //中断优先级
	.clear_bus_init = false //初始化期间清除总线
	}
	//初始化twi
	err_code = nrf_drv_twi_init(&m_twi,&config,twi_handler,NULL);
	APP_ERROR_CHECK(err_code);
	//使能twi
	nrf_drv_twi_enable(&m_twi);
}

. 2 // leídos desde el dispositivo
. Comentario 1)
ret_code_t nrf_drv_twi_tx (const * p_instance nrf_drv_twi_t, dirección uint8_t, uint8_t const * P_DATA, longitud uint8_t, BOOL no_stop);
p_instance: TWI puntero apunta ejemplo a
dirección: la dirección del esclavo
p_data: para enviar datos Buffer
longitud: número de bytes de datos para ser enviados
no_stop: no bit de parada se establece
2) leer
ret_code_t nrf_drv_twi_rx (const * p_instance nrf_drv_twi_t, dirección uint8_t, uint8_t const * P_DATA, longitud uint8_t);
p_instance: puntero apunta ejemplo TWI a
dirección : Dirección del dispositivo esclavo
p_data: leer la
longitud del búfer de almacenamiento de datos : leer la longitud del byte de datos

Publicado 81 artículos originales · 21 elogios · 30,000+ visitas

Supongo que te gusta

Origin blog.csdn.net/qq_33575901/article/details/90044495
Recomendado
Clasificación