Linux下flash操作读、写、擦除步骤

1、 背景介绍

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

Linux下flash操作读、写、擦除步骤

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

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

Linux下flash操作读、写、擦除步骤

Linux下flash操作读、写、擦除步骤

Linux下flash操作读、写、擦除步骤

Linux下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);
}

技术专区

  • Windows下linux权限管理问题解析
  • 据说嵌入式工程师年薪10万是门槛 达到后你满足了吗?
  • 嵌入式系统的主要特点是什么_嵌入式操作系统有什么优势
  • 嵌入式操作系统是什么_嵌入式操作系统有哪些
  • 睿赛德科技喜迁新址 ,RT-Thread进入新的加速发展阶段
  • Linux下flash操作读、写、擦除步骤已关闭评论
    A+
发布日期:2019年07月14日  所属分类:物联网