博客
关于我
强烈建议你试试无所不能的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://mjpci.baihongyu.com/

你可能感兴趣的文章
多线程使用随机函数需要注意的一点
查看>>
getpeername,getsockname
查看>>
关于对象赋值及返回临时对象过程中的构造与析构
查看>>
VS 2005 CRT函数的安全性增强版本
查看>>
Visual Studio 2010:C++0x新特性
查看>>
drwtsn32.exe和adplus.vbs进行dump文件抓取
查看>>
cppcheck c++静态代码检查
查看>>
在C++中使用Lua
查看>>
一些socket的编程经验
查看>>
socket编程中select的使用
查看>>
可以在线C++编译的工具站点
查看>>
关于无人驾驶的过去、现在以及未来,看这篇文章就够了!
查看>>
所谓的进步和提升,就是完成认知升级
查看>>
为什么读了很多书,却学不到什么东西?
查看>>
长文干货:如何轻松应对工作中最棘手的13种场景?
查看>>
如何用好碎片化时间,让思维更有效率?
查看>>
No.174 - LeetCode1305 - 合并两个搜索树
查看>>
No.175 - LeetCode1306
查看>>
No.176 - LeetCode1309
查看>>
No.182 - LeetCode1325 - C指针的魅力
查看>>