void FlashProgramWord(word address, word data) { Program_Byte(address++,(byte) (data >> 8)); // hi byte Program_Byte(address,(byte) (data & 0xff)); // lo byte return; }
#ifndef word#define word unsigned int#endfiword *pWord = (word*)0x1400;.... word data = *pWord++; data = pWord[i];Don't know if that is helpful This is the code from the other thread, but first you have to set the ECLK register, which should be explained in the data sheet. This is allawtterb's code, and I have no way to test it. You also have to erase it first. void ProgramByte( word address, byte data ) { if (FSTAT_FACCERR) //If EEPROM access error flag is set FSTAT_FACCERR = 1; //Then clear it while(!FSTAT_FCBEF); *address = data; //Set the data to be written to the address to be written to FCMD = BYTE_PROGRAM; //Flash/EEPROM command to program a byte (0x20) FSTAT_FCBEF = 1; //Clear command buffer empty flag if (FSTAT_FACCERR) //If EEPROM access error flag is set FSTAT_FACCERR = 1; //Then clear it if (FSTAT_FPVIOL) //If EEPROM write error flag is set FSTAT_FPVIOL = 1; //Then clear it while (!FSTAT_FCBEF); //Wait for command buffer to be empty while (!FSTAT_FCCF); //Wait for all commands to complete }
#ifndef word #define word unsigned int #endif
#ifndef byte #define word unsigned char #endif
void ProgramByte( byte* address, byte data )
#include <hidef.h> /* for EnableInterrupts macro */#include <stdio.h>#include "derivative.h" /* include peripheral declarations */#include "M68DEMO908DZ60.h"#define PASS 0#define FAIL 1#define ERASE 0x40#define PROG 0x20#define CBEF 0x80#define EEPROM_START 0x1400typedef unsigned char byte;typedef unsigned int word;typedef unsigned long dword;typedef unsigned long dlong[2];int dummy=0;/* address and program commands */int eraseSector(byte* address);int writeByte(byte* address, byte data);int readByte(byte* address);int readWord(byte* address);void progByte(byte* address,byte data);void progWord(byte* address,word data); int eraseSector(byte* address){ DisableInterrupts; FSTAT = 0x30; /*clear errors*/ if(FSTAT_FCBEF==1){ *address = dummy; FCMD = ERASE; FSTAT = CBEF; if( (FSTAT_FACCERR!=0) || (FSTAT_FPVIOL!=0)){ EnableInterrupts; return(FAIL); } while(FSTAT_FCCF!=1){ } EnableInterrupts; return(PASS); } else{ EnableInterrupts; return(FAIL); }}int writeByte(byte* address, byte data){ DisableInterrupts; FSTAT = 0x30; if(FSTAT_FCBEF==1){ *address = data; FCMD = PROG; FSTAT = CBEF; if( (FSTAT_FACCERR!=0) || (FSTAT_FPVIOL!=0)){ EnableInterrupts; return(FAIL); } while(FSTAT_FCCF!=1){ } EnableInterrupts; return(PASS); } else{ EnableInterrupts; return(FAIL); }}int readByte(byte* address){ int data = *address; return data;}int readWord(word* address){ int datar = *address; return datar;}void progByte(byte* address,byte data){ eraseSector((byte*)address); writeByte((byte*)address,data);}void progWord(byte* address,word data){ eraseSector((byte*)address); writeByte((byte*)address++,(byte) (data >> 8)); writeByte((byte*)address, (byte) (data & 0xFF));}
From MCU_init:/* ### MC9S08DZ60_64 "Cpu" init code ... */ /* PE initialization code after reset */ /* Common initialization of the write once registers */ /* SOPT1: COPT=0,STOPE=0,SCI2PS=0,IICPS=0 */ SOPT1 = 0x00; /* SOPT2: COPCLKS=0,COPW=0,ADHTS=0,MCSEL=0 */ SOPT2 = 0x00; /* SPMSC1: LVWF=0,LVWACK=0,LVWIE=0,LVDRE=1,LVDSE=1,LVDE=1,BGBE=0 */ SPMSC1 = 0x1C; /* SPMSC2: LVDV=0,LVWV=0,PPDF=0,PPDACK=0,PPDC=0 */ SPMSC2 = 0x00; /* System clock initialization */ MCGTRM = *(unsigned char*far)0xFFAF; /* Initialize MCGTRM register from a non volatile memory */ MCGSC = *(unsigned char*far)0xFFAE; /* Initialize MCGSC register from a non volatile memory */ /* MCGC2: BDIV=1,RANGE=0,HGO=0,LP=0,EREFS=0,ERCLKEN=0,EREFSTEN=0 */ MCGC2 = 0x40; /* Set MCGC2 register */ /* MCGC1: CLKS=0,RDIV=0,IREFS=1,IRCLKEN=0,IREFSTEN=0 */ MCGC1 = 0x04; /* Set MCGC1 register */ /* MCGC3: LOLIE=0,PLLS=0,CME=0,VDIV=1 */ MCGC3 = 0x01; /* Set MCGC3 register */ while(!MCGSC_LOCK) { /* Wait until FLL is locked */ }/* FCDIV: DIVLD=0,PRDIV8=0,DIV5=0,DIV4=0,DIV3=1,DIV2=0,DIV1=0,DIV0=0 */ FCDIV = 0x08; /* Set clock divider */From main.c; /* System clock initialization */ ; MCGTRM = *(unsigned char*)0xFFAF; /* Initialize MCGTRM register from a non volatile memory */ LDA $FFAF ;65455 STA MCGTRM ; MCGSC = *(unsigned char*)0xFFAE; /* Initialize MCGSC register from a non volatile memory */ LDA $FFAE ;65454 STA MCGSC ; /* MCGC2: BDIV=1,RANGE=1,HGO=1,LP=0,EREFS=1,ERCLKEN=1,EREFSTEN=0 */ ; setReg8(MCGC2, 0x36); /* Set MCGC2 register */ MOV #$36,MCGC2 ; /* MCGC1: CLKS=2,RDIV=7,IREFS=0,IRCLKEN=1,IREFSTEN=0 */ ; setReg8(MCGC1, 0xBA); /* Set MCGC1 register */ MOV #$B2,MCGC1 ; /* MCGC3: LOLIE=0,PLLS=0,CME=0,—–=0,VDIV=1 */ ; setReg8(MCGC3, 0x01); /* Set MCGC3 register */ MOV #1,MCGC3
bdayberr wrote:
I noticed in your code you have a Pause(4), is that what you are talking about for the 20ms delay?
#define EEPROM_ACCESS(x) *(uint8_t *)(x + EEPROM_BASE)....void EEPROM_Write_Byte( uint16_t address, uint8_t data)/* Writes a byte of data to the EEPROM address passed to it */{ if (FSTAT_FACCERR) // If EEPROM access error flag is set FSTAT_FACCERR = 1; // Then clear it while(!FSTAT_FCBEF); // Make sure command buffer is empty EEPROM_ACCESS(address) = data; // Write the data FCMD = BYTE_PROGRAM; // Flash/EEPROM command to program a byte (0x20) _asm NOP; // Wait at least 4 cycles before clearing FSTAT_FCBEF _asm NOP; FSTAT = 0x80; // Clear command buffer empty flag if (FSTAT_FACCERR) // If EEPROM access error flag is set FSTAT_FACCERR = 1; // Then clear it if (FSTAT_FPVIOL) // If EEPROM write error flag is set FSTAT_FPVIOL = 1; // Then clear it while (!FSTAT_FCCF); // Wait for all commands to complete}
anunalge wrote:I am using DZ128 micro.I am programming EEPROM for storing data.What ever the timings u mentioned are common for both Flash and EEPROM.But the sector size is different for both,is there any change in timings for EEPROM with respect to size.
anunalge wrote:yes it is DZ128 only.I have the softec EVB with me.Sector size is 512 for flash and 4 bytes for EEPROM,so u mean for erasing 4 bytes of EEPROM it will take 20ms.
#define READ_EEPROM_BYTE(x) *(byte *)(x) #define READ_EEPROM_WORD(x) *(word *)(x)