EMBEDDED LINUX - EMBEDDED SYSTEMS AND INTERNET OF THINGS

Jeevanandhams14 60 views 83 slides Aug 28, 2025
Slide 1
Slide 1 of 83
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50
Slide 51
51
Slide 52
52
Slide 53
53
Slide 54
54
Slide 55
55
Slide 56
56
Slide 57
57
Slide 58
58
Slide 59
59
Slide 60
60
Slide 61
61
Slide 62
62
Slide 63
63
Slide 64
64
Slide 65
65
Slide 66
66
Slide 67
67
Slide 68
68
Slide 69
69
Slide 70
70
Slide 71
71
Slide 72
72
Slide 73
73
Slide 74
74
Slide 75
75
Slide 76
76
Slide 77
77
Slide 78
78
Slide 79
79
Slide 80
80
Slide 81
81
Slide 82
82
Slide 83
83

About This Presentation

EMBEDDED LINUX
Introduction – Advantages– Embedded Linux Distributions – Architecture of Embedded Linux – Linux
kernel architecture – User space – Linux startup sequence – GNU cross platform Tool chain – Device
Driver: Introduction and Types – Tracing and Profiling tools.


Slide Content

SRI RAMAKRISHNA ENGINEERING COLLEGE [Educational Service : SNR Sons Charitable Trust] [Autonomous Institution, Rea ccredited by NAAC with ‘A+’ Grade] [Approved by AICTE and Permanently Affiliated to Anna University, Chennai] [ISO 9001:2015 Certified and all Eligible Programmes Accredited by NBA] VATTAMALAIPALAYAM, N.G.G.O. COLONY POST, COIMBATORE – 641 022. 20EC211 – EMBEDDED SYSTEMS AND INTERNET OF THINGS Module 2: EMBEDDED LINUX AND DEVICE DRIVERS Department of Information Technology

EMBEDDED LINUX AND DEVICE DRIVERS Introduction - Advantages- Embedded Linux Distributions - Architecture of Embedded Linux - Linux kernel architecture - User space - Linux startup sequence -GNU cross platform Tool chain - Device Driver: Introduction and Types-Tracing and Profiling tools.

CO2 RELATED TO EXPLAIN THE BASIC CONCEPTS AND ARCHITECTURE OF EMBEDDED LINUX. CO3 RELATED TO SUMMARIZE EMBEDDED DEVICE DRIVERS WITH THE DEBUGGING TECHNIQUES. Course Outcome

What is Embedded Linux? Embedded Linux is a customized version of the Linux operating system optimized for embedded systems. It provides the stability, performance, and flexibility of Linux while being tailored for specific hardware and application needs. Components of Embedded Linux: Kernel: The core of the operating system that manages hardware, system resources, and system calls. Bootloader: A small program that initializes the hardware and loads the kernel into memory. Common bootloaders include U-Boot and Barebox . Root Filesystem: Contains the basic system utilities and libraries required for the system to operate. It often includes the GNU C Library ( glibc ), BusyBox , or similar lightweight tools. Device Drivers: Software modules that allow the kernel to communicate with hardware components. ESIOT

History of Embedded Linux Linus Benedict Torvalds at the University of Helsinki created the Linux® operating system in 1991 Its open source development model and GNU General Public License (GPL), under which Linux is released This license allowed all the Linux kernel source code to be freely available for personal or commercial use. As the Linux kernel source code is freely available, it encouraged many developers to contribute to the Linux kernel. * 5 ESIOT

Key Features: Modularity: Linux’s modularity allows for creating a minimal and efficient system tailored to specific requirements. Open Source: The source code is freely available, enabling customization and flexibility. Community Support: A large community of developers and users can provide support, patches, and updates. ESIOT

Development Process: Cross-Compilation: Since embedded systems often use different architectures than development machines, software is usually compiled on a more powerful host system for the target architecture. Toolchains: Tools like GCC (GNU Compiler Collection) are used to build applications for embedded systems. Cross-compilation toolchains are specific to the target hardware. Board Support Packages (BSPs): Include kernel patches, bootloader configurations, and drivers specific to a particular hardware platform. Root Filesystem Creation: Tools like Buildroot or Yocto Project help in creating a root filesystem with the necessary libraries and applications. ESIOT

Challenges in Embedded Linux: Resource Constraints: Embedded systems often have limited memory and storage, requiring careful management of resources. Real-Time Requirements: Some applications need real-time processing, which may require specialized real-time extensions or configurations. Hardware Compatibility: Ensuring that the kernel and drivers support all the hardware features of the embedded system. ESIOT

Common Applications: Consumer Electronics: Smart TVs, gaming consoles, and home automation devices. Industrial Automation: Controllers for machinery, monitoring systems. Automotive Systems: Infotainment systems, navigation, and driver assistance systems. Networking Devices: Routers, switches, and access points. ESIOT

Why Embedded Linux? Vendor Independence 1. Selecting a proprietary OS may lock you up with the same vendor for the lifetime of your product. 2. Bad support from the vendor can result in increased time to market of your product. 3. You may end up waiting days or even weeks for the solution to even small problems. 4. Changing the vendor may mean restarting the whole product life cycle. Embedded Linux brings vendor independence. * 10 ESIOT

Time to Market One advantage of using Linux in an embedded platform is reduced development time. By using a Linux-based host development environment, most of the applications that are to be run on the target hardware can be tested on a Linux host, reducing time to port applications Varied Hardware Support Linux support for many architectures and high-end I/O devices gives you the independence to choose appropriate hardware for your system. Linux is also a preferred OS for any hardware or software innovation. * 11 ESIOT

Low Cost Embedded Linux brings minimal cost for development, training, and hiring needs. (Development Cost - utilities such as compilers, linkers, libraries, shells, and the like that constitute its development environment can be downloaded for free., Training and Hiring Costs - learning curve for embedded Linux is very small., Runtime Royalty - Embedded Linux is royalty free) Open Source One of the main reasons why Linux became so popular is its open source model of development * 12 ESIOT

Embedded Linux Versus Desktop Linux EMBEDDED LINUX DESKTOP LINUX Linux kernel running in the embedded system product / single board computer / development board. Linux kernel running on Desktop / Laptop. Real time Linux kernel is used. Kernel response is in real time or is deterministic. Linux kernel running in desktop or laptop is not real time. Kernel response is not deterministic for response against events Purpose of using Linux kernel is to perform particular function.  Example Linux kernel running in Video Streamer is doing function of converting video format into MPEG4 and sending video stream on network Purpose of using Linux kernel to many works for user Example: Linux kernel running in desktop is running so many different applications for tasks like running videos, running internet explorer, playing mp3 songs, running java program, C program etc. So many purposes are involved. * 13 ESIOT

It is very clear from above that Linux has to do specific task, so the module added with components are also limited to the requirement of application.  Like in above example there may be need of following modules or library with kernel: TCP/IP Stack  Ethernet Driver Graphic Controller Driver Graphic Library USB Stack if USB is also involved in the application  Sound Driver So, It can be said that so many modules / library or drivers are needed depend on components present on board.  Like  TCP/IP Stack Ethernet Driver Graphic Controller Driver Graphic Library USB Driver Serial Port Driver PCI Driver Sound Driver etc * 14 ESIOT

In nutshell kernel used in embedded system is the customized version of original kernel. user configures the  kernel as per target processor, Components present on the board, need of driver etc Complete version of kernel is used with all possible drivers and libraries. Whenever any new device or protocol is released then its driver patch is provided by either linux community or by vendor. Embedded Linux kernel footprint is less. Around 1 MBs. Desktop Linux kernel footprint is more around 100 MB. * 15 ESIOT

Popular Embedded Linux Distributions: Yocto Project Buildroot OpenWrt Raspberry Pi OS (formerly Raspbian ) OpenEmbedded Armbian Linux From Scratch (LFS) PetaLinux SUSE Linux Enterprise Embedded Android Things ESIOT

Yocto Project ESIOT Overview: The Yocto Project is not a distribution itself but a framework for creating custom Linux distributions for embedded systems. It provides a set of tools and metadata to build tailored Linux distributions from source. Features: Customizability: Allows for building highly customized distributions. Layers: Uses a layered approach to manage different components and configurations. OpenEmbedded Build System: The Yocto Project is based on the OpenEmbedded build system, which provides a robust environment for building software for embedded devices. Use Cases: Ideal for creating bespoke distributions tailored to specific hardware and application needs.

Buildroot ESIOT Overview: Buildroot is a lightweight and easy-to-use tool that simplifies the creation of custom Linux systems. It generates a minimal root filesystem, Linux kernel, and toolchain. Features: Simplicity: Easy to configure and use, with a simple menu-based configuration interface. Minimalism: Focuses on producing small and efficient images. Fast Builds: Faster build times compared to other systems like Yocto . Use Cases: Suitable for projects requiring a small, customized Linux system with minimal overhead.

OpenEmbedded ESIOT Overview: OpenEmbedded is a build framework used by the Yocto Project. It enables the creation of custom Linux distributions for embedded systems, and it provides a rich set of packages and configurations. Features: Extensibility: Supports a wide range of hardware and software packages. Layered Architecture: Allows for modular and maintainable build configurations. Use Cases: Ideal for developers needing a comprehensive build environment with extensive package support.

Raspberry Pi OS (formerly Raspbian ) ESIOT Overview: Raspberry Pi OS is a Debian-based distribution optimized for the Raspberry Pi family of single-board computers. Features: User-Friendly: Provides a desktop environment with user-friendly tools and applications. Hardware Support: Optimized for the hardware features of Raspberry Pi boards. Use Cases: Best for projects and applications running on Raspberry Pi hardware, including educational and hobbyist projects.

Armbian ESIOT Overview: Armbian is a distribution designed for single-board computers with ARM processors, such as those from the Banana Pi, Orange Pi, and other similar boards. Features: Performance: Focuses on performance and stability for ARM-based boards. Optimized Kernels: Provides custom kernels with performance enhancements and optimizations. Use Cases: Suitable for various ARM-based single-board computers, providing a balance of performance and ease of use.

OpenWrt ESIOT Overview: OpenWrt is a distribution designed for network devices like routers and access points. It provides a robust platform for building custom firmware with advanced networking features. Features: Networking Features: Includes advanced networking capabilities, firewall configuration, and VPN support. Package Management: Uses the opkg package manager for adding and managing software. Use Cases: Ideal for networking hardware where customized firmware with advanced networking features is required.

Linux From Scratch (LFS) ESIOT Overview: Linux From Scratch is a project that provides step-by-step instructions for building a custom Linux system from source code. Features: Educational: Offers deep insight into how a Linux system is built and configured. Customizability: Allows for creating a highly customized and minimal Linux system. Use Cases: Best for educational purposes or for those who want complete control over their Linux system configuration.

PetaLinux ESIOT Overview: PetaLinux is a distribution provided by Xilinx for their FPGA and SoC platforms. It is tailored to work with Xilinx’s hardware and tools. Features: Integration: Integrated with Xilinx’s development tools and hardware. Customization: Provides tools for configuring and building a Linux system for Xilinx devices. Use Cases: Ideal for embedded systems that use Xilinx FPGAs or SoCs.

SUSE Linux Enterprise Embedded ESIOT Overview: SUSE Linux Enterprise Embedded is a commercial distribution from SUSE designed for embedded systems, providing enterprise-grade support and stability. Features: Enterprise Support: Offers commercial support and certifications. Stability: Provides a stable and well-supported platform for embedded systems. Use Cases: Suitable for industrial and enterprise applications requiring reliable, long-term support.

Android Things ESIOT Overview: Android Things is a version of Android designed for embedded devices, particularly IoT applications. Features: Android Ecosystem: Leverages the Android ecosystem and tools. IoT Focus: Optimized for Internet of Things (IoT) devices. Use Cases: Ideal for IoT projects where the Android ecosystem and application framework are beneficial.

Architecture of Embedded Linux Hardware Layer Hardware Components: This is the physical layer of the embedded system, including: Processor (CPU): Executes instructions and manages system operations. Common architectures include ARM, MIPS, and x86. Memory: Includes RAM (for temporary data storage) and flash storage (for persistent data and firmware). Peripherals: Devices like GPIOs, ADCs, DACs, serial ports, and network interfaces that interact with the CPU. Interfaces: Hardware interfaces such as UART, SPI, I2C, and USB that facilitate communication between different hardware components. ESIOT

Bootloader Layer Bootloader: A small, initial piece of code that prepares the system to load and execute the operating system. Key responsibilities include: Hardware Initialization: Configures hardware components and initializes memory. Kernel Loading: Loads the Linux kernel into memory. Device Tree: Provides hardware configuration information to the kernel. Common Bootloaders: U-Boot, Barebox , or RedBoot ESIOT

Kernel Layer Linux Kernel: The core of the Linux operating system, responsible for: Process Management: Schedules and manages processes and threads. Memory Management: Handles memory allocation, paging, and swapping. Device Drivers: Manages communication between the kernel and hardware peripherals. Filesystems: Manages data storage and retrieval from various filesystems. Networking: Manages network interfaces and protocols. ESIOT

Root Filesystem Layer Root Filesystem ( Rootfs ): The filesystem that contains the essential components needed for the system to run. This includes: System Libraries: Shared libraries such as glibc (GNU C Library) or musl for application support. Utilities and Commands: Basic commands and tools needed for system operation, often provided by BusyBox or similar. Configuration Files: Files that configure system settings and services. Applications: User-space applications and services specific to the embedded system’s purpose. ESIOT

System Initialization Layer System Initialization: This is the process that starts and manages system services and applications after the kernel boots. It includes: Init System: The first process launched by the kernel, responsible for booting up the system. Common init systems are: SysVinit : Traditional init system with runlevels . systemd : Modern init system with parallel service management and dependency handling. OpenRC : A lightweight init system used in some distributions. Startup Scripts: Scripts and configuration files that define services to be started and their dependencies. ESIOT

User-Space Applications Layer User-Space Applications: Programs and services that perform the specific functions of the embedded system. This includes: Application Software: Custom applications designed for the device’s specific use case (e.g., a graphical user interface for a touchscreen device). Background Services: Daemons and background processes that handle tasks such as logging, networking, or data processing. Development Libraries: Libraries and frameworks that applications use, such as GTK for graphical interfaces or OpenCV for computer vision tasks. ESIOT

Development and Debugging Tools Development Tools: Tools used to create, compile, and debug software for embedded systems. These include: Cross-Compilers: Toolchains like GCC for cross-compiling software for the target architecture. IDEs: Integrated Development Environments like Eclipse or Visual Studio Code, configured for embedded development. Debuggers: Tools like GDB (GNU Debugger) for debugging applications running on the embedded system. Kernel Debugging: Tools such as dmesg , ftrace , and kdb for debugging kernel issues. User-Space Debugging: Tools like strace and ltrace for monitoring system calls and library calls. ESIOT

Security Layer Security Measures: Embedded Linux systems often implement various security measures to protect the system from threats: Access Controls: User permissions and role-based access controls. Encryption: Data encryption for secure storage and communication. Secure Boot: Ensures that only trusted code is executed during the boot process. Regular Updates: Patching and updating software to fix vulnerabilities ESIOT

Generally operating systems come in three flavors: On these operating systems, the entire address space is flat or linear with no memory protection between the kernel and applications The core kernel, kernel subsystems, and applications share the same address space. real-time executive, monolithic, and microkernel Architecture of traditional RTOS * 16EC213 EMBEDDED SYSTEMS AND IOT - S.BHAGGIARAJ 35 ESIOT

Monolithic Kernels Monolithic kernels have a distinction between the user and kernel space. When software runs in the user space normally it cannot access the system hardware nor can it execute privileged instructions. Using special entry points (provided by hardware), an application can enter the kernel mode from user space. The user space programs operate on a virtual address so that they cannot corrupt another application’s or the kernel’s memory. * 36 ESIOT

Microkernel Microkernel architecture where kernel subsystems such as network stack and file systems have private address space similar to applications. Microkernels require robust message-passing schemes. Only if the message passing is proper are real-time and modularity ensured. real-time kernel that provides no memory protection. microkernel provides memory protection to individual kernel subsystems at the cost of complexity * 16EC213 EMBEDDED SYSTEMS AND IOT - S.BHAGGIARAJ 37

* 38 ESIOT

Linux Kernel Architecture The Linux kernel can be split into the following subsystems. The hardware abstraction layer Memory manager Scheduler File system IO subsystem Networking subsystem IPC * 39 ESIOT

Hardware Abstraction Layer (HAL) The hardware abstraction layer (HAL) virtualizes the platform hardware so that the different drivers can be ported easily on any hardware. The HAL is equivalent to the BSP provided on most of the RTOSs except that the BSP on commercial RTOSs normally has standard APIs that allow easy porting. Initially linux did not support HAL. Recent linux come with HAL. Example ARM and PowerPC, have a well-described notation of data structures and APIs that make porting to a new board easier. Embedded processors (other than x86) supported on the Linux 2.6 kernel. MIPS PowerPC ARM M68K CRIS V850 SuperH The HAL has support for the following hardware components. Processor, cache, and MMU Setting up the memory map Exception and interrupt handling support DMA Timers System console Bus management Power management * 16EC213 EMBEDDED SYSTEMS AND IOT - S.BHAGGIARAJ 40 ESIOT

Memory Manager The memory manager on Linux is responsible for controlling access to the hardware memory resources. The memory manager is responsible for providing dynamic memory to kernel subsystems such as drivers, file systems, and networking stack. It also implements the software necessary to provide virtual memory to user applications. Each process in the Linux subsystem operates in its separate address space called the virtual address. By using virtual address, a process can corrupt neither another process’s nor the operating system’s memory. Any pointer corruptions within the process are localized to the process without bringing down the system; thus it is very important for system reliability. The Linux kernel divides the total memory available into pages. The typical size of a page is 4 KB * 16EC213 EMBEDDED SYSTEMS AND IOT - S.BHAGGIARAJ 41

Scheduler The Linux scheduler provides the multitasking capabilities Kernel thread: These are processes that do not have a user context. They execute in the kernel space as long as they live. User process: Each user process has its own address space thanks to the virtual memory. They enter into the kernel mode when an interrupt, exception, or a system call is executed. Note that when a process enters the kernel mode, it uses a totally different stack. This is referred to as the kernel stack and each process has its own kernel stack. User thread: The threads are different execution entities that are mapped to a single user process. The user space threads share a common text, data, and heap space. They have separate stack addresses. Other resources such as open files and signal handlers are also shared across the threads. * 42 ESIOT

File System On Linux, the various file systems are managed by a layer called the VFS or the Virtual File System. The virtual file system provides a consistent view of data as stored on various devices on the system. It does this by separating the user view of file systems using standard system calls but allowing the kernel developer to implement logical file systems on any physical device. Thus it abstracts the details of the physical device and the logical file system and allows users to access files in a consistent way. It is necessary for every Linux system to have a master file system, the root file system . * 43 ESIOT

File System The following are some of the commonly used embedded file systems. EXT2: A classical Linux file system that has a broad user base CRAMFS: A compressed read-only file system ROMFS: A read-only file system RAMFS: A read-write, memory-based file system JFFS2: A journaling file system built specifically for storage on flash PROCFS: A pseudo file system used for getting system information DEVFS: A pseudo file system for maintaining the device files * 16EC213 EMBEDDED SYSTEMS AND IOT - S.BHAGGIARAJ 44 ESIOT

Networking Subsystems One of the major strengths of Linux has been its robust support for various networking protocols. * 45 ESIOT

Networking Subsystems * 46 ESIOT

IPC The interprocess communication on Linux includes signals (for asynchronous communication), pipes, and sockets as well as the System V IPC mechanisms such as shared memory, message queues, and semaphores. The 2.6 kernel has the additional support for POSIX-type message queues. * 47 ESIOT

User Space The user space on Linux is based on the following concepts Program: This is the image of an application. It resides on a file system. When an application needs to be run, the image is loaded into memory and run. Note that because of virtual memory the entire process image is not loaded into memory but only the required memory pages are loaded. Virtual memory: This allows each process to have its own address space. Virtual memory allows for advanced features such as shared libraries. Each process has its own memory map in the virtual address space; this is unique for any process and is totally independent of the kernel memory map. System calls: These are entry points into the kernel so that the kernel can execute services on behalf of the application . * 16EC213 EMBEDDED SYSTEMS AND IOT - S.BHAGGIARAJ 48

Kernel Upgrade steps Step 1: Check Your Current Kernel Version. Step 2: Update the Repositories. Step 3: Run the upgrade. Step 1: Back Up Your Important Files. Step 2: Use the Software Updater. Step 3: Configure the Software Updater. Step 4: Force the Upgrade. Step 1: Install OS. ESIOT

Linux Start-Up Sequence The Linux start-up sequence can be split into three phases. Boot loader phase: Typically this stage does the hardware initialization and testing, loads the kernel image, and transfers control to the Linux kernel. Kernel initialization phase: This stage does the platform-specific initialization, brings up the kernel subsystems, turns on multitasking, mounts the root file system, and jumps to user space. User- space initialization phase: Typically this phase brings up the services, does network initialization, and then issues a log-in prompt. * 16EC213 EMBEDDED SYSTEMS AND IOT - S.BHAGGIARAJ 50 ESIOT

Boot Loader Phase The boot loader is the piece of software that starts executing immediately after the system is powered on. The boot loader is an important part of the development process and one of the most complicated ones too. * 51 ESIOT

Kernel Start-Up The kernel start-up can be split into the following phases. CPU/Platform-Specific Initialization Subsystem Initialization Driver Initialization Mounting Root File System Doing Initcall and Freeing Initial Memory Moving to User Space * 16EC213 EMBEDDED SYSTEMS AND IOT - S.BHAGGIARAJ 52 ESIOT

a)CPU/Platform-Specific Initialization Setting up the environment for the first C routine:initialize MMU, kernel stack,etc The setup_arch () function:platform - and CPU specific initialization, recognize the processor,ram space,board Initialization of exceptions — the trap_init () function- specifies exception handler Initialization of interrupt handling procedure — the init_IRQ () function :- initializes the interrupt controller Initialization of timers — the time_init () function-initializes the timer tick hardware,system heartbeat Initialization of the console—the console_init () function- serial device Calculating the delay loops for the platform — the calibrate_delay () function:- used to implement microdelays within the kernel ( udelay () ) * 53 ESIOT

b)Subsystem Initialization Scheduler initialization Memory manager initialization VFS initialization most of the subsystem initialization is done in the start_kernel () function. At the end of this function, the kernel creates another process, the init process, to do the rest of the initialization (driver initialization, initcalls , mounting the root file system, and jumping to user space) and the current process becomes the idle process with process id of 0. c)Driver Initialization The driver initialization is done after the process and memory management is up. It gets done in the context of the init process. * 16EC213 EMBEDDED SYSTEMS AND IOT - S.BHAGGIARAJ 54 ESIOT

d)Mounting Root File System The root file system is the master file system using which other file systems can be mounted There are three kinds of root file systems that are normally used on embedded systems: The initial ram disk- simulates a block device using the system memory. Network-based file system using NFS-debugging builds; Flash-based file system Note that the NFS-based root file system is mainly used for debugging builds The other two are used for production builds. The ram disk simulates a block device using the system memory The ram disk can be used as a root file system and this usage of the ram disk is known as initrd (short form for initial ram disk). * 16EC213 EMBEDDED SYSTEMS AND IOT - S.BHAGGIARAJ 55 ESIOT

e)Doing Initcall and Freeing Initial Memory If you open the linker script for any architecture, it will have an init section. The start of this section is marked using __init_begin and the end is marked using __init_end If driver function is statically linked to the kernel does its registration and initialization, then the function will not be invoked again and hence it can be thrown away. This process is known as “use-and-throw” A use-and-throw function or variable is declared using the __init directive. Once all the driver and subsystem initialization is done, the start-up code frees all the memory. This is done just before moving to user space. * 56 ESIOT

f)Moving to User Space The kernel that is executing in the context of the init process jumps to the user space by overlaying itself (using execve) with the executable image of a special program also referred to as init This executable normally resides in the root file system in the directory /sbin. Note that the user can specify the init program using a command line argument to the kernel * 57 ESIOT

3. User Space Initialization The responsibility of the kernel ends with the transition to the init process. The init process can be configured on any system using the inittab file, which typically resides in the /etc directory. init reads the inittab file and does the actions accordingly in a sequential manner. init also decides the system state known as run level. A run level is a number that is passed as an argument to init. Various run level 0 – Halt the system 1 – Single-user mode (used for administrative purposes) 2 – Multi-user mode with restricted networking capabilities 3 – Full multi-user mode 4 – Unused 5 – Graphics mode * 58 ESIOT

The inittab file has a special format. It generally has the following details. The default run level. The actions to be taken when init is moved to a run level. Typically a script /etc/rc.d/rc is invoked with the run level as the argument. The process that needs to be executed during system start-up. This is typically the file /etc/rc.d/rc.sysinit file. init can respawn a process if it is so configured in the inittab file. This feature is used for respawning the log-in process after a user has logged out from his previous log-in. Actions to trap special events such as Ctrl-Alt_Del or power failure. * 59 ESIOT

5.GNU Cross-Platform Toolchain This process of compiling on a HOST to generate code for the TARGET system is called cross-compilation and the compiler used for the purpose is called a cross-compiler. Any compiler requires a lot of support libraries (such as libc) and binaries (such as assemblers and linkers). One would require a similar set of tools for cross-compilation too. This whole set of tools, binaries, and libraries is collectively called the cross-platform toolchain. * 60 ESIOT

CONCEPT GCC referred to the GNU C Compiler, but now refers to the GNU Compiler Collection. GCC is a collection of integrated compilers for the C, C++, Objective-C, Java™, FORTRAN, and Ada programming languages. However, this document limits its discussion to the C and C++ compilers. Specifically addressed are GCC's basic operation and options, the operation of GCC features specific to POWER architecture, and the most recent features supported by GCC, as packaged with the two leading distributions of Linux for POWER architecture: Red Hat Enterprise Server AS and SUSE LINUX Enterprise Server. * 61 ESIOT

GCC Compilation Process * 62 ESIOT

Pre-processing: via the GNU C Preprocessor (cpp.exe), which includes the headers (#include) and expands the macros (#define). > cpp hello.c > hello.i The resultant intermediate file "hello.i" contains the expanded source code. Compilation: The compiler compiles the pre-processed source code into assembly code for a specific processor. > gcc -S hello.i The -S option specifies to produce assembly code, instead of object code. The resultant assembly file is "hello.s". * 63 ESIOT

Assembly: The assembler (as.exe) converts the assembly code into machine code in the object file "hello.o". > as -o hello.o hello.s Linker: Finally, the linker (ld.exe) links the object code with the library code to produce an executable file "hello.exe". > ld -o hello.exe hello.o   (or) > gcc -o hello hello.o * 64 ESIOT

Device drivers The device drivers are embedded software modules that contain the functionality to operate the individual hardware devices. The reason for the device driver software is to remove the need for the application to know how to control each piece of hardware. Each individual device driver would typically need to know only how to control its hardware device. For instance, for a microwave oven, separate device drivers control the keypad, display, temperature probe, and radiation control. A device driver is nothing more than a collection of software routines that control the operation of a specific peripheral and isolate the application software from the details of that particular hardware device. * 65 ESIOT

Device drivers - Software Layers * 66 ESIOT

Device Driver Design Interrupt priorities : If interrupts are used for the device drivers in a system, you need to determine and set appropriate priority levels. Complete requirements : You need to be aware of the requirements of the various peripherals in the system. You don't want to design and implement your software in a manner that unknowingly handicaps the peripheral from operating as it is intended Resource usage : It is important to understand what resources are necessary for each device driver. For example, imagine designing an Ethernet device driver in a system with a very limited amount of memory. Resource sharing: Beware of possible situations where multiple device drivers need to access common hardware (such as I/O pins) or common memory ESIOT

Benefits of good device driver design Because of the modularity, the structure of the overall software is easier to understand. In addition, it is easier to add or modify features of the overall application as it evolves and matures, even in deployed units. Because there is only one module that ever interacts directly with the peripheral's registers, the state of the hardware device can be more accurately tracked. Software changes that result from hardware changes are localized to the device driver, thereby making the software more portable. ESIOT

Device drivers Device drivers in Linux are classified into three types: Character device drivers: These are used for driving sequential access devices. The amount of data accessed is not of fixed size. The character device drivers are accessed by the application using the standard calls such as open, read, write. For example, a serial driver is a character device driver. * 69 ESIOT

Block device drivers: These are used for driving random access devices. The data exchange happens in terms of blocks. The block device drivers are used for storing file systems. Unlike character drivers, the applications cannot directly access block device drivers; they can be accessed only through a file system. A file system is mounted on a block device thus making the block device driver a mediator between the storage media and the file system. For example, a disk driver is a block device driver. * 70 ESIOT

Network device drivers: Network device drivers are treated as a separate class of device drivers because they interact with the network protocol stack. Applications do not access them directly; only the networking subsystem interacts with them. * 71 ESIOT

Example Simple Device Driver Program Building modules Commands * 72 ESIOT

Simple Device Driver Program //save this file as SREC_IT.c .... #include< linux / kernel.h > #include< linux / init.h > #include< linux / module.h > static int __ init hello_world_init (void) { printk (KERN_INFO "Welcome to Virtual Class Room\n"); printk (KERN_INFO "This is the Simple Module\n"); printk (KERN_INFO "Kernel Module Inserted Successfully...\n"); return 0; } ESIOT

Simple Device Driver Program void __exit hello_world_exit (void) { printk (KERN_INFO "Kernel Module Removed Successfully...\n"); } module_init ( hello_world_init ); module_exit ( hello_world_exit ); MODULE_LICENSE("GPL"); MODULE_AUTHOR("BHAGGIARAJ<[email protected]>"); MODULE_DESCRIPTION("A demo for Device driver"); ESIOT

Building modules obj -m := SREC_IT.o all: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules clean: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean ESIOT

Commands  1 . > uname -r  2. >make Makefile -C / usr / src /linux-headers-4.15.0-20-generic  3.> ls - to check object file created or not in the specified directory  4.sudo dmesg - to see the message communicated by modules to the kernel  5. > sudo dmesg -C - to clear the communicated message   > sudo dmesg - to check again  6.> lsmod - list all the modules running in the systems   7.> sudo insmod simpleDriver.ko - (here simpleDriverf is user defined file.. ko kernel object file) it insert the simpleDriver module in the list  8.> lsmod then followed by > sudo dmesg  9.sudo rmmod simpleDriver.k - to remove kernel object (now the module is removed,  successfully check the command ---  > lsmod to ensure that module is removed or not ESIOT

Ethernet Driver NW_IOADDR : This is the base address for accessing the IO on the card. We assume that system initialization has given a valid base address. NW_IRQ : The interrupt line used for the network card. FILL_ETHER_ADDRESS : A macro that programs the hardware with an Ethernet address. INIT_NW : A routine that initializes the network card. RESET_NW : A routine that resets the network card. READ_INTERRUPT_CONDITION : This macro specifies what has caused the interrupt to happen. In our case, there are two reasons: one is receipt of incoming data and the other is end of transmission. FILL_TX_NW : A routine to copy data from the network buffers to the hardware memory. It is used on the transmit path. READ_RX_NW : A routine that copies the data from hardware memory to network buffers. It is used on the receive path. ESIOT

Driver Initialization and Start-Up struct uart_driver: This data structure contains information about the name, major and minor numbers, and number of ports of this driver. struct uart_port: This data structure contains all the configuration data of the low-level hardware. struct uart_ops: This data structure contains the pointers to functions that operate on the hardware. ESIOT

Tracing Typically, profiling shows the distribution of execution time across routines. It can show the code locations associated with specific bottlenecks, but it does not show the temporal aspect of performance variations. The advantage of profiling is that statistics can be maintained during execution and it requires a small amount of storage. Tracing the execution of a parallel program shows when an event occurred and where it occurred, in terms of the location in the source code and the process that executed it. ESIOT

CSCOPE Cscope is a Linux tool for browsing source code in a terminal environment. C was originally built to work with C code, but also works well with C++, Java, and some other languages. ESIOT

Profiling Profiling shows the summary statistics of performance metrics that characterize the performance of an application. Examples of metrics include: the CPU time associated with a routine The count of the secondary data cache misses associated with a group of statements The number of times a routine executes; etc. These metrics are typically presented as sorted lists that show the contribution of the routines ESIOT

Profiling Profiling is a way to find bottlenecks during program execution so that the results can be used to increase its performance. ESIOT

51 * 16EC213 EMBEDDED SYSTEMS AND IOT - S.BHAGGIARAJ 83 ESIOT