2.1 HPI接口设备数据结构及其文件系统接口定义
HPI接口设备数据结构是自定义的,它完成各个不同系统调用之间的协调工作,因此在设备驱动中是全局数据结构变量。具体定义如下: Struct HPI_DEVICE{
devfs_handle_t devfs ; //devfs device
char isopen ; //device status: 1=opened, 0=closed
int MajorID ;
kdev_t MinorID ;
U16 DriverType ;
char *HpiBaseBufRead ;
char * HpiBaseBufWrite ;
wait_queue_head_t rd_wait ; //read timeouts
struct semaphore sem ; //lock to prevent concurrent reads or writes
#if defined(DMA_SUPPORT) //DMA
DMA_CHANNEL_INFO DmaInfo[NUMBER_OF_DMA_CHANNELS];
Spinlock_t LockDmaChannel ;
#endif
struct file_operations hpi_fops ;
}
文件系统接口定义是用户使用HPI设备的接口,合理定义设备驱动程序在内核中的源码就能简化应用程序的设计。
Static struct file_operations hpi_fops={
owner : THIS_MODULE ,
open : hpi_open ,
read : hpi_read ,
write : hpi_write ,
ioctl : hpi_ioctl ,
mmap : hpi_mmap ,
release : hpi_release ,
};
HPI设备驱动程序的开发大多数工作都集中在struct file_operations中接口函数的编写上,这些函数是应用程序通过内核操作硬件设备的入口函数,下面将给出对HPI接口读数据的关键代码。 #define HPI_BASEADDR 0x08000000 // BANK 1
#define bHPI(Nb) __REG1(HPI_BASEADDR +(Nb))
#define HPIC_WRITE bHPI(0x0)
#define HPIC_READ bHPI(0x40)
#define HPIA_WRITE bHPI(0x10)
#define HPIA_READ bHPI(0x50)
#define HPID_WRITE bHPI(0x20)
#define HPID_READ bHPI(0x60)
Static ssize_t hpi_read(struct file *file,char *buf,size_t count,loff_t *oppos)
{
Struct HPI_DEVICE *pHpiDevice ;
int i , hpi_size ;
size_t ret ;
down(&(pHpiDevice->sem));
hpi_size = 1024;
for(i=0;i
{ HPIC_WRITE = 0x00000000 ; //初始化HPI控制寄存器
HPIA_WRITE = 0x80000000 ; //初始化HPI地址寄存器,读取DSP地址为0x80000000的数据
(__U8*)(&pHpiDevice-> HpiBaseBufRead) = HPID_READ ; //读取1k到HpiBaseBufRead缓冲区
}
IBUF_SIZE = hpi_size ;
ret=copy_to_user(buf,(__U8*)(&pHpiDevice->HpiBaseBufRead),IBUF_SIZE)?-EFAULT : ret ;
up(&(pHpiDevice->sem));
return IBUF_SIZE ;
}
|