Try mo to. eto gamit ko nang kailangan kong mag write sa flash. based ito sa app note ng zilog. di ko kasi matest kung gumagana pa. wala akong slimboard.
FlashIO.c
#include <stdio.h>
#include <ez8.h>
#include "flash_IO.h"
#define PAGE_SIZE 512
char far Flash_Page[PAGE_SIZE]; // Far is used so this variable is placed in Edata
char near RamByte;
void init_flash(void) {
FPFH = 0x48;
FPFL = 0x00;
}
/* This function unlocks Flash for Flash Programming */
unsigned int UnlockFlash(unsigned int pagenum)
{
FCTL=0x00;
FPS=pagenum;
if( (FSTAT & 0x3F) == 0x00 ) // Flash Controller Locked state
{
FCTL = 0x73; // First unlock command
if ( (FSTAT & 0x3F) == 0x01 ) // First unlock Command recieved
{
FCTL = 0x8C; // Second unlock command
if ( (FSTAT & 0x3F) == 0x02 ) // Unlocked programming state
{
FPS=pagenum;
return 0x00;}
else
return 0xFF;
}
else
return 0xFF;
}
else
return 0xFF;
}
/* Locks the Flash controller */
void LockFlash(void)
{
while( (FSTAT & 0x3F) != 0x00 )
FCTL = 0x00; // write some value other than 0x73, 0x8c, 0x95, and 0x63
// to Flash control Register
}
/* This function Erases the page "pagenum" in Flash */
unsigned int EraseFlash(unsigned int pagenum)
{
unsigned int status;
status = UnlockFlash(pagenum);
if (status == 0x00)
{ // check if Flash is unlocked
//FPS = pagenum; // Set the page to be erased
FCTL = 0x95; // Page erase
LockFlash();
}
// No need to unlock as Flash controller locks immediatetly after page erase
return status;
}
/* This function writes a single byte value to the Flash location. It assumes the Page
that corresponds to the location needs to be erased. */
void WriteByteToFlash(int location, unsigned int value)
{
unsigned int pagenum;
int i, TempLoc, counter=0;
char Interrupts;
pagenum = (location>>9); // compute the page number of byte address
TempLoc = (location & 0xFE00);
for(i=0; i< PAGE_SIZE; i++) // save the Flash page in RAM
Flash_Page[i] = ReadByteFromFlash(TempLoc+i);
// Modify the byte location in RAM that corresponds to Flash saved page
Flash_Page[location - TempLoc] = value;
EraseFlash(pagenum); // erase Flash Page
// Restore the page from RAM back to Flash
for(i=0; i< PAGE_SIZE; i++)
{
WriteByteToFlash1(TempLoc+i, Flash_Page[i]);
if(FSTAT==0x03) counter++;
}
LockFlash();
}
/* This function writes a single byte value in the Flash location. It assumes the Byte
value of the Flash location is 0xFF and need NOT be erased before programming */
void WriteByteToFlash1(int location, unsigned int value)
{
UnlockFlash(location>>9);
// printf("FSTAT = %x, FPFH = %x, FPFL = %x\n", FSTAT, FPFH, FPFL);
// printf("location = %d, value = %2x\n", location, value);
RamByte=(location>>8);
asm("LD R8, _RamByte");
RamByte = (location & 0x00FF);
asm("LD R9, _RamByte");
RamByte = value;
asm("LD R10, _RamByte");
asm("LDC @RR8, R10"); // Load Byte into Flash location
}
/* This function returns a single byte value read from the Flash location */
unsigned int ReadByteFromFlash(int location)
{
unsigned int value;
RamByte=(location>>8);
asm("LD R8, _RamByte");
RamByte = (location & 0x00FF);
asm("LD R9, _RamByte");
asm("LDC R10, @RR8");
asm("LD _RamByte, R10");
value = RamByte;
return (value&0xFF);
}
FlashIO.h
/*************************************************
* Copyright (C) 1999-2002 by ZiLOG, Inc.
* All Rights Reserved
*
* Flash_IO.h : Header file for Flash Driver
**************************************************/
//Prototype definitions
void init_flash(void);
unsigned int UnlockFlash(unsigned int pagenum);
void LockFlash(void);
unsigned int EraseFlash(unsigned int pagenum);
void WriteByteToFlash(int location, unsigned int value);
void WriteByteToFlash1(int location, unsigned int value);
unsigned int ReadByteFromFlash(int location);