📄 drvparalflash.c
字号:
////////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2006-2007 MStar Semiconductor, Inc.
// All rights reserved.
//
// Unless otherwise stipulated in writing, any and all information contained
// herein regardless in any format shall remain the sole proprietary of
// MStar Semiconductor Inc. and be kept in strict confidence
// (¨MStar Confidential Information〃) by the recipient.
// Any unauthorized act including without limitation unauthorized disclosure,
// copying, use, reproduction, sale, distribution, modification, disassembling,
// reverse engineering and compiling of the contents of MStar Confidential
// Information is unlawful and strictly prohibited. MStar hereby reserves the
// rights to any and all damages, losses, costs and expenses resulting therefrom.
//
////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///@file drvflash.h
///@brief Driver interface for accessing the flash.
///
///@author MStarSemi Inc.
///
///- Providing Read/Write and Erase function for accessing the flash.
///- Support 2 types of flash: Serial flash and Parallel flash.
///- Selecting different flash type by flag, but the Driver interface for serial/parallel flash is the same.
///
///@image html flash.jpg
///
///@par Example
///@code
/// //Erase database in flash
/// //Input StartAddr:Database base address in flash
/// //Input databaseSize: database size
/// BOOL msAPI_EraseFlashDatabase(U32 StartAddr, U16 databaseSize)
/// {
/// // Erase flash from "Database Start Address" to "End address"
/// MDrv_Flash_Erase(StartAddr, StartAddr+databaseSize);
/// }
///@endcode
///
///@internal This file is for Parallel flash ONLY
///
///////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include "datatype.h"
#include "hwreg.h"
#include "sysinfo.h"
#include "mreg51.h"
#include "drvuart.h"
/********************************************************************************/
///Local functions (will not include in DDI document)
/********************************************************************************/
/********************************************************************************/
/// Erase 1 sector data from serial flash starting from u32addr
/// @param -u32addr \b IN: Starting address in flash to erase
/********************************************************************************/
static void ParalFlash_SectorErase(U32 u32addr)
{
U8 header[] = { 0x05, 0x55, 0xAA, 0x02, 0xAA, 0x55, 0x05, 0x55, 0x80, 0x05,
0x55, 0xAA, 0x02, 0xAA, 0x55 };
U8 i;
LONG32_BYTE atmp;
///- Clear header FIFO, enable header and data ctrl, set flash data inc
XBYTE[ISP_CTRL] = 0xAF;
///- Set [flash write] command
for(i=0;i<15;i++)
XBYTE[ISP_WR_HEAD] = header[i];
///- Write flash address
atmp.u32Num = u32addr;
XBYTE[ISP_WR_AL] = atmp.u8Num[3];
XBYTE[ISP_WR_AM] = atmp.u8Num[2];
XBYTE[ISP_WR_AH] = atmp.u8Num[1];
XBYTE[ISP_WR_DATA] = 0x30;
}
/******************************************************************************/
/*
void MDrv_Flash_Init()
{
}
*/
/******************************************************************************/
/********************************************************************************/
/// Write data to flash
/// @param -u32addr \b IN: Starting address in flash to write
/// @param -u32size \b IN: Total size to write
/// @param -pdat \b IN: Pointer to the data buffer which is going to be written to flash
/// @par Function Actions:
/********************************************************************************/
void MDrv_Flash_Write(U32 u32addr, U32 u32size, U8* pdat)
{
U8 header[] = { 0x05, 0x55, 0xAA, 0x02, 0xAA, 0x55, 0x05, 0x55, 0xA0 };
U8 i;
LONG32_BYTE atmp;
U32 j;
///- Clear header FIFO, enable header and data ctrl, set flash data inc
XBYTE[ISP_CTRL] = 0xAF;
///- Set [flash write] command
for(i=0;i<9;i++)
XBYTE[ISP_WR_HEAD] = header[i];
///- Write flash address
atmp.u32Num = u32addr;
XBYTE[ISP_WR_AL] = atmp.u8Num[3];
XBYTE[ISP_WR_AM] = atmp.u8Num[2];
XBYTE[ISP_WR_AH] = atmp.u8Num[1];
///- Write data
for(j=0;j<u32size;j++)
{
XBYTE[ISP_WR_DATA] = *pdat;
pdat++;
}
///- Unset flash data inc
XBYTE[ISP_CTRL] = 0x8F;
}
/********************************************************************************/
/// Erase data from flash
/// @param -u32start \b IN : Starting address in flash to Erase
/// @param -u32end \b IN : End address in flash to Erase
/// @par Function Actions:
/********************************************************************************/
void MDrv_Flash_Erase(U32 u32start, U32 u32end)
{
U32 s ,e;
U16 i, count;
///- Sector alignment
s = u32start & 0xFFF000;
e = (u32end + 1 + 0xFFF) & 0xFFF000;
///- Count sectors
count = (e - s) / 0x1000;
///- Erase flash sector by sector
for(i=0;i<count;i++)
ParalFlash_SectorErase(s + i*0x1000);
}
/********************************************************************************/
/// Read data from flash
/// @param -u32addr \b IN : Starting address in flash to Read
/// @param -u32size \b IN : Total size to Read
/// @param -pdat \b IN : Pointer to return data buffer
/// @par Function Actions:
/********************************************************************************/
void MDrv_Flash_Read(U32 u32addr, U32 u32size, U8* pdat)
{
LONG32_BYTE atmp;
U32 j;
///- Write flash address
atmp.u32Num = u32addr;
XBYTE[ISP_WR_AL] = atmp.u8Num[3];
XBYTE[ISP_WR_AM] = atmp.u8Num[2];
XBYTE[ISP_WR_AH] = atmp.u8Num[1];
///- Read data
for(j=0;j<u32size;j++)
{
XBYTE[ISP_CTRL] = 0xEB;
*pdat = XBYTE[ISP_RD_DATA];
pdat++;
}
XBYTE[ISP_CTRL] = 0xF8;
}
/********************************************************************************/
/// Read Factory Test Setting from flash
/********************************************************************************/
U8 MDrv_Flash_GetFactoryTestSetting( void )
{
///- Write flash address
XBYTE[ISP_WR_AH] = (SYSTEM_BANK_DATADASE_BASE & 0x00FF0000) >> 16; // MSB
XBYTE[ISP_WR_AM] = (SYSTEM_BANK_DATADASE_BASE & 0x0000FF00) >> 8;
XBYTE[ISP_WR_AL] = (SYSTEM_BANK_DATADASE_BASE & 0x000000FF);
XBYTE[ISP_CTRL] = 0xEB;
XBYTE[ISP_CTRL] = 0xF8;
return ( XBYTE[ISP_RD_DATA] );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -