LPC 1768 上的 IAP [英] IAP on LPC 1768

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

问题描述

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

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) {


    }
}

这是输出:

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?

谢谢

推荐答案

您忘记设置函数地址中的 thumb 位:

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.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆