sc2440串口驱动

 


 

arm平台上的应用程序

app.c源代码 

[c-sharp:nogutter] view plain copy print ?
  1. /* 
  2.  * ===================================================================================== 
  3.  * 
  4.  *       Filename:  app.c 
  5.  * 
  6.  *    Description:  可以在arm平台上运行的应用程序 
  7.  * 
  8.  *        Version:  1.0 
  9.  *        Created:  2011年01月11日 00时13分58秒 
  10.  *       Revision:  none 
  11.  *       Compiler:  gcc 
  12.  * 
  13.  *         Author:  sunsea1026 
  14.  *          Email:  sunsea1026@gmail.com  
  15.  * 
  16.  * ===================================================================================== 
  17.  */  
  18. #include <stdio.h>  
  19. #include <string.h>  
  20. #include <fcntl.h>    //open(), read(), write()  
  21. #define BUFSIZE 100  
  22. int main(int argc, char* argv[])  
  23. {  
  24.     int fd, ret;  
  25.     char buf[BUFSIZE];  
  26.     char *msg = "We have open ttyS0, we waited in the receiving data...";  
  27.     fd = open("/dev/ttyS0", O_RDWR);  
  28.     if(fd < 0)  
  29.     {  
  30.         printf("Error: open /dev/ttyS0 error!/n");  
  31.         return 1;  
  32.     }  
  33.     ret = write(fd, msg, strlen(msg));  
  34.     if(ret < 0)  
  35.     {  
  36.         printf("Error: write msg error!/n");   
  37.         return 1;  
  38.     }  
  39.     while(1)  
  40.     {  
  41.         memset(buf, 0, sizeof(buf));  
  42.         ret = read(fd, buf, BUFSIZE);  
  43.         if(ret < 0)  
  44.         {  
  45.             printf("Error: read device error!/n");   
  46.             return 1;  
  47.         }  
  48.         if(buf[0] != '/0')  
  49.         {  
  50.             printf("%s/n", buf);   
  51.         }  
  52.     }  
  53.     return 0;   
  54. }  
 

编译方法

arm-linux-gcc app.c -o app

 


 

驱动部分

s3c2440_seial.c源代码  

[c-sharp:nogutter] view plain copy print ?
  1. /* 
  2.  * ===================================================================================== 
  3.  * 
  4.  *       Filename:  s3c2440_serial.c 
  5.  * 
  6.  *    Description:  s3c2440 串口驱动程序 
  7.  * 
  8.  *        Version:  1.0 
  9.  *        Created:  2011年01月11日 00时29分43秒 
  10.  *       Revision:  none 
  11.  *       Compiler:  linux 内核 
  12.  * 
  13.  *         Author:  sunsea1026 
  14.  *          Email:  sunsea1026@gmail.com 
  15.  * 
  16.  * ===================================================================================== 
  17.  */  
  18. #include <linux/module.h>  
  19. #include <linux/kernel.h>  
  20. #include <asm/uaccess.h>          //copy_to_use, copy_from_user  
  21. #include <linux/serial_core.h>  
  22. #include <asm/plat-s3c/regs-serial.h>     //寄存器宏  
  23. #include <asm/io.h>               //readl, readb, writel, writeb  
  24. #define BUFSIZE 100  
  25. #define ttyS0_MAJOR 240  
  26. #define iobase      S3C24XX_VA_UART1  
  27. #define UART_ULCON1 iobase  
  28. #define UART_UCON1  iobase + 0x4  
  29. #define UART_UFCON1 iobase + 0x8  
  30. #define UART_UTRSTAT1   iobase + 0x10  
  31. #define UART_UTXH1  iobase + 0x20  
  32. #define UART_URXH1  iobase + 0x24  
  33. #define UART_UBRDIV1    iobase + 0x28  
  34.   
  35. MODULE_AUTHOR("sunsea");  
  36. MODULE_DESCRIPTION("s3c2440 serial driver");  
  37. MODULE_LICENSE("GPL");  
  38. int sunsea_open(struct inode *inode, struct file *filp)  
  39. {  
  40.     /* 8N1 */  
  41.     writel(3, UART_ULCON1);  
  42.     /* poll mode */  
  43.     writel(5, UART_UCON1);  
  44.     /* 115200 */  
  45.     writel(26, UART_UBRDIV1);  
  46.     return 0;  
  47. }  
  48. ssize_t sunsea_write(struct file *filp, const char __user *buf, size_t count, loff_t *f_pos)  
  49. {  
  50.     char wbuf[BUFSIZE] = {0};  
  51.     int state;  
  52.     int i = 0;  
  53.     copy_from_user(wbuf, buf, count);  
  54.     while(wbuf[i] != '/0')  
  55.     {  
  56.         state = readl(UART_UTRSTAT1);  
  57.         if((0x02 & state) == 2)  
  58.         {  
  59.             writeb(wbuf[i], UART_UTXH1);  
  60.             i++;  
  61.         }  
  62.     }  
  63.     return 0;  
  64. }  
  65. ssize_t sunsea_read(struct file *filp, char __user *buf, size_t count, loff_t *f_ops)  
  66. {  
  67.     char rbuf[1] = {0};  
  68.     int state;  
  69.     state = readl(UART_UTRSTAT1);  
  70.     if((0x01 & state) == 1)  
  71.     {  
  72.         rbuf[0] = readb(UART_URXH1);  
  73.         copy_to_user(buf, rbuf, 1);  
  74.     }  
  75.     else  
  76.     {  
  77.         set_current_state(TASK_INTERRUPTIBLE);  
  78.         schedule_timeout(10);  
  79.     }  
  80.     return 0;   
  81. }  
  82. int sunsea_release(struct inode *inode, struct file *filp)  
  83. {  
  84.     return 0;  
  85. }  
  86. struct file_operations ttyS0_fops =   
  87. {  
  88.     .owner = THIS_MODULE,  
  89.     .open = sunsea_open,  
  90.     .write = sunsea_write,  
  91.     .read = sunsea_read,  
  92.     .release = sunsea_release,  
  93. };  
  94. int __init  
  95. sunsea_init(void)  
  96. {  
  97.     int rc;  
  98.     printk("s3c2440 serial module loaded!/n");   
  99.     rc = register_chrdev(ttyS0_MAJOR, "ttyS0", &ttyS0_fops);  
  100.     if(rc < 0)  
  101.     {  
  102.         printk("Error: register ttyS0 device error!/n") ;  
  103.         return -1;  
  104.     }  
  105.     return 0;   
  106. }  
  107. void __exit  
  108. sunsea_exit(void)  
  109. {  
  110.     unregister_chrdev(ttyS0_MAJOR, "ttyS0");  
  111.     printk("s3c2440 serial module exit!/n");   
  112.     return;  
  113. }  
  114. module_init(sunsea_init);  
  115. module_exit(sunsea_exit);  
 

Makefile源代码  

[c-sharp:nogutter] view plain copy print ?
  1. ifneq ($(KERNELRELEASE),)  
  2. obj-m := s3c2440_serial.o  
  3. else  
  4. #在zsx-ubuntu下编译使用下面一行  
  5. #KERNELSRC := /home/zsx/study/term6/build-embedded/kernel-2.6.27-android_ok  
  6. #在aka-ubuntu下编译使用下面一行  
  7. KERNELSRC := /home/akaedu/build-embedded/kernel-2.6.27-android_ok  
  8. modules:  
  9.     make -C $(KERNELSRC) SUBDIRS=$(PWD) $@  
  10. clean:  
  11.     rm -f *.o *.ko *.mod *.mod.c *.order *.symvers  
  12. endif 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值