📄 sbl_iap.c
字号:
/*****************************************************************************
* sbl_iap.c: SBL_IAP API file for NXP LPC210x Family Microprocessors
*
* Copyright(C) 2006, NXP Semiconductor
* All rights reserved.
*
* History
* 2005.10.01 ver 1.00 Prelimnary version, first Release
******************************************************************************/
#include "sbl_iap.h"
#include "sbl_config.h"
#if LPC2103
const unsigned sector_start_map[MAX_FLASH_SECTOR] = {SECTOR_0_START, \
SECTOR_1_START,SECTOR_2_START,SECTOR_3_START,SECTOR_4_START,SECTOR_5_START, \
SECTOR_6_START,SECTOR_7_START};
#else
const unsigned sector_start_map[MAX_FLASH_SECTOR] = {SECTOR_0_START, \
SECTOR_1_START,SECTOR_2_START,SECTOR_3_START};
#endif
#if LPC2103
const unsigned sector_end_map[MAX_FLASH_SECTOR] = {SECTOR_0_END,SECTOR_1_END, \
SECTOR_2_END,SECTOR_3_END,SECTOR_4_END,SECTOR_5_END,SECTOR_6_END,SECTOR_7_END};
#else
const unsigned sector_end_map[MAX_FLASH_SECTOR] = {SECTOR_0_END,SECTOR_1_END, \
SECTOR_2_END,SECTOR_3_END};
#endif
unsigned param_table[5];
unsigned result_table[5];
/*****************************************************************************
** Function name: write_flash
** Description: This function prepares and erase the sector where the block
** of data belong before uploading the data buffer into the
** flash.
** Parameter: cclk: System clock frequency (kHz)
** i2c_data_buf: data buffer to be uploaded to the flash
** data_block_ctr: data block number (0-11 for 12 kBytes of data)
** Returned value: None
*****************************************************************************/
unsigned write_flash(unsigned cclk,unsigned * i2c_data_buf,unsigned data_block_ctr)
{
unsigned flash_address;
flash_address = USER_CODE_START_ADDRESS + (data_block_ctr * DATA_BLOCK_SIZE);
find_erase_prepare_sector(cclk, flash_address);
if(result_table[0] != CMD_SUCCESS)
{
return(result_table[0]);
}
write_data(cclk, flash_address, i2c_data_buf, DATA_BLOCK_SIZE);
if(result_table[0] != CMD_SUCCESS)
{
return(result_table[0]);
}
return(CMD_SUCCESS);
} // write_flash
/*****************************************************************************
** Function name: find_erase_prepare_sector
** Description: This function prepares and erases the sector where the
** block belongs.
** Parameter: cclk: System clock frequency (kHz)
** flash_address: data buffer to be uploaded to the flash
** Returned value: None
*****************************************************************************/
void find_erase_prepare_sector(unsigned cclk, unsigned flash_address)
{
unsigned i;
for(i=USER_START_SECTOR; i<=MAX_USER_SECTOR; i++)
{
if(flash_address < sector_end_map[i])
{
if( flash_address == sector_start_map[i])
{
prepare_sector(i,cclk);
erase_sector(i,cclk);
}
prepare_sector(i,cclk);
break;
}
}
} // find_erase_prepare_sector
/*****************************************************************************
** Function name: write_data
** Description: This function uploads 1 kByte block of data to the flash
** Parameter: cclk: System clock frequency (kHz)
** flash_address: data buffer to be uploaded to the flash
** flash_data_buf: pointer to the data buffer containing 1 kB
** of data
** count: data block size (1 kByte)
** Returned value: None
*****************************************************************************/
void write_data(unsigned cclk,unsigned flash_address,unsigned * flash_data_buf, unsigned count)
{
param_table[0] = COPY_RAM_TO_FLASH;
param_table[1] = flash_address;
param_table[2] = (unsigned)flash_data_buf;
param_table[3] = count;
param_table[4] = cclk;
iap_entry(param_table,result_table);
} // write_data
/*****************************************************************************
** Function name: erase_sector
** Description: This function erases a sector of 4 kByte
** Parameter: sector_num: Sector number (LPC2102: 0-3; LPC2103: 0-7)
** cclk: System clock frequency (kHz)
** Returned value: None
*****************************************************************************/
void erase_sector(unsigned sector_num,unsigned cclk)
{
param_table[0] = ERASE_SECTOR;
param_table[1] = sector_num;
param_table[2] = sector_num;
param_table[3] = cclk;
iap_entry(param_table,result_table);
} // erase_sector
/*****************************************************************************
** Function name: prepare_sector
** Description: This function prepares a sector for write operation
** Parameter: sector_num: Sector number (LPC2102: 0-3; LPC2103: 0-7)
** cclk: System clock frequency (kHz)
** Returned value: None
*****************************************************************************/
void prepare_sector(unsigned sector_num,unsigned cclk)
{
param_table[0] = PREPARE_SECTOR_FOR_WRITE;
param_table[1] = sector_num;
param_table[2] = sector_num;
param_table[3] = cclk;
iap_entry(param_table,result_table);
} // prepare_sector
/*****************************************************************************
** Function name: iap_entry
** Description: This function prepares a sector for write operation
** Parameter: param_tab: Input parameters for IAP commands (Please refer to
** Pg. 245 to 250 of LPC2101/02/03 user manual)
** result_tab: Results returned from execution of IAP commands
** (Please refer to 'IAP status code' at Pg. 251
** of LPC2101/02/03 user manual)
** Returned value: None
*****************************************************************************/
void iap_entry(unsigned param_tab[],unsigned result_tab[])
{
void (*iap)(unsigned [],unsigned []);
iap = (void (*)(unsigned [],unsigned []))IAP_ADDRESS;
iap(param_tab,result_tab);
} // iap_entry
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -