飞思卡尔单片机读flash

2019-03-26 08:00发布

飞思卡尔单片机怎么读flash 我仿真的时候,读出全是FF 也不知道读对不

读的过程 dat = *Addr  Addr是十六位的,dat是8位的

中间需要不需要加修饰符来表明指针是指向flash区的,高手们给指导一下吧,我就不用再摸索了 此帖出自小平头技术问答
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
7条回答
Li_Lei
2019-03-26 19:01
#include <hidef.h> /* for EnableInterrupts macro */
#include "derivative.h" /* include peripheral declarations */
#include "MC9S08DZ60.h"
#include "board.h"
#include "Flash.h"


uint8 EraseSector(byte* address){
  DisableInterrupts;
  FSTAT = 0x30; /*clear errors*/
  if ((uint16)address >= EEPROM_END_ADDRESS) return FAIL;
  if(FSTAT_FCBEF==1){
    *address = 0x00;//dummy;
    FCMD = FLASHCMD_ERASE;
    FSTAT = FLASHCMD_CBEF;
  _asm NOP; //Wait 4 cycles
  _asm NOP;
  _asm NOP;
  _asm NOP;
    if( (FSTAT_FACCERR!=0) || (FSTAT_FPVIOL!=0)){
      EnableInterrupts;
      return(FAIL);
    }
    while(FSTAT_FCCF!=1){
    }
    EnableInterrupts;
    return(PASS);
  } else{
    EnableInterrupts;
    return(FAIL);
  }
}

uint8 WriteByte(byte* address, byte data){
  DisableInterrupts;
  FSTAT = 0x30;
  if(FSTAT_FCBEF==1){
    *address = data;
    FCMD = FLASHCMD_PROG;
    FSTAT = FLASHCMD_CBEF;
  _asm NOP; //Wait 4 cycles
  _asm NOP;
  _asm NOP;
  _asm NOP;
    if( (FSTAT_FACCERR!=0) || (FSTAT_FPVIOL!=0)){
      EnableInterrupts;
      return(FAIL);
    }
    while(FSTAT_FCCF!=1){
    }
    EnableInterrupts;
    return(PASS);
  } else{
    EnableInterrupts;
    return(FAIL);
  }
}

uint8 WriteWord(byte* address,uint16 data) {
  if (WriteByte(address+1,(byte)data) == FAIL) return FAIL;
  if (WriteByte(address,(byte)(data>>8)) == FAIL) return FAIL;
  return PASS;
}
/***********************************switch eeprom********************************************/
#define SYSTEM_CONFIG_SIZE        sizeof(struct System_Config)
struct System_Config * System_Config_Current=(struct System_Config *)EEPROM_START_ADDRESS;  //系统当前设置
struct S_Channel_Setting * Channel_Setting_Array=(struct S_Channel_Setting *)EEPROM_CHANNELSETTING_STARTADDRESS;
//系统参数出厂设置
const struct System_Config System_Config_Default={
  EEPROM_VERIFICATION_CODE,       //unsigned int Verification_Code; //检验码
  EEPROM_FACTORY_VERIFICATION,0,0,
/***********************can总线设置*******************************/
  128,                    //uint16 Default_ID;                //默认11位标准ID值,扩展帧此数值在高11位即28~18
  0,0,0,0,0,0,0,//uint16 notused3;
//滤波器手动设置
  0x0000,               //uint16 Can_Identifier_Acceptance01;      //滤波值
  0x0000,               //uint16 Can_Identifier_Mask01;            //掩位
  0x0000,               //uint16 Can_Identifier_Acceptance23;      
  0x0000,               //uint16 Can_Identifier_Mask23;            
  0x0000,               //uint16 Can_Identifier_Acceptance45;      
  0x0000,               //uint16 Can_Identifier_Mask45;
  0x0000,               //uint16 Can_Identifier_Acceptance67;      
  0x0000,               //uint16 Can_Identifier_Mask67;
};    //系统参数默认值

//通道参数默认值
const struct S_Channel_Setting Channel_Setting_Default[32]= {
//通道1报警下限//通道1提示下限 //通道1提示上限 //通道1报警上限
  {1000,1500,2000,2500},     
  {1000,1500,2000,2500},   
  {1000,1500,2000,2500},     
  {1000,1500,2000,2500},  
  {1000,1500,2000,2500},     
  {1000,1500,2000,2500},   
  {1000,1500,2000,2500},     
  {1000,1500,2000,2500},  
  {1000,1500,2000,2500},     
  {1000,1500,2000,2500},   
  {1000,1500,2000,2500},     
  {1000,1500,2000,2500},  
  {1000,1500,2000,2500},     
  {1000,1500,2000,2500},   
  {1000,1500,2000,2500},     
  {1000,1500,2000,2500},
  {1000,1500,2000,2500},     
  {1000,1500,2000,2500},   
  {1000,1500,2000,2500},     
  {1000,1500,2000,2500},  
  {1000,1500,2000,2500},     
  {1000,1500,2000,2500},   
  {1000,1500,2000,2500},     
  {1000,1500,2000,2500},  
  {1000,1500,2000,2500},     
  {1000,1500,2000,2500},   
  {1000,1500,2000,2500},     
  {1000,1500,2000,2500},  
  {1000,1500,2000,2500},     
  {1000,1500,2000,2500},   
  {1000,1500,2000,2500},     
  {1000,1500,2000,2500}   
};

uint8 EEPRom_Init(void) {
  if ((*System_Config_Current).Verification_Code != EEPROM_VERIFICATION_CODE) {
    //如果eeprom未初始化则使用rom中的出厂设置
    System_Config_Current = (struct System_Config *)(&System_Config_Default);
    Channel_Setting_Array = (struct S_Channel_Setting *)(&Channel_Setting_Default);
    if ((*System_Config_Current).Verification_Code != EEPROM_VERIFICATION_CODE)
      return FAIL;    //参数设置错误
    if ((*System_Config_Current).Factory_Setting_Verification_Code != EEPROM_FACTORY_VERIFICATION)
      return FAIL;
   
  }
  return PASS;   
}

/******************复制出厂默认值到eeprom**********************************/
uint8 Save_System_Config_Default(void) {
  uint8 i;
  for (i=0;i<(SYSTEM_CONFIG_SIZE/EEPROM_PAGE_SIZE+1);i++)
    if (EraseSector((byte*)(EEPROM_START_ADDRESS+i*EEPROM_PAGE_SIZE))==FAIL)
      return FAIL;
  if (WriteWord((byte *)(EEPROM_START_ADDRESS),EEPROM_VERIFICATION_CODE)==FAIL)
    return FAIL;
  for (i=2;i<SYSTEM_CONFIG_SIZE;i++)
    if (WriteByte((byte *)(EEPROM_START_ADDRESS+i),*((unsigned char *)System_Config_Current+i))==FAIL)
      return FAIL;   
  System_Config_Current = (struct System_Config *)EEPROM_START_ADDRESS;
  //复制默认参数到eeprom
  for (i=0;i<32;i++)
    if (EraseSector((byte*)(EEPROM_CHANNELSETTING_STARTADDRESS+i*EEPROM_PAGE_SIZE))==FAIL)
      return FAIL;
  i=0;
  do {
    if (WriteByte((byte *)(EEPROM_CHANNELSETTING_STARTADDRESS+i),*((unsigned char *)Channel_Setting_Default+i))==FAIL)
      return FAIL;
  } while(++i);
  Channel_Setting_Array=(struct S_Channel_Setting *)EEPROM_CHANNELSETTING_STARTADDRESS;
  return PASS;
}

/*//保存参数*/
uint8 Save_System_Parameter(unsigned char offset,unsigned int val) {
  unsigned char i,buf[EEPROM_PAGE_SIZE];
  unsigned int address;
  offset*=sizeof(unsigned int);
  address=EEPROM_START_ADDRESS+(offset&~(EEPROM_PAGE_SIZE-1));  
  if (address>EEPROM_START_ADDRESS+SYSTEM_CONFIG_SIZE) return -1;
  if ((uint16)System_Config_Current != EEPROM_START_ADDRESS) { //eeprom未启用
    if (Save_System_Config_Default()) return -2;     
  }
  if ((*System_Config_Current).Verification_Code != EEPROM_VERIFICATION_CODE)
    return -3;   
  for (i=0;i<EEPROM_PAGE_SIZE;i++)  
    buf=*(unsigned char *)(address+i);
  buf[offset%EEPROM_PAGE_SIZE]=(byte)(val>>8);
  buf[offset%EEPROM_PAGE_SIZE+1]=(byte)val;   
  if (EraseSector((byte*)address)!=PASS)
      return -4;
  for (i=0;i<EEPROM_PAGE_SIZE;i++)
    if(WriteByte((byte*)(address+i),buf)==FAIL) return -5;
  return 0;
}
/*保存通道设置,min在高,max在低,chx<0数据为alarm*/
uint8 Save_Channel_Setting(unsigned char chx,uint32 val) {
  uint16 address;
  uint8 ch;
  uint32 ala,war;
  if ((uint16)System_Config_Current != EEPROM_START_ADDRESS) { //eeprom未启用
    if (Save_System_Config_Default()) return -1;     
  }
  if ((*System_Config_Current).Verification_Code != EEPROM_VERIFICATION_CODE)
    return -3;  
  if (chx&0x80) {
    ch=~chx;
    ala=val;
    war=WARNING_VAL32(ch);
  } else {
    ch=chx;
    war=val;
    ala=ALARM_VAL32(ch);
  }  
  address=EEPROM_CHANNELSETTING_STARTADDRESS+ch*EEPROM_PAGE_SIZE;  
  if (address>=EEPROM_END_ADDRESS) return -2;
   
  if (EraseSector((byte*)address)!=PASS)
      return -4;
  if (WriteWord((unsigned char *)(address+0),(unsigned int)(ala>>16))) return -5;
  if (WriteWord((unsigned char *)(address+2),(unsigned int)(ala))) return -5;
  if (WriteWord((unsigned char *)(address+4),(unsigned int)(war>>16))) return -5;
  if (WriteWord((unsigned char *)(address+6),(unsigned int)(war))) return -5;
  return 0;
}

一周热门 更多>