官术网_书友最值得收藏!

Custom drivers

The exact format and integration of drivers (kernel modules) with the OS kernel differs for each OS and thus would be impossible to fully cover here. We will, however, look at how the driver for the RTC module we used earlier is implemented for Linux.

In addition, we will look at how to use an I2C peripheral from user space later in this chapter, in the club room monitoring example. Using a user space-based driver (library) is often a good alternative to implementing it as a kernel module.

The RTC functionality is integrated into the Linux kernel, with the code for it found in the /drivers/rtc folder (on GitHub, at https://github.com/torvalds/linux/tree/master/drivers/rtc).

The rtc-ds1307.c file contains two functions we need to read and set the RTC, respectively: ds1307_get_time() and ds1307_set_time(). The basic functionality of these functions is very similar to what we'll be using in the club room monitoring example later in this chapter, where we simply integrate I2C device support into our application.

A major advantage of communicating with I2C, SPI, and other such peripherals from user space is that we are not limited by the compile environment supported by the OS kernel. Taking the Linux kernel as an example, it is written mostly in C with some assembly. Its APIs are C-style APIs and thus we would have to use a distinctly C-style coding approach to writing our kernel modules.

Obviously, this would negate most of the advantages, not to mention the point, of attempting to write these modules in C++ to begin with. When moving our module code to user space and using it either as part of an application or as a shared library, we have no such limitations and can freely use any and all C++ concepts and functionality.

For completeness' sake, the basic template for a Linux kernel module looks as follows:

#include <linux/module.h>       // Needed by all modules 
#include <linux/kernel.h>       // Needed for KERN_INFO 
 
int init_module() { 
        printk(KERN_INFO "Hello world.n"); 
 
        return 0; 
} 
 
void cleanup_module() { 
        printk(KERN_INFO "Goodbye world.n"); 
} 

This is the requisite Hello World example, written in C++-style.

One final consideration when considering kernel- and user space-based driver modules is that of context switches. From an efficiency point of view, kernel modules are faster and have lower latency because the CPU does not have to switch from a user to kernel space context and back repeatedly to communicate with a device and pass messages from it back to the code communicating with it.

For high bandwidth devices (such as storage and capturing), this could make the difference between a smoothly functioning system and one that severely lags and struggles to perform its tasks.

However, when considering the club room monitoring example in this chapter and its occasional use of an I2C device, it should be obvious that a kernel module would be severe overkill without any tangible benefits.

主站蜘蛛池模板: 洛扎县| 天镇县| 陆川县| 奎屯市| 湘阴县| 平遥县| 海阳市| 河东区| 孝昌县| 甘泉县| 彰武县| 班戈县| 巴彦县| 丹阳市| 会泽县| 抚远县| 溧水县| 滕州市| 遵义市| 廊坊市| 五大连池市| 天峨县| 莎车县| 灵台县| 清镇市| 荥阳市| 平和县| 长丰县| 西昌市| 汉源县| 龙胜| 凤庆县| 田东县| 绍兴县| 静乐县| 三江| 天气| 浠水县| 涞源县| 从江县| 抚松县|