📄 hpitest.c
字号:
#include<stdio.h>
#include<unistd.h>
#include<sys/mman.h>
#include<fcntl.h>
#include<sys/types.h>
#define HPI_VA_BASE (*ss).hpi_mmap_start
/*定义HPI控制寄存器读/写地址*/
#define HPIC_W_F(HPI_VA_BASE) *((unsigned long*)((HPI_VA_BASE)+0x00))
#define HPIC_W_S(HPI_VA_BASE) *((unsigned long*)((HPI_VA_BASE)+0x20))
#define HPIC_R_F(HPI_VA_BASE) *((unsigned long*)((HPI_VA_BASE)+0x10))
#define HPIC_R_S(HPI_VA_BASE) *((unsigned long*)((HPI_VA_BASE)+0x30))
/*定义HPI数据寄存器地址(自增模式)*/
#define HPID_W_F(HPI_VA_BASE) *((unsigned long*)((HPI_VA_BASE)+0x04))
#define HPID_W_S(HPI_VA_BASE) *((unsigned long*)((HPI_VA_BASE)+0x24))
#define HPID_R_F(HPI_VA_BASE) *((unsigned long*)((HPI_VA_BASE)+0x14))
#define HPID_R_S(HPI_VA_BASE) *((unsigned long*)((HPI_VA_BASE)+0x34))
/*定义HPI地址寄存器地址*/
#define HPIA_W_F(HPI_VA_BASE) *((unsigned long*)((HPI_VA_BASE)+0x08))
#define HPIA_W_S(HPI_VA_BASE) *((unsigned long*)((HPI_VA_BASE)+0x28))
#define HPIA_R_F(HPI_VA_BASE) *((unsigned long*)((HPI_VA_BASE)+0x18))
#define HPIA_R_S(HPI_VA_BASE) *((unsigned long*)((HPI_VA_BASE)+0x38))
#define LEN (4096*4)
struct str_HPI
{int hpi_fd;
unsigned int hpi_mmap_start;
unsigned int hpi_dsp_add;
int hpi_dsp_data_length;
int buffer[4096*2] ;
};
int main()
{ int dev_hpi_open(struct str_HPI *);
int hpi_auto_write(struct str_HPI *);
int hpi_auto_read(struct str_HPI *);
int i,m,d;
struct str_HPI hpi;
struct str_HPI *ss;
ss=&hpi;
printf("please input the add:\n");
scanf("%d\n,&d");
hpi.hpi_dsp_add=d;
printf("please input the length:\n");
scanf("%d\n",&m);
hpi.hpi_dsp_data_length=m;
dev_hpi_open(&hpi);
HPIC_W_F((*ss).hpi_mmap_start)=((0x00000808>>8)&0xff);
HPIC_W_S((*ss).hpi_mmap_start)=(0x00000808&0xff);//初始化HPIC控制器;
hpi_auto_write(&hpi);
hpi_auto_read(&hpi);
for(i=0;i<m;i++)
{ printf("%/d\n",(*ss).buffer[i]);}
//munmap((*ss).hpi_mmap_start,LEN);
close((*ss).hpi_fd);
return 0;
}
int dev_hpi_open(struct str_HPI *ss)
{ int i;
if(((*ss).hpi_fd=open("/dev/hpi",O_RDWR))<0)
{ perror("open");
exit(-1);
}
(* ss).hpi_mmap_start=mmap(NULL,LEN,PROT_READ|PROT_WRITE,MAP_SHARED,((*ss).hpi_fd),0);
if((* ss).hpi_mmap_start=MAP_FAILED)
{ perror("mmap");
exit(-1);
}
printf("%s\n",(*ss).hpi_mmap_start);
return 0;
}
int hpi_auto_write(struct str_HPI *ss)
{ volatile unsigned long dsp_addr_high_write_auto;
volatile unsigned long dsp_addr_low_write_auto;
volatile unsigned long dsp_add_w;
int j,k;
k=(* ss).hpi_dsp_data_length;
int a[10]={0,1,2,3,4,5,6,7,8,9};
dsp_add_w=((*ss).hpi_dsp_add);//应用程序传过来的参数,指明希望从DSP的那个地址写起;
dsp_addr_low_write_auto=((dsp_add_w)&0x00ff)+((dsp_add_w)<<8);
dsp_addr_high_write_auto=((dsp_add_w)&0xff00)+((dsp_add_w)>>8);
/*写HPIA控制器,以确定从DSP哪一地址写起*/
HPIA_W_F((*ss).hpi_mmap_start)=(dsp_addr_high_write_auto);
HPIA_W_S((*ss).hpi_mmap_start)=(dsp_addr_low_write_auto);
for(j=0;j<10;j++)
{ HPID_W_F((*ss).hpi_mmap_start)=((a[j]>>8)&0xff);
HPID_W_S((*ss).hpi_mmap_start)=((a[j])&0xff);
}
}
int hpi_auto_read(struct str_HPI *ss)
{ volatile unsigned long dsp_addr_high_read_auto;
volatile unsigned long dsp_addr_low_read_auto;
volatile unsigned long dsp_data_high_read_auto;
volatile unsigned long dsp_data_low_read_auto;
//volatile unsigned long dsp_ctl_high_write_auto;
//volatile unsigned long dsp_ctl_low_write_auto;
volatile unsigned long dsp_add_temp;
volatile unsigned long data_length;
volatile unsigned long polltest;
int i;
polltest=HPIC_R_F((*ss).hpi_mmap_start);
while((polltest&0x00000008)!=0x00000008)
{ polltest=HPIC_R_F((*ss).hpi_mmap_start);
}
dsp_add_temp=((*ss).hpi_dsp_add);//应用程序传过来的参数,指明希望从DSP的那个地址读起;
dsp_addr_low_read_auto=((dsp_add_temp)&0x00ff)+((dsp_add_temp)<<8);
dsp_addr_high_read_auto=((dsp_add_temp)&0xff00)+((dsp_add_temp)>>8);
/*写HPIA控制器,以确定从DSP哪一地址读起*/
HPIA_W_F((*ss).hpi_mmap_start)=(dsp_addr_high_read_auto);
HPIA_W_S((*ss).hpi_mmap_start)=(dsp_addr_low_read_auto);
data_length=(*ss).hpi_dsp_data_length;//从应用程序中传过来的参数,指明期望读取多少字;
for(i=0;i<data_length;i++)
{ polltest=HPIC_R_F((*ss).hpi_mmap_start);
while((polltest&0x00000008)!=0x00000008)
{polltest=HPIC_R_F((*ss).hpi_mmap_start);
}
dsp_data_high_read_auto=HPID_R_F((*ss).hpi_mmap_start);
dsp_data_low_read_auto=HPID_R_S((*ss).hpi_mmap_start);
(*ss).buffer[(i)]=(dsp_data_high_read_auto&0xff00)+(dsp_data_low_read_auto&0x00ff);
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -