用SPI控制RTL8367

Tim_czp Tim_czp 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区

通常我们都是用MDC/MDIO来与RTL8367通信的,但RTL8367也提供了其他方法,所以尝试用SPI来控制RTL8367.

用RK3308的SPI1控制器与RTL8367通信

根据RTL8367的数据手册,按照时序图配置了读写,代码如下:

static int spi_write_and_read_xfer(const void *tx_buf, void *rx_buf, size_t len)
{
	int ret = -1;
	struct spi_device *spi = NULL;
	struct spi_transfer     t = {
			.tx_buf         = tx_buf,
			.rx_buf         = rx_buf,
			.len            = len,
			.speed_hz       = 2000000,
            .bits_per_word  = 8,
		};
	struct spi_message      m;

	spi = g_spi_info->spi;    
    
	spi_message_init(&m);
	spi_message_add_tail(&t, &m);
	ret = spi_sync(spi, &m);
    
    return ret;
}

int spi_write_rtl8367_reg(u16 reg, u16 val)//写
{
    u8 tx_buf[5] = {0x02,0,0,0,0};
    u8 rx_buf[5] = {0xFF,0xFF,0xFF,0xFF,0xFF};
	//int i=0;
    if (!g_spi_info)
    {
		pr_err("g_spi_info is NULL\n");
		return 0xFF;
	} 
    
    tx_buf[0] = 0x02;
    tx_buf[1] = (reg & 0xFF00) >> 8;
	tx_buf[2] = reg & 0xFF;
	tx_buf[3] = (val & 0xFF00) >> 8;
    tx_buf[4] = val & 0xFF;
    
    mutex_lock(&g_spi_info->lock); 
	spi_set_cs_pin_val(rtl8367_cs,0);          //CS用的GPIO
    spi_write_and_read_xfer(tx_buf,rx_buf,5);
	spi_set_cs_pin_val(rtl8367_cs,1);	
    mutex_unlock(&g_spi_info->lock);
	
	return 0;
}

int spi_read_rtl8367_reg(u16 reg, u16 *rdata)//读
{
    u8 tx_buf[5] = {0x03,0,0,0xFF,0xFF};
    u8 rx_buf[5] = {0xFF,0xFF,0xFF,0,0};

    if (!g_spi_info)
    {
		pr_err("g_spi_info is NULL\n");
		return 0xFF;
	}
    
    tx_buf[0] = 0x03;
    tx_buf[1] = (reg & 0xFF00) >> 8;
	tx_buf[2] = reg & 0xFF;
	tx_buf[3] = 0xFF;
	tx_buf[4] = 0xFF;

    mutex_lock(&g_spi_info->lock);
    spi_set_cs_pin_val(rtl8367_cs,0);          //CS用的GPIO	
    spi_write_and_read_xfer(tx_buf,rx_buf,5);
	spi_set_cs_pin_val(rtl8367_cs,1);	
    mutex_unlock(&g_spi_info->lock);

    *rdata = (rx_buf[3]<<8|rx_buf[4]);
	
	return 0;
}

用逻辑分析仪,查看了时序

读: 

 写:

 用的模式0,发现读ID能成功,但写却没反应...

分开一个或两个字节的写也不行

时序是按照数据手册写的,感觉是对的,但是读可以,写却不行,不知道为什么,所以有些郁闷!!!

有大神遇到同样的问题?求解惑...

 

...全文

相关推荐

评论 3
收藏 已收藏
分享
返回顶部
Tim_czp
驱动开发/核心开发 21,620 成员 21,709 内容