Mmap Direct control of the bottom "turn"

Source: Internet
Author: User

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
  1. /* Example How to access the value of the on-board DIP switches on
  2. * hico.sh7760. Can compile the program with command:
  3. *
  4. * Sh4-linux-gcc-wall Dip_switch.c-o Dip_switch
  5. */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <assert.h>
  9. #include <unistd.h>
  10. #include <errno.h>
  11. #include <fcntl.h>
  12. #include <sys/mman.h>
  13. #define Map_size 4096UL
  14. #define MAP_MASK (MAP_SIZE-1)
  15. int main (void) {
  16. int fd;
  17. void *map_base, *virt_addr;
  18. /* Physical Address of the DIP switch GPIO Register on hico.sh7760 ( see
  19. * The hico.sh7760 Hardware manual
  20. *
  21. * _note_: The dipswitches also define how the system boots (i.e. the value
  22. * is used by the bootloader in startup), so Oafter testting, leave the
  23. * Switches in the same position as they were * *
  24. off_t target = 0x7f008800; //gpkcon0 0x7f008800
  25. if (fd = open ("/dev/mem", O_RDWR | O_sync)) = =-1) {
  26. printf ("/dev/mem could not being opened.\n");
  27. Perror ("open");
  28. Exit (1);
  29. } Else {
  30. printf ("/dev/mem opened.\n");
  31. }
  32. / * MAP one page * /
  33. Map_base = mmap (0, map_size, Prot_read | Prot_write, map_shared, FD, Target & ~map_mask);
  34. if (map_base = = (void *)-1) {
  35. printf ("Memory map failed.\n");
  36. Perror ("mmap");
  37. } Else {
  38. printf ("Memory mapped at address%p.\n", map_base);
  39. }
  40. VIRT_ADDR = Map_base + (target & Map_mask);
  41. / * acess remapped Region here */
  42. printf ("Now try to change the value of the on-board DIP switche\n");
  43. * (unsigned int *) virt_addr=0x11110000; //0x1111<<16;//set to output state
  44. int ab=* (unsigned int *) virt_addr;
  45. //* (unsigned int *) (virt_addr+8) =0xffffffff;
  46. printf ("ab=%x\n", AB);
  47. #if 1
  48. While (1) {
  49. * (volatile unsigned int *) (virt_addr+8) =0x00; //Must be if virt_addr cannot be map_base//0 bright
  50. printf ("1value is 0x%x\n", * (unsigned int *) (virt_addr+8));
  51. printf ("2value is 0x%x\n", * (unsigned int *) VIRT_ADDR);
  52. Sleep (1); //must have or not soon, the human eye can not find
  53. * (volatile unsigned int *) (virt_addr+8) =0xf0; //Reset 1 off
  54. printf ("3value is 0x%x\n", * (unsigned int *) (virt_addr+8));
  55. printf ("4value is 0x%x\n", * (unsigned int *) VIRT_ADDR);
  56. Sleep (1); //must have or not soon, the human eye can not find
  57. }
  58. #endif
  59. /* We' ll never get here on this example
  60. * unmapped. */
  61. if (Munmap (map_base, map_size) = =-1) {
  62. printf ("Memory unmap failed.\n");
  63. }
  64. Close (FD);
  65. }

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"

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.