天天看点

LED驱动实例4(mmap)

第一个:

#include <sys/mman.h>

#include <unistd.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <fcntl.h>

#include <stdio.h>

#define LED_PHY_BASE 0x56000050

int main(void)

{

 int fd = -1;

 volatile  unsigned int *pAddr = NULL;

 fd = open("/dev/mem",O_RDWR);

 if(fd < 0) {

  printf("Can't open the /dev/mem device!\n");

 }

 pAddr = mmap(0, getpagesize(), PROT_READ|PROT_WRITE, MAP_SHARED,fd, LED_PHY_BASE);

 *pAddr = ~0xff00;

 *pAddr |= 0x5500;

    *(pAddr +8) = 0xf0; //diable pull-up funaction 

 while(1) {

   *(pAddr+4) = 0x70; //Lighten D9

  sleep(1);

  *(pAddr +4)= 0xf0;

  sleep(1);

 }

 return 0;

}

第二个:

#include <sys/mman.h>

#include <unistd.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <fcntl.h>

#include <stdio.h>

#define GPIO_BASE 0x56000000

#define GPFCONF_OFFSET 0x50

#define GPFDAT_OFFSET 0x54

#define GPFUP_OFFSET 0x58

volatile unsigned int *GPFCONF;

volatile unsigned int *GPFDAT;

volatile unsigned int *GPFUP;

int main(void)

{

 int fd = -1;

 unsigned char *pAddr = NULL;

 fd = open("/dev/mem",O_RDWR|O_SYNC);

 if(fd < 0) {

  printf("Can't open the /dev/mem device!\n");

 }

 pAddr = (unsigned char *)mmap(0, getpagesize(), PROT_READ|PROT_WRITE, MAP_SHARED,fd, GPIO_BASE);

 if (pAddr == MAP_FAILED) {

  printf("mmap error!\n");

  close(fd);

  return -1;

 }

 GPFCONF = (unsigned int *)(pAddr+GPFCONF_OFFSET);

  GPFDAT  = (unsigned int *)(pAddr+GPFDAT_OFFSET);

 GPFUP   = (unsigned int *)(pAddr+GPFUP_OFFSET);

 *GPFCONF &= ~0xff00;

 *GPFCONF |= 0x5500;

 printf("GPFCONF=0x%0x\n", *GPFCONF);

    *GPFUP = 0xf0; //diable pull-up funaction 

  printf("GPFUP=%0x\n", *GPFUP);

 while(1) {

   *GPFDAT = 0x70; //Lighten D9

  printf("GPFDAT=%0x\n", *GPFDAT);

  usleep(200000);

  *GPFDAT= 0xf0;

  printf("GPFDAT=%0x\n", *GPFDAT);

  usleep(200000);

 }

 munmap((void *)pAddr, getpagesize());

 return 0;

}

第三个:

#include <stdio.h>

#include <sys/mman.h>

#include <termios.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <unistd.h>

#include <fcntl.h>

#include <string.h>

#define BLOCKSIZE (unsigned long) 1

#define OFFSET_A (unsigned long) 0

#define OFFSET_B (unsigned long) 1

#define OFFSET_C (unsigned long) 2

#define ASCII_a 97

#define ASCII_z 122

int main(int argc, char **argv)

{

  int fd_flash, fd_ram;

  int index;

  struct termios lterm, old_lterm;

  char * block0;

  char * block1;

  char * block2;

  int pagesize;

  unsigned char data;

  tcgetattr(0, &lterm);

  old_lterm = lterm;

  lterm.c_lflag &= ~ICANON;

  tcsetattr(0, TCSANOW, &lterm);

  if (argc != 3) {

    printf("usage: mmap_demo <flash_file> <ram_file>\n");

    return -1;

  }

  pagesize = getpagesize();

  printf("Pagesize is %d\n", pagesize);

  if ((fd_flash = open (argv[1], O_RDWR)) < 0)

   perror ("can't open flash file for rd/wr");

  if ((fd_ram = open (argv[2], O_RDWR)) < 0)

   perror ("can't open ram file for rd/wr");

  if ((block0 = mmap (0, BLOCKSIZE*pagesize, PROT_WRITE|PROT_READ, MAP_SHARED, fd_flash, OFFSET_A * pagesize ))

      == (caddr_t) -1)

    perror ("mmap error for block0");

  if ((block1 = mmap (0, BLOCKSIZE*pagesize, PROT_WRITE|PROT_READ, MAP_PRIVATE, fd_flash, OFFSET_B * pagesize ))

      == (caddr_t) -1)

    perror ("mmap error for block1");

  if ((block2 = mmap (0, BLOCKSIZE*pagesize, PROT_WRITE|PROT_READ, MAP_SHARED, fd_ram, OFFSET_C * pagesize ))

      == (caddr_t) -1)

    perror ("mmap error for block2");

  index = 0;

  data = ASCII_a;

  while ( 1 ) {

    printf("%4X: %2X %2X %2X\n cmd: ", index, *(char *)(block0+index), *(char *)(block1+index), *(char *)(block2+index) );

    int c = getchar();

    printf("\n");

    switch (c) {

    case '+':

      index++;

      if (index>(BLOCKSIZE*pagesize-1)) index=(BLOCKSIZE*pagesize-1);

      printf("index = %4X\n", index);

      break;

    case '-':

      index--;

      if (index<0) index=0;

      printf("index = %4X\n", index);

      break;

    case 'u':

      index+=0x100;

      if (index>(BLOCKSIZE*pagesize-1)) index=(BLOCKSIZE*pagesize-1);

      printf("index = %4X\n", index);

      break;

    case 'd':

      index-=0x100;

      if (index<0) index=0;

      printf("index = %4X\n", index);

      break;

    case 'r':

      data=ASCII_a;

      break;

    case 'w':

      printf("write %2X: ", data);

      if (memcpy(block0+index, (void *)&data, sizeof(unsigned char)) != block0+index) {

 printf("F "); } else { printf("P "); }

      if (memcpy(block1+index, (void *)&data, sizeof(unsigned char)) != block1+index) {

 printf("F "); } else { printf("P "); }

      if (memcpy(block2+index, (void *)&data, sizeof(unsigned char)) != block2+index) {

 printf("F "); } else { printf("P "); }

      printf("\n");

      data++;

      if (data>ASCII_z) data=ASCII_a;

      break;

    case 'c':

      printf("Copying block2 to block1\n");

      if (munmap (block1, BLOCKSIZE != 0))

 perror ("munmap error for block1");

      if ((block1 = mmap (0, BLOCKSIZE, PROT_WRITE|PROT_READ, MAP_SHARED, fd_flash, OFFSET_B * pagesize ))

   == (caddr_t) -1)

 perror ("mmap error for block1");

      if (memcpy(block1, block2, BLOCKSIZE*pagesize) != block1)

 perror("Error copying data over to block1\n");

      if (munmap (block1, BLOCKSIZE != 0))

 perror ("munmap error for block1");

      if ((block1 = mmap (0, BLOCKSIZE, PROT_WRITE|PROT_READ, MAP_PRIVATE, fd_flash, OFFSET_B * pagesize ))

   == (caddr_t) -1)

 perror ("mmap error for block1");

      break;

    case 'q':

      goto all_done;

      break;

    case 's':

      printf("sync'ing writes\n");

      if (msync(block0, BLOCKSIZE, MS_SYNC) != 0)

 perror ("error msync of block0");

      break;

    }

  }

  all_done:

  if (munmap (block0, BLOCKSIZE != 0))

     perror ("munmap error for block0");

  if (munmap (block1, BLOCKSIZE != 0))

     perror ("munmap error for block1");

  if (munmap (block2, BLOCKSIZE != 0))

     perror ("munmap error for block2");

  if (close(fd_flash) != 0)

    perror ("error closing fd_flash");

  if (close(fd_ram) != 0)

    perror ("error closing fd_ram");

  tcsetattr(0, TCSANOW, &old_lterm);

  printf("\n");

  return 0;

}