利用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
阅读(2438) | 评论(0) | 转发(0) |