Transferred from: http://blog.csdn.net/xyyangkun/article/details/7830149
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
This is a successful test on the mini6410, with no driver in the case of direct control of the LED lamp test_mmap.c:
[CPP]View PlainCopy
- /* Example How to access the value of the on-board DIP switches on
- * hico.sh7760. Can compile the program with command:
- *
- * Sh4-linux-gcc-wall Dip_switch.c-o Dip_switch
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <assert.h>
- #include <unistd.h>
- #include <errno.h>
- #include <fcntl.h>
- #include <sys/mman.h>
- #define Map_size 4096UL
- #define MAP_MASK (MAP_SIZE-1)
- int main (void) {
- int fd;
- void *map_base, *virt_addr;
- /* Physical Address of the DIP switch GPIO Register on hico.sh7760 ( see
- * The hico.sh7760 Hardware manual
- *
- * _note_: The dipswitches also define how the system boots (i.e. the value
- * is used by the bootloader in startup), so Oafter testting, leave the
- * Switches in the same position as they were * *
- off_t target = 0x7f008800; //gpkcon0 0x7f008800
- if (fd = open ("/dev/mem", O_RDWR | O_sync)) = =-1) {
- printf ("/dev/mem could not being opened.\n");
- Perror ("open");
- Exit (1);
- } Else {
- printf ("/dev/mem opened.\n");
- }
- / * MAP one page * /
- Map_base = mmap (0, map_size, Prot_read | Prot_write, map_shared, FD, Target & ~map_mask);
- if (map_base = = (void *)-1) {
- printf ("Memory map failed.\n");
- Perror ("mmap");
- } Else {
- printf ("Memory mapped at address%p.\n", map_base);
- }
- VIRT_ADDR = Map_base + (target & Map_mask);
- / * acess remapped Region here */
- printf ("Now try to change the value of the on-board DIP switche\n");
- * (unsigned int *) virt_addr=0x11110000; //0x1111<<16;//set to output state
- int ab=* (unsigned int *) virt_addr;
- //* (unsigned int *) (virt_addr+8) =0xffffffff;
- printf ("ab=%x\n", AB);
- #if 1
- While (1) {
- * (volatile unsigned int *) (virt_addr+8) =0x00; //Must be if virt_addr cannot be map_base//0 bright
- printf ("1value is 0x%x\n", * (unsigned int *) (virt_addr+8));
- printf ("2value is 0x%x\n", * (unsigned int *) VIRT_ADDR);
- Sleep (1); //must have or not soon, the human eye can not find
- * (volatile unsigned int *) (virt_addr+8) =0xf0; //Reset 1 off
- printf ("3value is 0x%x\n", * (unsigned int *) (virt_addr+8));
- printf ("4value is 0x%x\n", * (unsigned int *) VIRT_ADDR);
- Sleep (1); //must have or not soon, the human eye can not find
- }
- #endif
- /* We' ll never get here on this example
- * unmapped. */
- if (Munmap (map_base, map_size) = =-1) {
- printf ("Memory unmap failed.\n");
- }
- Close (FD);
- }
The above procedure proves that under Linux, the MMAP function can directly control the bottom layer without the driver.
I can only control the GPIO device on the Internet, and I have the ability to control other devices directly, pending verification.
Mmap Direct control of the bottom "turn"