I have no experience writing kernel modules. I build some examples with gpio interrupts and they worked prety well. Now I want to use SPI from my kernel module and I will explain why I need it.

I am working with a ADC which has a continuous mode, so this device generates a falling edge in a pin warning that there is a new sample available. When this edge is detected, it's necessary to read that sample via SPI. I was trying to do this operation from user space, but I realized that I had not time guarantee.

I wrote a kernel module that when detectes the rising edge, it generates a pulse with the same width that my reading will take. In the interrupt pin I applied a square wave with the same period that my ADC's sample rate. With an oscilloscope I monitored the signals. And the time accuracy results where very good, they fit my application very well.

I follow the same method with a code in user space and the results in oscilloscope where a total disaster. That's why I believe I have to read my SPI bus from kernel space.

I was taking a look at spidev source code, but I am not sure about all the things I have to implement. Does anybody have some example reading and writing to SPI from kernel space?

Thanks in advance.

  • At a glance the protocol looks pretty simple, simpler than even UART (although all I did was glance and there may be more to it): circuitbasics.com/basics-of-the-spi-communication-protocol – goldilocks Apr 3 at 15:03
  • @goldilocks, thanks for your answer. I know the protocol is simple, but the BCM2835 has integrated SPI, so I would like to use the hardware solution, without having to manage gpio pins on my own. The same way we have linux/gpio.h api, we have linux/spi.h. But I have no experience using this API. I would appreciate if anybody could help me sending some example. – Murilo Machado Pinto Apr 3 at 15:12
  • Ah right, silly me. The hardware does the protocol (flex: I've implemented UART using an FPGA before and was thinking like that about it). Have you looked at the kernel docs? Dunno if they have any relevance: kernel.org/doc/html/latest/spi/index.html – goldilocks Apr 3 at 15:19
  • @goldilocks, I am following this documentantion, but it's a kind of hard for me. Because as I said, I have no experience writing kernel modules. I will try more and I let you know if I have new progress. Right now I am reading SPI kernel docs: kernel.org/doc/html/v4.14/driver-api/spi.html – Murilo Machado Pinto Apr 6 at 0:10
  • I've only fooled around a bit with writing kernel modules, and it has been a long time; I'm no expert. The best place for general questions about that will be Stack Overflow; there is also a quite busy mail list, the LKML. I am not sure what the possibilities are there re seeking advice like this. Also, although it is getting on in years, if you have not yet you should have a look at the the LDD3: lwn.net/Kernel/LDD3 (there never was an LDD4, btw). It should be helpful with general stuff; various people maintain updated versions of the code examples: github.com/search?q=ldd3 – goldilocks Apr 7 at 10:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.