📄 eeprom_drv.c
字号:
/*******************************************************************************/
/*
Copyright (c) 2008 Freescale Semiconductor
Freescale Confidential Proprietary
\file EEPROM_Drv.c
\brief EEPROM driver for S08DZ60
\author Freescale Semiconductor
\author B01246/ B07297
\version 0.1
\date January/2008
*/
/*******************************************************************************/
/** Variable types */
#include "typedefs.h"
#include "EEPROM_Drv.h"
/*-- Global Variables ----------------------------------------------------------*/
UINT8 u8EEPROM_Tasks_Status;
UINT8 u8EEPROM_Tasks_Status_Previous;
UINT8 *pu8EEPROM_ByteData_ptr;
UINT16 u16EEPROM_ByteAddress;
UINT16 u16EEPROM_ByteEraseAddress;
UINT16 u16EEPROM_SectAddress;
UINT8 u8EEPROM_Bytes;
UINT8 u8EEPROM_EraseBytes;
UINT8 u8EEPROM_Sectors;
/*******************************************************************************/
/**
* \brief vfnEE_Init - Initialize EEPROM and FLASH Clock divisor.
* \author B01246
* \param unsigned char val : Value to be written to FCDIV.
* \return void
* \todo
*/
void vfnEE_Init(UINT8 val)
{
if (FCDIV_DIVLD == 0) FCDIV = val;
u8EEPROM_Tasks_Status = NO_TASK_PENDING;
}
/*******************************************************************************/
/**
* \brief IsEEPROMBusy - Perform command buffer empty check.
* \author B01246/ B07297
* \param void
* \return Error code (unsigned char)
* \todo
*/
UINT8 IsEEPROMBusy(void)
{
if ((FSTAT_FCBEF == 0)||(u8EEPROM_Tasks_Status != NO_TASK_PENDING))
return EEPROM_BUSY;
else
return EEPROM_READY;
}
/*******************************************************************************/
/**
* \brief EE_GetByte - Reads an EEPROM byte from the address provided into the pointer provided.
* \author B01246/ B07297
* \param unsigned int Addr :Address of byte to be extracted
* \param unsigned char * Data: Pointer to store the result.
* \return Error code (unsigned char)
* \todo
*/
UINT8 EE_GetByte(UINT16 Addr,byte *Data)
{
if ((Addr < EEPROMStart)||(Addr > EEPROMEnd)) // Is given address out of EEPROM area array ?
return ERR_RANGE; // If yes then error
if ((FSTAT & 0xC0) != 0xC0) // Is reading from EEPROM possible?
return ERR_BUSY; // If no then error
*Data = *(volatile byte *) Addr; // Return data from given address
return ERR_OK; // OK
}
/*******************************************************************************/
/**
* \brief EE_GetWord - Reads an EEPROM word from the address provided into the pointer provided.
* \author B01246/ B07297
* \param unsigned int Addr : Address of word value to be extracted
* \param unsigned int * Data: Pointer to store the result.
* \return void
* \todo
*/
UINT8 EE_GetWord(UINT16 Addr,UINT16 *Data)
{
if ((Addr < EEPROMStart)||(Addr > (EEPROMEnd - 1))) // Is given address out of EEPROM area array ?
return ERR_RANGE; // If yes then error
if ((FSTAT & 0xC0) != 0xC0) // Is reading from EEPROM possible?
return ERR_BUSY; // If no then error
*Data = *(volatile UINT16 *) Addr; // Return data from given address
return ERR_OK; // OK
}
/*******************************************************************************/
/**
* \brief u8SectorEraseEEPROM - Requests the erasure of a given number of sectors starting from the address provided.
* \author B01246/ B07297
* \param unsigned int Addr : Address of the first sector to be erased.
* \param unsigned char Sectors: Number of sectors to be erased
* \return Error code
* \todo
*/
UINT8 u8SectorEraseEEPROM(UINT16 AddrSect, UINT8 Sectors)
{
/* Verify address within range */
if ((AddrSect < EEPROMStart) ||
(AddrSect + 4*Sectors > EEPROMEnd+1))//each sector is 4 byte long
{
/* Address out of EEPROM valid range */
return ERR_RANGE;
}
/* Perform command buffer empty check; if EEPROM is accessible, proceed */
if ((FSTAT_FCBEF == 0)||(u8EEPROM_Tasks_Status != NO_TASK_PENDING))
{
/* EEPROM is busy, command cannot be performed now, try later */
return ERR_BUSY;
}
/* Perform address alignment verification; if alignment = 4 byte-aligned, proceed */
if ((AddrSect & 0x0003) != 0)
{
/* Unaligned EEPROM Address (non-4-byte aligned), notify upper layer about this error */
return ERR_MISALIGN;
}
/* Update Address and Data variables */
u16EEPROM_SectAddress = AddrSect;
u8EEPROM_Sectors = Sectors;
/* Proceed with Sector erase command */
u8EEPROM_Tasks_Status = SECTOR_ERASE_REQUEST;
/* Notify upper layer about request current status */
return ERR_OK;
}
/*******************************************************************************/
/**
* \brief u8ByteWriteOnlyEEPROM - Requests the programming of an array of values, starting at a given address.
* \brief The destination is assumed to be ALREADY ERASED!
* \breif This function will NOT erase it prior to programming.
* \author B01246/ B07297
* \param unsigned int AddrByte : Address of the first byte destination to be programmed
* \param unsigned char *Data8_ptr: Pointer to the values to be written
* \param unsigned char Bytes: Number of bytes to be written.
* \return Error code
* \todo
*/
UINT8 u8ByteWriteOnlyEEPROM(UINT16 AddrByte,UINT8 *Data8_ptr, UINT8 Bytes)
{
UINT8 index;
//Verify address within range
if ((AddrByte < EEPROMStart) ||
(AddrByte + Bytes > EEPROMEnd+1))
{
//Address out of EEPROM valid range
return ERR_RANGE;
}
//Perform command buffer empty check; if EEPROM is accessible, proceed
if ((FSTAT_FCBEF == 0)||(u8EEPROM_Tasks_Status != NO_TASK_PENDING))
{
//EEPROM is busy, command cannot be performed now, try later
return ERR_BUSY;
}
//Check if Bytes are blank
for (index=0; index<Bytes; index++)
{
if (*(volatile UINT8 *)(AddrByte +index) != 0xFF)
{
return ERR_NON_BLANK;
}
}
//Update Address and Data variables
u16EEPROM_ByteAddress = AddrByte;
pu8EEPROM_ByteData_ptr = Data8_ptr;
u8EEPROM_Bytes = Bytes;
// Proceed with Word program command
u8EEPROM_Tasks_Status = BYTE_PROGRAM_REQUEST;
// Notify upper layer about request current status
return ERR_OK;
}
/*******************************************************************************/
/**
* \brief u8ByteWriteEEPROM - Requests the programming of an array of values, starting at a given address.
* \brief An ERASE request is sumbitted prior to Programming the bytes.
* \author B01246/ B07297
* \param unsigned int AddrByte : Address of the first byte destination to be programmed
* \param unsigned char *Data8_ptr: Pointer to the values to be written
* \param unsigned char Bytes: Number of bytes to be written.
* \return Error code
* \todo
*/
UINT8 u8ByteWriteEEPROM(UINT16 AddrByte,UINT8 *Data8_ptr, UINT8 Bytes)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -