📄 bsl_flash.c
字号:
/******************************************************************************\
* Copyright (C) 2004 by RTD Embedded Technologies, Inc. All rights reserved.
* Confidential and Proprietary, Not for Public Release
*------------------------------------------------------------------------------
* PROJECT.......... Board Support Library for SPM6420
* VERSION.......... (Defined in README.TXT)
*------------------------------------------------------------------------------
* CONTENT.......... C source file for API of peripheral FLASH
* FILENAME......... bsl_flash.c
* GENERATED BY..... Peripheral Generator v1.1
* GENERATED AT..... 02/10/2004 08:50:53
*------------------------------------------------------------------------------
* PERIPHERAL NAME.. FLASH
* PERIPHERAL TYPE.. Single-device peripheral
* ACCESS MODE...... Direct (no handle)
* REGISTER ACCESS.. 16-bit wide
\******************************************************************************/
#define _BSL_FLASH_MOD_
#include <bsl_flash.h>
#include <csl_irq.h>
#include <csl_dat.h>
#include <bsl_uint.h>
#if (BSL_FLASH_SUPPORT)
/******************************************************************************\
* L O C A L S E C T I O N
\******************************************************************************/
/******************************************************************************\
* static macro declarations
\******************************************************************************/
// valid values for Flash_lock
#define FLASH_UNLOCKED 0
#define FLASH_LOCKED 1
// flash write macro using flash relative offset
#define FLASH_WRITE16(flashByteOffset,val16) \
*(volatile Uint16 *)(((Uint32)(FLASH_STARTING_ADDRESS)+(Uint32)(flashByteOffset))) \
= ((Uint16)(val16))
// flash read macro using flash relative offset
#define FLASH_READ16(flashByteOffset) \
(*(volatile Uint16 *)((Uint32)(FLASH_STARTING_ADDRESS)+(Uint32)(flashByteOffset)))
// DSP read macro using DSP absolute address
#define DSP_READ16(dspByteAddress) \
(*(volatile Uint16 *)((Uint32)(dspByteAddress)))
// operations
#define FLASH_OP_TRANSFER_FROM_DSP (0x00000000u)
#define FLASH_OP_TRANSFER_TO_FLASH (0x00000000u)
#define FLASH_OP_TRANSFER_FROM_FLASH (0x00000001u)
#define FLASH_OP_TRANSFER_TO_DSP (0x00000001u)
#define FLASH_OP_ERASE_SECTORS (0x00000002u)
#define FLASH_OP_ERASE_CHIP (0x00000003u)
#define FLASH_OP_IS_BLANK (0x00000004u)
/******************************************************************************\
* static typedef declarations
\******************************************************************************/
// Flash Resource Access Request Object, or Flash RARO for short
typedef struct
{
Uint32 operation; // operation
Uint32 param1; // parameter 1
Uint32 param2; // parameter 2
Uint32 numOf16bWs; // number of 16-bit words to be transferred
// at read and write
FLASH_PReturn pReturn; // pointer to the user's variable that
// will be set after the flash operation
} FLASH_AccessReqObj;
/* param1 and param2 mean different things at different operation
*
* operation param1 param2
*
* TRANSFER_xxx byte offset within byte address within DSP's
* flash's address memory space; it must not be
* space an address within flash
*
* ERASE_SECTORS ordinal number of ordinal number of
* beginning sector, last sector, counted from 0
* counted from 0
*
* ERASE_CHIP do not care do not care
*
* IS_BLANK ordinal number of ordinal number of
* beginning sector, last sector, counted from 0
* counted from 0
*/
/******************************************************************************\
* static function declarations
\******************************************************************************/
void FLASH_operation();
void FLASH_cmdUnlockBypass();
void FLASH_cmdUnlockBypassProg16(Uint32 flashOffset, Uint16 data16);
void FLASH_cmdUnlockBypassReset();
void FLASH_cmdChipErase();
void FLASH_cmdSectorErase(Uint32 sectorIndex);
Uint32 FLASH_testAndLock();
void FLASH_release();
Uint32 FLASH_checkParams(Uint32 flashOffset, Uint32 dspAddress, Uint32 numOf16bWs);
Uint32 FLASH_checkSiParams(Uint32 startSI, Uint32 endSI);
/******************************************************************************\
* static variable definitions
\******************************************************************************/
// lock, to determine there is an operation (access request) in process
volatile Uint32 FLASH_lock;
// flash size in bytes
Uint32 FLASH_sizeInBytes;
// flash device's last byte address in the DSP memory space
Uint32 FLASH_lastAddress;
// the one and only Flash RARO
FLASH_AccessReqObj FLASH_accessReqObj;
// variables used while an operation is running
Uint32 FLASH_OP_param1;
Uint32 FLASH_OP_param2;
Uint32 FLASH_OP_count;
/******************************************************************************\
* static function definitions
\******************************************************************************/
//==============================================================================
// Initializes the flash controller module.
//
// Called from the BLS_init().
//
// The state of flash device's Ready/Busy# pin can be accessed through an
// EPLD register, and an interrupt can be generated for the Busy#-to-Ready
// transition on the ExtInt4 to ExtInt7.
//
// The Ready/Busy# pin is redirected to the ExtInt6. The ExtInt6 provides an
// interrupt controlled flash access method.
// In the vector.asm, the ExtInt6 interrupt vector must be unmodified!
//==============================================================================
void FLASH_init()
{
// enable the Flash Ready/Busy# IT for ExtInt7
UINT_FSETS( EIT7SRC, FRYBYIT, ENABLE );
// init FLASH module variables
FLASH_lock = FLASH_UNLOCKED;
FLASH_sizeInBytes = BSL_boardDescriptor.flashSize << 20;
FLASH_lastAddress = FLASH_STARTING_ADDRESS + FLASH_sizeInBytes - 2;
// reset flash device (does not generate Busy# signal --> no IT)
FLASH_WRITE16( 0x000, 0xF0 );
// clear the sticky bit
UINT_CLEAR_STATUS_S( FRYBYIA );
// open DAT (if it has not been opened); it will be used for blockRead16() DAT_open( DAT_CHAANY, DAT_PRI_LOW, 0 );
}
//==============================================================================
// FLASH module's Callback Interrupt Service Routine
//
// This function is called, when there is a rising edge on the flash device's
// Ready/Busy# pin (that is, there is a Busy#-to-Ready change).
//==============================================================================
void FLASH_ISR_CB()
{
// clearing the sticky bit of the Flash's Ready/Busy# Interrupt from the
// Interrupt Status register to enable further interrupts
UINT_CLEAR_STATUS_S( FRYBYIA );
// doing the current flash operation
FLASH_operation();
}
//==============================================================================
// The is the main loop of the flash controller
//==============================================================================
void FLASH_operation()
{
if( FLASH_accessReqObj.operation == FLASH_OP_TRANSFER_TO_FLASH )
{
// the physical programming of a 16-bit word has been completed
// checking the last programmed word
if( FLASH_READ16(FLASH_OP_param1) != DSP_READ16(FLASH_OP_param2) )
{
// the programming failed
FLASH_accessReqObj.pReturn->errorCode = FLASH_RETERR_PROGRAMMING_ERROR;
FLASH_accessReqObj.pReturn->isCompleted = FLASH_COMPLETED;
FLASH_release();
return;
}
// are there more words to be programmed?
FLASH_OP_count--;
if( FLASH_OP_count != 0 )
{
// step addresses
FLASH_OP_param1 += 2; // param1 = flashOffset
FLASH_OP_param2 += 2; // param2 = dspAddress
// start a new 16-bit word programming
FLASH_cmdUnlockBypassProg16( FLASH_OP_param1, DSP_READ16(FLASH_OP_param2) );
return;
}
else
{
// the end of the block programming
// leave the Unlock Bypass mode, back to the normal read mode
FLASH_cmdUnlockBypassReset();
// the programming was successful
FLASH_accessReqObj.pReturn->errorCode = FLASH_RETERR_SUCCESSFUL;
FLASH_accessReqObj.pReturn->isCompleted = FLASH_COMPLETED;
FLASH_release();
return;
}
}
else if( FLASH_accessReqObj.operation == FLASH_OP_ERASE_SECTORS )
{
// the physical erasing of a whole sector of the flash memory has been completed
// there is no "blank check"
// FLASH_OP_count = index of the current sector that has just been erased
// FLASH_OP_param2 = index of the last sector to be erased
// are there more sectors to be erased?
FLASH_OP_count++;
if( FLASH_OP_count <= FLASH_OP_param2 )
{
// step addresses
// FLASH_OP_param2 = index of the last sector to be erased
// start a new 16-bit word programming
FLASH_cmdSectorErase( FLASH_OP_count );
return;
}
else
{
// the end of the sector erases
// the erasing was successful
FLASH_accessReqObj.pReturn->errorCode = FLASH_RETERR_SUCCESSFUL;
FLASH_accessReqObj.pReturn->isCompleted = FLASH_COMPLETED;
FLASH_release();
return;
}
}
else if( FLASH_accessReqObj.operation == FLASH_OP_ERASE_CHIP )
{
// the physical erasing of the entire flash memory has been completed
// there is no "blank check"
// the erasing was successful
FLASH_accessReqObj.pReturn->errorCode = FLASH_RETERR_SUCCESSFUL;
FLASH_accessReqObj.pReturn->isCompleted = FLASH_COMPLETED;
FLASH_release();
return;
}
}
//==============================================================================
// "Unlock bypass" flash command
// Enters unlock bypass mode
//==============================================================================
void FLASH_cmdUnlockBypass()
{
FLASH_WRITE16( 0xAAA, 0xAA );
FLASH_WRITE16( 0x555, 0x55 );
FLASH_WRITE16( 0xAAA, 0x20 );
}
//==============================================================================
// "A single 16-bit word programming in unlock bypass mode" flash command
// Starts the programming of a single 16-bit word in unlock bypass mode
//
// parameters:
// flashOffset Byte offset within the flash address space
// data16 16-bit word to be written in the flash at the
// address flashOffset
//==============================================================================
void FLASH_cmdUnlockBypassProg16(Uint32 flashOffset, Uint16 data16)
{
FLASH_WRITE16( flashOffset, 0xA0 );
FLASH_WRITE16( flashOffset, data16 );
}
//==============================================================================
// "Unlock bypass reset" flash command
// Leaves unlock bypass mode
//==============================================================================
void FLASH_cmdUnlockBypassReset()
{
FLASH_WRITE16( 0x000, 0x90 );
FLASH_WRITE16( 0x000, 0x00 );
}
//==============================================================================
// "Chip erase" flash command
// Starts the erasing of the entire flash memory
//==============================================================================
void FLASH_cmdChipErase()
{
FLASH_WRITE16( 0xAAA, 0xAA );
FLASH_WRITE16( 0x555, 0x55 );
FLASH_WRITE16( 0xAAA, 0x80 );
FLASH_WRITE16( 0xAAA, 0xAA );
FLASH_WRITE16( 0x555, 0x55 );
FLASH_WRITE16( 0xAAA, 0x10 );
}
//==============================================================================
// "A single sector erasing" flash command
// Starts the erasing of a single sector's all the 16-bit words
//
// parameters:
// sectorIndex Index of the sector to be erased. Counted from 0.
// The 0-index sector is at the beginning of the flash
// memory.
//==============================================================================
void FLASH_cmdSectorErase(Uint32 sectorIndex)
{
FLASH_WRITE16( 0xAAA, 0xAA );
FLASH_WRITE16( 0x555, 0x55 );
FLASH_WRITE16( 0xAAA, 0x80 );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -