SPI总线(三):驱动实例

本文介绍了一款针对Firefly RK3399平台的SPI演示程序,该程序能够通过两种不同的SPI接口操作方式来读取并打印设备ID。一种方式使用spi_transfer和spi_message进行数据传输;另一种方式则利用spi_write_then_read来完成读写操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文转自https://blog.youkuaiyun.com/kai_zone/article/details/78043303

平台:firefly-rk3399


详细配置参照:http://www.t-firefly.com/doc/product/info/id/92.html#SPI.E5.B7.A5.E4.BD.9C.E6.96.B9.E5.BC.8F



   
  1. /*
  2. * Driver for pwm demo on Firefly board.
  3. *
  4. * Copyright (C) 2016, Zhongshan T-chip Intelligent Technology Co.,ltd.
  5. * Copyright 2006 Sam Chan
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #define DEBUG
  12. #include <linux/module.h>
  13. #include <linux/kernel.h>
  14. #include <linux/init.h>
  15. #include <linux/err.h>
  16. #include <linux/io.h>
  17. #include <linux/of.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/spi/spi.h>
  21. #include <linux/spi/spidev.h>
  22. #define FIREFLY_SPI_READ_ID_CMD 0x9F
  23. #define FIREFLY_SPI_PRINT_ID(rbuf) \
  24. do { \
  25. if (status == 0) \
  26. dev_dbg(&spi->dev, ”%s: ID = %02x %02x %02x %02x %02x\n” , __FUNCTION__, \
  27. rbuf[0], rbuf[1], rbuf[2], rbuf[3], rbuf[4]); \
  28. else \
  29. dev_err(&spi->dev, ”%s: read ID error\n” , __FUNCTION__); \
  30. }while(0)
  31. static int firefly_spi_read_w25x_id_0(struct spi_device *spi)
  32. {
  33. int status;
  34. char tbuf[]={FIREFLY_SPI_READ_ID_CMD};
  35. char rbuf[ 5];
  36. struct spi_transfer t = {
  37. .tx_buf = tbuf,
  38. .len = sizeof(tbuf),
  39. };
  40. struct spi_transfer r = {
  41. .rx_buf = rbuf,
  42. .len = sizeof(rbuf),
  43. };
  44. struct spi_message m;
  45. spi_message_init(&m);
  46. spi_message_add_tail(&t, &m);
  47. spi_message_add_tail(&r, &m);
  48. status = spi_sync(spi, &m);
  49. FIREFLY_SPI_PRINT_ID(rbuf);
  50. return status;
  51. }
  52. static int firefly_spi_read_w25x_id_1(struct spi_device *spi)
  53. {
  54. int status;
  55. char tbuf[] = {FIREFLY_SPI_READ_ID_CMD};
  56. char rbuf[ 5];
  57. status = spi_write_then_read(spi, tbuf, sizeof(tbuf), rbuf, sizeof(rbuf));
  58. FIREFLY_SPI_PRINT_ID(rbuf);
  59. return status;
  60. }
  61. static int firefly_spi_probe(struct spi_device *spi)
  62. {
  63. int ret = 0;
  64. struct device_node __maybe_unused *np = spi->dev.of_node;
  65. dev_dbg(&spi->dev, ”Firefly SPI demo program\n”);
  66. if(!spi)
  67. return -ENOMEM;
  68. dev_dbg(&spi->dev, ”firefly_spi_probe: setup mode %d, %s%s%s%s%u bits/w, %u Hz max\n”,
  69. ( int) (spi->mode & (SPI_CPOL | SPI_CPHA)),
  70. (spi->mode & SPI_CS_HIGH) ? ”cs_high, “ : ”“,
  71. (spi->mode & SPI_LSB_FIRST) ? ”lsb, “ : ”“,
  72. (spi->mode & SPI_3WIRE) ? ”3wire, “ : ”“,
  73. (spi->mode & SPI_LOOP) ? ”loopback, “ : ”“,
  74. spi->bits_per_word, spi->max_speed_hz);
  75. firefly_spi_read_w25x_id_0(spi);
  76. firefly_spi_read_w25x_id_1(spi);
  77. return ret;
  78. }
  79. static struct of_device_id firefly_match_table[] = {
  80. { .compatible = ”firefly,rk3399-spi”,},
  81. {},
  82. };
  83. static struct spi_driver firefly_spi_driver = {
  84. .driver = {
  85. .name = ”firefly-spi”,
  86. .owner = THIS_MODULE,
  87. .of_match_table = firefly_match_table,
  88. },
  89. .probe = firefly_spi_probe,
  90. };
  91. static int firefly_spi_init(void)
  92. {
  93. return spi_register_driver(&firefly_spi_driver);
  94. }
  95. module_init(firefly_spi_init);
  96. static void firefly_spi_exit(void)
  97. {
  98. spi_unregister_driver(&firefly_spi_driver);
  99. }
  100. module_exit(firefly_spi_exit);
  101. MODULE_DESCRIPTION( ”Firefly SPI demo driver”);
  102. MODULE_ALIAS( ”platform:firefly-spi”);
  103. MODULE_LICENSE( ”GPL”);

此驱动只是实现简单的读取设备的ID,然后打印出来,并没有实现API接口


firefly_spi_probe中使用了两种接口操作读取W25Q128FV的ID:
firefly_spi_read_w25x_id_0接口直接使用了spi_transfer和spi_message来传送数据。
firefly_spi_read_w25x_id_1接口则使用SPI接口spi_write_then_read来读写数据。

编写spi驱动时,读写设备科参照此驱动,添加spi的API接口。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值