at45db642_WR.C
#include "main.h"
#define RESERVED_BITS 4
#define PAGE_ADDRESS_BITS 13
#define BUFF_ADDRESS_BITS 9
#define FLASH_READY BIT7+BIT5+BIT4+BIT3
#define COMP BIT6
const unsigned char CMD_flash_continousread = 0xE8;
const unsigned char CMD_flash_continousRead_a = 0x69;
const unsigned char CMD_flash_mainpagesread = 0xd2;
const unsigned char CMD_flash_mainpagesRead_a = 0x52;
const unsigned char CMD_flash_buffer1read = 0xd4;
const unsigned char CMD_flash_buffer1Read_a = 0x54;
const unsigned char CMD_flash_buffer2read = 0xd6;
const unsigned char CMD_flash_buffer2Read_a = 0x56;
const unsigned char CMD_flash_registerread = 0xd7;
const unsigned char CMD_flash_registerRead_a = 0x57;
const unsigned char CMD_flash_buffer1thmain = 0x82;
const unsigned char CMD_flash_buffer1thmain_f = 0x92;
const unsigned char CMD_flash_buffer2thmain = 0x85;
const unsigned char CMD_flash_buffer2thmain_f = 0x95;
const unsigned char CMD_flash_page_erase = 0x81;
const unsigned char CMD_flash_block_erase = 0x50;
const unsigned char CMD_flash_buffer1_write = 0x84;
const unsigned char CMD_flash_buffer2_write = 0x87;
const unsigned char CMD_flash_buffer1tomain_e = 0x83;
const unsigned char CMD_flash_buffer2tomain_e = 0x86;
const unsigned char CMD_flash_buffer1tomain = 0x88;
const unsigned char CMD_flash_buffer2tomain = 0x89;
const unsigned char CMD_flash_buffer1tomain_ef = 0x93;
const unsigned char CMD_flash_buffer2tomain_ef = 0x96;
const unsigned char CMD_flash_buffer1tomain_f = 0x98;
const unsigned char CMD_flash_buffer2tomain_f = 0x99;
const unsigned char CMD_flash_maintobuffer1 = 0x53;
const unsigned char CMD_flash_maintobuffer2 = 0x55;
const unsigned char CMD_flash_maincmpbuffer1 = 0x60;
const unsigned char CMD_flash_maincmpbuffer2 = 0x61;
const unsigned char CMD_flash_rewritethbuffer1 = 0x58;
const unsigned char CMD_flash_rewritethbuffer2 = 0x59;
#define CLOCK_DELAY() _NOP();_NOP();_NOP()
#define SET_FLASH_CS_LOW() at45db642ctr_out &= ~cs_ctr;CLOCK_DELAY()
#define SET_FLASH_CS_HIGH() at45db642ctr_out |= cs_ctr;CLOCK_DELAY()
#define SET_FLASH_PORT_OUT() at45db642io_dir = 0xff
#define SET_FLASH_PORT_INPUT() at45db642io_dir = 0x0
unsigned char check_busy(void);
void write_a_Byte(unsigned char w_type);
void write_nocareBit(unsigned short nocare_number);
void write_comand(unsigned char cmd,unsigned short adr_page,unsigned short buffer);
unsigned char Intialize_Write_at45db642memory(void)
{
unsigned short j;
if(Error_Code>RESULT_READY)return(Error_Code);
at45db642ctr_sel &= ~(parall_ctr + busy_ctr +cs_ctr);
at45db642spi_dir |= clk_ctr;
at45db642spi_out |= clk_ctr;
at45db642io_sel = 0x00;
at45db642io_dir = 0xff;
at45db642io_out = 0x00;
at45db642ctr_dir |= parall_ctr;
at45db642ctr_dir |= cs_ctr;
CLOCK_DELAY();
at45db642ctr_dir &= ~busy_ctr;
at45db642ctr_out &= ~busy_ctr;
CLOCK_DELAY();
at45db642spi_sel &= ~clk_ctr;
at45db642spi_dir |= clk_ctr;
at45db642spi_out &= ~clk_ctr;
CLOCK_DELAY();
at45db642ctr_out &= ~parall_ctr;
at45db642ctr_out |= cs_ctr;
at45db642spi_out &= ~clk_ctr;
for(j=1000;j>0;j--);
Error_Location=1;
if((Read_Status()&FLASH_READY)!=FLASH_READY)return(ERROR_FLASH_BUSY);
return(NO_ERROR);
}
unsigned char Wait_busy(void)
{
unsigned short j;
if(Error_Code>RESULT_READY)return(Error_Code);
for(j=10000;j>0;j--)if(check_busy()==0)return(NO_ERROR);
Error_Location=5;
return(ERROR_FLASH_BUSY);
}
unsigned char Read_Status(void)
{
unsigned char cmd_adr[1];
unsigned char i;
cmd_adr[0] = CMD_flash_registerread;
SET_FLASH_PORT_OUT();
SET_FLASH_CS_LOW();
write_a_Byte(cmd_adr[0]);
SET_FLASH_PORT_INPUT();
i = Read_a_Byte();
SET_FLASH_CS_HIGH();
return (i);
}
unsigned char Erease_Block(unsigned short BlockNo)
{
if(Error_Code>RESULT_READY)return(Error_Code);
write_comand(CMD_flash_block_erase,0,0);
SET_FLASH_CS_HIGH();
return(NO_ERROR);
}
unsigned char Transfer_Page_to_Buffer(unsigned char bufferNo,unsigned short adr_page)
{
unsigned char cmd;
if(Error_Code>RESULT_READY)return(Error_Code);
if(bufferNo==0)cmd = CMD_flash_maintobuffer1;
else cmd = CMD_flash_maintobuffer2;
write_comand(cmd,adr_page,0);
SET_FLASH_CS_HIGH();
return(NO_ERROR);
}
unsigned char Compare_Page_to_Buffer(unsigned char bufferNo,unsigned short adr_page)
{ unsigned char cmd;
if(Error_Code>RESULT_READY)return(Error_Code);
if(bufferNo==0)cmd = CMD_flash_maintobuffer1;
else cmd = CMD_flash_maintobuffer2;
write_comand(cmd,adr_page,0);
SET_FLASH_CS_HIGH();
return(NO_ERROR);
}
unsigned char Auto_Page_rewrite(unsigned char bufferNo,unsigned short adr_page)
{
unsigned char cmd;
if(Error_Code>RESULT_READY)return(Error_Code);
if(bufferNo==0)cmd = CMD_flash_rewritethbuffer1;
else cmd = CMD_flash_rewritethbuffer2;
write_comand(cmd,adr_page,0);
SET_FLASH_CS_HIGH();
return(NO_ERROR);
}
unsigned char Start_Continuous_Array_Read(unsigned short adr_page,unsigned short adr_buffer)
{
if(Error_Code>RESULT_READY)return(Error_Code);
write_comand(CMD_flash_continousRead_a,adr_page,adr_buffer);
write_nocareBit(60);
SET_FLASH_PORT_INPUT();
Read_a_Byte();
return(NO_ERROR);
}
void End_Continuous_Array_Read(void)
{
SET_FLASH_CS_HIGH();
}
unsigned char Read_nByte_buffer(unsigned char bufferNo,type_datas_toFlash *rdata,unsigned short adr_buffer)
{
unsigned char cmd;
unsigned short i;
unsigned char * data = rdata->data;
unsigned short data_len = rdata->data_len;
if(Error_Code>RESULT_READY)return(Error_Code);
if(bufferNo==0)cmd = CMD_flash_buffer1read;
else cmd = CMD_flash_buffer2read;
write_comand(cmd,0,adr_buffer);
write_nocareBit(0x01);
SET_FLASH_PORT_INPUT();
for (i = 0; i <DATA_LEN; data="rdata-" * char unsigned i; short { *flash_address) *rdata,type_flash_address Read_nByte_main(type_datas_toFlash } (NO_ERROR); return SET_FLASH_CS_HIGH(); ++data; *data="Read_a_Byte();" i++)>data;
unsigned short data_len = rdata->data_len;
if(Error_Code>RESULT_READY)return(Error_Code);
write_comand(CMD_flash_mainpagesread,flash_address->adr_page,flash_address->adr_buffer);
write_nocareBit(60);
SET_FLASH_PORT_INPUT();
for (i = 0; i <DATA_LEN; data="rdata-" * char unsigned short { *flash_address) *rdata,type_flash_address } (NO_ERROR); return SET_FLASH_CS_HIGH(); ++data; *data="Read_a_Byte();" i++) i,adr_buff; cmd; bufferNo,type_datas_toFlash Write_mainpage_thr_buffer(unsigned buffer1 throuth pages main to types 1056 write>data;
unsigned short data_len = rdata->data_len;
if(Error_Code>RESULT_READY)return(Error_Code);
if(Wait_busy()>RESULT_READY)return(ERROR_FLASH_BUSY);
Error_Location=4;
adr_buff=flash_address->adr_buffer;
if(adr_buff>=AT45DB642_BUFFER_LENGTH)return(ERROR_FLASH_BUFFER_ADR);
if(bufferNo==0)cmd = CMD_flash_buffer1thmain;
else cmd = CMD_flash_buffer2thmain;
write_comand(cmd,flash_address->adr_page,flash_address->adr_buffer);
for (i = 0 ; i < data_len; i++)
{
write_a_Byte(*data);
++data;
if(++adr_buff>AT45DB642_BUFFER_LENGTH)
{
SET_FLASH_CS_HIGH();
return(ERROR_FLASH_BUFFER_ADR);
}
}
SET_FLASH_CS_HIGH();
return(NO_ERROR);
}
unsigned char Write_1byte_to_buffer(unsigned char bufferNo,unsigned char data,unsigned short adr_buffer)
{
unsigned char cmd;
if(Error_Code>RESULT_READY)return(Error_Code);
if(adr_buffer>=AT45DB642_BUFFER_LENGTH)return(ERROR_FLASH_BUFFER_ADR);
if(bufferNo==0)cmd = CMD_flash_buffer1_write;
else cmd = CMD_flash_buffer2_write;
write_comand(cmd,0,adr_buffer);
write_a_Byte(data);
SET_FLASH_CS_HIGH();
return(NO_ERROR);
}
unsigned char Write_nbyte_to_buffer(unsigned char bufferNo,type_datas_toFlash *rdata,unsigned short adr_buffer)
{
unsigned char cmd;
unsigned short i;
unsigned char * data = rdata->data;
unsigned short data_len = rdata->data_len;
if(Error_Code>RESULT_READY)return(Error_Code);
if(adr_buffer>=AT45DB642_BUFFER_LENGTH)return(ERROR_FLASH_BUFFER_ADR);
if(bufferNo==0)cmd = CMD_flash_buffer1_write;
else cmd = CMD_flash_buffer2_write;
write_comand(cmd,0,adr_buffer);
for (i = 0 ; i < data_len; i++)
{
write_a_Byte(*data);
data++;
if(++adr_buffer>AT45DB642_BUFFER_LENGTH)
{
SET_FLASH_CS_HIGH();
return(ERROR_FLASH_BUFFER_ADR);
}
}
SET_FLASH_CS_HIGH();
return(NO_ERROR);
}
unsigned char Write_buffer_to_main_Erase_Fast(unsigned char bufferNo,unsigned short adr_page)
{
unsigned char cmd;
if(Error_Code>RESULT_READY)return(Error_Code);
if(bufferNo==0)cmd = CMD_flash_buffer1tomain_ef;
else cmd = CMD_flash_buffer1tomain_ef;
write_comand(cmd,adr_page,0);
SET_FLASH_CS_HIGH();
return(NO_ERROR);
}
unsigned char Write_buffer_to_main_Erase(unsigned char bufferNo, unsigned short adr_page)
{
unsigned char cmd;
if(Error_Code>RESULT_READY)return(Error_Code);
if(bufferNo==0)cmd = CMD_flash_buffer1tomain_e;
else cmd = CMD_flash_buffer2tomain_e;
write_comand(cmd,adr_page,0);
SET_FLASH_CS_HIGH();
return(NO_ERROR);
}
void write_comand(unsigned char cmd,unsigned short adr_page,unsigned short adr_buff)
{
unsigned char cmd_adr[4];
unsigned short i,j;
cmd_adr[0] = cmd;
i = adr_page;
i >>= 5;
cmd_adr[1] = i;
i = adr_page;
i <<= 3;
cmd_adr[2] = i;
i &= 0xf8;
j = adr_buff;
j >>= 8;
j &= 0x07;
i += j;
cmd_adr[2] = i;
cmd_adr[3] = adr_buff;
SET_FLASH_PORT_OUT();
SET_FLASH_CS_LOW();
for (i = 0 ; i < 4; i++)write_a_Byte(cmd_adr[i]);
}
unsigned char check_busy(void)
{
if((at45db642ctr_in & busy_ctr)==0)return(1);
return (0);
}
void write_a_Byte(unsigned char w_type)
{
at45db642io_out = w_type;
at45db642spi_out |= clk_ctr;
CLOCK_DELAY();
at45db642spi_out &= ~clk_ctr;
}
unsigned char Read_a_Byte(void)
{
unsigned char r_type;
at45db642spi_out |= clk_ctr;
CLOCK_DELAY();
r_type = at45db642io_in;
at45db642spi_out &= ~clk_ctr;
return r_type;
}
void write_nocareBit(unsigned short nocare_number)
{
unsigned short i;
for (i = 0; i < nocare_number; i++)
{
at45db642io_out = i;
at45db642spi_out |= clk_ctr;
CLOCK_DELAY();
at45db642spi_out &= ~clk_ctr;
}
}
at45db642_WR.h
#include "Mem_Global.h"
#ifdefEXTERN
#undefEXTERN
#endif
#ifdefMAIN_INCLUDE
#defineEXTERN
#else
#defineEXTERN extern
#endif
#ifdef DEFINE_AT45DB642
#else
#define DEFINE_AT45DB642
#define AT45DB642_BUFFER_LENGTH 1024
#define AT45DB642_TOTAL_BLOCK 1024
#define AT45DB642_TOTAL_PAGE 8192
#define AT45DB642_TOTAL_SECTOR 33
#endif
#define at45db642io_sel P4SEL
#define at45db642io_dir P4DIR
#define at45db642io_out P4OUT
#define at45db642io_in P4IN
#define clk_ctr BIT3
#define simo_ctr BIT1
#define somi_ctr BIT2
#define at45db642spi_sel P3SEL
#define at45db642spi_in P3IN
#define at45db642spi_out P3OUT
#define at45db642spi_dir P3DIR
#define at45db642ctr_sel P2SEL
#define at45db642ctr_dir P2DIR
#define at45db642ctr_out P2OUT
#define at45db642ctr_in P2IN
#define parall_ctr BIT0
#define cs_ctr BIT1
#define gnd_ctr BIT2
#define busy_ctr BIT3
EXTERN unsigned char Intialize_Write_at45db642memory(void);
EXTERN unsigned char Read_Status(void);
EXTERN unsigned char Wait_busy(void);
EXTERN unsigned char Read_a_Byte(void);
EXTERN unsigned char Erease_Block(unsigned short BlockNo);
EXTERN unsigned char Transfer_Page_to_Buffer(unsigned char bufferNo,unsigned short adr_page);
EXTERN unsigned char Compare_Page_to_Buffer(unsigned char bufferNo,unsigned short adr_page);
EXTERN unsigned char Auto_Page_rewrite(unsigned char bufferNo,unsigned short adr_page);
EXTERN unsigned char Start_Continuous_Array_Read(unsigned short adr_page,unsigned short adr_buffer);
EXTERN void End_Continuous_Array_Read(void);
EXTERN unsigned char Read_nByte_buffer(unsigned char bufferNo,type_datas_toFlash *rdata,unsigned short adr_buffer);
EXTERN unsigned char Read_nByte_main(type_datas_toFlash *rdata,type_flash_address *flash_address);
EXTERN unsigned char Write_1byte_to_buffer(unsigned char buffer,unsigned char data,unsigned short adr_buffer);
EXTERN unsigned char Write_nbyte_to_buffer(unsigned char bufferNo,type_datas_toFlash *rdata,unsigned short adr_buffer);
EXTERN unsigned char Write_buffer_to_main_Erase(unsigned char bufferNo,unsigned short adr_page);
EXTERN unsigned char Write_buffer_to_main_Erase_Fast(unsigned char bufferNo,unsigned short adr_page);
EXTERN unsigned char Write_mainpage_thr_buffer(unsigned char bufferNo,type_datas_toFlash *rdata,type_flash_address *flash_address);
#undefEXTERN
//---------------------------------------------------------------------------------