⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 storage.c

📁 mm36sb020存储器读写
💻 C
字号:
#include "storage.h"
#include "utility.h"

static BOOL bSPI;

void out(BYTE a)
{
	SPDAT=a;
	for(bSPI=0;bSPI==0;);
}
//LOW LEVEL //essential read & write operations//////////////////////////////////////
void reset_storage(void)
{
	out(0xfe);
	out(0xff);
	out(0xff);
	out(0xff);
  	while(EE_BUSY);
}

/*void clr_storage(void)
{
	out(0xf6);
	out(0x90);
 	while(EE_BUSY);
}*/

//read a byte by page & offset
BYTE read_byte(UINT page,BYTE offset)
{
	out(0x9c);
	out(offset);
	out(page);
    out(page>>8);  
  	out(DUMMY);  
  	return SPDAT;
}
//continue to read a byte
BYTE read_byte1()
{
	out(0xa0);
	out(DUMMY);
	return SPDAT;   
}
//read a byte by address
BYTE read_byte2(ULONG addr)
{
	out(0x9c);
	out(addr & 0x7f);
  	addr <<= 1;
  	out(addr >> 8);
  	out(addr >> 16);
	out(DUMMY);
  	return SPDAT;
}

//read total page
/*void read_page(UINT page,BYTE *pb)
{
	BYTE i;
	pb[0]=read_byte(page,0);
	for(i=1;i<128;i++)pb[i]=read_byte1();
}*/

//read "len" bytes sequentialy from "addr" to "pb" buffer
void read_seql1(ULONG addr,BYTE *pb,BYTE len)
{
	BYTE i;
	pb[0]=read_byte2(addr);
	for(i=1;i<len;i++)pb[i] = read_byte1();
}
//another version
void read_seql2(UINT page,BYTE offset,BYTE *pb,BYTE len)
{
	BYTE i;
	pb[0]=read_byte(page,offset);
	for(i=1;i<len;i++)pb[i]=read_byte1();
}

void auto_prog(UINT page)
{
	out(0xb0);
	out(page);
	out(page>>8);
	delay();
  	while(EE_BUSY);
}

void write_cache(BYTE offset,BYTE *buf,BYTE len)
{
	BYTE i;
	if(len)
	{
  		out(0xa8);
		out(offset);
		out(buf[0]);
  		for (i = 1;i < len;i++)
    	{
    		out(0xac);
			out(buf[i]);
    	}
	}
}
 
void write_page(UINT page,BYTE *buf)
{
	write_cache(0,buf,128);
	auto_prog(page);
}

void write_seql(ULONG addr,BYTE *pb,BYTE len)
{
	BYTE i,offset;
	UINT page;
	BYTE xdata buf[256];
	
	offset=addr&0x7f;
	page  =addr>>7;

	read_seql2(page,0,buf,offset);//read ahead bytes 
	for(i=offset;i<offset+len;i++)buf[i]=*pb++;//move to buffer

	if(i<128)read_seql2(page,i,&buf[i],128-i);//remainder
    write_page(page,buf);
	delay();
  	while(EE_BUSY);
	
	if(i>128)
	{	
		i%= 128;
		page++;
		read_seql2(page,i,&buf[128+i],128-i);
		write_page(page,&buf[128]);
		delay();
  		while(EE_BUSY);
	}
}


//HIGH LEVEL //operation on the information of holidays & weeks//

//get the holiday group mask:x x x x x hg2 hg1 hg0
//use bcd2hex

BYTE get_hg(BCD mon,BCD dat)
{
	BYTE m,d;
	ULONG addr;

	m = bcd2hex(mon)-1;	d = bcd2hex(dat)-1;
	addr = HOLIDAY_ADDR_START + m*31 + d;

	return read_byte2(addr);
}

//get the period from the storage by user group & the index of the PERIOD array

void get_period(BYTE group,BYTE prd_index,PERIOD *prd)
{
	ULONG addr;
	BYTE *ptr;

	addr = PERIOD_ADDR_START + group * 30 + prd_index*5;
	
	ptr=(BYTE *)&prd->t1;
	*ptr++=read_byte2(addr);
	*ptr=read_byte1();

	ptr=(BYTE *)&prd->t2;
	*ptr++=read_byte1();
	*ptr= read_byte1();
	prd->mask = read_byte1();
}

//user information operation

void read_user(BYTE user_page,BYTE index,USER *usr)
{
	BYTE i;
	BYTE *ptr;
	UINT page;
	page = USER_PAGE_START + user_page;
	usr->group = read_byte(page,index*5);
	ptr =(BYTE *) &usr->tag;

	for(i=0;i<4;i++)*ptr++ = read_byte1();	
}

void read_user_page(BYTE user_page,USER *users,BYTE cnt)
{
	BYTE i,j;
	BYTE *ptr;

	read_user(user_page,0,users);//read the first user

	for(i=1;i<cnt;i++)
	{
		users[i].group = read_byte1();
		
		ptr = (BYTE *) &users[i].tag;	

		for(j=0;j<4;j++)*ptr++ = read_byte1();			
	}	
}

void spi_int(void) interrupt 9
{
	SPSTAT =0x80;
	bSPI = TRUE;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -