ARM嵌入式应用系统开发典型实例.rar
源代码在线查看: flash.c
/**************************************************************************************************
* FILENAME
* flash.c
*
* VERSION
* 1.0
*
* DESCRIPTION
* This file contains the timer initial routine and timer ISR.
*
* DATA STRUCTURES
* None
*
* FUNCTIONS
* 1. Fun_flash_writepage()
* 2. Fun_flash_readpage()
* 3. Fun_wr_template()
* 4. Fun_rd_template()
* 5. Fun_del_template()
*
* HISTORY
* 10/28/2003 Ver 1.0 Created by Hank Ji
*
* REMARK
* None
*
*************************************************************************************************/
#include
#include "740defs.h"
#define C_Flash
#include "ist.h"
/************************************************/
/* Name: Fun_flash_writepage */
/* In: page--要写的页数 */
/* Out: None */
/* Description: */
/* 写FLASH中的某页 */
/************************************************/
void Fun_flash_writepage(uint16 page)
{
uint32 i;
/* write register */
Flash_Ctlreg1 = 0xaa;
Flash_Ctlreg2 = 0x55;
Flash_Ctlreg1 = 0xa0;
for(i=0;i {
VPchar(Flash_baseaddr+page*128+i) = flash_buffer[i];
}
Fun_delay_1ms(15);
}
/************************************************/
/* Name: Fun_flash_readpage */
/* In: page--要读的页数 */
/* Out: None */
/* Description: */
/* 读碏LASH中的某页 */
/************************************************/
void Fun_flash_readpage(uint16 page)
{
uint32 i;
for(i=0;i {
flash_buffer[i] = VPchar(Flash_baseaddr+page*128+i);
}
}
/************************************************/
/* Name: Fun_wr_template */
/* In: UserNum: 要写的用户(1,2 or 3) */
/* pImageTemplate: 特征值存储缓冲指针 */
/* Out: NONE */
/* Description: */
/* 写某个用户的指纹特征值 */
/************************************************/
void Fun_wr_template(uint8 UserNumber,uint8 *pImageTemplate)
{
uint16 num,page,i;
uint8 j;
uint32 y;
// uint8 temp_buffer[128];
Fun_rd_parameter();
start_wr:
for(i=0;i {
if((flash_buffer[i]==0xff)||(flash_buffer[i]==UserNumber))
{
page = i;
flash_buffer[i] = UserNumber;
break;
}
}
num = MIN_template_size;
//write template to flash
for(i=0;i {
if(num > 128)
{
for(j=0;j {
flash_buffer[j] = pImageTemplate[i*128+j];
}
Fun_flash_writepage(MA_USER1_PAGE + page*32 + i);
}
else
{
for(j=0;j {
flash_buffer[j] = pImageTemplate[i*128+j];
}
for(j=num;j {
flash_buffer[j] = 0xff;
}
Fun_flash_writepage(MA_USER1_PAGE + page*32 + i);
break;
}
num = num - 128;
}
for(i=0;i {
y = Flash_baseaddr + (MA_USER1_PAGE+page*32)*128;
if(pImageTemplate[i] != VPchar(y+i))
{
Fun_rd_parameter();
flash_buffer[page] = 0xaa;
Fun_wr_parameter();
goto start_wr;
}
}
//then write parameter to flash page 0
//end_wr:
Fun_rd_parameter();
flash_buffer[page] = UserNumber;
Fun_wr_parameter();
}
/************************************************/
/* Name: Fun_rd_template */
/* In: UserNum: 要读的用户(1,2 or 3) */
/* Out: pImageTemplate: 特征值存储缓冲指针 */
/* Description: */
/* 读某个用户的指纹特征值 */
/************************************************/
void Fun_rd_template(uint8 UserNumber,uint8 *pImageTemplate)
{
uint16 page,i;
Fun_rd_parameter();
for(i=0;i {
if(flash_buffer[i]==UserNumber)
{
page = i;
break;
}
}
for(i=0;i {
pImageTemplate[i] = VPchar(Flash_baseaddr+(page*32+MA_USER1_PAGE)*128+i);
}
}
/************************************************/
/* Name: Fun_del_template */
/* In: UserNum -- 要删除的用户(1,2 or 3) */
/* Out: NONE */
/* Description: */
/* 删除某个用户 */
/************************************************/
void Fun_del_template(uint8 UserNumber)
{
uint8 i;
Fun_rd_parameter();
for(i=0;i {
if(flash_buffer[i]==UserNumber)
{
flash_buffer[i] = 0xff;
break;
}
}
// flash_buffer[User1_inf_offset + UserNumber-1] = 0xff;
Fun_wr_parameter();
}
uint8 Fun_rd_parameter(void)
{
uint16 i;
for(i=0;i {
flash_buffer[i] = VPchar(Flash_baseaddr+MA_PARAM_PAGE1*128+i);
}
if(flash_buffer[Valid_inf_offset] == Valid_inf)
{
if(flash_buffer[Valid_inf_offset+128] != Valid_inf)
{
Fun_flash_writepage(MA_PARAM_PAGE2);
}
return 1;
}
else
{
if(flash_buffer[Valid_inf_offset+128] != Valid_inf)return 0;
for(i=0;i {
flash_buffer[i] = flash_buffer[i+128];
}
Fun_flash_writepage(MA_PARAM_PAGE1);
return 2;
}
}
void Fun_wr_parameter(void)
{
uint8 ch=0;
uint8 i;
for(i=0;i {
ch = ch+ flash_buffer[i];
}
flash_buffer[127] = ch;
flash_buffer[Valid_inf_offset] = Valid_inf;
Fun_flash_writepage(MA_PARAM_PAGE1);
Fun_flash_writepage(MA_PARAM_PAGE2);
}