加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

LED驱动实例4(mmap)

发布时间:2020-12-15 06:57:48 所属栏目:百科 来源:网络整理
导读:? 第一个: #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("/
?

第一个:

#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;
}

?

第二个:

/*this led user program demo runs on FS2410,By Shenzhen Farsight Inc.*/
#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,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%0xn",*GPFCONF);

?? ?*GPFUP = 0xf0; //diable pull-up funaction?
? printf("GPFUP=%0xn",*GPFUP);

?while(1) {
???*GPFDAT = 0x70; //Lighten D9
??printf("GPFDAT=%0xn",*GPFDAT);

??usleep(200000);
??
??*GPFDAT= 0xf0;
??printf("GPFDAT=%0xn",*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;

? /* Get current termios settings so we can restore them later */
? tcgetattr(0,&lterm);
? old_lterm = lterm;
? lterm.c_lflag &= ~ICANON;
? tcsetattr(0,TCSANOW,&lterm);

? /*Args*/
? if (argc != 3) {
??? printf("usage: mmap_demo <flash_file> <ram_file>n");
??? return -1;
? }

? /* Set up mmap */
? pagesize = getpagesize();
? printf("Pagesize is %dn",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,fd_flash,OFFSET_A * pagesize ))
????? == (caddr_t) -1)
??? perror ("mmap error for block0");

? if ((block1 = mmap (0,MAP_PRIVATE,OFFSET_B * pagesize ))
????? == (caddr_t) -1)
??? perror ("mmap error for block1");

? if ((block2 = mmap (0,fd_ram,OFFSET_C * pagesize ))
????? == (caddr_t) -1)
??? perror ("mmap error for block2");

? /* Parent's main control loop */
? index = 0;
? data = ASCII_a;
? while ( 1 ) {
??? printf("%4X: %2X %2X %2Xn 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 = %4Xn",index);
????? break;
??? case '-':
????? index--;
????? if (index<0) index=0;
????? printf("index = %4Xn",index);
????? break;
??? case 'u':
????? index+=0x100;
????? if (index>(BLOCKSIZE*pagesize-1)) index=(BLOCKSIZE*pagesize-1);
????? printf("index = %4Xn",index);
????? break;
??? case 'd':
????? index-=0x100;
????? if (index<0) index=0;
????? printf("index = %4Xn",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,sizeof(unsigned char)) != block1+index) {
?printf("F "); } else { printf("P "); }
????? if (memcpy(block2+index,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 block1n");
????? /* unmap and remap as MAP_SHARED */
????? if (munmap (block1,BLOCKSIZE != 0))
?perror ("munmap error for block1");
????? if ((block1 = mmap (0,BLOCKSIZE,OFFSET_B * pagesize ))
?? == (caddr_t) -1)
?perror ("mmap error for block1");

????? /* Copy the data */
????? if (memcpy(block1,block2,BLOCKSIZE*pagesize) != block1)
?perror("Error copying data over to block1n");

????? /* unmap and remap as MAP_PRIVATE */
????? if (munmap (block1,OFFSET_B * pagesize ))
?? == (caddr_t) -1)
?perror ("mmap error for block1");
????? break;
??? case 'q':
????? goto all_done;
????? break;
??? case 's':
????? printf("sync'ing writesn");
????? if (msync(block0,MS_SYNC) != 0)
?perror ("error msync of block0");
????? break;
??? }
? }

? all_done:
? /* unmap */
? 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");

? /* close */
? if (close(fd_flash) != 0)
??? perror ("error closing fd_flash");

? if (close(fd_ram) != 0)??? perror ("error closing fd_ram");? ? tcsetattr(0,&old_lterm);? printf("n");? return 0;}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读