使用 spi-bitbang 驱动程序

Using spi-bitbang driver

我正在编写一个内核模块来读写 SPI 设备 (CC1200)。 我的 linux 设备没有原生 SPI,因此我正在尝试对总线进行位冲击。

我发现linux有内置的bitbang代码(linux/spi/spi_bitbang.h),但我不知道如何设置它。它需要结构 spi_device 和 spi_master,每个都需要结构设备,它需要结构作为 kobject 等等,其中大部分我不知道如何处理它们,以及如何需要它们简单的位撞击。

我在网上查找示例,但我确实找到了 none。没有一次使用包含的 bitbang 代码,只有一些引用 "easy"

我将非常感谢任何帮助,也许 bitbang 库甚至不是好的路径。也许我可以自己写(如何有效地做到这一点?我有 4 个内核,但它在后台 运行 很多东西)

谢谢

由于 SPI 的性质,其中数据由主机计时和读取,因此主机的位碰撞驱动程序没有任何问题,因为从机 应该 不依赖于稳定的时钟。但是当然这在实践中是否有效取决于从属设备。

如果您使用的是 linux 内核,则无需实现您自己的 bit-banging 驱动程序,因为已经有一个 spi-gpio.c

我的猜测是如何启动它,运行 将通过定义 GPIOdevicetree 中使用的引脚,然后驱动程序将能够充当任何其他物理层驱动程序。

我快速浏览了 drivers/spi/spi-gpio.c 源代码,甚至还有一个简短的用户指南,说明如何在不使用通用 GPIO 层开销的情况下直接访问内联 GPIO 引脚。

/*
 * Because the overhead of going through four GPIO procedure calls
 * per transferred bit can make performance a problem, this code
 * is set up so that you can use it in either of two ways:
 *
 *   - The slow generic way:  set up platform_data to hold the GPIO
 *     numbers used for MISO/MOSI/SCK, and issue procedure calls for
 *     each of them.  This driver can handle several such busses.
 *
 *   - The quicker inlined way:  only helps with platform GPIO code
 *     that inlines operations for constant GPIOs.  This can give
 *     you tight (fast!) inner loops, but each such bus needs a
 *     new driver.  You'll define a new C file, with Makefile and
 *     Kconfig support; the C code can be a total of six lines:
 *
 *    #define DRIVER_NAME  "myboard_spi2"
 *    #define  SPI_MISO_GPIO  119
 *    #define  SPI_MOSI_GPIO  120
 *    #define  SPI_SCK_GPIO   121
 *    #define  SPI_N_CHIPSEL  4
 *    #include "spi-gpio.c"
 */

PS你确定你的平台没有spi吗,我用过的所有海思SoC都有。我会先仔细检查一下

编辑:看来他们已经更改了界面,您不能再从 4.19 及更高版本的现代内核中执行此操作,原因与您不能用 i2c 做。 https://forum.openwrt.org/t/i2c-kernel-4-19-i2c-gpio-custom/49213 我暂时把它留在这里,也许它会有用。我自己正在使用旧内核进行家庭自动化,但它在专用网络上。

我原来的回答:

虽然你可以通过破解你自己的内核模块来实现它,而不是你可以研究 spi-gpio-custom,只需加载模块并传递你想用作参数的引脚,你可以在 运行-time 并且不需要 'compile-in' 您要使用的引脚。你可以在OpenWrt项目中找到这个模块:

https://github.com/openwrt/openwrt/tree/openwrt-19.07/package/kernel/spi-gpio-custom/src

我希望在初始化时为您提供某种 SPI 设备,您可以从用户 space 向其写入。请注意,我自己没有使用过这个模块,但前段时间我使用了以类似方式工作的 i2c 对应物。

或者,您可以考虑引入一个 Arduino 来与您的 SPI 总线通信,然后将 SPI 数据转换成您的 Linux 设备可以理解的内容。

如果您不想自己制作电路板,Nano 电路板可能会满足您的需求,而且成本极低。

然后对于接口,您将有几个选择:USB、UART、i2c 或 1wire,如果您有引脚,甚至是并行接口。 github 上有一个 1-wire 从库,例如:https://github.com/smurfix/owslave.