linux下SPI驱动的补充

本文介绍在Linux 2.6内核环境下SPI驱动的编写方法,包括spi.h中的接口使用及spi设备读写操作示例。此外,还概述了SPI四种工作模式及如何从时序图中确定CPOL和CPHA。

先转网上的一篇文章:


【转】请教:Linux 2.6内核下spi驱动的编写

如果你需要使用spi驱动的接口,不是去修改驱动的实现代码,因为它只负责完成spi的硬件交互功能。 

你使用spi功能的代码只需要用到spi.h中定义的方法就可以了,这就是linux driver layers framework的可人之处。 

我们通过一个简单的例子来实际理解一下:

     
  1. #include <linux/config.h> 
  2. #include <linux/platform_device.h> 
  3. #include <linux/spi/spi.h> 
  4.  
  5. #define TEST_REG 0x02 
  6.  
  7. static int test_read_reg(struct spi_device *spi, int reg) 
  8. char buf[2]; 
  9. buf[0] = reg << 2; 
  10. buf[1] = 0; 
  11. spi_write_then_read(spi, buf, 2, buf, 2); 
  12. return buf[1] << 8 | buf[0]; 
  13.  
  14. static int spi_test_probe(struct spi_device *spi) 
  15. printk("TEST_REG: 0x%02x\n", test_read_reg(spi, TEST_REG)); 
  16. return 0; 
  17.  
  18. static int spi_test_remove(struct spi_device *spi) 
  19. return 0; 
  20.  
  21. static struct spi_driver spi_test_driver = { 
  22. .probe = spi_test_probe, 
  23. .remove = spi_test_remove, 
  24. .driver = { 
  25. .name = "testHW"
  26. }, 
  27. }; 
  28.  
  29. static int __init spi_test_init(void
  30. return spi_register_driver(&spi_test_driver); 
  31.  
  32. static void __exit spi_test_exit(void
  33. spi_unregister_driver(&spi_test_driver); 
  34.  
  35. module_init(spi_test_init); 
  36. module_exit(spi_test_exit); 
  37.  
  38. MODULE_DESCRIPTION("spi device test"); 
  39. MODULE_LICENSE("GPL");  

在这个驱动中,你只需要用spi_register_driver向系统进行注册,就可以让系统用你指定的与 .name 相匹配的硬件交互代码 
去执行你的读写请求。 

几乎在所有与spi相关的函数中都会用到struct spi_device *spi这个指针,probe函数正好把这个指针传给你,保存好这个指针,你就可以在驱动的任何地方通过他去处理与spi设备相关的操作。


另外,补充一些,在内核的文档中Documentation/spi,spi-summary较详细的介绍了spi的四种模式,以及如何在linux中编写master驱动和slave驱动,摘抄部分内容:

It's easy to be confused here, and the vendor documentation you'll
find isn't necessarily helpful.  The four modes combine two mode bits:

 - CPOL indicates the initial clock polarity.  CPOL=0 means the
   clock starts low, so the first (leading) edge is rising, and
   the second (trailing) edge is falling.  CPOL=1 means the clock
   starts high, so the first (leading) edge is falling.

 - CPHA indicates the clock phase used to sample data; CPHA=0 says
   sample on the leading edge, CPHA=1 means the trailing edge.

   Since the signal needs to stablize before it's sampled, CPHA=0
   implies that its data is written half a clock before the first
   clock edge.  The chipselect may have made it become available.

Chip specs won't always say "uses SPI mode X" in as many words,
but their timing diagrams will make the CPOL and CPHA modes clear.

In the SPI mode number, CPOL is the high order bit and CPHA is the
low order bit.  So when a chip's timing diagram shows the clock
starting low (CPOL=0) and data stabilized for sampling during the
trailing clock edge (CPHA=1), that's SPI mode 1.



评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值