1、  背景介绍

在板上,ZYNQ PL部分通过EMC连接一片NOR FLASH,地址空间如下:


可以看到NOR FLASH的起始地址为0x80000000,这是物理地址,可以把数据存放在以该地址起始的一段区域。

需要注意的是,在对NOR FLASH进行读写数据时,需要参考对应的datasheet,例如这里选用的NOR FLASH读、写、擦除步骤如下:





通过上面的表格就知道进行相应操作每一步该做什么,可以转换为SDK中裸奔程序的C代码。

 

2、  Linux下flash操作

之前提到过zynq中Linux用户应用程序可以通过/dev/mem访问到物理地址,xil_in32和xil_out32等裸奔程序中常见的函数可以通过这一机制移植到linux下。于是,对flash的操作其实就是基于xil_in和xil_out对物理地址进行读写。

这里只需要实现三个函数,erase,write_sector以及read_sector.代码如下:

 

//xil_io.h

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>

#include <unistd.h>
#include <fcntl.h>

#define PAGE_SIZE  ((size_t)getpagesize())
#define PAGE_MASK ((uint64_t) (long)~(PAGE_SIZE - 1))

void Xil_Out32(uint64_t phyaddr, uint32_t val)
{
	int fd;
	volatile uint8_t *map_base;
	uint64_t base = phyaddr & PAGE_MASK;
	uint64_t pgoffset = phyaddr & (~PAGE_MASK);

	if((fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1)
	{
		perror("open /dev/mem:");
	}

	map_base = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED,
			fd, base);
	if(map_base == MAP_FAILED)
	{
		perror("mmap:");
	}
	*(volatile uint32_t *)(map_base + pgoffset) = val; 
	close(fd);
	munmap((void *)map_base, PAGE_SIZE);
}

int Xil_In32(uint64_t phyaddr)
{
	int fd;
	uint32_t val;
	volatile uint8_t *map_base;
	uint64_t base = phyaddr & PAGE_MASK;
	uint64_t pgoffset = phyaddr & (~PAGE_MASK);
	//open /dev/mem
	if((fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1)
	{
		perror("open /dev/mem:");
	}
	//mmap
	map_base = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED,
			fd, base);
	if(map_base == MAP_FAILED)
	{
		perror("mmap:");
	}
	val = *(volatile uint32_t *)(map_base + pgoffset);
	close(fd);
	munmap((void *)map_base, PAGE_SIZE);

	return val;
}

void Xil_Out16(uint64_t phyaddr, uint16_t val)
{
	int fd;
	volatile uint8_t *map_base;
	uint64_t base = phyaddr & PAGE_MASK;
	uint64_t pgoffset = phyaddr & (~PAGE_MASK);

	if((fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1)
	{
		perror("open /dev/mem:");
	}

	map_base = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED,
			fd, base);
	if(map_base == MAP_FAILED)
	{
		perror("mmap:");
	}
	*(volatile uint16_t *)(map_base + pgoffset) = val; 
	close(fd);
	munmap((void *)map_base, PAGE_SIZE);
}

int Xil_In16(uint64_t phyaddr)
{
	int fd;
	uint16_t val;
	volatile uint8_t *map_base;
	uint64_t base = phyaddr & PAGE_MASK;
	uint64_t pgoffset = phyaddr & (~PAGE_MASK);
	//open /dev/mem
	if((fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1)
	{
		perror("open /dev/mem:");
	}
	//mmap
	map_base = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED,
			fd, base);
	if(map_base == MAP_FAILED)
	{
		perror("mmap:");
	}
	val = *(volatile uint16_t *)(map_base + pgoffset);
	close(fd);
	munmap((void *)map_base, PAGE_SIZE);

	return val;
}

void Xil_Out8(uint64_t phyaddr, uint16_t val)
{
	int fd;
	volatile uint8_t *map_base;
	uint64_t base = phyaddr & PAGE_MASK;
	uint64_t pgoffset = phyaddr & (~PAGE_MASK);

	if((fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1)
	{
		perror("open /dev/mem:");
	}

	map_base = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED,
			fd, base);
	if(map_base == MAP_FAILED)
	{
		perror("mmap:");
	}
	*(volatile uint8_t *)(map_base + pgoffset) = val; 
	close(fd);
	munmap((void *)map_base, PAGE_SIZE);
}

int Xil_In8(uint64_t phyaddr)
{
	int fd;
	uint8_t val;
	volatile uint8_t *map_base;
	uint64_t base = phyaddr & PAGE_MASK;
	uint64_t pgoffset = phyaddr & (~PAGE_MASK);
	//open /dev/mem
	if((fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1)
	{
		perror("open /dev/mem:");
	}
	//mmap
	map_base = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED,
			fd, base);
	if(map_base == MAP_FAILED)
	{
		perror("mmap:");
	}
	val = *(volatile uint8_t *)(map_base + pgoffset);
	close(fd);
	munmap((void *)map_base, PAGE_SIZE);

	return val;
}

 

//move.h

#ifndef SMARTCARMOVE_H
#define SMARTCARMOVE_H

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <syslog.h>
#include <memory.h>

static int fd;

#define MODE (O_WRONLY | O_TRUNC)

#define SECSIZE   65536
#define XPAR_AXI_EMC_0_S_AXI_MEM0_BASEADDR 0x80000000
#define WAIT_TICKS	0xffffffff

typedef unsigned short u16;

extern void flash_set16(unsigned int addrBase,unsigned int addrOffset,unsigned int value);
extern int flash_get16(unsigned int addrBase,unsigned int addrOffset);

extern int Erase_Flash(int sectorNum);
extern int Write_Sector(int sectorNum,int offset,u16 *src_addr,int wr_num);
extern int Read_Sector(int sectorNum,int rd_num,unsigned short *buf);


#endif

 

#include "xil_io.h"
#include "move.h"


/**
read and write phy mem
 * */
void flash_set16(unsigned int addrBase,unsigned int addrOffset,unsigned int value)
{
	Xil_Out16(addrBase+2*addrOffset, value);
	//printf("flash set reg 0x%x value is 0x%x\n",addrBase+2*addrOffset,value);
}

int flash_get16(unsigned int addrBase,unsigned int addrOffset)
{
	int ans=0;
	ans=Xil_In16(addrBase+2*addrOffset);
	return ans;
}

int Erase_Flash(int sectorNum)
{
	unsigned short flag;
	int i=0;
	int val=0;
	unsigned short *base_addr;
	base_addr = (unsigned short*)(XPAR_AXI_EMC_0_S_AXI_MEM0_BASEADDR+SECSIZE*sectorNum*2);
	printf("erase start addr 0x%x\n",base_addr);

	flash_set16(base_addr,0x555,0x00AA);
	flash_set16(base_addr,0x2AA,0x0055);
	flash_set16(base_addr,0x555,0x0080);
	flash_set16(base_addr,0x555,0x00AA);
	flash_set16(base_addr,0x2AA,0x0055);
	flash_set16(base_addr,0,0x0030);

	val=flash_get16(base_addr,0);
	printf("base addr 0x%x val is 0x%x\n",base_addr,val);

	while((val&0x80)!=0x80)
	{
		i++;
		if( i>=WAIT_TICKS)
		{
			 printf("addr %x Erase_Flash failed!\n",base_addr);
			 flash_set16(base_addr,0x555,0x0090);
			 flash_set16(base_addr,0x000,0x0000);
			 return -1;
		}
		//flag = base_addr;
		val=flash_get16(base_addr,0);
	}
	flash_set16(base_addr,0x555,0x0090);
	flash_set16(base_addr,0x000,0x0000);

	printf("erase end addr %x\n",base_addr+SECSIZE);
	return 0;  
}

int Write_Sector(int sectorNum,int offset,u16 *src_addr,int wr_num)
{	
        int i,j;
	u16 *dst_addr;
	int val=0;
	int ans=0;
	dst_addr = (u16*)(XPAR_AXI_EMC_0_S_AXI_MEM0_BASEADDR+SECSIZE*sectorNum*2+offset);

	/* Unlock Bypass program */

	flash_set16(dst_addr,0x555,0x00AA);
	flash_set16(dst_addr,0x2AA,0x0055);

	printf("dst addr is 0x%x\n",dst_addr);
	for(i=0;i<wr_num;i++)
	{
		
	        flash_set16(dst_addr,0x555,0x0020);
		flash_set16(dst_addr,0x000,0x00A0);
//	printf("i is %d\n",i);

		val=src_addr[i];
		printf("val is 0x%x\n",val);
		flash_set16(dst_addr,i,val);

		ans=flash_get16(dst_addr,i);
		//printf("ans is 0x%x\n",ans);
		j=0;
		while( ans!= val)
		{
			j++;
			if(j>=WAIT_TICKS)
			{
			        flash_set16(dst_addr,0x555,0x0090);
				flash_set16(dst_addr,0x000,0x0000);
				return -1;
			}
			ans=flash_get16(dst_addr,i);
		 }
	}
	flash_set16(dst_addr,0x555,0x0090);
	flash_set16(dst_addr,0x000,0x0000);
	return 0;  
}

int Read_Sector(int sectorNum,int rd_num,unsigned short *buf)
{
	unsigned short *sector_addr;
	int i;
	int val=0;

	if( rd_num <= 0)
	{
	   return -1;
	}
	printf("flash read start--sectorNum= %d  rd_num= %d\n",sectorNum,rd_num);
	sector_addr = (u16*)(XPAR_AXI_EMC_0_S_AXI_MEM0_BASEADDR+SECSIZE*sectorNum*2);
	
	printf("sector addr is 0x%x\n",sector_addr);
	if( rd_num <= SECSIZE )
	{
		for(i=0;i<rd_num;i++)
		{
			printf("val is 0x%x\n",flash_get16(sector_addr,0));
			val = flash_get16(sector_addr,0);
			*buf=val;		
			buf++;
			sector_addr++;
		}
	}
	else
	{
	   printf("error\n");
	}
	return 0;
}


int main(int argc, char *argv[])
{	
	
	//Erase_Flash(1);
	
	u16 dat[100];
	int i;
	Read_Sector(0,10,dat);
	for(i=0;i<10;i++)
	{
		printf("0x%x\n",dat[i]);
	}
/*
	for(i=0;i<100;i++)
	{
		dat[i]=10+i;
	}
	Erase_Flash(0);
	Write_Sector(0,0,dat,10);
*/
	return 0;
}

3、  测试

这里没有驱动的概念,一切都放在用户态执行,编译产生flash应用程序,放到板子上执行程序进行测试即可。注意,写之前要擦除。

下图为写文件的测试,这里写了10个数:


写完以后再读


可以看到,读出来的和写入的值一样,测试通过。

 

4、  补充

关于/dev/mem可以看看下面这段代码,写的很详细,用户态直接访问物理地址,对于ZYNQ中PL部分的操作确实帮助极大。

 

/*
 * devmem2.c: Simple program to read/write from/to any location in memory.
 *
 *  Copyright (C) 2000, Jan-Derk Bakker (J.D.Bakker@its.tudelft.nl)
 *
 *
 * This software has been developed for the LART computing board
 * (http://www.lart.tudelft.nl/). The development has been sponsored by
 * the Mobile MultiMedia Communications (http://www.mmc.tudelft.nl/)
 * and Ubiquitous Communications (http://www.ubicom.tudelft.nl/)
 * projects.
 *
 * The author can be reached at:
 *
 *  Jan-Derk Bakker
 *  Information and Communication Theory Group
 *  Faculty of Information Technology and Systems
 *  Delft University of Technology
 *  P.O. Box 5031
 *  2600 GA Delft
 *  The Netherlands
 *
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <fcntl.h>
#include <ctype.h>
#include <termios.h>
#include <sys/types.h>
#include <sys/mman.h>
  
#define FATAL do { fprintf(stderr, "Error at line %d, file %s (%d) [%s]\n", \
  __LINE__, __FILE__, errno, strerror(errno)); exit(1); } while(0)
 
#define MAP_SIZE 4096UL
#define MAP_MASK (MAP_SIZE - 1)

int main(int argc, char **argv) {
    int fd;
    void *map_base, *virt_addr; 
    unsigned long read_result, writeval;
    off_t target;
    int access_type = 'w';
    
    if(argc < 2) {
        fprintf(stderr, "\nUsage:\t%s { address } [ type [ data ] ]\n"
            "\taddress : memory address to act upon\n"
            "\ttype    : access operation type : [b]yte, [h]alfword, [w]ord\n"
            "\tdata    : data to be written\n\n",
            argv[0]);
        exit(1);
    }
    target = strtoul(argv[1], 0, 0);

    if(argc > 2)
        access_type = tolower(argv[2][0]);


    if((fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1) FATAL;
    printf("/dev/mem opened.\n"); 
    fflush(stdout);
    
    /* Map one page */
    map_base = mmap(0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, target & ~MAP_MASK);
    if(map_base == (void *) -1) FATAL;
    printf("Memory mapped at address %p.\n", map_base); 
    fflush(stdout);
    
    virt_addr = map_base + (target & MAP_MASK);
    switch(access_type) {
        case 'b':
            read_result = *((unsigned char *) virt_addr);
            break;
        case 'h':
            read_result = *((unsigned short *) virt_addr);
            break;
        case 'w':
            read_result = *((unsigned long *) virt_addr);
            break;
        default:
            fprintf(stderr, "Illegal data type '%c'.\n", access_type);
            exit(2);
    }
    printf("Value at address 0x%X (%p): 0x%X\n", target, virt_addr, read_result); 
    fflush(stdout);

    if(argc > 3) {
        writeval = strtoul(argv[3], 0, 0);
        switch(access_type) {
            case 'b':
                *((unsigned char *) virt_addr) = writeval;
                read_result = *((unsigned char *) virt_addr);
                break;
            case 'h':
                *((unsigned short *) virt_addr) = writeval;
                read_result = *((unsigned short *) virt_addr);
                break;
            case 'w':
                *((unsigned long *) virt_addr) = writeval;
                read_result = *((unsigned long *) virt_addr);
                break;
        }
        printf("Written 0x%X; readback 0x%X\n", writeval, read_result); 
        fflush(stdout);
    }
    
    if(munmap(map_base, MAP_SIZE) == -1) FATAL;
    close(fd);
    return 0;
}



 

运行截图如下:



GitHub 加速计划 / li / linux-dash
10.39 K
1.2 K
下载
A beautiful web dashboard for Linux
最近提交(Master分支:2 个月前 )
186a802e added ecosystem file for PM2 4 年前
5def40a3 Add host customization support for the NodeJS version 4 年前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐