本文介绍了IAP在LPC 1768上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用IAP(在应用程序编程中)将一个函数的代码从内存空间中的一个点复制到另一个点(我知道这没有任何用处,但这对于一个项目来说绝对至关重要)正在努力)。
代码的复制似乎可以顺利进行,但是当我调用复制的函数时,它总是会导致硬错误。我正在复制的函数没有任何其他函数或数据的引用(这是一个简单的返回函数),所以不存在相对地址错误的问题。
我在下面给出了我的代码以及我得到的输出。

  #includembed.h 
#includeIAP.h

#define TARGET_SECTOR 14


char code [1024];
IAP iap;
char val = 0;


typedef int(* function)();
功能闪烁功能;

MPU_Type mpu;


externC
void HardFault_Handler(){
register unsigned int _msp __asm(msp);
printf(Hard Fault!%x(%x)\r\\\
,SCB-> HFSR,*((unsigned int *)(_ msp + 24)));
printf(HFSR:0x%X \ n \r,SCB-> HFSR);
printf(MMFAR:0x%X \ tMMFSR:0x%X \ n \r,SCB-> MMFAR,SCB-> CFSR);
printf(BFAR:0x%X \ tBFSR:0x%X \\\\r,SCB-> BFAR,SCB-> CFSR);
printf( - %x \r\\\
,(*(volatile uint32_t *)0xe000ed24));
// printf(Hard Fault!%x \r \\\
,SCB-> HFSR);

printf(*********** MPU Settings ************* \\\
\r);
printf(TYPE:0x%X \\\
\r,mpu.TYPE);
printf(CTRL:0x%X \\\
\r,mpu.CTRL);
exit(-1);





int blink(){
int a = 1,b = 1;
返回a + b;




void copy_code_ram(){
$ b $ char * charptr;

charptr =(char *)& blink;
int i;
for(i = 0; i code [i] = * charptr;
charptr ++;





void print_function(char * ptr,int num){
for(; num> 0; num - ){
printf(0x%X,* ptr);
ptr ++;
}
}


int main(){
int r;

printf(blink code:\\\
);
print_function((char *)& blink,100);


printf(\\\
\r\\\
\r\\\
\r);
copy_code_ram();

//打印扇区
print_function(sector_start_adress [TARGET_SECTOR],100);
printf(\\\
\r\\\
\r\\\
\r);

iap.prepare(TARGET_SECTOR,TARGET_SECTOR);
iap.erase(TARGET_SECTOR,TARGET_SECTOR);
iap.prepare(TARGET_SECTOR,TARGET_SECTOR);
r = iap.write(code,sector_start_adress [TARGET_SECTOR],256);

printf(\\\
\r\\\
\r\\\
\r); %d字节(结果= 0x%08X)\ r \ n的代码,sector_start_adress [TARGET_SECTOR]为
printf(复制:SRAM(0x%08X) - > Flash(0x%08X) ,1024,r);
printf(\\\
\r\\\
\r\\\
\r);

blinkfunction =(function)(sector_start_adress [TARGET_SECTOR]);

printf(\\\
\r\\\
\r\\\
\r);
print_function((char *)blinkfunction,100);

r = 0;
r = blink();
printf(眨眼的返回值是%d \\\
\r,r);

r = blinkfunction();
printf(blink函数的返回值是%d \ n \ r,r);

while(1){


}
}

以下是输出:

 硬故障! 4000000(e000)
HFSR:0x400000000
MMFAR:0xE000ED34 MMFSR:0x20000
BFAR:0xE000ED38 BFSR:0x20000

我认为我在调用函数时做错了什么。有人可以指出我的错误吗?



谢谢

忘记在功能地址中设置 thumb 位:

  blinkfunction = (函数)(sector_start_adress [TARGET_SECTOR] | 1); 

编译器知道在大多数情况下会设置它,但它不会直接转换为函数指针。


I am trying to use IAP (in application programming) to copy the code of a function from one point in memory space to another (I know this doesn't have any use, but this is absolutely crucial for a project that I'm working on).The copying of the code seems to work with out a hitch, but when I call the copied function it always results in hardfaults. The function I'm copying doesn't have any references to other functions or data (It is a simple return function), so there is no problem of relative address errors.I've given my code below along with the output that I get.

#include "mbed.h"
#include "IAP.h"

#define TARGET_SECTOR       14


char code[1024];
IAP     iap;
char val = 0;


typedef int (*function) ();
function blinkfunction;

MPU_Type mpu;


extern "C"
void HardFault_Handler() {
    register unsigned int _msp __asm("msp");
    printf("Hard Fault! %x (%x)\r\n", SCB->HFSR, *((unsigned int *)(_msp + 24)));
    printf("HFSR: 0x%X\n\r", SCB->HFSR);
    printf("MMFAR: 0x%X\tMMFSR: 0x%X\n\r", SCB->MMFAR, SCB->CFSR);
    printf("BFAR: 0x%X\tBFSR: 0x%X\n\r", SCB->BFAR, SCB->CFSR);
    printf(" - %x\r\n", (*(volatile uint32_t*)0xe000ed24));
//    printf("Hard Fault! %x\r\n", SCB->HFSR);

        printf("*********** MPU Settings *************\n\r");
        printf("TYPE: 0x%X\n\r", mpu.TYPE);
        printf("CTRL: 0x%X\n\r", mpu.CTRL);
    exit(-1);
}




int blink() {
    int a = 1, b = 1;
    return a + b;
}



void copy_code_ram() {

    char *charptr;

    charptr = (char *)&blink;
    int i;
    for(i = 0; i <200 ; i++) {
        code[i] = *charptr;
        charptr++;
    }
}



void print_function(char *ptr, int num) {
    for(; num > 0; num--) {
        printf("0x%X  ", *ptr);
        ptr++;
    }
}


int main() {
    int r;

    printf("blink code:\n");
    print_function((char *)&blink, 100);


    printf("\n\r\n\r\n\r");
    copy_code_ram();

    //Print sector
    print_function(sector_start_adress[TARGET_SECTOR], 100);
    printf("\n\r\n\r\n\r");

    iap.prepare( TARGET_SECTOR, TARGET_SECTOR);
    iap.erase (TARGET_SECTOR, TARGET_SECTOR);
    iap.prepare( TARGET_SECTOR, TARGET_SECTOR);
    r   = iap.write( code, sector_start_adress[TARGET_SECTOR], 256);

    printf("\n\r\n\r\n\r");
    printf( "copied: SRAM(0x%08X)->Flash(0x%08X) for %d bytes. (result=0x%08X)\r\n", code, sector_start_adress[ TARGET_SECTOR ], 1024, r );
    printf("\n\r\n\r\n\r");

    blinkfunction = (function) (sector_start_adress[TARGET_SECTOR]);

    printf("\n\r\n\r\n\r");
        print_function((char *)blinkfunction, 100);

    r = 0;
    r = blink();
    printf("The return value from blink is %d\n\r", r);

    r = blinkfunction();
    printf("The return value from blinkfunction is %d\n\r", r);

    while(1) {


    }
}

Here is the output:

Hard Fault! 4000000 (e000)
HFSR: 0x400000000
MMFAR: 0xE000ED34  MMFSR: 0x20000
BFAR: 0xE000ED38   BFSR: 0x20000

I think I'm doing something wrong in calling the function. Can someone please point out my mistake?

Thanks

解决方案

You forgot to set the thumb bit in the function address:

blinkfunction = (function) (sector_start_adress[TARGET_SECTOR] | 1);

The compiler knows to set this in most cases, but it not in a direct cast to a function pointer.

这篇关于IAP在LPC 1768上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 15:18