⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 flash_api.c

📁 用MCS51 单片机的TCIP协议的测试,很基本的程序,对新手可能有帮助!
💻 C
字号:
/*****************************************************************************
*
*           (c) ATMEL-Wireless and Microcontrollers 2000 
*
*
******************************************************************************/

/*C**************************************************************************
* NAME: flash_api.c  
*----------------------------------------------------------------------------
* CREATED_BY:    Jean-sebastien Berthy
* CREATION_DATE: 2001/10/15
*----------------------------------------------------------------------------
* PURPOSE: 
* Read/Write flash
*****************************************************************************/

/*_____ I N C L U D E - F I L E S ____________________________________________*/
#include "config.h"


/*_____ G L O B A L S ________________________________________________________*/
#ifdef LARGE_MEMORY_MODEL
  Uint16 data data_addr_xram;
  Uint16 data data_addr_flash;
  Uchar  data data_nb_data;
  Uchar  data data_value;
#endif


/*_____ P R I V A T E - F U N C T I O N S - D E C L A R A T I O N ____________*/


/*_____ L O C A L S __________________________________________________________*/

#define MSK_AUXR1_ENBOOT  0x20
#define MSK_AUXR_M0       0x20
#define MAP_BOOT          AUXR1 |= MSK_AUXR1_ENBOOT;
#define UNMAP_BOOT        AUXR1 &= ~MSK_AUXR1_ENBOOT;


/*_____ EXTERNAL - F U N C T I O N S - D E C L A R A T I O N ____________*/

extern void ASM_MOV_R1_A(void);
extern void __API_FLASH_ENTRY_POINT(void);

/*F**************************************************************************
* NAME: __api_wr_code_byte 
*----------------------------------------------------------------------------
* AUTHOR: Jean-sebastien Berthy 
*----------------------------------------------------------------------------
* PARAMS:  
*     Uint16 address : address to program   
*     Uchar value : data to write
*     Uchar return : 
*             return  = 0x00 -> pass                            
*             return != 0x00 -> fail
*----------------------------------------------------------------------------
* PURPOSE: 
*  Program data byte in Flash memory 
*****************************************************************************/
#ifdef __API_WR_CODE_BYTE
Uchar __api_wr_code_byte (Uint16 address, Uchar value)
{
  bit ea_save;

  ea_save = EA;
  EA = 0;
#ifdef LARGE_MEMORY_MODEL
  data_addr_flash = address;
  data_value = value;
  DPTR = data_addr_flash;
  ACC = 0x02;
  ASM_MOV_R1_A();
  ACC = data_value;   
  MAP_BOOT;
  __API_FLASH_ENTRY_POINT();
  UNMAP_BOOT;
#else
  DPTR = address;    
  ACC = 0x02;
  ASM_MOV_R1_A();
  ACC = value;  
  MAP_BOOT;
  __API_FLASH_ENTRY_POINT();
  UNMAP_BOOT;
#endif
  EA = ea_save;     // restore interrupt state
  return (ACC); 

}
#endif

/*F**************************************************************************
* NAME: __api_wr_code_page 
*----------------------------------------------------------------------------
* AUTHOR: Jean-sebastien Berthy 
*----------------------------------------------------------------------------
* 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
*     Uchar return : 
*           return = 0x00 -> pass                           
*           return != 0x00 -> fail 
*----------------------------------------------------------------------------
* PURPOSE: 
*  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.               
*****************************************************************************/
#ifdef __API_WR_CODE_PAGE
Uchar __api_wr_code_page (Uint16 add_flash, Uint16 add_xram, Uchar nb_data)
{
  Uchar save_auxr1;

  bit ea_save;

  ea_save = EA;
  EA = 0;
  save_auxr1 = AUXR1;
  
#ifdef LARGE_MEMORY_MODEL
  data_addr_flash = add_flash;
  data_addr_xram  = add_xram;
  data_nb_data    = nb_data;
  AUXR1 &= ~0x01;         // Set DPTR = DPTR0
  DPTR = data_addr_flash;
  AUXR1++;                // DPTR = DPTR1
  DPTR = data_addr_xram;
  ACC = 0x09;
  ASM_MOV_R1_A();
  ACC = data_nb_data;     // Number of bytes to program
  AUXR1 &= ~0x01;         // Set DPTR = DPTR0
  MAP_BOOT;
  __API_FLASH_ENTRY_POINT();
  UNMAP_BOOT;
  AUXR1 = save_auxr1;           
#else
  AUXR1 &= ~0x01;         // Set DPTR = DPTR0
  DPTR = add_flash;
  AUXR1++;                // DPTR = DPTR1
  DPTR = add_xram;
  ACC = 0x09;
  ASM_MOV_R1_A();
  ACC = nb_data;
  AUXR1 &= ~0x01;         // Set DPTR = DPTR0
  MAP_BOOT
  __API_FLASH_ENTRY_POINT();
  UNMAP_BOOT;
  AUXR1 = save_auxr1;           
#endif
  EA = ea_save;     // restore interrupt state
return (ACC);

}
#endif


/*F**************************************************************************
* NAME: __api_fct_set_1 
*----------------------------------------------------------------------------
* AUTHOR: Jean-sebastien Berthy 
*----------------------------------------------------------------------------
* PARAMS:  
*     Uchar _R1 : 
*     Uint16 _DPTR : 
*     Uchar return : 
*----------------------------------------------------------------------------
* PURPOSE: 
* 
*****************************************************************************/
#ifdef __API_FCT_SET_1
Uchar __api_fct_set_1 (Uchar _R1, Uint16 _DPTR)
{
  bit ea_save;

  ea_save = EA;
  EA = 0;
#ifdef LARGE_MEMORY_MODEL
  data_value      = _R1;
  data_addr_flash = _DPTR;
  DPTR  = data_addr_flash;
  ACC   = data_value;
  ASM_MOV_R1_A();
  MAP_BOOT;
  __API_FLASH_ENTRY_POINT();
  UNMAP_BOOT;
#else
  DPTR  = _DPTR;
  ACC   = _R1;
  ASM_MOV_R1_A();
  MAP_BOOT;
  __API_FLASH_ENTRY_POINT();
  UNMAP_BOOT;
#endif
  EA = ea_save;     // restore interrupt state
  return (ACC);

}
#endif


/*F**************************************************************************
* NAME: __api_fct_set_2
*----------------------------------------------------------------------------
* AUTHOR: Jean-sebastien Berthy 
*----------------------------------------------------------------------------
* PARAMS:  
*     Uchar _ACC : 
*     Uchar _DPL : 
*     Uchar return : 
*----------------------------------------------------------------------------
*****************************************************************************/
#ifdef __API_FCT_SET_2
Uchar __api_fct_set_2 (Uchar _ACC, Uchar _DPL)
{
  bit ea_save;

  ea_save = EA;
  EA = 0;
#ifdef LARGE_MEMORY_MODEL
  data_value  = _ACC;
  data_nb_data= _DPL;
  DPH = 0x00;
  DPL = data_nb_data;
  ACC = 0x06;
  ASM_MOV_R1_A();
  ACC = data_value;
  MAP_BOOT;
  __API_FLASH_ENTRY_POINT();
  UNMAP_BOOT;
#else
  DPH = 0x00;
  DPL = _DPL;
  ACC = 0x06;
  ASM_MOV_R1_A();
  ACC = _ACC;
  MAP_BOOT;
  __API_FLASH_ENTRY_POINT();
  UNMAP_BOOT;
#endif
  EA = ea_save;     // restore interrupt state
  return 1;
}
#endif

/*F**************************************************************************
* NAME: __api_fct_set_3
*----------------------------------------------------------------------------
* AUTHOR: Jean-sebastien Berthy 
*----------------------------------------------------------------------------
* PARAMS:  
*     Uchar _ACC : 
*     Uchar _DPL : 
*     Uchar return : 
*----------------------------------------------------------------------------
*****************************************************************************/
#ifdef __API_FCT_SET_3
Uchar __api_fct_set_3 (Uchar _ACC, Uchar _DPL)
{
  bit ea_save;

  ea_save = EA;
  EA = 0;

#ifdef LARGE_MEMORY_MODEL
  data_value  = _ACC;
  data_nb_data= _DPL;
  DPH = 0x00;
  DPL = data_nb_data;
  ACC = 0x0A;
  ASM_MOV_R1_A(); // Mov R1, 0x08 
  ACC = data_value;
  MAP_BOOT;
  __API_FLASH_ENTRY_POINT();
  UNMAP_BOOT;
#else
  DPH = 0x00;
  DPL = _DPL;
  ACC = 0x0A;
  ASM_MOV_R1_A();
  ACC = _ACC;
  MAP_BOOT;
  __API_FLASH_ENTRY_POINT();
  UNMAP_BOOT;
#endif
  EA = ea_save;     // restore interrupt state
  return 1;
}
#endif




























































































⌨️ 快捷键说明

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