UP-MOBNET-Ⅱ型实验箱摄像机视频采集实验
文章目录
实验箱型号
名称 | 移动互联网教学科研平台Ⅱ型 |
---|---|
型号 | UP-MOBNET-Ⅱ |
编号 | 03019024 |
批号 | 32017040520 |
实验内容和材料准备
实验内容:
实验了解v4l2视频采集过程和原理、熟悉试验箱开发环境和烧写流程、交叉编译原理和程序移植。
硬件:
UP-MobNet-II 型平台、12V 电源线、串口线、USB 数据线
软件:
Xshell、fastboot.exe、fastboot.bat、WinImage.exe
实验环境准备
整体流程:
试验箱烧写linux系统
UP-MobNet-II 型出厂程序烧写
注:出厂烧写文件镜像及工具存放在光盘的 IMG 文件夹下!且 u-boot 一般用户无需更改,该烧写步骤可以略过。
该试验箱u-boot不需要改动;出厂时BootLoader已经写好
1. 连线:
将产品附带串口线一端连接到 PC 机端串口,另一端连接到 UP-MobNet-II 型平台串口 0(RS232-0 即开发板左侧起靠近网口的串口)上。
将产品附带 USB 数据线连接 PC 机 USB 口与 UP-MobNet-II 型平台 OTG 接口。
其中串口大概是4、电源开关是1、网线口是2、usb数据线口(OTG接口)是3
2. 跳线: 备注:核心板内侧为 1,外侧为 0,跳线模式出厂已经默认跳到 iNAND 模式,根据需要更改。
这里试验箱跳线一般已经调好,不需要改动
3. 使用Xshell登录UP-MobNet-II 型系统串口终端
(如果这时间有需要安装驱动,在Win10中可以下载驱动精灵,其可以帮助管理所需要的驱动)
首先查看电脑的端口号:电脑->管理->设备管理器(Win10需要在查看中打开显示隐藏设备)->端口(COM)
Xshell连接UP-MobNet-II :
Xshell->文件->新建->连接->协议serial->串口->选择自己的COM->波特率115200->连接
确定连接后,连接电源。按下 UP-MobNet-II 型平台左上角 POWER 电源键,系统上电。Xshell 进入开发板的 U-BOOT 功能界面(3秒等待),(3秒内)按下回车,进入 U-BOOT 界面
在 u-boot 界面下,输入 fastboot 命令.
因为该板子BootLoader出厂写好了不需要烧写 u-boot.bin
直接烧写系统镜像 zImage、rootfs_up4412.cramfs:
双击光盘/IMG/fastboot.bat 文件->选择'2'kernel 烧写 zImage(完成后)->选择‘3’system 烧写 rootfs_up4412.cramfs
完成烧写,重启实验平台,加载启动信息
如下图所示:
查看端口号:
Xshell连接实验箱:
进入Uboot模式
使用fastboot工具
烧写完成,加载启动信息
redHat配置交叉编译环境
将光盘中交叉编译工具的源码 arm-2009q3.tar.bz2 拷贝到/usr/local/arm(没有该目录就新建一个)目录下,并解压:#tar jxvf arm-2009q3.tar.bz2
2. 修改环境变量:
#vim ~/.bashrc
在文件末添加
export PATH=/usr/local/arm/arm-2009q3/bin:$PATH
编译器安装成功。
#source ~/.bashrc
测试
arm-none-linux-gnueabi-gcc -v
注意,如果是64位Linux系统(ubuntu20),需要再安装额外的一些软件。如下操作。
.安装32位库(64位系统需要)
因为ia32-libs数据库的问题需安装32位库
依次执行下列命令即可
1. sudo dpkg --add-architecture i386
2. sudo apt-get update
3. sudo apt-get dist-upgrade
ubuntu 20安装以上库之后,还是提示找不到arm-none-linux-gnueabi-gcc错误。之后安装libc6-i386库,解决问题。
apt-get install libc6-i386
测试
arm-none-linux-gnueabi-gcc -v
arm-none-linux-gnueabi-gcc -v
V4l2视频采集原理
采集方式
打开视频设备后,可以设置该视频设备的属性,例如裁剪、缩放等。这一步是可选的。在Linux编程中,一般使用ioctl函数来对设备的I/O通道进行管理:
extern int ioctl (int __fd, unsigned long int __request, …) __THROW;
__fd:设备的ID,例如刚才用open函数打开视频通道后返回的cameraFd;
__request:具体的命令标志符。
在进行V4L2开发中,一般会用到以下的命令标志符:
VIDIOC_REQBUFS:分配内存
VIDIOC_QUERYBUF:把VIDIOC_REQBUFS中分配的数据缓存转换成物理地址
VIDIOC_QUERYCAP:查询驱动功能
VIDIOC_ENUM_FMT:获取当前驱动支持的视频格式
VIDIOC_S_FMT:设置当前驱动的频捕获格式
VIDIOC_G_FMT:读取当前驱动的频捕获格式
VIDIOC_TRY_FMT:验证当前驱动的显示格式
VIDIOC_CROPCAP:查询驱动的修剪能力
VIDIOC_S_CROP:设置视频信号的边框
VIDIOC_G_CROP:读取视频信号的边框
VIDIOC_QBUF:把数据放回缓存队列
VIDIOC_DQBUF:把数据从缓存中读取出来
VIDIOC_STREAMON:开始视频显示函数
VIDIOC_STREAMOFF:结束视频显示函数
VIDIOC_QUERYSTD:检查当前视频设备支持的标准,例如PAL或NTSC。
这些IO调用,有些是必须的,有些是可选择的。
操作流程
1. 打开设备文件。
int fd=open(”/dev/video0″,O_RDWR);
2. 取得设备的capability,看看设备具有什么功能,比如是否具有视频输入,或者音频输入输出等。VIDIOC_QUERYCAP,struct v4l2_capability
v4l2_std_id std;
do {
ret= ioctl(fd, VIDIOC_QUERYSTD, &std);
} while (ret == -1 && errno == EAGAIN);
switch (std) {
case V4L2_STD_NTSC:
//……
case V4L2_STD_PAL:
//……
}
操作流程
- 打开设备文件。
int fd=open(”/dev/video0″,O_RDWR);
- 取得设备的capability,看看设备具有什么功能,比如是否具有视频输入,或者音频输入输出等。VIDIOC_QUERYCAP,struct v4l2_capability
v4l2_std_id std;
do {
ret= ioctl(fd, VIDIOC_QUERYSTD, &std);
} while (ret == -1 && errno == EAGAIN);
switch (std) {
case V4L2_STD_NTSC:
//……
case V4L2_STD_PAL:
//……
}
- 选择视频输入,一个视频设备可以有多个视频输入。
VIDIOC_S_INPUT,struct v4l2_input(可不要)
- 设置视频的制式和帧格式,制式包括PAL,NTSC,帧的格式个包括宽度和高度等。
VIDIOC_S_STD,VIDIOC_S_FMT,struct v4l2_std_id,struct v4l2_format
struct v4l2_format fmt;
v4l2_format 结构如下:
struct v4l2_format
{
enum v4l2_buf_type type; // 数据流类型,必须永远是V4L2_BUF_TYPE_VIDEO_CAPTURE
union
{
struct v4l2_pix_format pix;
struct v4l2_window win;
struct v4l2_vbi_format vbi;
__u8 raw_data[200];
} fmt;
};
struct v4l2_pix_format
{
__u32 width; // 宽,必须是16 的倍数
__u32 height; // 高,必须是16 的倍数
__u32 pixelformat; // 视频数据存储类型,例如是YUV 4 :2 :2 还是RGB
enum v4l2_field field;
__u32 bytesperline;
__u32 sizeimage;
enum v4l2_colorspace colorspace;
__u32 priv;
};
样例:
memset ( &fmt, 0, sizeof(fmt) );
fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
fmt.fmt.pix.width = 320;
fmt.fmt.pix.height = 240;
fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_JPEG;
if (ioctl(fd, VIDIOC_S_FMT, &fmt) < 0)
{
printf("set format failed\n");
//return 0;
}
- 向驱动申请帧缓冲,一般不超过5个。struct v4l2_requestbuffers
struct v4l2_requestbuffers
{
__u32 count; // 缓存数量,也就是说在缓存队列里保持多少张照片
enum v4l2_buf_type type; // 数据流类型,必须永远是V4L2_BUF_TYPE_VIDEO_CAPTURE
enum v4l2_memory memory; // V4L2_MEMORY_MMAP 或 V4L2_MEMORY_USERPTR
__u32 reserved[2];
};
样例:
struct v4l2_requestbuffers req;
memset(&req, 0, sizeof (req));
req.count = 4;
req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
req.memory = V4L2_MEMORY_MMAP;
if (ioctl(fd,VIDIOC_REQBUFS,&req) == -1)
{
perror("VIDIOC_REQBUFS error \n");
//return -1;
}
6.申请物理内存
将申请到的帧缓冲映射到用户空间,这样就可以直接操作采集到的帧了,而不必去复制。将申请到的帧缓冲全部入队列,以便存放采集到的数据.VIDIOC_QBUF,struct v4l2_buffer
VideoBuffer* buffers = calloc( req.count, sizeof(VideoBuffer) );
printf("sizeof(VideoBuffer) is %d\n",sizeof(VideoBuffer));
struct v4l2_buffer buf;
for (numBufs = 0; numBufs < req.count; numBufs++)
{
memset( &buf, 0, sizeof(buf) );
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_MMAP;
buf.index = numBufs;
if (ioctl(fd, VIDIOC_QUERYBUF, &buf) < 0)
{
printf("VIDIOC_QUERYBUF error\n");
//return -1;
}
printf("buf len is %d\n",sizeof(buf));
//内存映射
buffers[numBufs].length = buf.length;
buffers[numBufs].offset = (size_t) buf.m.offset;
buffers[numBufs].start = mmap (NULL, buf.length,PROT_READ | PROT_WRITE, MAP_SHARED, fd, buf.m.offset);
printf("buffers.length = %d,buffers.offset = %d ,buffers.start[0] = %d\n",buffers[numBufs].length,buffers[numBufs].offset,buffers[numBufs].start[0]);
printf("buf2 len is %d\n",sizeof(buffers[numBufs].start));
if (buffers[numBufs].start == MAP_FAILED)
{
perror("buffers error\n");
//return -1;
}
if (ioctl (fd, VIDIOC_QBUF, &buf) < 0)
{
printf("VIDIOC_QBUF error\n");
//return -1;
}
}
- 开始视频的采集。
enum v4l2_buf_type type;
type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
if (ioctl (fd, VIDIOC_STREAMON, &type) < 0)
{
printf("VIDIOC_STREAMON error\n");
// return -1;
}
7. 出队列以取得已采集数据的帧缓冲,取得原始采集数据。VIDIOC_DQBUF, 将缓冲重新入队列尾,这样可以循环采集。VIDIOC_QBUF
if (ioctl(fd, VIDIOC_DQBUF, &buf) < 0)
{
perror("VIDIOC_DQBUF failed.\n");
//return -1;
}
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_MMAP;
unsigned char *ptcur = buffers[numBufs].start;
DEBUG("buf.bytesused = %d \n",buf.bytesused);
int i1;
for(i1=0; i1<buf.bytesused; i1++)
{
if((buffers[numBufs].start[i1] == 0x000000FF) && (buffers[numBufs].start[i1+1] == 0x000000C4))
{
DEBUG("huffman table finded! \nbuf.bytesused = %d\nFFC4 = %d \n",buf.bytesused,i1);
break;
}
}
if(i1 == buf.bytesused)printf("huffman table don't exist! \n");
int i;
for(i=0; i<buf.bytesused; i++)
{
if((buffers[numBufs].start[i] == 0x000000FF) && (buffers[numBufs].start[i+1] == 0x000000D8)) break;
ptcur++;
}
DEBUG("i=%d,FF=%02x,D8=%02x\n",i,buffers[numBufs].start[i],buffers[numBufs].start[i+1]);
int imagesize =buf.bytesused - i;
DEBUG("buf.bytesused = %d \n",buf.bytesused);
DEBUG ("imagesize = %d \n",imagesize);
- 停止视频的采集。VIDIOC_STREAMOFF
- 关闭视频设备。close(fd);
编写摄像机驱动程序和测试程序
v4l2视频流采集:
camer.h
/*************************************************************************
> File Name: camer.h
> 作者:YJK
> Mail: [email protected]
> Created Time: 2020年11月18日 星期三 14时17分55秒
************************************************************************/
#ifndef __CAMER_H
#define __CAMER_H
#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
#include<stdlib.h>
#include<linux/videodev2.h>
#include<sys/ioctl.h>
#include<errno.h>
#include<string.h>
#include<assert.h>
#include<getopt.h>
#include<sys/stat.h>
#include<sys/mman.h>
#include<asm/types.h>
#include<linux/fb.h>
#define CLEAN(x) (memset(&(x), 0, sizeof(x)))
#define WIDTH 640
#define HEIGHT 480
typedef struct Video_Buffer{
void * start;
unsigned int length;
}Video_Buffer;
int ioctl_(int fd, int request, void *arg);
void sys_exit(const char *s);
int open_device(const char * device_name);
int open_file(const char * file_name);
void start_stream(void);
void end_stream(void);
int init_device(void);
int init_mmap(void);
static int read_frame(void);
int process_frame(void);
void close_mmap(void);
void close_device(void);
#endif
camer.c
/*
* Copyright (C) 2020 JunKe Yuan<[email protected]>
*
* This is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
* USA
*
*/
#include "camer.h"
#include <linux/i2c.h>
#include <linux/videodev2.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/select.h>
#include <time.h>
int fd;
int file_fd;
int frame_size;
static Video_Buffer * buffer = NULL;
int ioctl_(int fd, int request, void *arg)
{
int ret = 0;
do{
ret = ioctl(fd, request, arg);
}while(ret == -1 && ret == EINTR);
return ret;
}
int open_device(const char * device_name)
{
struct stat st;
if( -1 == stat( device_name, &st ) )
{
printf( "Cannot identify '%s'\n" , device_name );
return -1;
}
if ( !S_ISCHR( st.st_mode ) )
{
printf( "%s is no device\n" , device_name );
return -1;
}
fd = open(device_name, O_RDWR | O_NONBLOCK , 0);
if ( -1 == fd )
{
printf( "Cannot open '%s'\n" , device_name );
return -1;
}
return 0;
}
int init_device()
{
//查询设备信息
struct v4l2_capability cap;
if (ioctl_(fd, VIDIOC_QUERYCAP, &cap) == -1)
{
perror("VIDIOC_QUERYCAP");
return -1;
}
printf("---------------------LINE:%d\n", __LINE__);
printf("DriverName:%s\nCard Name:%s\nBus info:%s\nDriverVersion:%u.%u.%u\n",
cap.driver,cap.card,cap.bus_info,(cap.version>>16)&0xFF,(cap.version>>8)&0xFF,(cap.version)&0xFF);
//选择视频输入
struct v4l2_input input;
CLEAN(input);
input.index = 0;
if ( ioctl_(fd, VIDIOC_S_INPUT,&input) == -1){
printf("VIDIOC_S_INPUT IS ERROR! LINE:%d\n",__LINE__);
return -1;
}
/*查看摄像头支持的视频格式*/
struct v4l2_fmtdesc fmtdesc;
// struct v4l2_frmsizeenum frmsize;
fmtdesc.index = 0;
fmtdesc.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
printf("fm:\n");
while(ioctl(fd, VIDIOC_ENUM_FMT, &fmtdesc) != -1){
//列举出所有支持的格式
printf("%d.%s %c%c%c%c\n", fmtdesc.index + 1, fmtdesc.description,
fmtdesc.pixelformat & 0xFF,
(fmtdesc.pixelformat >> 8) & 0xFF,
(fmtdesc.pixelformat >> 16) & 0xFF,
(fmtdesc.pixelformat >> 24) & 0xFF);
#if 0
frmsize.pixel_format = fmtdesc.pixelformat;
frmsize.index = 0;
while(ioctl(fd, VIDIOC_ENUM_FRAMESIZES, &frmsize) != -1){
printf("%dx%d\n",frmsize.discrete.width, frmsize.discrete.height);
frmsize.index++;
}
#endif
fmtdesc.index++;
}
/*查看摄像头支持的分辨率*/
//设置帧格式
struct v4l2_format fmt;
CLEAN(fmt);
fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
fmt.fmt.pix.width = WIDTH;
fmt.fmt.pix.height = HEIGHT;
//视频格式
// fmt.fmt.pix.pixelformat = pixformat;
fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YVU420;
// fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
if (ioctl_(fd, VIDIOC_S_FMT, &fmt) == -1)
{
printf("VIDIOC_S_FMT IS ERROR! LINE:%d\n",__LINE__);
return -1;
}
fmt.type = V4L2_BUF_TYPE_PRIVATE;
if (ioctl_(fd, VIDIOC_S_FMT, &fmt) == -1){
printf("VIDIOC_S_FMT IS ERROR! LINE:%d\n", __LINE__);
return -1;
}
//查看帧格式
fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
if ( ioctl_(fd, VIDIOC_G_FMT, &fmt) == -1){
printf("VIDIOC_G_FMT IS ERROR! LINE:%d\n", __LINE__);
return -1;
}
printf("width:%d\nheight:%d\npixelformat:%c%c%c%c field:%d\n",
fmt.fmt.pix.width, fmt.fmt.pix.height,
fmt.fmt.pix.pixelformat & 0xFF,
(fmt.fmt.pix.pixelformat >> 8) & 0xFF,
(fmt.fmt.pix.pixelformat >> 16) & 0xFF,
(fmt.fmt.pix.pixelformat >> 24) & 0xFF,
fmt.fmt.pix.field
);
#if 0
/*设置流相关 帧率*/
struct v4l2_streamparm parm;
CLEAN(parm);
parm.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
parm.parm.capture.capability = V4L2_CAP_TIMEPERFRAME; //是否可以被timeperframe参数控制帧率
parm.parm.capture.timeperframe.denominator = 30; //时间间隔分母
parm.parm.capture.timeperframe.numerator = 1; //分子
if (ioctl_(fd, VIDIOC_S_PARM, &parm) == -1){
// printf("VIDIOC_S_PARM IS ERROR! \n");
perror("VIDIOC_S_PARM");
return -1;
}
if (ioctl_(fd, VIDIOC_G_PARM, (struct v4l2_streamparm*)&parm) == -1){
printf("VIDIOC_G_PARM IS ERROR! \n");
return -1;
}
#endif
#if 1
/*YUYV*/
__u32 min = fmt.fmt.pix.width * 2;
if ( fmt.fmt.pix.bytesperline < min )
fmt.fmt.pix.bytesperline = min;
/*YUV420*/
min = ( unsigned int )WIDTH * HEIGHT * 3 / 2;
if ( fmt.fmt.pix.sizeimage < min )
fmt.fmt.pix.sizeimage = min;
frame_size = fmt.fmt.pix.sizeimage;
printf("After Buggy driver paranoia\n");
printf(" >>fmt.fmt.pix.sizeimage = %d\n", fmt.fmt.pix.sizeimage);
printf(" >>fmt.fmt.pix.bytesperline = %d\n", fmt.fmt.pix.bytesperline);
printf("-#-#-#-#-#-#-#-#-#-#-#-#-#-\n");
printf("\n");
#endif
return 0;
}
int init_mmap()
{
//申请帧缓冲区
struct v4l2_requestbuffers req;
CLEAN(req);
req.count = 4;
req.memory = V4L2_MEMORY_MMAP; //使用内存映射缓冲区
req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
//申请4个帧缓冲区,在内核空间中
if ( ioctl_(fd, VIDIOC_REQBUFS, &req) == -1 )
{
printf("VIDIOC_REQBUFS IS ERROR! LINE:%d\n",__LINE__);
return -1;
}
//获取每个帧信息,并映射到用户空间
buffer = (Video_Buffer *)calloc(req.count, sizeof(Video_Buffer));
if (buffer == NULL){
printf("calloc is error! LINE:%d\n",__LINE__);
return -1;
}
struct v4l2_buffer buf;
int buf_index = 0;
for (buf_index = 0; buf_index < req.count; buf_index ++)
{
CLEAN(buf);
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.index = buf_index;
buf.memory = V4L2_MEMORY_MMAP;
if (ioctl_(fd, VIDIOC_QUERYBUF, &buf) == -1) //获取每个帧缓冲区的信息 如length和offset
{
printf("VIDIOC_QUERYBUF IS ERROR! LINE:%d\n",__LINE__);
return -1;
}
//将内核空间中的帧缓冲区映射到用户空间
buffer[buf_index].length = buf.length;
buffer[buf_index].start = mmap(NULL, //由内核分配映射的起始地址
buf.length,//长度
PROT_READ | PROT_WRITE, //可读写
MAP_SHARED,//可共享
fd,
buf.m.offset);
if (buffer[buf_index].start == MAP_FAILED){
printf("MAP_FAILED LINE:%d\n",__LINE__);
return -1;
}
//将帧缓冲区放入视频输入队列
if (ioctl_(fd, VIDIOC_QBUF, &buf) == -1)
{
printf("VIDIOC_QBUF IS ERROR! LINE:%d\n", __LINE__);
return -1;
}
printf("Frame buffer :%d address :0x%x length:%d\n",buf_index, (__u32)buffer[buf_index].start, buffer[buf_index].length);
}
return 0;
}
void start_stream()
{
enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
if (ioctl_(fd, VIDIOC_STREAMON, &type) == -1){
fprintf(stderr, "VIDIOC_STREAMON IS ERROR! LINE:%d\n", __LINE__);
exit(EXIT_FAILURE);
}
}
void end_stream()
{
enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
if (ioctl_(fd, VIDIOC_STREAMOFF, &type) == -1){
fprintf(stderr, "VIDIOC_STREAMOFF IS ERROR! LINE:%d\n", __LINE__);
exit(EXIT_FAILURE);
}
}
int read_frame()
{
struct v4l2_buffer buf;
int ret = 0;
CLEAN(buf);
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_MMAP;
if (ioctl_(fd, VIDIOC_DQBUF, &buf) == -1){
printf("VIDIOC_DQBUF! LINEL:%d\n", __LINE__);
return -1;
}
// time_t start,end;
// start = time(NULL);
#if 1
ret = write(file_fd, buffer[0].start ,frame_size);
// printf("write:%d\n",frame_size);
#endif
#if 0
ret = Encode_frame(en, pixformat, file_fd, buf_sp, buffer[0].start, WIDTH, HEIGHT, timer);
if (ret == -1){
fprintf(stderr,"Encode_frame\n");
return -1;
}
#endif
// end = time(NULL);
// printf("time:%0.f\n",difftime(end, start));
if (ioctl_(fd, VIDIOC_QBUF, &buf) == -1){
printf("VIDIOC_QBUF! LINE:%d\n", __LINE__);
return -1;
}
return 0;
}
int open_file(const char * file_name)
{
file_fd = open(file_name, O_RDWR | O_CREAT, 0777);
if (file_fd == -1)
{
printf("open file is error! LINE:%d\n", __LINE__);
return -1;
}
return 0;
// file = fopen(file_name, "wr+");
}
void close_mmap()
{
int i = 0;
for (i = 0; i < 4 ; i++)
{
munmap(buffer[i].start, buffer[i].length);
}
free(buffer);
}
void close_device()
{
close(fd);
close(file_fd);
}
int process_frame()
{
struct timeval tvptr;
int ret;
tvptr.tv_usec = 0; //等待50 us
tvptr.tv_sec = 2;
fd_set fdread;
FD_ZERO(&fdread);
FD_SET(fd, &fdread);
ret = select(fd + 1, &fdread, NULL, NULL, &tvptr);
if (ret == -1){
perror("EXIT_FAILURE");
exit(EXIT_FAILURE);
}
if (ret == 0){
printf("timeout! \n");
}
// struct timeval start,end;
// gettimeofday(&start, NULL);
if(read_frame() == -1)
{
fprintf(stderr, "readframe is error\n");
return -1;
}
// gettimeofday(&end, NULL);
// printf("time:%ldms\n",(end.tv_sec - start.tv_sec)*1000 + (end.tv_usec - start.tv_usec)/1000);
return 0;
}
main.c
/*************************************************************************
> File Name: main.c
> 作者:YJK
> Mail: [email protected]
> Created Time: 2020年11月21日 星期六 18时46分19秒
************************************************************************/
#include<stdio.h>
#include"camer.h"
#define DEVICE_NAME "/dev/video0"
#define FILE_NAME "./out.yuv"
int main(int argc,char *argv[])
{
int ret = 0;
int i;
ret = open_device(DEVICE_NAME);
if (ret == -1)
exit(EXIT_FAILURE);
open_file(FILE_NAME);
init_device();
init_mmap();
start_stream();
for (i = 0 ; i < 600; i++)
{
process_frame();
printf("frame:%d\n",i);
}
end_stream();
close_mmap();
close_device();
return 0;
}
Makefile
#########################################################################
# File Name: Makefile
# 作者:YJK
# mail: [email protected]
# Created Time: 2021年04月29日 星期四 20时47分10秒
#########################################################################
# $^ 当前规则中的所有依赖
# $< 当前规则中的第一个依赖
# $@ 当前规则中触发命令生成的目标
# @ 不把执行的信息打印到显示屏上
CC = arm-linux-gcc
TARGET = main
OBJS += main.o
OBJS += camer.o
$(TARGET) : $(OBJS)
$(CC) -o $@ $^
%.o : %.c
$(CC) -c $< -o $@
clean:
rm $(TARGET) $(OBJS)
特别强调
Makefile文件中CC指定的是交叉编译的编译工具,我的是arm-linux-gcc,
其他的交叉编译,比如之前的交叉编译环境是arm-none-linux-gnueabi-gcc
就把CC改为CC = arm-none-linux-gnueabi-gcc
CC = arm-linux-gcc
交叉编译驱动程序
在安装Gcc的redHat上新建一个目录:main.c、camer.h、camer.c、Makefile在同一目录下 make编译,编译后如下: 生成main,接下来把main移植到试验箱
移植和演示程序
移植程序的方法有很多:1. 拷贝到U盘中,再把U盘挂载到实验箱
2. 在PC端使用tftpd32软件,网络通信将程序发送到实验箱
3. 配置samba服务使得PC端、ubuntu和实验箱共享一个网络映射文件
这里只移植一个main所以,用U盘挂载的方法;
1、把main拷贝到U盘
2、把U盘插入实验箱对应的插口
插入以后实验箱打印如下信息: 3、查看U盘信息
命令: fdisk -l 4、挂载U盘
命令:mount -t vfat /dev/sda4 /mnt 5、运行程序
命令: ./main
结束:
7、卸载U盘
U盘大的可能时间长一点十多秒 8、在Win10播放
使用的播放器是YUVPlayer.exe
因为视频格式是yuv格式
参数设置