Porting i2c driver to Zephyr

I would like porting i2c driver from RF5_SDK_16 to Zephyr. Does Zephyr provide functions related to the following RF5_SDK_16 functions?

nrf_drv_twi_init()
nrf_drv_twi_enable()
nrf_drv_twi_uninit()
nrf_drv_twi_tx()

Does any sample code for nRF52832 i2c?

Parents
  • #include "i2c.h"
    #include "nrf_drv_twi.h"
    #include "app_error.h"
    
    #include "nrf.h"
    #include "bsp.h"
    #include "app_util_platform.h"
    
    #include "nrf_delay.h"
    
    #include <string.h>
    
    static uint8_t slave_addr;
    
    //static const nrf_drv_twi_t m_twi_master = NRF_DRV_TWI_INSTANCE(1);
    static const nrf_drv_twi_t m_twi_master = NRF_DRV_TWI_INSTANCE(1);
    static bool _gI2CInitialzed = false;
    
    ret_code_t I2C_Init(void)
    {
    	ret_code_t ret;
    	const nrf_drv_twi_config_t config =
    	{
    		.scl                = TWI_SCL_M,
    		.sda                = TWI_SDA_M,
    		.frequency          = (nrf_drv_twi_frequency_t) NRF_TWI_FREQ_400K,
    		.interrupt_priority = APP_IRQ_PRIORITY_HIGH
    	};
    	
    	//nrf_drv_twi_uninit(&m_twi_master);
    
    	do
    	{
    		ret = nrf_drv_twi_init(&m_twi_master,&config,NULL,NULL);
    		if(NRF_SUCCESS != ret)
    		{
    			break;
    		}
    		nrf_drv_twi_enable(&m_twi_master);
    	}while(0);
        _gI2CInitialzed = true;
    	return ret;
    }
    
    void I2C_Uninit(void)
    {
    	//nrf_drv_twi_disable(&m_twi_master);
    	if (_gI2CInitialzed) {
    	    nrf_drv_twi_uninit(&m_twi_master);
            _gI2CInitialzed = false;
        }    
    	
    	//nrf_gpio_pin_set(TWI_SCL_M);
    	//nrf_gpio_pin_set(TWI_SDA_M);
    	
    	//nrf_gpio_cfg_input(TWI_SCL_M,NRF_GPIO_PIN_PULLDOWN);
    	//nrf_gpio_cfg_input(TWI_SDA_M,NRF_GPIO_PIN_PULLDOWN);
    }
    
    void I2C_SlaveAddr(uint8_t addr)
    {
    	slave_addr = addr;
    }
    
    ret_code_t I2C_WriteReg(uint8_t reg,uint8_t data)
    {
    	static ret_code_t ret;
    	uint8_t buff[2];
    	//static ret_code_t ret;
    	//uint8_t data = 0;
    	
    	buff[0] = reg;
    	buff[1] = data;
    	
    	ret = nrf_drv_twi_tx(&m_twi_master, slave_addr, buff, sizeof(buff), false);
    	
    	return ret;
    }
    
    ret_code_t I2C_WriteData(uint8_t reg,uint8_t *data,int len)
    {
    	static ret_code_t ret;
    	uint8_t buff[256];
    	//uint8_t data = 0;
    	
    	//nrf_delay_ms(20);
    	
    	//buff = malloc(len+1);
    	buff[0] = reg;
    	memcpy(&buff[1],data,len);
    	
    	ret = nrf_drv_twi_tx(&m_twi_master, slave_addr, buff, len+1, false);
    	
    	//free(buff);
    	return ret;
    }
    
    
    ret_code_t I2C_ReadBatchData(uint8_t reg,uint8_t *data,int len)
    {
    	static ret_code_t ret;
    	//uint8_t data = 0;
    	
    	//nrf_delay_ms(7);
    	
    	ret = nrf_drv_twi_tx(&m_twi_master, slave_addr, &reg, 1, true);
    	if(NRF_SUCCESS != ret)
    		return ret;
    	
    	ret = nrf_drv_twi_rx(&m_twi_master, slave_addr, data,len);
    	
    	return ret;
    }
    
    ret_code_t I2C_ReadData(uint8_t reg,uint8_t *data,int len)
    {
    	static ret_code_t ret;
    	//uint8_t data = 0;
    	
    	//nrf_delay_ms(20);
    	
    	ret = nrf_drv_twi_tx(&m_twi_master, slave_addr, &reg, 1, true);
    	if(NRF_SUCCESS != ret)
    		return ret;
    	
    	ret = nrf_drv_twi_rx(&m_twi_master, slave_addr, data,len);
    	
    	return ret;
    }
    
    
    uint8_t I2C_ReadReg(uint8_t reg)
    {
    	static ret_code_t ret;
    	uint8_t data = 0xFD;
    	
    	//NRF_LOG("I2C_ReadReg\r\n");
    	ret = nrf_drv_twi_tx(&m_twi_master, slave_addr, &reg, 1, true);
    	//NRF_LOG("I2C_ReadReg End\r\n");
    	if(NRF_SUCCESS != ret)
    		return data;
    	ret = nrf_drv_twi_rx(&m_twi_master, slave_addr, &data,1);
    	
    	return data;
    }
    

Reply
  • #include "i2c.h"
    #include "nrf_drv_twi.h"
    #include "app_error.h"
    
    #include "nrf.h"
    #include "bsp.h"
    #include "app_util_platform.h"
    
    #include "nrf_delay.h"
    
    #include <string.h>
    
    static uint8_t slave_addr;
    
    //static const nrf_drv_twi_t m_twi_master = NRF_DRV_TWI_INSTANCE(1);
    static const nrf_drv_twi_t m_twi_master = NRF_DRV_TWI_INSTANCE(1);
    static bool _gI2CInitialzed = false;
    
    ret_code_t I2C_Init(void)
    {
    	ret_code_t ret;
    	const nrf_drv_twi_config_t config =
    	{
    		.scl                = TWI_SCL_M,
    		.sda                = TWI_SDA_M,
    		.frequency          = (nrf_drv_twi_frequency_t) NRF_TWI_FREQ_400K,
    		.interrupt_priority = APP_IRQ_PRIORITY_HIGH
    	};
    	
    	//nrf_drv_twi_uninit(&m_twi_master);
    
    	do
    	{
    		ret = nrf_drv_twi_init(&m_twi_master,&config,NULL,NULL);
    		if(NRF_SUCCESS != ret)
    		{
    			break;
    		}
    		nrf_drv_twi_enable(&m_twi_master);
    	}while(0);
        _gI2CInitialzed = true;
    	return ret;
    }
    
    void I2C_Uninit(void)
    {
    	//nrf_drv_twi_disable(&m_twi_master);
    	if (_gI2CInitialzed) {
    	    nrf_drv_twi_uninit(&m_twi_master);
            _gI2CInitialzed = false;
        }    
    	
    	//nrf_gpio_pin_set(TWI_SCL_M);
    	//nrf_gpio_pin_set(TWI_SDA_M);
    	
    	//nrf_gpio_cfg_input(TWI_SCL_M,NRF_GPIO_PIN_PULLDOWN);
    	//nrf_gpio_cfg_input(TWI_SDA_M,NRF_GPIO_PIN_PULLDOWN);
    }
    
    void I2C_SlaveAddr(uint8_t addr)
    {
    	slave_addr = addr;
    }
    
    ret_code_t I2C_WriteReg(uint8_t reg,uint8_t data)
    {
    	static ret_code_t ret;
    	uint8_t buff[2];
    	//static ret_code_t ret;
    	//uint8_t data = 0;
    	
    	buff[0] = reg;
    	buff[1] = data;
    	
    	ret = nrf_drv_twi_tx(&m_twi_master, slave_addr, buff, sizeof(buff), false);
    	
    	return ret;
    }
    
    ret_code_t I2C_WriteData(uint8_t reg,uint8_t *data,int len)
    {
    	static ret_code_t ret;
    	uint8_t buff[256];
    	//uint8_t data = 0;
    	
    	//nrf_delay_ms(20);
    	
    	//buff = malloc(len+1);
    	buff[0] = reg;
    	memcpy(&buff[1],data,len);
    	
    	ret = nrf_drv_twi_tx(&m_twi_master, slave_addr, buff, len+1, false);
    	
    	//free(buff);
    	return ret;
    }
    
    
    ret_code_t I2C_ReadBatchData(uint8_t reg,uint8_t *data,int len)
    {
    	static ret_code_t ret;
    	//uint8_t data = 0;
    	
    	//nrf_delay_ms(7);
    	
    	ret = nrf_drv_twi_tx(&m_twi_master, slave_addr, &reg, 1, true);
    	if(NRF_SUCCESS != ret)
    		return ret;
    	
    	ret = nrf_drv_twi_rx(&m_twi_master, slave_addr, data,len);
    	
    	return ret;
    }
    
    ret_code_t I2C_ReadData(uint8_t reg,uint8_t *data,int len)
    {
    	static ret_code_t ret;
    	//uint8_t data = 0;
    	
    	//nrf_delay_ms(20);
    	
    	ret = nrf_drv_twi_tx(&m_twi_master, slave_addr, &reg, 1, true);
    	if(NRF_SUCCESS != ret)
    		return ret;
    	
    	ret = nrf_drv_twi_rx(&m_twi_master, slave_addr, data,len);
    	
    	return ret;
    }
    
    
    uint8_t I2C_ReadReg(uint8_t reg)
    {
    	static ret_code_t ret;
    	uint8_t data = 0xFD;
    	
    	//NRF_LOG("I2C_ReadReg\r\n");
    	ret = nrf_drv_twi_tx(&m_twi_master, slave_addr, &reg, 1, true);
    	//NRF_LOG("I2C_ReadReg End\r\n");
    	if(NRF_SUCCESS != ret)
    		return data;
    	ret = nrf_drv_twi_rx(&m_twi_master, slave_addr, &data,1);
    	
    	return data;
    }
    

Children
Related