f53x_linsnooper_flash.c

来自「8051试验程序 基础教材」· C语言 代码 · 共 149 行

C
149
字号
//-----------------------------------------------------------------------------
// F53x_LINSnooper_Flash.c
//-----------------------------------------------------------------------------
// Copyright 2006 Silicon Laboratories, Inc.
// http://www.silabs.com
//
// File Description:
//
// This module contains a functions for Flash memory operations. It contains
// functions for erasing and writing flash memory. This function are using to
// storing setting. It also contains read function to restoring these values.
//
// FID:
// Target:          C8051F53x
// Tool chain:      KEIL C51 7.20
// Command Line:    None
// Project Name:    LIN Snooper
//
// Release 1.0
//    -Initial Revision
//    -01 NOV 2006
//

//-----------------------------------------------------------------------------
// Header File Preprocessor Directive
//-----------------------------------------------------------------------------

#ifndef __FLASH_C__
#define __FLASH_C__

//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------

#include <ioc8051f530.h>                 // SFR declarations
#include "F53x_LINSnooper_Main.h"      // Include common specific definitions
#include "F53x_LINSnooper_UART.h"      // Include UARTs specific definitions
#include "F53x_LINSnooper_Flash.h"     // Include Flash specific definitions

//-----------------------------------------------------------------------------
// Constant Definitions
//-----------------------------------------------------------------------------
#define  F53x_FLASH_SOURCE

//-----------------------------------------------------------------------------
// Global Definitions
//-----------------------------------------------------------------------------
const char ROM_Page[512] @ 0x1800;

//-----------------------------------------------------------------------------
// Global Variables
//-----------------------------------------------------------------------------
unsigned int    ADDR = 0x1800;                 // Flash Memory address

//----------------------------------------------------------------------------
// Flash_Erase
//----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : mem - address of memory page to clear
// Description  :   This function erases the page of flash memory when settings
//                      will be stored.
// Note:
//----------------------------------------------------------------------------
void Flash_Erase(unsigned char __code * mem)
{
    unsigned char EA_SAVE = IE_bit.EA;  // Preserve EA
    unsigned char * pwrite;             // FLASH write pointer
    unsigned char i;

    IE_bit.EA = 0;                      // Disable interrupts

    VDM0CN = 0xA0;                      // Enable VDD monitor and high threshold

    RSTSRC = 0x02;                      // Enable VDD monitor as a reset source

    pwrite = (unsigned char *)mem;

    FLKEY  = 0xA5;                      // Key Sequence 1
    FLKEY  = 0xF1;                      // Key Sequence 2
    PSCTL |= 0x03;                      // PSWE = 1; PSEE = 1

    VDM0CN = 0xA0;                      // Enable VDD monitor and high threshold

    RSTSRC = 0x02;                      // Enable VDD monitor as a reset source
    *pwrite = 0;                        // Initiate page erase

    PSCTL &= ~0x03;                     // PSWE = 0; PSEE = 0

    IE_bit.EA = EA_SAVE;                // Restore interrupts

    for ( i=0 ; i < 200 ; i++ );        // Wait (about 20ms)
}


//----------------------------------------------------------------------------
// Flash_Store
//----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : None
// Description  :   This function stores the settings values in flash memory.
// Note:
//----------------------------------------------------------------------------
void Flash_Store(unsigned char __code * mem, unsigned char byte)
{
    unsigned char EA_SAVE = IE_bit.EA;  // Preserve EA
    unsigned char * pwrite;             // FLASH write pointer

    VDM0CN = 0xA0;                      // Enable VDD monitor and high threshold

    RSTSRC = 0x02;                      // Enable VDD monitor as a reset source

    pwrite = (unsigned char *)mem;

    FLKEY  = 0xA5;                      // Key Sequence 1
    FLKEY  = 0xF1;                      // Key Sequence 2
    PSCTL |= 0x01;                      // PSWE = 1; PSEE = 0

    VDM0CN = 0xA0;                      // Enable VDD monitor and high threshold

    RSTSRC = 0x02;                      // Enable VDD monitor as a reset source
    *pwrite = byte;                     // Store data

    PSCTL &= ~0x01;                     // PSWE = 0; PSEE = 0

    IE_bit.EA = EA_SAVE;                // Restore interrupts
}

//----------------------------------------------------------------------------
// Flash_Read
//----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : None
// Description  : This function reads settings values from flash memory.
// Note:
//----------------------------------------------------------------------------
unsigned char Flash_Read(unsigned char offset)
{
    return ROM_Page[offset];
}

#endif                                 // endif definition of __FLASH_C__

//-----------------------------------------------------------------------------
// End Of File
//-----------------------------------------------------------------------------

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?