Chinaunix首页 | 论坛 | 博客
  • 博客访问: 39290
  • 博文数量: 18
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 228
  • 用 户 组: 普通用户
  • 注册时间: 2014-04-15 16:53
文章分类
文章存档

2015年(4)

2014年(14)

我的朋友

分类: C/C++

2015-08-07 09:49:13

利用SPI驱动框架来编写驱动,并给应用层留出接口来操作SPI
1.首先在板子的BSP文件(board_sabresd.c)里面给出 spi_board_info
static struct spi_board_info spi_device_info[] = {
{
.modalias = "spidev",
//.platform_data = &ads_info,
//.controller_data = &mx6q_ads7846_cfg,
.max_speed_hz = 125000 * 16 , /* max sample rate * clocks per sample */
.bus_num = 4,
.chip_select = 2,
.mode = SPI_MODE_0,
},
{
.modalias = "spidev",
.max_speed_hz = 125000 * 16 , /* max sample rate * clocks per sample */
.bus_num = 4,
.chip_select = 1,
.mode = SPI_MODE_0,
},
{
.modalias = "spidev",
.max_speed_hz = 125000 * 16 , /* max sample rate * clocks per sample */
.bus_num = 4,
.chip_select = 0,
.mode = SPI_MODE_0,
},
};

2,在驱动中才能注册名称为"spidev“的SPI驱动,才可以调用probe函数。
static struct spi_driver spidev_spi_driver = {
.driver = {
.name = "spidev",
.owner = THIS_MODULE,
},
.probe = spidev_probe,
.remove = __devexit_p(spidev_remove),
};


3.在spi_init()的函数中创建字符驱动来给应用层留出接口
static int __init spidev_init(void)
{
int status;


/* Claim our 256 reserved device numbers.  Then register a class
* that will key udev/mdev to add/remove /dev nodes.  Last, register
* the driver which manages those device numbers.
*/
BUILD_BUG_ON(N_SPI_MINORS > 256);
status = register_chrdev(SPIDEV_MAJOR, "spi", &spidev_fops);
if (status < 0)
return status;


spidev_class = class_create(THIS_MODULE, "spidev");
if (IS_ERR(spidev_class)) {
unregister_chrdev(SPIDEV_MAJOR, spidev_spi_driver.driver.name);
return PTR_ERR(spidev_class);
}


status = spi_register_driver(&spidev_spi_driver);
if (status < 0) {
class_destroy(spidev_class);
unregister_chrdev(SPIDEV_MAJOR, spidev_spi_driver.driver.name);
}
return status;
}


注意:SPI总线是通过片选来操作不同的芯片, IIC是通过不用是设备地址来操作不同的芯片spidev.txt

阅读(843) | 评论(0) | 转发(0) |
0

上一篇:数组

下一篇:没有了

给主人留下些什么吧!~~
评论热议
请登录后评论。

登录 注册