JZ2440 v3 用户程序 mmap()映射0x56000000函数返回的值 无法使用指针进行操作,在开发板上调试出现Segmentation fault错误

用户程序

int main(int argc, char **argv)

{

int fd;

int val = 1;

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

if (fd < 0)

{

printf("can't open!\n");

}

if (argc != 2)

{

printf("Usage :\n");

printf("%s <on|off>\n", argv[0]);

return 0;

}

gpio_base = (void *)mmap(NULL,0x1000,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0x56000000);

gpio_con = (unsigned long*)(gpio_base + 0x50);

gpio_dat = (unsigned long*)(gpio_base + 0x54);


printf("111   0x%x  0x%x 0x%x\n",gpio_base,gpio_con,gpio_dat);

printf("222   0x%x 0x%x 0x%x 0x%x\n",gpio_base,*(unsigned long*)gpio_base,gpio_con,gpio_dat);


*gpio_con &= ~((3<<8) | (3<<10) | (3<<12));

*gpio_con |= ((1<<8) | (1<<10) | (1<<12));


if (strcmp(argv[1], "on") == 0)

{

val  = 1;

*gpio_dat &= ~((1<<4)|(1<<5)|(1<<6));

printf("11111 :\n");

}

else

{

val = 0;

*gpio_dat |= (1<<4)|(1<<5)|(1<<6); 

printf("00000 :\n");

}



munmap(gpio_base, 0x1000);

close(fd);

return 0;

}

驱动程序

static int led_drv_mmap(struct file *file,struct vm_area_struct *vma)

{

unsigned long size;


//关闭CACHE功能  保证数据的一致性

vma->vm_flags |= VM_RESERVED;

   vma->vm_flags |= VM_IO;

vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);


size = vma->vm_end - vma->vm_start ;


//remap_pfn_range(vma, vma->vm_start, led_phy_add>>12, size, vma->vm_page_prot);

remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff, size, vma->vm_page_prot);


printk("%s 0x%x  %d  0x%x 0x%x \n",__FUNCTION__,vma->vm_pgoff,size,vma->vm_start,vma->vm_end);

}


调试结果

led_drv_mmap 0x56000  4096  0x40016000 0x40017000 

111   0x36  0x86 0x8a

Segmentation fault

显示用户mmap返回值是0x36   映射0x56000000   但是发现无法对0x36使用指针进行操作



请先 登录 后评论

1 个回答

百问网-黄老师
擅长:嵌入式开发

gpio_base = (void *)mmap(NULL,0x1000,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0x56000000);

改为

gpio_base =mmap(NULL,1024*4,PROT_READ | PROT_WRITE,MAP_SHARED,fd,0x56000000);

if(gpio_base == NULL)

{

printf(mmap err!n);

return -1;

}


判断下返回值,看是否这里的问题


请先 登录 后评论