博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
s3c2440驱动
阅读量:4052 次
发布时间:2019-05-25

本文共 2029 字,大约阅读时间需要 6 分钟。

#include <linux/module.h>
#include <linux/kernel.h>
#include <asm/uaccess.h> //copy_to_use, copy_from_user
#include <linux/serial_core.h>
#include <asm/plat-s3c/regs-serial.h> //寄存器宏
#include <asm/io.h> //readl, readb, writel, writeb
#define BUFSIZE 100
#define ttyS0_MAJOR 240
#define iobase S3C24XX_VA_UART1
#define UART_ULCON1 iobase
#define UART_UCON1 iobase + 0x4
#define UART_UFCON1 iobase + 0x8
#define UART_UTRSTAT1 iobase + 0x10
#define UART_UTXH1 iobase + 0x20
#define UART_URXH1 iobase + 0x24
#define UART_UBRDIV1 iobase + 0x28
MODULE_AUTHOR("sunsea");
MODULE_DESCRIPTION("s3c2440 serial driver");
MODULE_LICENSE("GPL");
int sunsea_open(struct inode *inode, struct file *filp)
{
writel(3, UART_ULCON1);
writel(5, UART_UCON1);
writel(26, UART_UBRDIV1);
return 0;
}
ssize_t sunsea_write(struct file *filp, const char __user *buf, size_t count, loff_t *f_pos)
{
char wbuf[BUFSIZE] = {0};
int state;
int i = 0;
copy_from_user(wbuf, buf, count);
while(wbuf[i] != '/0')
{
state = readl(UART_UTRSTAT1);
if((0x02 & state) == 2)
{
writeb(wbuf[i], UART_UTXH1);
i++;
}
}
return 0;
}
ssize_t sunsea_read(struct file *filp, char __user *buf, size_t count, loff_t *f_ops)
{
char rbuf[1] = {0};
int state;
state = readl(UART_UTRSTAT1);
if((0x01 & state) == 1)
{
rbuf[0] = readb(UART_URXH1);
copy_to_user(buf, rbuf, 1);
}
else
{
set_current_state(TASK_INTERRUPTIBLE);
schedule_timeout(10);
}
return 0; 
}
int sunsea_release(struct inode *inode, struct file *filp)
{
return 0;
}
struct file_operations ttyS0_fops = 
{
.owner = THIS_MODULE,
.open = sunsea_open,
.write = sunsea_write,
.read = sunsea_read,
.release = sunsea_release,
};
int __init
sunsea_init(void)
{
int rc;
printk("s3c2440 serial module loaded!/n"); 
rc = register_chrdev(ttyS0_MAJOR, "ttyS0", &ttyS0_fops);
if(rc < 0)
{
printk("Error: register ttyS0 device error!/n") ;
return -1;
}
return 0; 
}
void __exit
sunsea_exit(void)
{
unregister_chrdev(ttyS0_MAJOR, "ttyS0");
printk("s3c2440 serial module exit!/n"); 
return;
}
module_init(sunsea_init);
module_exit(sunsea_exit);

转载地址:http://ojpci.baihongyu.com/

你可能感兴趣的文章
分布式缓存负载均衡负载均衡的缓存处理:虚拟节点对一致性hash的改进
查看>>
分布式存储系统设计(1)—— 系统架构
查看>>
MySQL数据库的高可用方案总结
查看>>
常用排序算法总结(一) 比较算法总结
查看>>
SSH原理与运用
查看>>
SIGN UP BEC2
查看>>
S3C2440中对LED驱动电路的理解
查看>>
《天亮了》韩红
查看>>
Windows CE下USB摄像头驱动开发(以OV511为例,附带全部源代码以及讲解) [转]
查看>>
出现( linker command failed with exit code 1)错误总结
查看>>
iOS开发中一些常见的并行处理
查看>>
iOS获取手机的Mac地址
查看>>
ios7.1发布企业证书测试包的问题
查看>>
如何自定义iOS中的控件
查看>>
iOS 开发百问
查看>>
Mac环境下svn的使用
查看>>
github简单使用教程
查看>>
如何高效利用GitHub
查看>>
环境分支-git版本管理
查看>>
uni-app 全局变量
查看>>