AnsweredAssumed Answered

copy far function from flash to RAM

Question asked by liuweiwei on Mar 9, 2017
Latest reply on Mar 10, 2017 by liuweiwei

Dear sir,

I am using ms9c12xdp512 16 bit mcu.

Now I want to write data to flash. First i need to load the function from flash to RAM. But the function is in the far place. Every time, the content in WB[i] is invalid. Can you help me how to copy far function to ram and make sure it can run in ram(it is near) correctly?

My codes is :

main.c

{

    INT8U data[2] = {48, 49};   

    WriteByte(0xFA, (INT8U*)0xBFFE, data, 2); //failed

}

 

flash.c

INT8U* writeByte(INT8U page, INT8U* pDes, INT8U* pSou, INT16U len);
static INT16U WB[90];
INT8U*(*WriteByte)(INT8U page, INT8U* pDes, INT8U* pSou, INT16U len);

void FlashInit(){
    INT8U i;
    INT16U *p;
  
    FCLKDIV = 0x13;
    FCNFG=0x00;
    while(FCLKDIV_FDIVLD == 0);
    FPROT_FPOPEN=1;
    FPROT_FPHDIS=1;
    FPROT_FPLDIS=1;
  
    p = (INT16U*)writeByte;//0xEF827B is writeByte's absolute address,  use this, the content of WB[i] is right;
    for(i=0;i<90;i++){
        WB[i] = *p++;
    }
    WriteByte = (INT8U* (*)(INT8U page, INT8U* pDes, INT8U* pSou, INT16U len))WB;                 
}
  

INT8U* writeByte(INT8U page, INT8U* pDes, INT8U* pSou, INT16U len){
    INT16U i;
    INT8U flag, oldPage;
    INT16U data,address;
    oldPage =PPAGE;
    address = (INT16U)pDes;
     flag = (INT16U)pDes&1;
    while(!(FCLKDIV & 0x80));
    while(!(FSTAT & 0x80));
    while(FSTAT & 0x30){
     FSTAT |= 0x30;
    }
    while(!(FPROT & 0x80));
    PPAGE = page;
    page=page&0x0C;
    page=page>>2;
    FCNFG = 3-page;
     for( i =0; i<len;i++){
        if(flag){
            address = address&0xFFFE;
            data = (*pSou++)|0xFF00;
        }else{
            data = ((*pSou++)<<8)|0x00FF;
        }
        __DINT;
        *(INT16U*)address=data;
        FCMD = 0x20;
        FSTAT|=0x80;
        while( !FSTAT_CBEIF );
        while( !FSTAT_CCIF );
        __EINT;
        if(flag){
            address=address+2;
        }
        flag^=1;
     }
     PPAGE = oldPage;
     return pDes;

 

Thanks.

Outcomes