Mmap maps physical addresses to user space

Source: Internet
Author: User
1. Application space void *virt_mem = Kzalloc (page_size, Gfp_kernel);
    2. Drive Layer----->> implement File_operation interface. Mmap method int Led_drv_mmap (struct file *filp, struct vm_area_struct *vma) {

    VMA is an information body, the user provides MMAP request//Parameter 1, VMA information carrier//parameter 2, User space virtual start position//Parameter 3, physical address unsigned long addr;

    if (((Vma->vm_end-vma->vm_start)!= page_size) | | vma->vm_pgoff) return-einval;

    Addr = Virt_to_phys (LED_DEV->VIRT_MEM);
    Vma->vm_flags |= Vm_io;    

    Vma->vm_page_prot = pgprot_noncached (Vma->vm_page_prot); if (Io_remap_pfn_range (VMA, Vma->vm_start, addr >> page_shift, page_size, Vma->vm_page_pro
        T)) {PRINTK (kern_err "%s:io_remap_pfn_range failed\n", __func__);
    Return-eagain;

     return 0; } 3.

    User layer of the dropped FD = open ("/dev/led0", O_RDWR);
    mmap int len = page_size; The return value is a mapped physical address char *mmap_addr = mmap (NULL, Len, prot_read| Prot_write, map_shared, FD, 0);

    Write a value to the inside memcpy (Mmap_addr,str,strlen (str));

    Sleep (1);
    Reading data read (FD, RBUF, 64);
 printf ("Rbuf =%s\n", rbuf);
2. String or Chinese to display the number of rows in the screen position, parameter 1:x axis coordinate parameter 2:y axis coordinate parameter 3: Display content size parameter 4: Display content void Display_character (unsigned int x,unsigned int
    y,unsigned int len,unsigned char *string) {int k, XX;
    unsigned char qh,wh;
    const unsigned char *mould;

    unsigned int length =len;
    printf ("Length =%d\n", length);         for (k=0,xx=x;k<length-1;k++) {if (string[k]&0x80)//Chinese character {QH =string[k]-0xa0;       Area code WH =string[k+1]-0xa0;
            Bit number mould =&__chs[((qh-1) *94+wh-1) *32];
            Draw_text16 (4+xx,y,0x0000ff,mould);
            xx+=16;
        k++;
            else {mould =&__ascii[string[k]*16];
            Draw_ascii (4+xx,y,0xff0000,mould);
        xx+=8; 
    3. Gets the physical space size of the vinfo.xres *VINFO.YRES*VINFO.BITS_PER_PIXEL/8 to the LCD screen and maps to the application layer static int init_lcd (char *dev_name) {
    int framebuffer_fd;
    /*1. Open frame Buffer device */FRAMEBUFFER_FD =open (DEV_NAME,O_RDWR); if (Framebuffer_fd<0) {printf ("Error:failed Open framebuffer device!\n");
    return-1; /*2. Gets the fixed parameter/if (IOCTL (Framebuffer_fd,fbioget_fscreeninfo,&finfo)) {printf ("error:failed get th
        E framebuffer device ' s fix informations!\n ");
    return-1; /*3. Gets the variable parameter/if (IOCTL (Framebuffer_fd,fbioget_vscreeninfo,&vinfo)) {printf ("error:failed get th
        E framebuffer device ' s var informations!\n ");
    return-1;
    printf ("Xres =%ld\n", vinfo.xres);
    printf ("Yres =%ld\n", vinfo.yres);
    ScreenSize = (vinfo.xres *vinfo.yres*vinfo.bits_per_pixel/8);


    printf ("ScreenSize =%ld\n", screensize); /*5. Mapping/framebuffer_ptr = (char *) mmap (null,//If this value is NULL, the kernel is used to automatically assign you a virtual space screens ize,//Space size prot_read| prot_write,//permission map_shared,//whether can be shared framebuffer_ FD,//FileDescriptor 0);
        from which place to start if (framebuffer_ptr<0) {printf ("error:failed to Mmap device mem!\n");
    return-1;
    } memset (Framebuffer_ptr,0,screensize);
    printf ("Framebuffer_ptr is mmaped ok!\n");
return FRAMEBUFFER_FD; } 4.
    /*1 the operation of the picture file. Open a picture * * pic_fd =open (ARGV[2],O_RDWR);

    printf ("pic_fd=%d\n", PIC_FD);
    /*2. Get picture size/Len =lseek (pic_fd, 0, seek_end);

    printf ("Len =%ld\n", Len);

    Buffer = (char *) malloc (len);
    Lseek (pic_fd, 0, Seek_set);

/*3. Read the picture data * * (Pic_fd,buffer,len); 5. If you want to display a picture picture file--read data--parse picture data--------data into RGB565---------------what data (RGB) void Draw_bmp (char *bmpfilename) is required for the LCD screen
    unsigned short *FB) {unsigned char * bmpdata;

    int ret;
    ret = Bmp_read_file (Bmpfilename, &bmpdata);
    if (ret) {printf ("read Bmpfile error.\n"); printf ("bmpread0000000!!!!
    \ n ");
    bmp2fb16_rgb565 (Bmpdata, FB); printf ("bm2fb1600000000000!!!!
\ n "); }

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.