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

📄 image.cpp

📁 本文件为atmel公司的at45db芯片的驱动程序实例,对于串行flash编程有帮助
💻 CPP
字号:
#include "at45.h"
#include "stdio.h"
#include<windows.h>

struct phead{
  unsigned char atmel[10];
  short Version;
  unsigned char cardID[8];
  unsigned char pname[20];
  unsigned char birth[8];
  char sex;
  unsigned char pid[15];
  unsigned char imgCount;
  unsigned long imageSize[9];
  unsigned short imageOffset[9];
}xHead;

typedef struct anno{
	short Cat;
    short LocX;
    short LocY;
    unsigned char Annotation[36];
    unsigned char Para[16];
}xAnno;

struct imgAnno{
	unsigned char title[30];
	short AnnoCount;
	xAnno xAnnoInfo[4];
}imgAnnotation[9];

#define AT45_Page_Size 264
#define AT45_Page 2048

#define AT_SUCC       0
#define AT_ERR_FILE   1
#define AT_ERR_LENGTH 2
#define AT_ERR_MEMORY 3
#define AT_ERR_CARD   4

size_t  __cdecl strlen(const unsigned char *pt)
{
	return strlen( (char *)pt);
};

long GetImageOff(short id){
	return xHead.imageOffset[id];
}

NOMANGLE void CCONV Getversion(short* Ver){
  *Ver=xHead.Version;
}

NOMANGLE short CCONV GetPersonInfo(unsigned char **cardID,unsigned char ** pName,unsigned char **birth,short * sex,unsigned char ** PID)
{
   if(memcmp(xHead.atmel,"ATMEL XRAY",10)!=0)
   return 1;	
   memcpy(*cardID,xHead.cardID,8);  
   memcpy(*pName,xHead.pname,20);
   memcpy(*birth,xHead.birth,8);
   *sex=(int)xHead.sex;
   memcpy(*PID,xHead.pid,16);
   return 0;	
}

NOMANGLE short CCONV GetMaxImageID(void){
	return xHead.imgCount;
}

NOMANGLE short CCONV CardIssue(short Version, unsigned char *cardID,unsigned char * pName,unsigned char *birth,short sex,unsigned char * PID){
   int i;
   unsigned char *p=(unsigned char *)&xHead;
   memset(p,0,sizeof(xHead)) ;
   memcpy(xHead.atmel,(unsigned char *)"ATMEL XRAY",10);
   xHead.Version=Version;
   i = strlen(cardID)>8?8:strlen(cardID);
   memcpy(xHead.cardID,cardID, i);
   if(i<8) memset(xHead.cardID+i, 0, 8-i);
   i = strlen((char*)pName)>20?20:strlen((char*)pName);
   memcpy(xHead.pname,pName, i);
   if(i<20) memset(xHead.pname+i, 0, 20-i);
   i = strlen(birth)>8?8:strlen((char*)birth);
   memcpy(xHead.birth, birth, i);
   if(i<8) memset(xHead.birth+i, 0, 8-i);
   xHead.sex=(char)sex;
   i = strlen((char*)PID)>15?15:strlen((char*)PID);
   memcpy(xHead.pid,PID,16);
   if(i<16) memset(xHead.pid+i, 0, 16-i);
   xHead.imgCount =0;
   xHead.imageOffset[0] = 1;
   return 0;
}

void ClearAnno(void)
{
	short i,j;
	for(j=0;j<9;j++){
	  imgAnnotation[j].AnnoCount=0;
	  for(i=0;i<4;i++){
        imgAnnotation[j].xAnnoInfo[i].Cat=0;
	    imgAnnotation[j].xAnnoInfo[i].LocX=0;
	    imgAnnotation[j].xAnnoInfo[i].LocY=0;
	    memset(imgAnnotation[j].xAnnoInfo[i].Annotation,0,36);
	    memset(imgAnnotation[j].xAnnoInfo[i].Para,0,16);
	  }
	}
}

NOMANGLE short CCONV WriteHead(void){
	short r;
	AT45_PowerOn();
	r= WriteData(0,(unsigned char*)&xHead,sizeof(xHead));
	AT45_PowerOff();
    return r;
}

NOMANGLE short CCONV ReadHead(void)
{
	short r;
	ClearAnno();
	AT45_PowerOn();
	r= ReadData(0,(unsigned char*)&xHead,sizeof(xHead));
	AT45_PowerOff();
    return r;
}


NOMANGLE short CCONV AT45_Card2Img(char *outfile,unsigned char imgID)
{
  short r;
  FILE *fp;
  if (imgID>GetMaxImageID())   return 1; 
  fp=fopen(outfile,"wb");
  if (fp==NULL) return 1;
  AT45_PowerOn();
  fseek(fp,0,SEEK_SET);
  unsigned char *s=(unsigned char *)malloc(xHead.imageSize[imgID]);
  r=ReadData((xHead.imageOffset[imgID]+1)*264l,s,xHead.imageSize[imgID]);
  fwrite(s,xHead.imageSize[imgID],1,fp);
  fclose(fp); 
  free(s);
  r=ReadData(xHead.imageOffset[imgID]*264l,(unsigned char*)&imgAnnotation[imgID],264);
  AT45_PowerOff();
  return r;
}

NOMANGLE short CCONV AT45_Img2Card(char *infile,unsigned char *title)
{
  unsigned char b[264];  
  long FileSize,FilePage;
  short r,StartPage;
  FILE *fp;
  r=GetMaxImageID();
  if(r==5) r=AT_ERR_LENGTH;
  StartPage=xHead.imageOffset[r];

  fp=fopen(infile,"rb");
  if (fp==NULL) return AT_ERR_FILE;
  fseek(fp,0,SEEK_END);
  FileSize=ftell(fp);
  FilePage=FileSize/264+1; 
  if(FilePage > AT45_Page) return AT_ERR_LENGTH;
  unsigned char *s=(unsigned char *)malloc(FileSize);
  fseek(fp,0,SEEK_SET);
  fread(s,1,FileSize,fp);
  fclose(fp);
  if(StartPage+FilePage>AT45_Page) r=AT_ERR_LENGTH;
  
  if(s==NULL) return AT_ERR_MEMORY;
  AT45_PowerOn();
  r=AT_SUCC;
  memset(b,0,264);
  if(WriteData((StartPage)*264l,b,264)!=0)
  {
    AT45_PowerOff();	   
	free(s);
	return AT_ERR_CARD;
  }
  
  if(WriteData((StartPage+1)*264l,s,FileSize)!=0)
  {
    AT45_PowerOff();	   
	free(s);
	return AT_ERR_CARD;
  }
  xHead.imgCount++;
  xHead.imageSize[xHead.imgCount-1]= FileSize;
  xHead.imageOffset[xHead.imgCount]= StartPage+FilePage+1;
  r=WriteHead();

  /*i = strlen(title)>35?35:strlen(title);
  memcpy(imgAnnotation.title,title, i);
  if(i<8) memset(imgAnnotation.title+i, 0, 35-i);*/
  free(s);
  AT45_PowerOn();
  r=WriteData((StartPage)*264l,title,30);
  AT45_PowerOff();
  return r;
}

NOMANGLE short CCONV SetAnnoInfo(short imgID, short AnnoID, short Cat,short LocX, short LocY,
								 unsigned char *Annotation, unsigned char *Para,short Update)
{
	short StartPage;
	short r,i;
	if (imgID>GetMaxImageID())   return 1; 
	StartPage=xHead.imageOffset[imgID];
    AT45_PowerOn();
	r=ReadData(StartPage*264l,(unsigned char*)&imgAnnotation,264);
	if(!Update) imgAnnotation[imgID].AnnoCount++;
	imgAnnotation[imgID].xAnnoInfo[AnnoID].Cat =Cat;
    imgAnnotation[imgID].xAnnoInfo[AnnoID].LocX=LocX;
    imgAnnotation[imgID].xAnnoInfo[AnnoID].LocY=LocY;
	if(i<36) memset(imgAnnotation[imgID].xAnnoInfo[AnnoID].Annotation+i,0x20,36-i);
    i = strlen((char*)Annotation)>36?36:strlen((char*)Annotation);
    memcpy(imgAnnotation[imgID].xAnnoInfo[AnnoID].Annotation,Annotation, i);
	memset(imgAnnotation[imgID].xAnnoInfo[AnnoID].Para,0,16);
	memcpy(imgAnnotation[imgID].xAnnoInfo[AnnoID].Para,Para,16);
    r=WriteData(StartPage*264l,(unsigned char*)&imgAnnotation[imgID],264);
    AT45_PowerOff();
  	return r;
}


NOMANGLE short CCONV GetAnnoInfo(short imgID, short AnnoID,
								 short* Cat, short* LocX, short* LocY,
								 unsigned char **Annotation, unsigned char **Para)
{
	if (imgID>GetMaxImageID()) return 1;
	*Cat = imgAnnotation[imgID].xAnnoInfo[AnnoID].Cat;
    *LocX= imgAnnotation[imgID].xAnnoInfo[AnnoID].LocX;
    *LocY =imgAnnotation[imgID].xAnnoInfo[AnnoID].LocY;
    memcpy(*Annotation,imgAnnotation[imgID].xAnnoInfo[AnnoID].Annotation,36);
	memcpy(*Para,imgAnnotation[imgID].xAnnoInfo[AnnoID].Para,16);
  	return 0;
}

NOMANGLE short CCONV GetAnnoCount(short imgID)
{
	return imgAnnotation[imgID].AnnoCount;
}

⌨️ 快捷键说明

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