#include <assert.h>
#include <fcntl.h>
#include <getopt.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <pthread.h>
#include <semaphore.h>
#include <stdarg.h>
#include <syslog.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/sysinfo.h>
#include <unistd.h>
#include <dirent.h>
#include <string.h>
#include "rwif.h"
/* 判断机器是大端模式或者小端模式 */
/* ltoh: little to host */
/* htol: little to host */
#if __BYTE_ORDER == __LITTLE_ENDIAN
# define ltohl(x) (x)
# define ltohs(x) (x)
# define htoll(x) (x)
# define htols(x) (x)
#elif __BYTE_ORDER == __BIG_ENDIAN
# define ltohl(x) __bswap_32(x)
# define ltohs(x) __bswap_16(x)
# define htoll(x) __bswap_32(x)
# define htols(x) __bswap_16(x)
#endif
/* 设置映射空间大小为1M */
#define MAP_SIZE (1024*1024UL)
/* 这些是硬件相关的命令参数 */
#define S2MM_VDMA_BASE 0x10000
#define MM2S_VDMA_BASE 0x00000
#define VDMA_S2MM_SR 0x34
#define VDMA_S2MM_CR 0x30
#define VDMA_S2MM_START_ADDR 0xAC
#define VDMA_S2MM_HSIZE 0xA8
#define VDMA_S2MM_STRID 0xA4
#define VDMA_S2MM_VSIZE 0xA0
#define VDMA_MM2S_SR 0x04
#define VDMA_MM2S_CR 0x00
#define VDMA_MM2S_START_ADDR 0x5C
#define VDMA_MM2S_HSIZE 0x58
#define VDMA_MM2S_STRID 0x54
#define VDMA_MM2S_VSIZE 0x50
/* 推测是VDMA的内存寻址宽度,即对齐宽度 */
#define VDMA_DATA_BYTES 4
/* 这是加速卡的内存起始地址 */
#define FPGA_DDR_START_ADDR 0x00000000
/* 从名字看,是缓存的图片的数量,是否是说每次读取16帧 */
#define VIDEO_FRAME_STORE_NUM 16
/* 使用该变量来记录/dev/xdma0_user映射出来的1M空间的起始地址 */
void *control_base;
/* 定义变量来保存图片数量、宽、高这些全局数据*/
static int video_height;
static int video_width;
static int total_frame;
/* 定义信号量,以便线程间同步 */
static sem_t h2c_sem;
static sem_t c2h_sem;
/* 从这里可以推测,加速卡每次缓存16张图片 */
static unsigned int h2c_fpga_ddr_addr[VIDEO_FRAME_STORE_NUM];
static unsigned int c2h_fpga_ddr_addr[VIDEO_FRAME_STORE_NUM];
/* 定义两个变量,分别存放输入图片列表和输出图片列表 */
static ImageData *image_list;
static ImageData *image_list_out;
static unsigned char *h2c_align_mem;
static unsigned char *c2h_align_mem;
static void write_control(void *base_addr,int offset,uint32_t val);
//static uint32_t read_control(void *base_addr,int offset);
static struct timespec ts_start, ts_end;
static int cnt1=0;
static int cnt2=0;
/* 打开中断产生的文件 */
static int open_event(char *filename)
{
int fd;
fd = open(filename, O_RDWR | O_SYNC);
if(fd == -1)
{
printf("open event error\n");
return -1;
}
return fd;
}
/* 从文件中读取中断产生事件 */
static int read_event(int fd)
{
int val;
read(fd,&val,4);
return val;
}
static int open_control(char *filename)
{
int fd;
fd = open(filename, O_RDWR | O_SYNC);
if(fd == -1)
{
printf("open control error\n");
return -1;
}
return fd;
}
static void *mmap_control(int fd,long mapsize)
{
void *vir_addr;
vir_addr = mmap(0, mapsize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
return vir_addr;
}
/* 向某个地址写入值 */
static void write_control(void *base_addr,int offset,uint32_t val)
{
uint32_t writeval = htoll(val);
*((uint32_t *)(base_addr+offset)) = writeval;
}
/* 从名字看,是设置主机向计算卡发送数据的vdma */
static void config_mm2s_vdma(void *base_addr,int num_frame)
{
int i;
write_control(base_addr,MM2S_VDMA_BASE+VDMA_MM2S_CR,0x4);
usleep(100);
write_control(base_addr,MM2S_VDMA_BASE+VDMA_MM2S_CR,0x010110
为公司的pcie接口FPGA板卡编写上位机软件——源文件分析1——逐行阅读并加以注释
最新推荐文章于 2024-05-17 11:24:51 发布