用SPI控制RTL8367

Tim_czp 2022-07-19 10:40:31

通常我们都是用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能成功,但写却没反应...

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

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

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

 

...全文
587 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
Alie鱼 03-09
  • 打赏
  • 举报
回复

mdio的有成功吗

Lx_linux 2023-12-27
  • 打赏
  • 举报
回复

SPI控制那份资料可以发我一下吗? 谢谢 1014237912@qq.com

xzx123456xzx 2022-10-21
  • 打赏
  • 举报
回复

楼主后来调通了吗?

21,600

社区成员

发帖
与我相关
我的任务
社区描述
硬件/嵌入开发 驱动开发/核心开发
社区管理员
  • 驱动开发/核心开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧