The relationship between the virtual address and the physical address of the 2440--linux2.6.30.4

Source: Internet
Author: User
Tags volatile

1

You want to modify the permissions of all the files in a directory at once, including the file permissions in subdirectories, and to use the parameter-R to initiate recursive processing.
Just beginning to learn character device drivers, the most difficult thing to do is to drive and connect to the underlying hardware. Linux driver is based on the operating system, he does not directly deal with the underlying hardware, but we write the driver must be able to make the hardware "run", that is, the hardware closely connected.
Take the simplest led driver, our driver is running on the virtual memory, but ultimately, the LED light or must rely on the high and low level of the GPIO pin to control. So, how can our virtual memory correspond to the registers on the actual hardware?
This is written to ioremap this mapping function, which can map the registers on our hardware to virtual memory, so that the driver runs in our virtual memory.
#include <linux/module.h>
#include <linux/device.h>
#include <linux/types.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/mm.h>
#include <linux/sched.h>
#include <linux/init.h>
#include <linux/cdev.h>
#include <asm/io.h>
#include <asm/system.h>
#include <asm/uaccess.h>


2

#include <linux/module.h>
#include <linux/init.h>
#include <asm/io.h>
#include <linux/io.h>
Volatile unsigned long virt, phys;//for storing virtual and physical addresses
Volatile unsigned long *gpbcon, *gpbdat, *gpbup;//with and storage of three register addresses
void Led_device_init (void)
{
0x56000010 + 0x10 for all IO pin register addresses
phys = 0x56000010; 0x56000010=gpbcon
Request a contiguous space of length 0x10 in the virtual address space
In this way, the physical address Phys to phys+0x10 corresponds to the virtual address virt to virt+0x10
Virt = (unsigned long) Ioremap (Phys, 0x10);
Gpbcon = (unsigned long *) (virt + 0x00);//Specify the address of the three registers that need to be manipulated
Gpbdat = (unsigned long *) (virt + 0x04);
Gpbup = (unsigned long *) (virt + 0x08);
}
LED configuration function, configuring the GPIO registers of the Development Board
void Led_configure (void)
{
*gpbcon &= ~ (3 <<) &~ (3<<12) &~ (3 <<) &~ (3<<20);//GPB12 Defaule zeroing
*gpbcon |= (1 << 10) | (1<<12) | (1<<16) | (1&LT;&LT;20); Output mode
*gpbup |= (1 << 5) | (1 <<6) | (1 <<8) |  (1 <<10); Prohibit pull-up resistor
}
void Led_off (void)//illuminated LED
{
*gpbdat &= ~ (1 << 5) &~ (1 << 6) &~ (1 << 8) &~ (1 << 7);
}
void led_on (void)//Kill LED
{
*gpbdat |= (1 << 5) | (1 << 6) | (1 << 8) | (1 << 7);
}
static int __init led_init (void)//module initialization function
{
Led_device_init (); Implementing the mapping of IO memory
Led_configure (); Configuration GPB5 6 8 10 for output
Led_on ();
PRINTK ("Hello on!\n");
return 0;
}
static void __exit led_exit (void)//module unload function
{
Led_off ();
Iounmap ((void *) virt); To undo a mapping relationship
PRINTK ("led off!\n");
}
Module_init (Led_init);
Module_exit (Led_exit);
Module_license ("GPL");
Module_author ("litingting<>");
Module_version ("2015-5-26");


3 Makefile



Ifneq ($ (kernelrelease),)
Obj-m: =VIR.O
Else
Kerneldir: =/home/litingting/gec2440/linux-2.6.30.4
All
Make-c $ (Kerneldir) m=$ (PWD) modules Arch=arm cross_compile=arm-linux-
Clean
Rm-f *.o *.ko *.mod.o *.mod.c *.symvers modul* *.*~

endif

The relationship between the virtual address and the physical address of the 2440--linux2.6.30.4

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.