2

Is there a software block diagram that describes how a Python call (for example, GPIO) gets to the hardware?

I would assume there is a user/kernel interface. Maybe a kernel module that implements support for the Raspberry Pi hardware? I understand Python and port-mapped I/O, but I don't know what is in-between in the Raspberry Pi.

2

I don't have a block diagram, so I hope that a verbal explanation will be enough. In general, there are two ways Python modules interact with Raspberry Pi interfaces (like GPIO) - direct memory access or kernel-to-userspace interfaces.

Direct memory access

Linux offers /dev/mem, a special device which allows userspace programs (having appropriate privileges) to directly operate on the memory. This way one can easily access memory mapped hardware registers, providing he knows memory addresses to use, etc. (this information can be found in datasheet). This is very powerful way, and you can do everything that the hardware allows you to do this way. But there are no safeguards provided by kernel, and you can't run your programs as a normal user. And this is not portable to other devices.

So, what is happening here is that the Python process opens /dev/mem file and reads/writes to appropriate index in this file which corresponds to physical memory addresses where hardware registers are stored (like 0x20200000 address for GPIO).

Kernel interface

The second way is to use a kernel-provided interface to operate on devices. These interfaces are implemented as a kernel drivers. For example, for the GPIO kernel creates special files in the sysfs pseudo filesystem. They are in the /sys/class/gpio/ directory. There are a bunch of files which can be written, read or polled like regular files.

So the Python process opens virtual files in a virtual file system created by the kernel. It reads and writes to those files. Those read and write operations are then implemented by a kernel driver which basically does appropriate memory operations on memory-mapped hardware registers mentioned above.

The upside of this method is that the kernel can provide proper safeguards that guarantee that userspace application can't do any harm to the whole system. Files provided by the sysfs virtual filesystem can be configured for access by normal (unprivileged) users. And this interface is on all other platforms having GPIO. The downside is that the functionality you can use is limited to what the interface provides.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.