[RK3288][Android6.0] 串口驱动流程小结

本文总结了在RK3288平台上的Android 6.0系统中,如何进行串口驱动的流程。通过引用外部图片,展示了串口驱动的框架,详细说明了数据如何从serial_out最终输出到硬件串口。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Platform: ROCKCHIP
OS: Android 6.0
Kernel: 3.10.92


RK的调试串口使用的是另外一套方法(后面再提), 这里的流程针对普通的串口.


相关文件:
rk_serial.c:  rk uart驱动文件.
serial_core.c:     核心层文件, 通过它调用到具体的设备驱动文件.
tty_io.c: 字符设备操作相关,上层和它打交道.
tty_port.c: 每个串口设备用
proc_tty.c: proc文件系统下创建tty相关文件.
n_tty.c: 上层read/write需要经过line discipline, 通俗理解就是读写要经过解析,比如\n,\r,\t这些特殊字符.


借用别人画的图,方便理解串口驱动的框架:


初始化:
serial_rk_init ->
    uart_register_driver ->     serial_core.c //注册uart driver到系统中,参数是serial_rk_reg
        alloc_tty_driver    tty_driver.h //分配一个tty driver
        tty_set_operations    tty_io.c //设置tyy driver的ops为struct tty_operations uart_ops
android 串口驱动源代码 package android.serialport; import java.io.File; import java.io.FileDescriptor; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import android.util.Log; public class SerialPort { private static final String TAG = "SerialPort"; /* * Do not remove or rename the field mFd: it is used by native method close(); */ private FileDescriptor mFd; private FileInputStream mFileInputStream; private FileOutputStream mFileOutputStream; public SerialPort(File device, int baudrate) throws SecurityException, IOException { /* Check access permission */ if (!device.canRead() || !device.canWrite()) { try { /* Missing read/write permission, trying to chmod the file */ Process su; su = Runtime.getRuntime().exec("/system/bin/su"); /*String cmd = "chmod 777 " + device.getAbsolutePath() + "\n" + "exit\n";*/ String cmd = "chmod 777 /dev/s3c_serial0" + "\n" + "exit\n"; su.getOutputStream().write(cmd.getBytes()); if ((su.waitFor() != 0) || !device.canRead() || !device.canWrite()) { throw new SecurityException(); } } catch (Exception e) { e.printStackTrace(); throw new SecurityException(); } } mFd = open(device.getAbsolutePath(), baudrate); if (mFd == null) { Log.e(TAG, "native open returns null"); throw new IOException(); } mFileInputStream = new FileInputStream(mFd); mFileOutputStream = new FileOutputStream(mFd); } // Getters and setters public InputStream getInputStream() { return mFileInputStream; } public OutputStream getOutputStream() { return mFileOutputStream; } // JNI private native static FileDescriptor open(String path, int baudrate); public native void close(); static { System.loadLibrary("serial_port"); } }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值