s3c2440 串口驱动和测试程序

arm平台上的应用程序

app.c源代码 

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

编译方法

arm-linux-gcc app.c -o app

 

 

驱动部分

s3c2440_seial.c源代码  

 

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

Makefile源代码  

 

 
ifneq ($(KERNELRELEASE),)  
  1. obj-m := s3c2440_serial.o  
  2. else 
  3. #在zsx-ubuntu下编译使用下面一行 
  4. #KERNELSRC := /home/zsx/study/term6/build-embedded/kernel-2.6.27-android_ok 
  5. #在aka-ubuntu下编译使用下面一行  
  6. KERNELSRC := /home/akaedu/build-embedded/kernel-2.6.27-android_ok  
  7. modules:  
  8.     make -C $(KERNELSRC) SUBDIRS=$(PWD) $@  
  9. clean:  
  10.     rm -f *.o *.ko *.mod *.mod.c *.order *.symvers  
  11. endif  
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值