Uncategorized

spi driver notes

-2

I want to load my kernel module which communicates with my device over spi interface. I have tried loading my modules but it’s showing spidev is already installed.

I see 2 modules named spidev and spi_bcm2835 already installed. Can I know what does these modules do and help me in running my use case?

Also, I have tried removing spidev module but only driver is getting registered but probe is not getting called.

 New contributor
  • 1
    I am confused. Raspbian stretch has built in SPI, IC, UART. So it is not necessary to load the kernel modules yourself. – tlfong01 May 31 at 13:33   
  • I am also confused of your use of the term “use case”. Are you talking “UseCase Analysis” using the “UML/SysML” language? I also don’t understand “probe not getting called”. All sounds scary to a SPI newbie like me. BTW the SPI newbie, ie, me is now learning how to build a SPI touch LCD kernel module. He finds building a SPI kernel module very very hardt, not to mention developing one! 🙂 raspberrypi.stackexchange.com/questions/98549/… – tlfong01 Jun 1 at 1:33   
  • My above link to SPI kernel module building tutorial by developer uju on GitHub is very newbie friendly, with detailed instruction and explanation on how to remove unwanted old SPI touch LCD kernel module and build your own new SPI touch LCD kernel module. Perhaps you can let us know more about your SPI kernel module, if it is also on GitHub. – tlfong01 Jun 1 at 3:26   
  • Newbie True or False Qustions: (1) A kernel module is a compiled code that can be inserted into the kernel at run-time. (2) A driver is a compiled code that runs in the kernel to talk to hardware device. (3) A large part of a running kernel is driver code. (4) A driver may be built statically into the kernel file on Rpi SD card. (4) A driver may also be built as a kernel module so that it can be dynamically loaded later. (5) Not all kernel modules are drivers. (6) Some drivers are kernel modules. (7) A device tree is a kernel tree. (8) A kernel tree is a device tree. – tlfong01 yesterday    
  • “I want to load my kernel module which communicates with my device over spi interface.” – I am very confused. Can you tell us what is your “device”? Is it an SPI ADC such as MCP3008, or a SPI IO thing, such as MCP23S17? – tlfong01 yesterday   
  • @tlfong01, sorry for confusing you. What I want to do is: I want to load my driver which communicate over spi interface with my device. But some module that’s already present is not letting my driver to communicate with the device. – riteshseemakurty yesterday
  • And I don’t have my driver on Github. It’s proprietary. – riteshseemakurty yesterday
  • And regarding probe, probe is the function/call back called. by the kernel module, when a device info matched with the info provided by the device specific driver. – riteshseemakurty yesterday
  • Thank you for your explanation. But what confuses me is the following: (1) For the last 10 years as a Arduino and Rpi hobbyist, I have been writing drivers for sensors and actuators etc, and my drivers interface with SPI, I2C, UART, and also GPIO etc. I know I can use Raspbi Config to enable/disable Rpi’s SPI/I2C/UART drivers etc, by commenting out the /boot/config.txts’ dtoverly statements. What puzzles me is that you can do the very hard thing of writing a SPI kernel module, but you seem not to know the very simple thing of disabling a conflicting SPI kernel module. – tlfong01 yesterday   
  • I’m very new to work on SPI as well as Raspberry. What do you mean by conflicting? Is there anything that I have to change in my driver? – riteshseemakurty 9 hours ago
  • Ah, I did not realize that you are new to SPI and Rpi. I know your kernel module is proprietary, but perhaps you would at least let us know which platform and lanuage you are using: Arduino C++ or Win10IoT F#? To prevent further misunderstanding, I am drafting an answer. Please comment if my brief description of your question in my answer is totally wrong, or more or less OK? – tlfong01 8 hours ago   
  • Thanks a lot @tlfong01 My driver is written in c. And I didn’t install any kernel module. I’m just installing my driver. Sorry If my question is misleading. – riteshseemakurty 7 hours ago
  • You are welcome. No hurry, take your time. – tlfong01 7 hours ago   
  • Ya this is what i want and I also want to know what does spidev and spi_bcm2835 modules do and their differences? – riteshseemakurty 3 hours ago
  • Well, let us start with spidev, and forget spi_bcm8235 for now. To understand the spidev linux kernel driver, you need to have a very rough idea of what is DT, Device Tree, and DTO, DTB etc, and also the boot loader (which is in Rpi’s ROM, I guess). You might need to look at how a python/C module uses the spidev kernel driver to interface with a SPI device, such as a SPI touch LCD screen. I have summarized some points in the Appendices. – tlfong01 1 min ago   Edit   

2 Answers

0

Congratulations on writing a kernel module. That is quite hard.

The only thing which loads SPI on a modern Linux system will be device tree. You need to search /boot/config.txt and comment out or remove all SPI related lines. I believe most such lines will contain spi. You then need to reboot.

  • “only thing” … for many drivers, you can bind devices to drivers via sysfs. – domen yesterday
  • @domen I didn’t know that. If you want edit my reply to add that information or post a link and I will add it. – joan yesterday
  • I read that to run/launch a kernel module, eg, the ili9341 SPI touch LCD kernel module, the “only thing” you need to do is edit the file /etc/rc.local, adding the following line: sudo /path/to/fbcp-ili9341/build/fbcp-ili9341 &– tlfong01 yesterday    

-1

Question

  1. I want to load my kernel module which communicates with my device over spi interface. I have tried loading my modules but it’s showing spidev is already installed.
  2. I see 2 modules named spidev and spi_bcm2835 already installed. Can I know what does these modules do and help me in running my use case?
  3. Also, I have tried removing spidev module but only driver is getting registered but probe is not getting called.
  4. I’m very new to work on SPI as well as Raspberry. What do you mean by conflicting? Is there anything that I have to change in my driver?

Answer

Brief description of the OP’s question.

He has developed a kernel module and wishes to run in Rpi Raspbian with SPI interface. He found that Raspboian already has two drivers installed:

(1) spidev,

(2) spi_bcm2835.

He was wondering if he should

(a) remove those two SPI drivers, or

(b) interface with those two drivers.

/ to continue, …

References

/ to add later, …

Appendices

Appendix A – What is the difference between kernel drivers and kernel modules?

Reading notes

A kernel module is a bit of compiled code that can be inserted into the kernel at run-time, such as with insmod or modprobe.

A driver is a bit of code that runs in the kernel to talk to some hardware device. It “drives” the hardware. Most every bit of hardware in your computer has an associated driver. A large part of a running kernel is driver code.

A driver may be built statically into the kernel file on disk. A driver may also be built as a kernel module so that it can be dynamically loaded later. (And then maybe unloaded.)

Standard practice is to build drivers as kernel modules where possible, rather than link them statically to the kernel, since that gives more flexibility. There are good reasons not to, however:

Sometimes a given driver is absolutely necessary to help the system boot up. That doesn’t happen as often as you might imagine, due to the initrd feature.

Statically built drivers may be exactly what you want in a system that is statically scoped, such as an embedded system. That is to say, if you know in advance exactly which drivers will always be needed and that this will never change, you have a good reason not to bother with dynamic kernel modules.

If you build your kernel statically and disable Linux’s dynamic module loading feature, you prevent run-time modification of the kernel code. This provides additional security and stability at the expense of flexibility.

Not all kernel modules are drivers. For example, a relatively recent feature in the Linux kernel is that you can load a different process scheduler. Another example is that the more complex types of hardware often have multiple generic layers that sit between the low-level hardware driver and userland, such as the USB HID driver, which implements a particular element of the USB stack, independent of the underlying hardware

Appendix B – Example of a statically bound driver module

Reading Notes

To add the DS18B20 One Wire Temperature Sensor driver module to the device tree, add the following line in /boot/config.txt

dtoverlay=w1-gpio,gpiopin=14

Appendix C – Example of a dynamically bound kernel module

Reading notes

To launch the ILI9342 touch LCD kernel module at startup, add a line in the file /etc/rc.local

sudo /path/to/fbcp-ili9341/build/fbcp-ili9341 &

Appendix D – Device Tree Overlays (DT, DTB, DTO, Boot Loader etc) – Android Source

Reading Notes

A device tree (DT) is a data structure of named nodes and properties that describe non-discoverable hardware. Operating systems, such as the Linux kernel used in Android, use DTs to support a wide range of hardware configurations used by Android-powered devices. Hardware vendors supply their own DT source files, which Linux then compiles into the Device Tree Blob (DTB) file used by the bootloader.

A device tree overlay (DTO) enables a central device tree blob (DTB) to be overlaid on the device tree. A bootloader using DTO can maintain the system-on-chip (SoC) DT and dynamically overlay a device-specific DT, adding nodes to the tree and making changes to properties in the existing tree.

This page details a typical bootloader workflow for loading a DT and provides a list of common DT terms. Other pages in this section describe how to implement bootloader support for DTO, how to compile, verify, and optimize your DTO implementation, and how to use multiple DTs. You can also get details on DTO syntax and required DTO/DTBO partition formatting.

Appendix E – Python SPI Kernel Driver [Project]

This project contains a python module for interfacing with SPI devices from user space via the spidev linux kernel driver.

import spidev

spi = spidev.SpiDev()

spi.open(bus, device)

to_send = [0x01, 0x02, 0x03]

spi.xfer(to_send)

/ to continue, …

Categories: Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: