📄 flash_api.c
字号:
/*C**************************************************************************
* NAME: flash_api.c
*----------------------------------------------------------------------------
* Copyright (c) 2003 Atmel.
*----------------------------------------------------------------------------
* RELEASE: c5131-usb-flash-api-1_0_5
* REVISION: 1.3
*----------------------------------------------------------------------------
* PURPOSE:
* This file contains whole of functions to access AT89C5131 Flash and EEPROM.
*****************************************************************************/
/*_____ I N C L U D E S ____________________________________________________*/
#include "config.h"
/*_____ M A C R O S ________________________________________________________*/
/*_____ D E F I N I T I O N ________________________________________________*/
Uchar data api_command _at_ 0x1C;
Uchar data api_value _at_ 0x1D;
Uchar data api_dpl _at_ 0x1F;
Uchar data api_dph _at_ 0x1E;
#define MSK_AUXR1_ENBOOT 0x20
#define MAP_BOOT AUXR1 |= MSK_AUXR1_ENBOOT;
#define UNMAP_BOOT AUXR1 &= ~MSK_AUXR1_ENBOOT;
#define __API_FLASH_ENTRY_POINT (*((const void(code*)(void)) 0xFFC0 ))
#define __API_FLASH_ENTRY_POINT_PE (*((const void(code*)(void)) 0xFFF3 ))
#define __API_JMP_ISP_START (*((const void(code*)(void)) 0xFF00 ))
#define __API_JMP_BOOTLOADER (*((const void(code*)(void)) 0xF400 ))
/*_____ D E C L A R A T I O N ______________________________________________*/
/*F**************************************************************************
* NAME: __api_rd_generic
*----------------------------------------------------------------------------
* PARAMS:
* Uchar command: api_command to call
* Uchar dpl : api_dpl
* return:
* Uchar : read value
*----------------------------------------------------------------------------
* PURPOSE:
* This function allows to read xaf and special area
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
Uchar __api_rd_generic (Uchar command, Uchar dpl)
{
api_command = command;
api_dpl = dpl;
MAP_BOOT;
__API_FLASH_ENTRY_POINT();
UNMAP_BOOT;
return(api_value);
}
/*F**************************************************************************
* NAME: __api_wr_generic
*----------------------------------------------------------------------------
* PARAMS:
* Uchar command: api_command to call
* Uchar value : api_value
* Uchar dpl : api_dpl
* return:
* Uchar return: command status (1 - ok)
*----------------------------------------------------------------------------
* PURPOSE:
* This function allows to write in xaf
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
Uchar __api_wr_generic (Uchar command, Uchar value, Uchar dpl)
{
bit ea_save;
ea_save = EA;
EA = 0;
api_command = command;
api_dpl = dpl;
api_value = value;
MAP_BOOT;
__API_FLASH_ENTRY_POINT();
UNMAP_BOOT;
EA = ea_save; // restore interrupt state
return(1);
}
/*F**************************************************************************
* NAME: __api_rd_code_byte
*----------------------------------------------------------------------------
* PARAMS:
* Uint16 address : address in flash memory to read
* return:
* Uchar device : read value
*----------------------------------------------------------------------------
* PURPOSE:
* This function allows to read a flash memory byte.
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
* To use this function the constante __API_RD_CODE_BYTE must be define in
* C header file flash_api.h.
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
#ifdef __API_RD_CODE_BYTE
Uchar __api_rd_code_byte (Uchar code * pt_address)
{
return(*pt_address);
}
#endif
/*F**************************************************************************
* NAME: __api_wr_code_byte
*----------------------------------------------------------------------------
* PARAMS:
* Uint16 address : address to program
* Uchar value : data to write
* return:
* Uchar return :
* return = 0x00 -> pass
* return != 0x00 -> fail
*----------------------------------------------------------------------------
* PURPOSE:
* This function allows to program data byte in Flash memory.
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
* To use this function the constante __API_WR_CODE_BYTE must be define in
* C header file flash_api.h.
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
#ifdef __API_WR_CODE_BYTE
Uchar __api_wr_code_byte (Uchar xdata * pt_address, Uchar value)
{
bit ea_save;
ea_save = EA;
EA = 0;
api_command = _COMMAND_WR_CODE_BYTE;
FCON = 0x08;
*pt_address = value;
MAP_BOOT;
__API_FLASH_ENTRY_POINT_PE();
UNMAP_BOOT;
EA = ea_save; // restore interrupt state
return(api_value);
}
#endif
/*F**************************************************************************
* NAME: __api_wr_code_page
*----------------------------------------------------------------------------
* PARAMS:
* Uint16 add_flash : address of the first byte to program in the Flash
* Uint16 add_xram : address in XRAM of the first data to program
* Uchar nb_data : number of bytes to program
* return:
* Uchar return :
* return = 0x00 -> pass
* return != 0x00 -> fail
*----------------------------------------------------------------------------
* PURPOSE:
* This function allows to program until 128 Datas in Flash memory.
* Number of bytes to program is limited such as the Flash write remains in a
* single 128 bytes page.
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
* To use this function the constante __API_WR_CODE_PAGE must be define in
* C header file flash_api.h.
* This function used Dual Data Pointer DPTR0&1. At the end of this function
* DPTR = DPTR0.
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
#ifdef __API_WR_CODE_PAGE
Uchar __api_wr_code_page (Uchar xdata * pt_code, Uchar xdata * pt_xram, Uchar nb_data)
{
Uchar data i, temp, temp_nb_data;
bit ea_save;
Uint16 data add_pt_code, add_pt_xram;
add_pt_xram = pt_xram;
add_pt_code = pt_code;
temp_nb_data = nb_data;
ea_save = EA;
EA = 0;
api_command = _COMMAND_WR_CODE_BYTE;
for (i=0 ; i< temp_nb_data; i++,add_pt_xram++,add_pt_code++)
{
temp = *(Uchar xdata *)add_pt_xram;
FCON = 0x08;
*(Uchar xdata *)add_pt_code = temp;
FCON = 0x00;
}
MAP_BOOT;
__API_FLASH_ENTRY_POINT_PE();
UNMAP_BOOT;
EA = ea_save; // restore interrupt state
return(api_value);
}
#endif
/*F**************************************************************************
* NAME: __api_wr_code_page_fix
*----------------------------------------------------------------------------
* PARAMS:
* Uint16 add_flash : address of the first byte to program in the Flash
* Uint16 add_xram : address in XRAM of the first data to program
* Uchar nb_data : number of bytes to program
* return:
* Uchar return :
* return = 0x00 -> pass
* return != 0x00 -> fail
*----------------------------------------------------------------------------
* PURPOSE:
* This function allows to program until 128 Datas in Flash memory.
* Number of bytes to program is limited such as the Flash write remains in a
* single 128 bytes page.
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
* To use this function the constante __API_WR_CODE_PAGE must be define in
* C header file flash_api.h.
* This function used Dual Data Pointer DPTR0&1. At the end of this function
* DPTR = DPTR0.
*----------------------------------------------------------------------------
* REQUIREMENTS:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -