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

📄 flash.c

📁 Fujitsu 16bit mcu downlaod flash in application.
💻 C
字号:
/*------------------------------------------------------------------------
  flash.c
  - description and disclaimer see readme.txt
  flash.c is a code module to demonstrate how to use Flash memory write/erase. 
  Flash() is located in RAM area and all functions within Flash() are also 
  located in RAM. For that reason all functions are declared as near. 
  Flash() itself is declared as far because it is called by main(), which is 
  located in Flash Memory.

  PDR4 is used to indicate the status of the programmimg:
  PDR4_P40: 0  sector erase time out error   
  PDR4_P41: 0  write procedure timeout error         
  PDR4_P42: 0  write procedure finished successfully 

  When using Flash-Can-100P-M06 board, the status can be checked with the LED's
  (connected to port 4, writing '0' to a port will switch on the LED)

  /*----------------------------------------------------------------------*/

#include "mb90545.h"
#include "flashfunc.h"

#pragma section FAR_CODE=RAMCODE
#pragma section CODE=RAMCODE


/* Prototypes */
__near unsigned char Sector_Erase(__far unsigned int *sector_add);
__near unsigned char Flash_Write(__far unsigned int *Flash_Add, unsigned int *Buffer_Add, int numwords);
__near void Read(__far unsigned int *Start_Add, unsigned int *Buffer_Add, int size);
__near void Read_Reset(void);
__near void Dispatch(void);

/* globals */
volatile __far unsigned int *SEQ_AAAA; /* used for unlock sequence */
volatile __far unsigned int *SEQ_5554; /* used for unlock sequence */

unsigned char Buffer[0x80];  /* define buffer */

unsigned char iflag=0;

__far void InitFlash(void) 
{
  SEQ_AAAA = (__far unsigned int *)0xFFAAAA;    /* Define pointer for unlock sequence */
  SEQ_5554 = (__far unsigned int *)0xFF5554;
}

   
/* Flash Memory write function, writes a buffer (Buffer_Add) to the Flash memory, size is number of words */
__near unsigned char Flash_Write(__far unsigned int *Flash_Add, unsigned int *Buffer_Add, int numwords) {

  int flag;  /* flag to indicate end of Flash memory write cycle */
  
  /* when Flash is not in ready-state do an Read-Reset to get proper status of the Flash */
  
  if(FMCS_RDY == 0)Read_Reset();
  
  FMCS_WE = 1; /* enable write access to Flash memory */

  while (numwords >0) {
        flag=0;
        *SEQ_AAAA = 0xAA; /* unlock sequence for Flash memory write */
        *SEQ_5554 = 0x55;
        *SEQ_AAAA = 0xA0;
        *Flash_Add = *Buffer_Add;
        
        while (flag == 0) {
               if((*Flash_Add & 0x0080) == (*Buffer_Add & 0x0080)) { /* write cycle finished succesfully? */
                  flag = 1; 
                  numwords--;
                  Flash_Add++;
                  Buffer_Add++;
               }

               else {
                  if((*Flash_Add & 0x0020) == 0x0020) { /* time limit exceeded? */
                     if((*Flash_Add & 0x0080) == (*Buffer_Add & 0x0080)) { /* write cycle finished succesfully? */
                        flag = 1; 
                        numwords--;
                        Flash_Add++;
                        Buffer_Add++;
                     }

                     else {
                        flag = 2;
                        Read_Reset(); /* Reset Flash Commands, set FMCS_RDY to 1 */
                        FMCS_WE = 0; /* disable Flash write */ 
                        return(2);   /* return value 2, write buffer aborted due to timeout error */
                     }
                  }
               }   
        }
        
  }  
  
  FMCS_WE = 0; /* disable Flash write */
  return(3);   /* return value 3, write buffer finished successfully */
}


/* Flash Memory Sector Erase function, erases the specified Flash memory sector */
__near unsigned char Sector_Erase(__far unsigned int *sector_add) {
   	
  int flag=0;  /* flag to indicate end of Flash memory write cycle */

  if(FMCS_RDY == 0)Read_Reset();

  FMCS_WE = 1; /* enable write access to Flash memory */

  *SEQ_AAAA   = 0xAA; /* unlock sequence for Flash memory sector erase */
  *SEQ_5554   = 0x55;
  *SEQ_AAAA   = 0x80;
  *SEQ_AAAA   = 0xAA;
  *SEQ_5554   = 0x55;
  *sector_add = 0x30;

  while(flag==0) {
        flag=0;
        if((*sector_add & 0x0080) == 0x0080) { /* sector erase finished? */
           flag=1;
           FMCS_WE = 0; /* sector erase finished succesfully, disable Flash write */
        }
        else if((*sector_add & 0x0020) == 0x0020) {               /* timeout? */
                if((*sector_add & 0x0080) == 0x0080) {
                   flag=1;
                   FMCS_WE = 0; /* sector erase finished successfully, disable Flash write */
                }
                else {
                   flag=2;
                   Read_Reset();
                   FMCS_WE = 0; /* timeout error, disable Flash write */
                   return(1);   /* timeout error, return value 1      */
                }
             }
  }

  return(0); /* sector erase finished successfully, return value 0 */
}




/* Read Reset function, Reset the Flash when Write operation failed */
__near void Read_Reset(void) {

        *SEQ_AAAA = 0xF0;

} 

/* Read function, read memory from address Start_Add to a data buffer with address Buffer_Add  */
__near void Read(__far unsigned int *Start_Add, unsigned int *Buffer_Add, int numwords) {

  int i;
  
  for (i=0; i<numwords; i++) {
      *Buffer_Add = *Start_Add;
       Buffer_Add++;
       Start_Add++;
  } 
}

⌨️ 快捷键说明

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