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
评论
查看更多