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

📄 fapi.c

📁 瑞萨单片机开发软代码.非常实用小巧的平台,很多应用都可以在上面实现.
💻 C
📖 第 1 页 / 共 3 页
字号:

/*******************************************************************************
;* Copyright (C) 2002, HITACHI LTD.   All Rights Reserved
;*
;*	FILE NAME     : fapi.c
;*	DATE CREATED  : Mar 2002
;*
;*	DESCRIPTION   : API interface to USB driver software
;*
;*  Remark        : 
;*
;********************************************************************************/
               
#include <machine.h> 
#include <string.h> 
#include "flashdisk.h"
#include "genEcc.h"
#include "h8s2215.h" 
#include "sci.h"
    
// local function
unsigned char find_backup(unsigned long Addr);
unsigned long find_spare(unsigned long Addr, unsigned char *buf);   

// variables
extern FLASH_RW flash_rw;
extern DATA_BUFF Cache[];
extern volatile unsigned char errorcode;         // error code
extern volatile unsigned long backup_odd;        // backup block for odd blk address  
extern volatile unsigned long backup_even;       // backup block for even blk address 
extern volatile unsigned char write_protect;

extern unsigned int test_backup;
extern unsigned int test_spare;                                                                                      

//***********************************************************************
//**  API erase sector
//**  Notice that this erase sector will erase entire block to "FF"
//***********************************************************************
unsigned char EraseFlash (unsigned long Addr, unsigned char wait)
{
   unsigned char buff[2];
   unsigned long x;
   
   // locate which block
   Addr = Addr/512;                           // sector no.
   x = Addr/32;                               // block no.
   
   ChipSelect_F1 
   TurnOnLed();                               // LED indication

   // get physical block no from sector table
   flash_rw.addr = (x/256)+BLK_TBL_OFFSET;    // check for block address
   flash_rw.length = 2;
   flash_rw.buff_ptr = &buff[0];
   fread_data((unsigned int)((x%256)*2));     // physical block no. in location

   x = (((unsigned long)buff[1])<<8 | (unsigned long)buff[0])<<5;  // physical block location
   // erase blk
   
   if (wait == 1)
    ferase_blk((unsigned int)x);                        // erase sector wait until busy high
   else
    ferase_blk_nowait((unsigned int)x);                 // erase backup no wait
   
   De_ChipSelect_F1

   TurnOffLed();                               // LED indication
   return(0);
}

//***********************************************************************
//**  API read sector no ecc code
//***********************************************************************
unsigned char ReadFlashNoEcc (unsigned long AddrSec,unsigned short len, void *buf)
{
   ChipSelect_F1        
   TurnOnLed();                               // LED indication

   // get data
   flash_rw.addr = AddrSec; 
   flash_rw.length = len;
   flash_rw.buff_ptr = buf;
   fread_sector_no_ecc();
   De_ChipSelect_F1

   TurnOffLed();                               // LED indication
   return(0);
}

//***********************************************************************
//**  API read sector
//***********************************************************************
unsigned char ReadFlash (unsigned long Addr, void *buf)
{
   unsigned long x;
   unsigned char y;
   unsigned char buff[2];
   unsigned char ecc[16];
   
   // locate which block
   Addr = Addr/512;                           // sector no.
   x = Addr/32;                               // block no.
   y = (unsigned char)(Addr%32);              // page no. in block
   
   ChipSelect_F1        
   TurnOnLed();                               // LED indication

   // get physical block no from sector table
   flash_rw.addr = (x/256)+BLK_TBL_OFFSET;    // check for block address
   flash_rw.length = 2;
   flash_rw.buff_ptr = &buff[0];
   fread_data((unsigned int)((x%256)*2));     // physical block no. in location

   // get data
   flash_rw.addr = ((((unsigned long)buff[1])<<8 | (unsigned long)buff[0])<<5)+ y; 
   flash_rw.length = 512;
   flash_rw.buff_ptr = buf;
   flash_rw.ecc_ptr = &ecc[0]; 
   fread_sector();

   De_ChipSelect_F1

   TurnOffLed();                               // LED indication
   return(0);
}

//*** Read 4 sector, 2K data
unsigned char ReadFlash4s (unsigned long Addr, void *buf)
{
   unsigned long x;
   unsigned char y;
   unsigned char buff[2];
   unsigned char ecc[64];
   
   // locate which block
   Addr = Addr/512;                           // sector no.
   x = Addr/32;                               // block no.
   y = (unsigned char)(Addr%32);              // page no. in block
   
   ChipSelect_F1
   TurnOnLed();                               // LED indication

   // get physical block no from sector table
   flash_rw.addr = (x/256)+BLK_TBL_OFFSET;    // check for block address
   flash_rw.length = 2;
   flash_rw.buff_ptr = &buff[0];
   fread_data((unsigned int)((x%256)*2));     // physical block no. in location

   // get data
   flash_rw.addr = ((((unsigned long)buff[1])<<8 | (unsigned long)buff[0])<<5)+ y; 
   flash_rw.length = 2048;
   flash_rw.buff_ptr = buf;
   flash_rw.ecc_ptr = &ecc[0]; 
   fread_sector();

   De_ChipSelect_F1

   TurnOffLed();                               // LED indication
   return(0);
}


//***********************************************************************
//**  API write sector
//***********************************************************************
//**  write mutliple sectors according to WriteCache.Modified, no backup
//***********************************************************************
unsigned char WriteFlash (unsigned long Addr, void *buf)
{
   unsigned long x, z;
   unsigned char y, i;
   unsigned char buff[2];
   unsigned char aECC[16];
   
   test_spare++;
   
   // locate which block
   Addr = Addr/512;                   // sector no. (page no.)
   x = Addr/32;                       // block no.
   y = (unsigned char)(Addr%32);      // page no. in block

   ChipSelect_F1 
   TurnOnLed();                               // LED indication
   
   // get physical block no from sector table
   flash_rw.addr = (x/256)+BLK_TBL_OFFSET;    // check for block address
   flash_rw.length = 2;
   flash_rw.buff_ptr = &buff[0];
   fread_data((unsigned int)((x%256)*2));     // physical block no. in location
   
   x = (((unsigned long)buff[1])<<8 | (unsigned long)buff[0])<<5;  // physical block location

   // write data
   flash_rw.buff_ptr = buf;
   flash_rw.length = 512;
   x = x + (unsigned long)y;                  // start of actual address
   for (i=0;i<Cache->WriteCache.Modified;i++)
   {
     flash_rw.addr = x+(unsigned long)i;      // write data to actual address
     
//		SCI_Str("faddr:");
//		SCI_Hex(flash_rw.addr);

     //if (fwrite_sector()!=0 || test_spare==576)
     if (fwrite_sector()!=0)
     {
       SCI_Str("\r\ntest_spare: ");
       SCI_HexS((unsigned short)test_spare); 
       test_spare++; 

       SCI_Str("\r\nbad x:");
       SCI_Hex(x);  
       SCI_Str(" buf:");
       SCI_Hex((unsigned long)flash_rw.buff_ptr);

       x = find_spare(flash_rw.addr, buf);
/*
       if(x==0)       // indicate current address write fail
       {
           SCI_Str("\r\nInternal Error, No Spare Block!!");
           De_ChipSelect_F1
           return(NO_SPARE_BLK);
       }                            
*/       
       x = x + (unsigned long)y; 
       SCI_Str("\r\nnew x:");
       SCI_Hex(x);  
       SCI_Str(" buf:");
       SCI_Hex((unsigned long)flash_rw.buff_ptr);
     }    
     flash_rw.buff_ptr += 512;                // next sector of data 
   }
   
   De_ChipSelect_F1
   TurnOffLed();                               // LED indication
   return(0);
}                          

//***********************************************************************
//**  API Backup Sector sector
//**  input - Start Addr and number of sectors to backup
//***********************************************************************
unsigned char BackupFlash (unsigned long Addr, unsigned long length)
{
   unsigned long x, z;
   unsigned char y, i;
   unsigned char buff[2]; 

	SCI_Str("\r\n==>BackupFlash!!");
   
   // locate which block
   Addr = Addr/512;                   // sector no. (page no.)
   x = Addr/32;                       // block no.
   y = (unsigned char)(Addr%32);      // page no. in block
   length = length/512;               // no. of block of data to be written

   ChipSelect_F1
   TurnOnLed();                               // LED indication
   
   // get physical block no from sector table
   flash_rw.addr = (x/256)+BLK_TBL_OFFSET;    // check for block address
   flash_rw.length = 2;
   flash_rw.buff_ptr = &buff[0];
   fread_data((unsigned int)((x%256)*2));     // physical block no. in location
   
   x = (((unsigned long)buff[1])<<8 | (unsigned long)buff[0])<<5;  // physical block location

   // memory plane select for backup
   if ((x&0x0020)==0)                    
    z = backup_even;
   else
    z = backup_odd;
   
   // backup whole block to another block, except the being modify page
	SCI_Str("\r\n==>y= ");
	SCI_HexB( y );
	SCI_Str("\r\n==>x=");
	SCI_Hex( x );

   for(i=0;i<19;i++)
   {

⌨️ 快捷键说明

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