当前位置: 首页 > news >正文

练习uart和摄像头内核驱动开发测试

利用Linux内核驱动开发,目前仅完成串口测试和摄像头测试

1、串口:以内核自带的串口驱动为基础,编写测试程序

uart.h

#ifndef __UART_H__
#define __UART_H__#include <termios.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>// 打开串口设备并配置为115200波特率、8位数据位、无校验、1位停止位
int open_serial_port(const char *port);// 写入数据到串口
int write_to_serial(int fd, const char *data, size_t length);// 从串口读取数据
int read_from_serial(int fd, char *buffer, size_t buffer_size);// 关闭串口设备
void close_serial_port(int fd);#endif // TERMIOS_H

uart.c

#include "uart.h"
int open_serial_port(const char *port) {// 打开串口设备(阻塞模式)int fd = open(port, O_RDWR | O_NOCTTY);if (fd == -1) {perror("Error opening serial port");return -1;}// 获取当前串口配置struct termios options;if (tcgetattr(fd, &options) != 0) {perror("tcgetattr failed");close(fd);return -1;}// 设置波特率为115200cfsetispeed(&options, B115200);cfsetospeed(&options, B115200);// 配置数据位、停止位和校验位options.c_cflag &= ~CSIZE;  // 清除数据位掩码options.c_cflag |= CS8;     // 8位数据位options.c_cflag &= ~PARENB; // 无校验位options.c_cflag &= ~CSTOPB; // 1位停止位// 启用接收和本地模式options.c_cflag |= (CLOCAL | CREAD);// 设置原始输入模式(非规范模式)options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);// 禁用特殊输出处理options.c_oflag &= ~OPOST;// 设置超时:立即返回(VMIN=0, VTIME=0)//options.c_cc[VMIN] = 0;//options.c_cc[VTIME] = 0;// 修改 c_cc 配置,启用阻塞读options.c_cc[VMIN] = 1;   // 至少读取1个字符才返回options.c_cc[VTIME] = 10; // 等待超时:1秒(10 * 0.1秒)// 应用配置if (tcsetattr(fd, TCSANOW, &options) != 0) {perror("tcsetattr failed");close(fd);return -1;}// 清空缓冲区tcflush(fd, TCIOFLUSH);return fd;
}int write_to_serial(int fd, const char *data, size_t length) {ssize_t bytes_written = write(fd, data, length);if (bytes_written < 0) {perror("Error writing to serial port");}return bytes_written;
}int read_from_serial(int fd, char *buffer, size_t buffer_size) {ssize_t bytes_read = read(fd, buffer, buffer_size - 1);if (bytes_read < 0) {perror("Error reading from serial port");return -1;}buffer[bytes_read] = '\0'; // 添加字符串结束符return bytes_read;
}void close_serial_port(int fd) {if (fd >= 0) {close(fd);}
}

main.c

#include "uart.h"int main(void)
{int uartfd = 0;int nret = 0;char tmpbuff[4096] = {0};uartfd = open_serial_port("/dev/ttymxc2");if (-1 == uartfd){printf("open_serial_port failed\n");return -1;}while (1){   write_to_serial(uartfd, "hello world", 11);memset(tmpbuff, 0, sizeof(tmpbuff));nret = read_from_serial(uartfd, tmpbuff, sizeof(tmpbuff));printf("nret = %d, tmpbuff = %s\n", nret, tmpbuff);sleep(1);}close_serial_port(uartfd);return 0;
}

2、摄像头ov5640测试

一、修改设备树
1、    在i2c2下添加ov5640信息

2、    设置pinctrl子系统中的摄像头复位引脚

3、    开启CSI接口配置

二、修改内核加入ov5640相关驱动模块:
1、通过图形界面修改

2. 重新编译内核、设备树、驱动模块

make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- zImage
make dtbs
make modules


3. 将设备树和内核镜像拷贝到tftp管理目录下

cp arch/arm/boot/dts/imx6ull-alientek-emmc.dtb ~/tftpboot
cp arch/arm/boot/zImage ~/tftpboot


4. 将所有的内核驱动模块安装到文件系统下

make INSTALL_MOD_PATH=/home/linux/nfs/rootfs modules_install


5. 重新启动开发板,并插上ov5640摄像头
6. 在开发板端加载摄像头内核驱动模块

cd /lib/modules/4.1.15 depmod -a
modprobe mx6s_capture.ko modprobe ov5640_camera.ko


7. 查看是否生成摄像头设备节点

ls /dev/video1。


三、编写测试文件测试摄像头功能

http://www.xdnf.cn/news/1271413.html

相关文章:

  • 【Python 高频 API 速学 ⑦ · 完结篇】
  • Netbsd安装使用
  • Vue3的简单学习
  • java练习题:数字位数
  • Python(6) -- 数据容器
  • I2CHAL库接口
  • MCU-基于TC397的启动流程
  • nginx高性能web服务器
  • BroadcastChannel:轻松实现前端跨页面通信
  • 使用 Ansys Discovery 进行动态设计和分析
  • ​​​​​​​【Datawhale AI夏令营】多模态RAG财报问答挑战赛:学习笔记与上分思考
  • Java基础-完成局域网内沟通软件的开发
  • B.10.01.5-电商系统的设计模式应用实战
  • Day 8: 深度学习综合实战与进阶技术 - 从优化到部署的完整流程
  • 【Datawhale AI夏令营】从Baseline到SOTA:深度剖析金融问答RAG管道优化之路
  • Mybatis进阶
  • 机器学习第七课之支持向量机SVM
  • 本地进行语音文字互转
  • P1890 gcd区间
  • C++11中的移动语义
  • 【无标题】AI 赋能日常效率:实用案例与操作心得分享
  • B.10.01.6-DDD领域驱动设计:从理论到落地的完整指南
  • 数据挖掘2.6 Perceptron Modeling 感知器建模
  • Qdrant Filtering:must / should / must_not 全解析(含 Python 实操)
  • 心灵笔记:正念冥想
  • 解决python错误:playwright._impl._errors.TimeoutError: Timeout 30000ms exceeded.
  • 3.5.2_1 随机访问介质访问控制
  • Python中的Lambda函数详解
  • 【排序算法】④堆排序
  • NTP /Chrony 网络时间协议