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
#include
#include
#include
#include
#include
#include
#include
#include
#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
#include
#include
#include
#include
#include
#include
#include
#include
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
[cpp] view plain copy print?
#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
评论
查看更多