Помощь - Поиск - Пользователи - Календарь
Полная версия этой страницы: Не работает IAP в LPC2103
Форум разработчиков электроники ELECTRONIX.ru > Микроконтроллеры (MCs) > ARM
LCD
Здравствуйте. Есть программа для LPC2103, в которой используется сохранение данных во встроенную Flash-память. Ведет себя она довольно странно: при вызове из одного места программы она работает как положено, а из другого, похожего - в 90% случаев не работает, но закономерность появления этого сбоя я не нашел. Выражается сбой так: после попытки стирания или записи в память, память не стирается и не записывается, а остается с теми же данными, а result[0] = 0.

Код:
CODE
void SaveRAMToFlash(void)
{
int IntSave= VICIntEnable;
unsigned int command[5];
unsigned int result[2];

RAMdata.header=0x5a;
RAMdata.checksum=GetDataChecksum (&RAMdata);

ResetADC=1;
VICIntEnClr = IntSave;

command[0] = 50; //Prepare sector for a write operation
command[1] = 7; // first sector
command[2] = 7; // last sector
iap_entry (command, result);

command[0] = 52; //erase sector
command[1] = 7; // first sector
command[2] = 7; // last sector
command[3] = 32211; //cclk
iap_entry (command, result);

command[0] = 50; //Prepare sector for a write operation
command[1] = 7;
command[2] = 7;
iap_entry (command, result);

command[0] = 51; // copy ram to flash
command[1] = (unsigned long) &ROMdata; // destination
command[2] = (unsigned long) &RAMdata; // source
command[3] = 512; // byte count
command[4] = 32211; // CCLK in KHz
iap_entry (command, result);

VICIntEnable = IntSave;
ResetADC=0;
}
mempfis_
Цитата
Здравствуйте. Есть программа для LPC2103, в которой используется сохранение данных во встроенную Flash-память. Ведет себя она довольно странно: при вызове из одного места программы она работает как положено, а из другого, похожего - в 90% случаев не работает, но закономерность появления этого сбоя я не нашел. Выражается сбой так: после попытки стирания или записи в память, память не стирается и не записывается, а остается с теми же данными, а result[0] = 0.


Вот набор процедур IAP выдранный из серийного проекта для LPC2368.

CODE
/******************************************************************************
*
* Copyright:
* © 2005 Embedded Artists AB
*
* File:
* iap.h
*
* Description:
* Code that implements the iap interface.
*
*****************************************************************************/
#ifndef _IAP_H_
#define _IAP_H_


/******************************************************************************
* Defines and typedefs
*****************************************************************************/

//IAP Commands
#define IAP_SELSECTOR 50
#define IAP_RAMTOFLASH 51
#define IAP_ERASESECTOR 52
#define IAP_BLANKCHK 53
#define IAP_READPARTID 54
#define IAP_BOOTCODEID 55
#define IAP_COMPARE 56
#define IAP_REINVOKE_ISP 57

//IAP and ISP Result Status Codes
#define IAP_CMD_SUCCESS 0
#define IAP_INVALID_COMMAND 1
#define IAP_SRC_ADDR_ERROR 2
#define IAP_DST_ADDR_ERROR 3
#define IAP_SRC_ADDR_NOT_MAPPED 4
#define IAP_DST_ADDR_NOT_MAPPED 5
#define IAP_COUNT_ERROR 6
#define IAP_INVALID_SECTOR 7
#define IAP_SECTOR_NOT_BLANK 8
#define IAP_SECTOR_NOT_PREPARED_FOR_WRITE_OPERATION 9
#define IAP_COMPARE_ERROR 10
#define IAP_BUSY 11

//ISP Result Status Codes
#define ISP_PARAM_ERROR 12
#define ISP_ADDR_ERROR 13
#define ISP_ADDR_NOT_MAPPED 14
#define ISP_CMD_LOCKED 15
#define ISP_INVALID_CODE 16
#define ISP_INVALID_BAUD_RATE 17
#define ISP_INVALID_STOP_BIT 18
#define ISP_CODE_READ_PROTECTION_ENABLED 19


/*****************************************************************************
* Public function prototypes
****************************************************************************/

/*****************************************************************************
*
* Description:
* Performs the IAP command: Select Sector, which prepares the sectors for
* write operations.
*
* Params:
* [in] sector1 - start sector
* [in] sector2 - end sector (must be equal or larger to sector1)
*
* Returns:
* IAP status code (see list in iap.h)
*
****************************************************************************/
unsigned int iapSelectSector(unsigned int sector1, unsigned int sector2);


/*****************************************************************************
*
* Description:
* Performs the IAP command: Copy RAM to FLASH
*
* Params:
* [in] destAddress - desitnation address within the FLASH memory
* Must be on 256 byte boundary.
* [in] srcAddress - address to RAM data block (word boundary needed).
* [in] length - length of data block (256, 512, 1024 or 4096)
*
* Returns:
* IAP status code (see list in iap.h)
*
****************************************************************************/
unsigned int iapRamToFlash(unsigned int destAddress, unsigned int srcAddress, unsigned int length);


/*****************************************************************************
*
* Description:
* Performs the IAP command: Erase Sector
*
* Params:
* [in] sector1 - start sector
* [in] sector2 - end sector (must be equal or larger to sector1)
*
* Returns:
* IAP status code (see list in iap.h)
*
****************************************************************************/
unsigned int iapEraseSector(unsigned int sector1, unsigned int sector2);


/*****************************************************************************
*
* Description:
* Performs the IAP command: Blank Check Sectors
*
* Params:
* [in] sector1 - start sector
* [in] sector2 - end sector (must be equal or larger to sector1)
*
* Returns:
* IAP status code (see list in iap.h)
*
****************************************************************************/
unsigned int iapBlankCheck(unsigned int sector1, unsigned int sector2);


/*****************************************************************************
*
* Description:
* Performs the IAP command: Read Part ID
*
* Returns:
* The Part ID, or 0 (if command failed)
*
****************************************************************************/
unsigned int iapReadPartID(void);


/*****************************************************************************
*
* Description:
* Performs the IAP command: Read Boot Version
*
* Returns:
* The boot code version, or 0 (if command failed)
*
****************************************************************************/
unsigned int iapReadBootVer(void);


/*****************************************************************************
*
* Description:
* Performs the IAP command: Compare
*
* Params:
* [in] destAddress - desitnation address within the FLASH memory
* Must be on word boundary.
* [in] srcAddress - address to RAM data block (word boundary needed).
* [in] length - length of data block (must be a multiple of 4)
*
* Returns:
* IAP status code (see list in iap.h)
*
****************************************************************************/
unsigned int iapCompare(unsigned int destAddress, unsigned int srcAddress, unsigned int length);


/*****************************************************************************
*
* Description:
* Performs the IAP command: Reinvoke ISP
*
****************************************************************************/
void iapReinvokeIsp(void);

#endif


CODE
# /******************************************************************************
# *
# * Copyright:
# * © 2005 Embedded Artists AB
# *
# * File:
# * iap.c
# *
# * Description:
# * Code that implements the iap interface.
# *
# *****************************************************************************/


/******************************************************************************
* Defines and typedefs
*****************************************************************************/
//#define CRYSTAL_FREQUENCY FOSC

#define m_IAP_ENTRY(a, cool.gif ((void (*)(unsigned int[], unsigned int[]))(0x7ffffff1))(a, cool.gif


/*****************************************************************************
* Local function prototypes
****************************************************************************/
static void iapCommand(unsigned int *pCommand, unsigned int *pResult);


/*****************************************************************************
# * Implementation of local functions
# ****************************************************************************/
#
# /*****************************************************************************
# *
# * Description:
# * Performs the actual IAP command, based on input command stored in
# * 'pCommand'. The PLL, MAM, and VIC are disabled during the command
# * and restored afterwards.
# * This code must run in THUMB mode, since the IAP interface assumes this!
# *
# * Params:
# * [in] pCommand - pointer to command vector (up to five unsigned int words)
# * [inout] pResult - pointer to where the result can be stored
# *
# ****************************************************************************/
static void iapCommand(unsigned int *pCommand, unsigned int *pResult)
{
//perform IAP command
m_IAP_ENTRY(pCommand, pResult);
}

/*****************************************************************************
# * Implementation of public functions
# ****************************************************************************/

/*****************************************************************************
# *
# * Description:
# * Performs the IAP command: Select Sector, which prepares the sectors for
# * write operations.
# *
# * Params:
# * [in] sector1 - start sector
# * [in] sector2 - end sector (must be equal or larger to sector1)
# *
# * Returns:
# * IAP status code (see list in iap.h)
# *
# ****************************************************************************/
unsigned int iapSelectSector(unsigned int sector1, unsigned int sector2)
{
unsigned int parameter[3];
unsigned int result[1];

parameter[0] = IAP_SELSECTOR;
parameter[1] = sector1;
parameter[2] = sector2;
iapCommand(parameter, result);

return result[0];
}


/*****************************************************************************
# *
# * Description:
# * Performs the IAP command: Copy RAM to FLASH
# *
# * Params:
# * [in] destAddress - desitnation address within the FLASH memory
# * Must be on 256 byte boundary.
# * [in] srcAddress - address to RAM data block (word boundary needed).
# * [in] length - length of data block (256, 512, 1024 or 4096)
# *
# * Returns:
# * IAP status code (see list in iap.h)
# *
# ****************************************************************************/
unsigned int iapRamToFlash(unsigned int destAddress, unsigned int srcAddress, unsigned int length)
{
unsigned int parameter[5];
unsigned int result[1];

parameter[0] = IAP_RAMTOFLASH;
parameter[1] = destAddress;
parameter[2] = srcAddress;
parameter[3] = length;
parameter[4] = Fcclk / 1000;
iapCommand(parameter, result);

return result[0];
}


/*****************************************************************************
# *
# * Description:
# * Performs the IAP command: Erase Sector
# *
# * Params:
# * [in] sector1 - start sector
# * [in] sector2 - end sector (must be equal or larger to sector1)
# *
# * Returns:
# * IAP status code (see list in iap.h)
# *
# ****************************************************************************/
unsigned int iapEraseSector(unsigned int sector1, unsigned int sector2)
{
unsigned int parameter[4];
unsigned int result[1];

parameter[0] = IAP_ERASESECTOR;
parameter[1] = sector1;
parameter[2] = sector2;
parameter[3] = Fcclk / 1000;
iapCommand(parameter, result);

return result[0];
}


/*****************************************************************************
# *
# * Description:
# * Performs the IAP command: Blank Check Sectors
# *
# * Params:
# * [in] sector1 - start sector
# * [in] sector2 - end sector (must be equal or larger to sector1)
# *
# * Returns:
# * IAP status code (see list in iap.h)
# *
# ****************************************************************************/
unsigned int iapBlankCheck(unsigned int sector1, unsigned int sector2)
{
unsigned int parameter[3];
unsigned int result[1];

parameter[0] = IAP_BLANKCHK;
parameter[1] = sector1;
parameter[2] = sector2;
iapCommand(parameter, result);

return result[0];
}


/*****************************************************************************
# *
# * Description:
# * Performs the IAP command: Read Part ID
# *
# * Returns:
# * The Part ID, or 0 (if command failed)
# *
# ****************************************************************************/
unsigned int iapReadPartID(void)
{
unsigned int parameter[1];
unsigned int result[2];

parameter[0] = IAP_READPARTID;
iapCommand(parameter, result);

if (result[0] == IAP_CMD_SUCCESS)
return result[1];
else
return 0;
}


/*****************************************************************************
# *
# * Description:
# * Performs the IAP command: Read Boot Version
# *
# * Returns:
# * The boot code version, or 0 (if command failed)
# *
# ****************************************************************************/
unsigned int iapReadBootVer(void)
{
unsigned int parameter[1];
unsigned int result[2];

parameter[0] = IAP_BOOTCODEID;
iapCommand(parameter, result);

if (result[0] == IAP_CMD_SUCCESS)
return result[1];
else
return 0;
}


/*****************************************************************************
# *
# * Description:
# * Performs the IAP command: Compare
# *
# * Params:
# * [in] destAddress - desitnation address within the FLASH memory
# * Must be on word boundary.
# * [in] srcAddress - address to RAM data block (word boundary needed).
# * [in] length - length of data block (must be a multiple of 4)
# *
# * Returns:
# * IAP status code (see list in iap.h)
# *
# ****************************************************************************/
unsigned int iapCompare(unsigned int destAddress, unsigned int srcAddress, unsigned int length)
{
unsigned int parameter[4];
unsigned int result[1];

parameter[0] = IAP_COMPARE;
parameter[1] = destAddress;
parameter[2] = srcAddress;
parameter[3] = length;
iapCommand(parameter, result);

return result[0];
}


/*****************************************************************************
# *
# * Description:
# * Performs the IAP command: Reinvoke ISP
# *
# ****************************************************************************/
void iapReinvokeIsp(void)
{
unsigned int parameter[1];
unsigned int result[1];

parameter[0] = IAP_REINVOKE_ISP;
iapCommand(parameter, result);
}


А вот как производится запись сектора.
CODE
//-----------------------------------------
//стирание выбранного сектора
unsigned int eraseSector(unsigned int SECTOR)
{
unsigned int status=0;

//---------------------------------------
//подготовка сектора
status = iapSelectSector(SECTOR, SECTOR);
if(status != IAP_CMD_SUCCESS)
{
return 0; //сбой подготовки
}
//---------------------------------------


//---------------------------------------
//стирание сектора
__disable_interrupt();
status = iapEraseSector(SECTOR, SECTOR);
__enable_interrupt();

if(status != IAP_CMD_SUCCESS)
{
return 0; //сбой стирания
}
else
{
return 1; //стирание OK
}
//---------------------------------------
}
//-----------------------------------------

//-----------------------------------------
//запись сектора
unsigned int writeSector(unsigned int* pData, unsigned int SECTOR, unsigned int addr, unsigned int size)
{
unsigned int status=0;

//---------------------------------------
//подготовка сектора
status = iapSelectSector(SECTOR, SECTOR);
if(status != IAP_CMD_SUCCESS)
{
return 0; //сбой подготовки
}
//---------------------------------------

//---------------------------------------
//запись сектора
__disable_interrupt();
status = iapRamToFlash( addr, (unsigned int) pData, size);
__enable_interrupt();
if(status != IAP_CMD_SUCCESS)
{
return 0; //сбой записи
}
else
{
return 1; //запись OK
}
//---------------------------------------

}
//-----------------------------------------

etoja
Не забудьте в опциях компиляции указать линкеру не использовать секторы FLASH-памяти, предназначенные для IAP.
Для просмотра полной версии этой страницы, пожалуйста, пройдите по ссылке.
Invision Power Board © 2001-2025 Invision Power Services, Inc.