//(1)Init_SPI()完成SPI的初始化
void Init_SPI(void)
{
int i;
rSPPRE0=0x32;
rSPCON0=0x1e;
for(i=0;i<10;i++)
{
rSPTDAT0=0xff;
}
rGPECON |=0x0a800000;
rGPECON&=(~0x05400000);
rGPEUP |=0x3800;
//GPH5----->CS
rGPHCON |=0x0400;
rGPHCON&=(~0x0800);
rGPHUP&=(~0x20);
rGPHDAT |=0x20;
}
//(2)ad_wr()写入要求A/D转换的通道
static ssize_t ad_wr(struCt file *file,const char *bur,size_t count,loft_t *offset)
{
int ret;
int i;
ret = 0;
i = 0;
dbuf="kmalloc"(count *sizeof(unsigned char),GFP_KERNEL);
copy_from_user(dbuf,bur,count);
for(i = 0; i < count; i++)
{
ADTXdata[i]=dbuf[i];
}
kfree(dbuf);
return ret;
}
//(3)ad_rd()得到A/D转换的结果
statie ssize_t ad_rd(struet file *file,char *bur,size_t count,loft t *offset)
{
int ret;
int i;
ret = 0;
i = 0;
ad_convert(); // 用2个?
ad_convert();
dbuf= kmalloc(count * sizeof(unsigned char),GFP KERNEL);
for(i = 0; i < count; i++)
{
dbuf[i]=ADRXdata[i];
}
copy_to_user(bur,dbuf,count);
kfree(dbuf);
return ret;
}
//(4)ad_convert()实际完成A/D转换
void ad_convert(void)
{
rGPHDAT&=(~0x20);
udelay(100000);
spi_tx_data(ADTXdata[0]);
ADRXdata[0]=rSPRDATO;
spi_tx_data(0xff);
ADRXdata [1 ]=rSPRDATO;
rGPHDAT |=0x20;
}
//(5)spi_tx_data()完成发送数据
void spi_tx_data(unsigned char data)
{
spi_poll_done();
rSPTDAT0=data;
spi_poll_done();
}
//(6)spi_poll_done()轮询SPI状态
static void spi_poll_done(void)
{
while(!(rSPSTA0&0x01));
}
感觉最后一个函数很有问题,依据我的理解,这种是不是压根就不应该出现在驱动里面?但是各大网站疯狂转载,我又很疑惑,各位兄弟解疑下
PS:用的是s3c2410和AD7888 |