Turn from: 8745577
In the process of porting Android HAL, the primary task of porting is to ensure that the driver is intact and that the camera is a character device, but it is not as simple to write the test code as the general driver, because the camera's test code must be using the V4L2 API. So the next step is to introduce the process of writing the test code for our camera.
1. Turn on the device
[C]View PlainCopy
- static int Open_device (char *dev_name)
- {
- ASSERT (Dev_name);
- int fd =-1;
- FD = open (Dev_name, O_RDWR);
- if (-1 = = FD)
- {
- MYLOGD ("open%s fail:%s\n", Dev_name, Strerror (errno));
- Exit (Exit_failure);
- }
- MYLOGD ("The FD of%s is%d", Dev_name, FD);
- return FD;
- }
2, initialize camera, format camera output image
[C]View PlainCopy
- static int init_cam_device (int dev_fd)
- {
- int ret =-1;
- int input_index;
- //ret = Fimc_v4l2_querycap (DEV_FD);
- //assert (ret = = 0);
- //Get to input source channel
- Input_index = Cam_v4l2_enuminput (DEV_FD);
- ASSERT (Input_index = = 0);
- ret = Cam_v4l2_s_input (DEV_FD, Input_index);
- ASSERT (ret = = 0);
- MYLOGD ("vidioc_s_fmt start ... dev_fd =%d\n", dev_fd);
- ret = Cam_v4l2_s_format (DEV_FD, Image_height, Image_width, V4L2_PIX_FMT_YUYV);
- ASSERT (ret = = 0);
- ret = CAM_V4L2_G_FMT (DEV_FD);
- ASSERT (ret = = 0);
- Init_cam_mmap (DEV_FD);
- return 1;
- }
4, request buffer to the kernel and mmap buffer map to reference space
struct buffer
{
void *start; The address after mmap;
size_t length//size;
}USER_BUFFERS[4]; Used to record the address and size of the kernel buffer mapping mmap to the user space
Request 4 Buffer
Cam_v4l2_reqbuf (DEV_FD, 4);
Query the buffer information requested, such as the location and size of each buffer
Cam_v4l2_querybuf (DEV_FD, &buf, Buf_index);
Map buffer to User space
User_buffers[buf_index].length = Buf.length;
User_buffers[buf_index].start = mmap (NULL buf.length,
Prot_read | Prot_write/* required */,
map_shared/* Recommended */,
DEV_FD, Buf.m.offset);
Comment: NULL:/* Start anywhere */
Buf.length:buffer the address of the kernel
Buf.m.offset: Buffer size in the kernel
5, put all the buffer in the loop work queue, centralized management
for (i = 0; i < max_buffers; ++i)
{
Cam_v4l2_qbuf (DEV_FD, i);
}
6. Start getting images:
IOCTL (DEV_FD, Vidioc_streamon, &type);
7, select to monitor camera data is ready
Fd_zero (&rd_set);
Fd_set (DEV_FD, &rd_set);
ret = SELECT (Maxfd + 1, &rd_set, NULL, NULL, NULL);
if (Fd_isset (DEV_FD, &rd_set))//If the camera is ready, you can read the data.
Read_frame (DEV_FD)
Implementation of 8,read_frame (DEV_FD):
Select only knows that there is data to read, but in multiple buffer,
Do not know which buffer is ready, so will be ready to buffer out of the queue, so that the number of buffer
int index = CAM_V4L2_DQBUF (DEV_FD);
Convert the yuv420 data in buffer to rgb565,
Because the LCD is not able to display YUV, it is necessary to convert YUV into rgb565
Yuyv_to_rgb (src_address, data_buf);
The converted rgb565 data is displayed in the framebuffer.
Show_rgb565_img (Data_buf, Lcd_width, lcd_height);
The process is simply to write down a process, each function is encapsulated once, no way to write it out,
So I will upload the source code files for everyone to download. Contains Android.mk
Can be downloaded to this link:
http://download.csdn.net/detail/ldswfun/5208766
And some pictures to get you to understand the process.
Android Camera (camera) v4l2 C test Code "go"