Videos
Hi, I am an engineering graduate and have been working with microcontrollers for the past 3 years. Recently I had started learning linux programming with gcc for embedded based applications on an ARM based SOC. I now want to learn linux device driver implementation. Are there some good online courses that I can use. What kind of projects can I do for learning purpose.
I'm coming from web development and learning C has been my first experience with a low level language. I have enjoyed learning the basics of the language but now I am having an issue: I have been unable to find any resources for learning how to actually write anything substantial in C. I can find loads of absolute beginner content for learning the basic syntax of the language, and I can find some advanced content, but I seem to be totally unable to find any intermediate content that bridges the large gap between those two extremes. The reason I decided to learn C to begin with was because I wanted to learn to write Linux device drivers, but after learning the basics of the language, I'm at a loss for where to go next. Can anyone point me to some resources? Maybe some books to read or concepts to learn?
First, start by writing a generic kernel module. There are multiple places to look up for information but I found this link to be very useful. After you have gone through all examples specified there you can start writing your own Linux Driver Module.
Please note, that you will not get away with just copy-pasting the example code and hope it will work, no. Kernel API can sometimes change and examples will not work. Examples provided there should be looked at as a guide on how to do something. Depending on the kernel version you are using you have to modify the example in order to work.
Consider using TI platform-provided functions as much as you can, because that can really do a lot of work for you, like requesting and enabling needed clocks, buses, and power supplies. If I recall correctly you can use the functions to acquire memory-mapped address ranges for direct access to registers. I have to mention that I have a bad experience with TI-provided functions because they do not properly release/clean up all acquired resources, so for some resources, I had to call other kernel services to release them during module unload.
Edit 1:
I'm not entirely familiar with Linux SPI implementation but I would start by looking at omap2_mcspi_probe() function in drivers/spi/spi-omap2-mcspi.c file. As you can see there, it registers it's methods to Linux master SPI driver using this API: Linux/include/linux/spi/spi.h. In contrast to char driver, the main functions here are *_transfer() functions. Look up the struct descriptions in spi.h file for further details. Also, have a look at this alternative device driver API, too.
I assume your OMAP4 linux uses one of arch/arm/boot/dts/{omap4.dtsi,am33xx.dtsi} device-tree, thus it compiles drivers/spi/spi-omap2-mcspi.c (if you don't know about device-tree, read this). Then:
- the SPI master driver is done,
- it (most probably) registers with Linux SPI core framework
drivers/spi/spi.c, - it (probably) works fine on your OMAP4.
You actually don't need to care about the master driver to write your slave device driver. How do I know spi-omap2-mcspi.c is a master driver? It calls spi_register_master().
SPI master, SPI slave ?
Please refer to Documentation/spi/spi_summary. The doc refers to Controller driver (master) and Protocol drivers (slave). From your description, I understand you want to write a Protocol/Device driver.
SPI protocol ?
To understand that, you need your slave device datasheet, it shall tell you:
- the SPI mode understood by your device,
- the protocol it expects on the bus.
Contrary to i2c, SPI does not define a protocol or handshake, SPI chips manufacturers have to define their own. So check the datasheet.
SPI mode
From include/linux/spi/spi.h:
* @mode: The spi mode defines how data is clocked out and in. * This may be changed by the device's driver. * The "active low" default for chipselect mode can be overridden * (by specifying SPI_CS_HIGH) as can the "MSB first" default for * each word in a transfer (by specifying SPI_LSB_FIRST).
Again, check your SPI device datasheet.
An example SPI device driver?
To give you a relevant example, I need to know your SPI device type. You would understand that a SPI flash device driver is different from a SPI FPGA device driver. Unfortunately there are not so many SPI device drivers out there. To find them:
$ cd linux
$ git grep "spi_new_device\|spi_add_device"
Hey everyone, I'm interested in writing drivers for Linux but I'm not sure where to start. Can anyone recommend any resources or tutorials for beginners? Also, what programming languages are commonly used for writing Linux drivers? Any advice would be greatly appreciated!