13

I'd like to write a Linux device driver for some real hardware device. What Raspberry Pi peripherals are a good fit? Basically I need the following:

  1. It must be small enough for a beginner (few month at most, better few weeks).

  2. Datasheet must be available. At first I was thinking about USB-Ethernet adapter, but it looks like it has very limited datasheet.

Any ideas? May be something like making serial console work over GPIO?

Ghanima
  • 15,855
  • 15
  • 61
  • 119
  • 2
    Linux drivers are kernel modules, and technically they don't have to have anything to do with hardware at all, so the first thing you want to try is probably a char device interface -- i.e., something that provides a char device file that can be read from and/or written to (much like the stuff in proc) for some trivial purpose. – goldilocks Aug 12 '13 at 12:49
  • I did all that, completed tutorials and wrote kernel modules for pseudo-devices, along with reading books on kernel/driver development. This is all good, but now I want to work with real hardware.. –  Aug 12 '13 at 15:42
  • Greg K-H wrote this a while back (it is maybe slightly outdated in some respects): http://www.linuxjournal.com/article/7353 So there are USB devices with public data sheets around. He has some reverse engineering tips there (I would not want to do it with anything too complicated tho). Stuff for the GPIO pins I would guess generally work using the existing I2C etc. drivers, but you could just ignore that and do something yourself. – goldilocks Aug 12 '13 at 16:04

3 Answers3

3

It can be hard to learn writing drivers when interfacing with complicated devices and/or complicated buses. Because of that I would recommend either using some simple and well known devices (ignoring existing implementation of their drivers in the kernel) like I²C/SPI devices that are usually used with microcontrollers. For example you could find any device that is "supported" by Arduino community (meaning there is a library/documentation for it) and try using it with RaspberryPi.

If that's not enough or you don't want to buy too much hardware, you can create one yourself. Just get some microcontroller (like atmega or something), create a program for it to become some device and then try interfacing with it using Linux drivers. This way you can easily create programs that will emulate different classes of devices. And since you will write your "firmware" yourself, it will help you debug problems.

You need some microcontroller programming skills for that but it's not hard to learn programming with Arduino and I believe it's useful knowledge for driver programmer anyway.

Krzysztof Adamski
  • 9,615
  • 1
  • 37
  • 53
  • How about implementing serial communication in software via GPIO pins? E.g. connect serial to PC and try to at least output something to console. Is it viable? –  Aug 14 '13 at 10:41
  • @ivan: I'm not sure if I understood that right. If, by serial, you mean UART/rs232 and by software, you mean bitbanging it, then it's not possible because of timing constraints. If, on the other hand, you mean (re)implementing Linux serial device using RaspberryPi UART hardware, then it's of course doable. I believe all the information needed about this hardware can be found in BCM2835 ARM Peripherals document. But remember that in order to use it, you first have to disable existing driver in the kernel. – Krzysztof Adamski Aug 14 '13 at 15:12
  • Thank you, but are you sure about impossibility of bit-banging UART? I found this link: http://www.ganssle.com/articles/auart.htm so it looks potentially doable for low baud rate and for at least only implementing sending part (which is easier). –  Aug 15 '13 at 07:43
  • @ivan: This article does not seem to be about systems with general purpose operating systems like Linux. In UART you have real strict timing constraints that are very hard to meet without real time operating system. That being said, It may be possible to do so at low baudrate but it may be not really reliable. – Krzysztof Adamski Aug 15 '13 at 09:11
  • Looks like you are right: http://raspberrypi.stackexchange.com/questions/1987/can-raspberry-pi-reliably-bit-bang-a-9600-baud-serial-and-is-there-example-code (however there's an interesting solution there). Thanks again for your help. –  Aug 16 '13 at 08:24
2

Personally I would start with a very simple device, such as one or more LEDs connected directly to GPIO pins.

You could either buy a device ready to plug in, or wire your own one.

The reason I would suggest this is that debugging drivers is generally much more difficult then a normal program, therefore a simple challenge to get started is useful, also you can then use that code as a method of debug for more complex devices (status out to a GPIO pin to attach to an oscilloscope) where timing is important.

If it is of interest there is a kernel driver for LedBorg available here, the source should be a reasonably simple example for driving GPIO pins at a regular interval.

PiBorg
  • 1,497
  • 10
  • 11
  • Hi @PiBorg. can you tell some resources that help me to learn to code simple driver for a device such as one or more LEDs connected directly to GPIO pins ? – Sagar Jun 17 '15 at 11:33
0

The simplest "device" that you can write a hardware driver for (if hardware driver development is your thing) can also be as simple as a LED (I added the quotation marks because technically a LED is not a device but it's still a piece of hardware) as @PiBorg has suggested.

Other choices would be some easy-to-interface device/components like a photoresistors, passive infrared sensors (short: PIR), temperature sensors, PC fans (preferably a 4-wire fan that allows you not only to monitor but also to control the RPM), LED dot matrices and so on. Basically such simple devices will allow you to have the minimum on hardware (so that you can see and touch what you've actually accomplished) and at the same time you can learn about a lot of topics that are used for much more complex devices where the complexity comes mostly from the protocol they use.

Mind also that you don't need to go the extra mile digging into the kernel modules. Of course if you want to do that nobody is stopping you. :)

Here is an example of interfacing a passive infrared sensor (I'm going to test it soon when my PIR gets delivered :3). You can take this and start digging deeper into the world of the Linux kernel in order to see how you can for example create a kernel driver, which works with the PIR.

rbaleksandar
  • 257
  • 1
  • 3
  • 11