Engineers Garage

  • Projects and Tutorials
    • Electronic Projects
      • 8051
      • Arduino
      • ARM
      • AVR
      • PIC
      • Raspberry pi
      • STM32
    • Tutorials
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Products News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • EE Resources
    • DesignFast
    • LEAP Awards
    • Oscilloscope Product Finder
    • White Papers
    • Webinars
  • EE Learning Center
  • Women in Engineering

Hello world module programming (Part 24/24)

By Ashish Vara

           Hello World Module Programming
 
Linux kernel supports modular approach.  It supports insertion and removal of code from itself at a routine. Module is loadable kernel object which we can load and unload during run time. Module is single binary image which contains the information of related subroutine, group, data, entry and exit. Device driver is also one type of module which provides the facilities to reload, debugging,  and loading new driver on demand in response to hot plugging of new device.  It is allowed to access hardware connected to the system. We can build monolithic kernel using module kernel and new functionality in kernel image. 
 
Let’s understand  how module would be created and work in Linux. Module is like a simple file and we can save it as  C file with .c extension but  it is somewhat different.  It does not need  to write code inside main function.
 
This is simple hello world module programming for understanding how module is created in Linux.
 
//********************************hello_module.c******************************//
#include <linux/module.h> /* Needed by all modules */
#include <linux/kernel.h> /* Needed for KERN_INFO */
#include <linux/init.h> /* Needed for the macros */
 
static int __init hello_enter(void)
{
printk (KERN_INFO “Welcome In hello worldn”);
return 0;
}
static void __exit hello_exit(void)
{
printk (KERN_INFO “Goodbye, worldn”);
}
module_init (hello_enter);
module_exit (hello_exit);
//********************************hello_module.c******************************//
 
Here printk ( ) function behaves like printf ( ) in C language. You must be thinking if printk ( ) behaves same like printf ( ), why printk ( ) created. printk ( ) function gives the state information of kernel system. Module programming is done in kernel mode but kernel mode cannot use standard C library that’s why special printk ( ) function  is used in debugging mode of kernel module programming.
 
How to create Make file
 
Makefile is compiling and rebuilding  all the files with only single file. Make file needs to be rebuilt and compiled when some changes are done by programmer in file. Each application has main source file, header file and device file. We can make the executable file by compiling these files using make file.  It has some rules to write command and build / rebuild the file which are managed by make program.
 
Let’s make the Makefile  to compile and build hello_module.c file.
 
Create new Directory named Hello and Open the text document file and save it as Makefile in same directory.
 
//************************************Makefile********************************//
 
obj-m += hello_module.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
 
//************************************Makefile********************************//
 
Write the same code as above in Makefile and save it in Hello directory. 
obj-m += hello_module.o indicates the object file of hello_module.c 
 
Build and compile Make File
 
Make sure you are in same directory where Makefile is stored (inside Hello directory in this tutorial) before building and compiling it. Write the following command line and press ENTER key:
 
$ Make
 
Now your hello module is loaded into kernel. This command  creates a device module file and kernel object file inside the Hello directory. 
 
Following output is displayed on screen:
 
make -C /lib/modules/3.16.0-30-generic/build M=/home/ashish/DD modules
make[1]: Entering directory `/usr/src/linux-headers-3.16.0-30-generic’
CC [M]  /home/ashish/DD/ hello_module.o
Building modules, stage 2.
MODPOST 1 modules
CC      /home/ashish/DD/ hello_module.mod.o
LD [M]  /home/ashish/DD/ hello_module.ko
make[1]: Leaving directory `/usr/src/linux-headers-3.16.0-30-generic’
 
You need to enter the following command line for installing the hello module by running hello_module.ko file:
 
insmod hello_module.ko
 
This insmode command executes initial hello_enter ( ) function and installs the hello module in kernel.
Let’s see if the hello module is loaded or not. Read the message of hello module by following command and then press ENTER key:
 
dmesg | tail
 
You can read the last content of file and display the message “Welcome In hello world” on screen.
 
Remove the hello module by entering following command:
 
rmmod hello_module
 
This rmmod command unloads the module which is currently in run. Let’s see if the hello module is unloaded or not. Read the message of hello module by following command and then press ENTER key:
 
dmesg | tail
 
You can read the last content of file and display the message “Goodbye, world” on screen which indicates that the hello module is unloaded.

Filed Under: Tutorials

 

Questions related to this article?
👉Ask and discuss on Electro-Tech-Online.com and EDAboard.com forums.



Tell Us What You Think!! Cancel reply

You must be logged in to post a comment.

HAVE A QUESTION?

Have a technical question about an article or other engineering questions? Check out our engineering forums EDABoard.com and Electro-Tech-Online.com where you can get those questions asked and answered by your peers!


Featured Tutorials

  • Designing Gate Driver Circuit and Switching Mechanism for Modified Sine Wave Inverter – (Part 9/17)
  • Completing Modified Sine Wave Inverter Design with Full Bridge Circuit and Step Up Transformer – (Part 10/17)
  • Designing an Offline UPS – Part (12 /17)
  • How to reduce Switching Time of a Relay – (Part 15/17)
  • Testing MOSFET – (Part 16/17)
  • Driving High Side MOSFET using Bootstrap Circuitry – (Part 17/17)

Stay Up To Date

Newsletter Signup

Sign up and receive our weekly newsletter for latest Tech articles, Electronics Projects, Tutorial series and other insightful tech content.

EE Training Center Classrooms

EE Classrooms

Recent Articles

  • New automotive radar sensor enables reliable in-cabin monitoring system
  • TI breaks ground on 300-mm semiconductor wafer-fabrication plants in Texas
  • New wireless AR Smart Viewer reference design
  • Infineon launches scalable and wireless charging platform with configurable controllers
  • First single-core MPU with MIPI CSI-2 camera interface and audio

Most Popular

5G 555 timer circuit 8051 ai Arduino atmega16 automotive avr dc motor display Electronic Part Electronic Parts Fujitsu ic infineontechnologies integratedcircuit Intel IoT ir lcd ldr led maximintegratedproducts microchip microchiptechnology Microchip Technology microcontroller microcontrollers mosfet motor powermanagement Raspberry Pi remote renesaselectronics renesaselectronicscorporation Research samsung semiconductor sensor software STMicroelectronics switch Technology vishayintertechnology wireless

RSS EDABOARD.com Discussions

  • A circuit that can adjust a resistance and probing a voltage node
  • DC to DC buck converter
  • A circuit that can probe 2 currents and adjust the resistor
  • Microsoft Project 2019 dependencies
  • MOSFET ORing circuit simulation (LTspice)

RSS Electro-Tech-Online.com Discussions

  • Enclosure sought
  • Need help using a common power supply for two devices
  • Fletcher's Law
  • Setting the 18F24K20 to digital.
  • Multistage BJT amplifier
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • DesignFast
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • About Us
  • Contact Us
  • Advertise

Copyright © 2022 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy | Advertising | About Us

Search Engineers Garage

  • Projects and Tutorials
    • Electronic Projects
      • 8051
      • Arduino
      • ARM
      • AVR
      • PIC
      • Raspberry pi
      • STM32
    • Tutorials
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Products News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • EE Resources
    • DesignFast
    • LEAP Awards
    • Oscilloscope Product Finder
    • White Papers
    • Webinars
  • EE Learning Center
  • Women in Engineering