新手上路
- 积分
- 35
- 金钱
- 35
- 注册时间
- 2023-8-14
- 在线时间
- 6 小时
|
楼主 |
发表于 2023-8-17 11:57:52
|
显示全部楼层
void spi_writeread(unsigned char * tx_buf, unsigned char * rx_buf, unsigned int len)
{
u32 val;
int i;
val = readl(statreg);
printk("statreg is %x.\r\n", val);
if (len>512) len=512;
val = readl(conreg);
val &= 0x000FFFFF;
val |= ((len * 8 - 1) << 20);
writel(val, conreg);
i=0;
while(i<len)
{
switch((len - i)%4)
{
case 1:
while((readl(statreg) & (1 << 0))==0){}
writel(tx_buf[i], txdata);
while((readl(statreg) & (1 << 3))==0){}
rx_buf[i] = readl(rxdata);
i++;
break;
case 2:
while((readl(statreg) & (1 << 0))==0){}
writel(tx_buf[i]*256 + tx_buf[i+1], txdata);
while((readl(statreg) & (1 << 3))==0){}
val = readl(rxdata);
rx_buf[i] = val >> 8;
rx_buf[i+1] = val & 0xFF;
i+=2;
break;
case 3:
while((readl(statreg) & (1 << 0))==0){}
writel(tx_buf[i]*65536 + tx_buf[i+1]*256 + tx_buf[i+2], txdata);
while((readl(statreg) & (1 << 3))==0){}
val = readl(rxdata);
rx_buf[i] = val >> 16;
rx_buf[i+1] = (val >> 8) & 0xFF;
rx_buf[i+2] = val & 0xFF;
i+=3;
break;
default:
while((readl(statreg) & (1 << 0))==0){}
writel((tx_buf[i]<<24) | (tx_buf[i+1]<<16) | (tx_buf[i+2]<<8) | tx_buf[i+3], txdata);
while((readl(statreg) & (1 << 3))==0){}
val = readl(rxdata);
rx_buf[i] = val >> 24;
rx_buf[i+1] = (val >> 16) & 0xFF;
rx_buf[i+2] = (val >> 8) & 0xFF;
rx_buf[i+3] = val & 0xFF;
i+=4;
break;
};
};
printk("spi send: ");
i=0;
while(i<len)
{
printk("%X ",tx_buf[i]);
i++;
}
printk("\r\n");
printk("spi receive: ");
i=0;
while(i<len)
{
printk("%X ",rx_buf[i]);
i++;
}
printk("\r\n");
} |
|