Linx Port Devices Driver

Posted on  by 



The Linux kernel provides a device driver for the SPI controller of the STM32F429. To enable the driver in the kernel configuration, run make kmenuconfig, go to Device Drivers and enable SPI Support. Then from SPI Support enable STM32 SPI Controller (CONFIG_SPI_STM32 in the kernel configuration):

Having enabled CONFIG_SPI_STM32, go to System Type -> STM32 I/O interfaces and enable the specific SPI interfaces you require in your application:

For each SPI interface you enable in the kernel configuration, the platform initialization code will register a platform device with the kernel. Refer to linux/arch/arm/mach-stm32/spi.c in the kernel tree:

See full list on opensource.com. Device drivers - Character devices - Serial device bus - Serial device TTY port controller SERIAL DEV BUS =y Tristate Driver dependency SERIAL DEV CTRL TTYPORT =y Boolean Only in-kernel controller implementation Should default to y (patch posted) Depends on TTY and SERIAL DEV BUS!= m. Universal Serial Bus Controllers headings. The COM port drivers should be uninstalled before the USB drivers. To uninstall the COM port driver, right-click on Linx SDM-USM-QS-S listing under the Ports header and select “Uninstall”. Application Note AN-00201 A confirmation box will be displayed to confirm that you want to uninstall the drivers.

..
#if defined(CONFIG_STM32_SPI4)
platform_set_drvdata(&spi_stm32_dev4,
(void *) stm32_clock_get(CLOCK_PCLK2));
platform_device_register(&spi_stm32_dev4);
#endif
..

Do not enable any SPI interfaces except for those that you plan to use in your application. For one thing, unless an SPI interface is actually required, you want to keep it in reset in order to save some power. Another consideration is that SPI signals may conflict with other I/O interfaces on the STM32F4 pins. More on that right below.

Allocation of the STM32F429 pins to specific I/O interfaces is defined in linux/arch/arm/mach-stm32/iomux.c. When you have enabled SPI interfaces that you require in your application, make sure that there is appropriate code in iomux.c that routes the SPI signals to those STM32F429 pins that you have allocated for SPI in your application. For example, for SPI4 there is the following code defined in iomux.c:

#if defined(CONFIG_STM32_SPI4)
gpio_dsc.port = 4; /* CLCK */
gpio_dsc.pin = 2;
stm32f2_gpio_config(&gpio_dsc, STM32F2_GPIO_ROLE_SPI4);
gpio_dsc.port = 4; /* DI */
gpio_dsc.pin = 5;
stm32f2_gpio_config(&gpio_dsc, STM32F2_GPIO_ROLE_SPI4);
gpio_dsc.port = 4; /* DO */
gpio_dsc.pin = 6;
stm32f2_gpio_config(&gpio_dsc, STM32F2_GPIO_ROLE_SPI4);
gpio_dsc.port = 4; /* CS */
gpio_dsc.pin = 4;
stm32f2_gpio_config(&gpio_dsc, STM32F2_GPIO_ROLE_OUT);
#endif

Having configured the STM32F4 pins as defined above, the kernel will register a platform device for the SPI4 controller. You should see the following line in the kernel boot messages on the target (note that the kernel counts the SPI interfaces from 0 rather than 1):

spi_stm32 spi_stm32.3: SPI Controller 3 at 40013400,hz=90000000

The next step is to define population of SPI devices residing on each SPI bus in your concrete hardware configuration. This is done in linux/arch/arm/mach-stm32/spi.c using a call to spi_register_board_info:

/*
* Register SPI slaves
*/
spi_register_board_info(&spi_stm32_flash_info__dongle,
sizeof(spi_stm32_flash_info__dongle) /
sizeof(struct spi_board_info));

The first argument to spi_register_board_info is an array of the data structures typed as struct spi_board_info. The second argument is the size of the array. In the example above, we define just a single SPI device so there is a single element in the array and the first argument is an address of a struct spi_board_info variable rather than a pointer to an array. However, you can have more than one SPI device in your hardware configuration, in which case you need an array of several elements.

You must provide some information for each SPI device you define in the struct spi_board_info array. Let's refer to the example defined in spi.c:

static struct spi_board_info
spi_stm32_flash_info__dongle = {
#if defined(CONFIG_SPI_SPIDEV)
.modalias = 'spidev',
#endif
.max_speed_hz = 25000000,
.bus_num = 3,
.chip_select = 0,
.controller_data = &spi_stm32_flash_slv__dongle,
};

The modalias field provides a link to a client SPI device driver, which will be used by the kernel to service a specific SPI device. In the example above, the client SPI device driver is SPIDEV, which provides access to the SPI device from user space using 'raw' SPI transactions. This interface is frequently used in embedded applications to control SPI devices (such as, for instance, SPI sensors) directly from user space code. Note that to enable the SPIDEV interface in the kernel, you need to enable User mode SPI device driver support in the SPI Support kernel configuration menu (see the first capture in the above text).

The max_speed_hz field defines the frequency that the SPI device driver is to use to access a specific SPI device. The device driver will attempt to use a maximum frequency that is less or equal to max_speed_hz and possible to derive from the clock the SPI bus is running from.

The bus_num field is the number assigned by the kernel to the specific SPI interface. As mentioned above, the kernel counts interfaces from 0 so bus_num must be set to 3 for SPI4.

The chip_select is the chip select index you assign to a specific SPI device. A unique chip_select must be assigned to each device on a single SPI bus.

It is important to note that the STM32F429 SPI device driver controls the Chip Select signals as plain GPIO pins. The driver activates the Chip Select GPIO signal when there is an SPI transaction targeting the corresponding SPI device and then de-activates the GPIO signal when an SPI transaction is completed. A GPIO pin used for Chip Select to a specific SPI device is described in a data structure pointed by the controller_data field:

static struct spi_stm32_slv spi_stm32_flash_slv__dongle = {
.cs_gpio =
STM32_GPIO_PORTPIN2NUM(
SPI_FLASH_CS_PORT__STM32F4_DISCO,
SPI_FLASH_CS_PIN__STM32F4_DISCO),
.timeout = 3,
};
..
.controller_data = &spi_stm32_flash_slv__dongle,
};

Make sure you de-activate the Chip Select GPIO before registering the SPI device:

gpio_direction_output(STM32_GPIO_PORTPIN2NUM(
SPI_FLASH_CS_PORT__STM32F4_DISCO,
SPI_FLASH_CS_PIN__STM32F4_DISCO), 1);

Having defined a population of SPI devices in the kernel as described above, you will have an SPIDEV device on SPI4 at Chip Select 0. You will need to create a device node that your application code would be able to open() on the target in order to get access to the device via the SPIDEV API. The easiest way to create the device node on the target is to issue a call to mdev -s. mdev is a user-space Linux utility that can be used to populate the /dev directory with device files corresponding to devices present on the system. mdev is part of the multi-call busybox utility. To enable mdev in the busbox configuration, run make bmenuconfig, then go to Linux system utilities and enable mdev:

Also, you will need to add a symlink for mdev to the target file system. This is done by adding the following line to the <project>.initramfs file:

slink /bin/mdev busybox 777 0 0

Linx Port Devices Driver

Having updated your project configuration as described above, build the bootable Linux image (<project>.uImage) by running make in the project directory.

On the target run mdev -s, either from the shell interactive interface or from a start-up script such as /etc/rc, to create device nodes for devices registered with the kernel:

~ # mdev -s
~ # ls -lt /dev/spi*
crw-rw---- 1 root root 153, 0 Jan 1 00:00 /dev/spidev3.0
~ #

Now finally everything is ready to access the SPI device from a user-space application using the SPIDEV interface. There is abundant documentation in the Internet on how to use SPIDEV in application code. The basics are described, for instance, in the following article:

Here is a very simple demo application that shows how to read the Flash ID from an SPI Flash device:

/*
* Sample application that makes use of the SPIDEV interface
* to access an SPI slave device. Specifically, this sample
* reads a Device ID of a JEDEC-compliant SPI Flash device.
*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <linux/types.h>
#include <linux/spi/spidev.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
int main(int argc, char **argv)
{
/*
* This assumes that a 'mdev -s' has been run to create
* /dev/spidev* devices after the kernel bootstrap.
* First number is the 'bus' (SPI contoller id), second number
* is the 'chip select' of the specific SPI slave
* ..
* char *name = '/dev/spidev1.1';
*/
char *name;
int fd;
struct spi_ioc_transfer xfer[2];
unsigned char buf[32], *bp;
int len, status;
name = argv[1];
fd = open(name, O_RDWR);
if (fd < 0) {
perror('open');
return 1;
}
memset(xfer, 0, sizeof xfer);
memset(buf, 0, sizeof buf);
len = sizeof buf;
/*
* Send a GetID command
*/
buf[0] = 0x9f;
len = 6;
xfer[0].tx_buf = (unsigned long)buf;
xfer[0].len = 1;
xfer[1].rx_buf = (unsigned long) buf;
xfer[1].len = 6;
status = ioctl(fd, SPI_IOC_MESSAGE(2), xfer);
if (status < 0) {
perror('SPI_IOC_MESSAGE');
return;
}
printf('response(%d): ', status);
for (bp = buf; len; len--)
printf('%02x ', *bp++);
printf('n');
}

Here is how the application runs on the STM32F4 target:

Libusb-win32 driver. ~ # /spidev_flash /dev/spidev3.0
response(7): 20 20 16 10 00 00
~ #

“Do you pine for the nice days of Minix-1.1, when men were men and wrote their own device drivers?” Linus Torvalds

In order to develop Linux device drivers, it is necessary to have an understanding of the following:

  • C programming. Some in-depth knowledge of C programming is needed, like pointer usage, bit manipulating functions, etc.
  • Microprocessor programming. It is necessary to know how microcomputers work internally: memory addressing, interrupts, etc. All of these concepts should be familiar to an assembler programmer.

There are several different devices in Linux. For simplicity, this brief tutorial will only cover type char devices loaded as modules. Kernel 2.6.x will be used (in particular, kernel 2.6.8 under Debian Sarge, which is now Debian Stable).

When you write device drivers, it’s important to make the distinction between “user space” and “kernel space”.

  • Kernel space. Linux (which is a kernel) manages the machine's hardware in a simple and efficient manner, offering the user a simple and uniform programming interface. In the same way, the kernel, and in particular its device drivers, form a bridge or interface between the end-user/programmer and the hardware. Any subroutines or functions forming part of the kernel (modules and device drivers, for example) are considered to be part of kernel space.
  • User space. End-user programs, like the UNIX shell or other GUI based applications (kpresenter for example), are part of the user space. Obviously, these applications need to interact with the system's hardware . However, they don’t do so directly, but through the kernel supported functions.

All of this is shown in figure 1.

The kernel offers several subroutines or functions in user space, which allow the end-user application programmer to interact with the hardware. Usually, in UNIX or Linux systems, this dialogue is performed through functions or subroutines in order to read and write files. The reason for this is that in Unix devices are seen, from the point of view of the user, as files.

On the other hand, in kernel space Linux also offers several functions or subroutines to perform the low level interactions directly with the hardware, and allow the transfer of information from kernel to user space.

Usually, for each function in user space (allowing the use of devices or files), there exists an equivalent in kernel space (allowing the transfer of information from the kernel to the user and vice-versa). This is shown in Table 1, which is, at this point, empty. It will be filled when the different device drivers concepts are introduced.

EventsUser functionsKernel functions
Load module
Open device
Read device
Write device
Close device
Remove module

Table 1. Device driver events and their associated interfacing functions in kernel space and user space.

There are also functions in kernel space which control the device or exchange information between the kernel and the hardware. Table 2 illustrates these concepts. This table will also be filled as the concepts are introduced.

EventsKernel functions
Read data
Write data

Table 2. Device driver events and their associated functions between kernel space and the hardware device.

I’ll now show you how to develop your first Linux device driver, which will be introduced in the kernel as a module.

For this purpose I’ll write the following program in a file named nothing.c

<nothing.c> =

Since the release of kernel version 2.6.x, compiling modules has become slightly more complicated. First, you need to have a complete, compiled kernel source-code-tree. If you have a Debian Sarge system, you can follow the steps in Appendix B (towards the end of this article). In the following, I’ll assume that a kernel version 2.6.8 is being used.

Next, you need to generate a makefile. The makefile for this example, which should be named Makefile, will be:

=

Unlike with previous versions of the kernel, it’s now also necessary to compile the module using the same kernel that you’re going to load and use the module with. To compile it, you can type:

This extremely simple module belongs to kernel space and will form part of it once it’s loaded.

In user space, you can load the module as root by typing the following into the command line:

# insmod nothing.ko

The insmod command allows the installation of the module in the kernel. However, this particular module isn’t of much use.

It is possible to check that the module has been installed correctly by looking at all installed modules:

# lsmod

Finally, the module can be removed from the kernel using the command:

# rmmod nothing

By issuing the lsmod command again, you can verify that the module is no longer in the kernel.

The summary of all this is shown in Table 3.

EventsUser functionsKernel functions
Load moduleinsmod
Open device
Read device
Write device
Close device
Remove modulermmod

Table 3. Device driver events and their associated interfacing functions between kernel space and user space.

When a module device driver is loaded into the kernel, some preliminary tasks are usually performed like resetting the device, reserving RAM, reserving interrupts, and reserving input/output ports, etc.

These tasks are performed, in kernel space, by two functions which need to be present (and explicitly declared): module_init and module_exit; they correspond to the user space commands insmod and rmmod , which are used when installing or removing a module. To sum up, the user commands insmod and rmmod use the kernel space functions module_init and module_exit.

Let’s see a practical example with the classic program Hello world:

<hello.c> =

The actual functions hello_init and hello_exit can be given any name desired. However, in order for them to be identified as the corresponding loading and removing functions, they have to be passed as parameters to the functions module_init and module_exit.

The printk function has also been introduced. It is very similar to the well known printf apart from the fact that it only works inside the kernel. The <1> symbol shows the high priority of the message (low number). In this way, besides getting the message in the kernel system log files, you should also receive this message in the system console.

This module can be compiled using the same command as before, after adding its name into the Makefile.

=

In the rest of the article, I have left the Makefiles as an exercise for the reader. A complete Makefile that will compile all of the modules of this tutorial is shown in Appendix A.

When the module is loaded or removed, the messages that were written in the printk statement will be displayed in the system console. If these messages do not appear in the console, you can view them by issuing the dmesg command or by looking at the system log file with cat /var/log/syslog.

Table 4 shows these two new functions.

EventsUser functionsKernel functions
Load moduleinsmodmodule_init()
Open device
Read device
Write device
Close device
Remove modulermmodmodule_exit()
Linx Port Devices Driver

Table 4. Device driver events and their associated interfacing functions between kernel space and user space.

I’ll now show how to build a complete device driver: memory.c. This device will allow a character to be read from or written into it. This device, while normally not very useful, provides a very illustrative example since it is a complete driver; it's also easy to implement, since it doesn’t interface to a real hardware device (besides the computer itself).

To develop this driver, several new #include statements which appear frequently in device drivers need to be added:

=

This new function is now shown in Table 5.

EventsUser functionsKernel functions
Load moduleinsmodmodule_init()
Open devicefopenfile_operations: open
Read device
Write device
Close device
Remove modulermmodmodule_exit()

Table 5. Device driver events and their associated interfacing functions between kernel space and user space.

The corresponding function for closing a file in user space (fclose) is the release: member of the file_operations structure in the call to register_chrdev. In this particular case, it is the function memory_release, which has as arguments an inode structure and a file structure, just like before.

When a file is closed, it’s usually necessary to free the used memory and any variables related to the opening of the device. But, once again, due to the simplicity of this example, none of these operations are performed.

The memory_release function is shown below:

=

The reading position in the file (f_pos) is also changed. If the position is at the beginning of the file, it is increased by one and the number of bytes that have been properly read is given as a return value, 1. If not at the beginning of the file, an end of file (0) is returned since the file only stores one byte.

In Table 7 this new function has been added.

EventsUser functionsKernel functions
Load moduleinsmodmodule_init()
Open devicefopenfile_operations: open
Read devicefreadfile_operations: read
Write device
Close devicefclosefile_operations: release
Remove modulesrmmodmodule_exit()

Table 7. Device driver events and their associated interfacing functions between kernel space and user space.

To write to a device with the user function fwrite or similar, the member write: of the file_operations structure is used in the call to register_chrdev. It is the function memory_write, in this particular example, which has the following as arguments: a type file structure; buf, a buffer in which the user space function (fwrite) will write; count, a counter with the number of bytes to transfer, which has the same values as the usual counter in the user space function (fwrite); and finally, f_pos, the position of where to start writing in the file.

driver. Incom is the world’s leader in polymer and glass microstructure innovation. Our newest revolutionary technologies range from the highest resolving imaging optic ever made to the world’s largest and fastest MCP based photodetector. Incom’s customers are researchers and instrument makers at the forefront of technology. Sep 16, 2020 The median annual wage for heavy and tractor-trailer truck drivers was $45,260 in May 2019. We’ve identified nine states where the typical salary for a CDL Truck Driver job is above the national average. Topping the list is Massachusetts, with Hawaii and Connecticut close behind in second and third. Connecticut beats the national average by 4.7%, and Massachusetts furthers that trend with another $3,549 (6.8%) above the $51,910.

# insmod memory.ko

It’s also convenient to unprotect the device:

# chmod 666 /dev/memory

If everything went well, you will have a device /dev/memory to which you can write a string of characters and it will store the last one of them. You can perform the operation like this:

$ echo -n abcdef >/dev/memory

To check the content of the device you can use a simple cat:

$ cat /dev/memory

The stored character will not change until it is overwritten or the module is removed.

I’ll now proceed by modifying the driver that I just created to develop one that does a real task on a real device. I’ll use the simple and ubiquitous computer parallel port and the driver will be called parlelport.

The parallel port is effectively a device that allows the input and output of digital information. More specifically it has a female D-25 connector with twenty-five pins. Internally, from the point of view of the CPU, it uses three bytes of memory. In a PC, the base address (the one from the first byte of the device) is usually 0x378. In this basic example, I’ll use just the first byte, which consists entirely of digital outputs.

The connection of the above-mentioned byte with the external connector pins is shown in figure 2.

The previous memory_init function needs modification—changing the RAM memory allocation for the reservation of the memory address of the parallel port (0x378). To achieve this, use the function for checking the availability of a memory region (check_region), and the function to reserve the memory region for this device (request_region). Both have as arguments the base address of the memory region and its length. The request_region function also accepts a string which defines the module.

=

In this case, a real device reading action needs to be added to allow the transfer of this information to user space. The inb function achieves this; its arguments are the address of the parallel port and it returns the content of the port.

=

Table 10 summarizes this new function.

EventsKernel functions
Read datainb
Write dataoutb

Device driver events and their associated functions between kernel space and the hardware device.

I’ll proceed by looking at the whole code of the parlelport module. You have to replace the word memory for the word parlelport throughout the code for the memory module. The final result is shown below:

<parlelport.c> =

Initial section

Linux Port Devices Driver Windows 7

Intel input devices driver download for windows 7. In the initial section of the driver a different major number is used (61). Also, the global variable memory_buffer is changed to port and two more #include lines are added: ioport.h and io.h.

=

Closing the device as a file

Again, the match is perfect.

=

Writing to the device

It is analogous to the memory one except for writing to a device.

Linx Port Devices Driver

To compile a 2.6.x kernel on a Debian Sarge system you need to perform the following steps, which should be run as root:

  1. Install the “kernel-image-2.6.x” package.
  2. Reboot the machine to make this the running kernel image. This is done semi-automatically by Debian. You may need to tweak the lilo configuration file /etc/lilo.conf and then run lilo to achieve this.
  3. Install the “kernel-source-2.6.x” package.
  4. Change to the source code directory, cd /usr/src and unzip and untar the source code with bunzip2 kernel-source-2.6.x.tar.bz2 and tar xvf kernel-source-2.6.x.tar. Change to the kernel source directory with cd /usr/src/kernel-source-2.6.x
  5. Copy the default Debian kernel configuration file to your local kernel source directory cp /boot/config-2.6.x .config.
  6. Make the kernel and the modules with make and then make modules.

If you would like to take on some bigger challenges, here are a couple of exercises you can do:

  1. I once wrote two device drivers for two ISA Meilhaus boards, an analog to digital converter (ME26) and a relay control board (ME53). The software is available from the ADQ project. Get the newer PCI versions of these Meilhaus boards and update the software.
  2. Take any device that doesn’t work on Linux, but has a very similar chipset to another device which does have a proven device driver for Linux. Try to modify the working device driver to make it work for the new device. If you achieve this, submit your code to the kernel and become a kernel developer yourself!

Three years have elapsed since the first version of this document was written. It was originally written in Spanish and intended for version 2.2 of the kernel, but kernel 2.4 was already making its first steps at that time. The reason for this choice is that good documentation for writing device drivers, the Linux device drivers book (see bibliography), lagged the release of the kernel in some months. This new version is also coming out soon after the release of the new 2.6 kernel, but up to date documentation is now readily available in Linux Weekly News making it possible to have this document synchronized with the newest kernel.

Fortunately enough, PCs still come with a built-in parallel port, despite the actual trend of changing everything inside a PC to render it obsolete in no time. Let us hope that PCs still continue to have built-in parallel ports for some time in the future, or that at least, parallel port PCI cards are still being sold.

This tutorial has been originally typed using a text editor (i.e. emacs) in noweb format. This text is then processed with the noweb tool to create a LaTeX file ( .tex ) and the source code files ( .c ). All this can be done using the supplied makefile.document with the command make -f makefile.document.

I would like to thank the “Instituto Politécnico de Bragança”, the “Núcleo Estudantil de Linux del Instituto Politécnico de Bragança (NUX)”, the “Asociación de Software Libre de León (SLeón)” and the “Núcleo de Estudantes de Engenharia Informática da Universidade de Évora” for making this update possible.

Fremantle counselling -- does it interest you?

If software development is stressing you out, or if you need help, you can have Perth Counselling at your fingertips!





Coments are closed