This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Writing to Flash on C8051f350

Hello,

I have some problem when I try to write to Flash memory.

I read AN201 "WRITING TO FLASH FROM FIRMWARE" and I use write functions for F350 (page 100).

There is no problem with a light program.
But now I try to integrate these functions on a large program. When I look at memory, any data has been written.

In AN20 (page 3), I found this : "Be cautious when using the 'Large' and 'Compact' memory models, which target xdata and pdata spaces for user variables, both of which generate MOVX write opcodes."
Indeed, my first program use small memory model (no pb) and my last program use large one.

Is there a solution to write to flash memory with a large program ?

Parents
  • yes, interrupts MUSt, of course be disabled, the trouble the OP is seing is the compiler wiring/reading XDATA (with large memory model) after the SFR (whatever the name, whatever the bit) is cahnged to redirect XDATA operations/writes to flash.

    an illustration from my codes where I use the high part of a f126 flash as permanent data storage

    U8 ReadFlashChar (U8 code *  RFSaddr)
    {
    U8 RFCintsav;
    U8 RFCdata;
    
      RFCintsav = SG_IE;
      SG_IE = 0;
      SG_SFRPAGE = 0;
      if (RFSaddr > 0x7fff)
      {
        SG_PSBANK = 0x30;
      }
      else
      {
        SG_PSBANK = 0x20;
        RFSaddr   += 0x8000   ;
      }
      RFCdata = *RFSaddr;
      SG_PSBANK = 0x10      ;
      SG_IE = RFCintsav;
      return (RFCdata) ;
    }
    
    If RFCdata were mapped to XDATA, it would be written to flash.
    
    
    Erik
    

Reply
  • yes, interrupts MUSt, of course be disabled, the trouble the OP is seing is the compiler wiring/reading XDATA (with large memory model) after the SFR (whatever the name, whatever the bit) is cahnged to redirect XDATA operations/writes to flash.

    an illustration from my codes where I use the high part of a f126 flash as permanent data storage

    U8 ReadFlashChar (U8 code *  RFSaddr)
    {
    U8 RFCintsav;
    U8 RFCdata;
    
      RFCintsav = SG_IE;
      SG_IE = 0;
      SG_SFRPAGE = 0;
      if (RFSaddr > 0x7fff)
      {
        SG_PSBANK = 0x30;
      }
      else
      {
        SG_PSBANK = 0x20;
        RFSaddr   += 0x8000   ;
      }
      RFCdata = *RFSaddr;
      SG_PSBANK = 0x10      ;
      SG_IE = RFCintsav;
      return (RFCdata) ;
    }
    
    If RFCdata were mapped to XDATA, it would be written to flash.
    
    
    Erik
    

Children
  • Thank you for your help,

    Finally, I just forgot a word (very important).

    In my write function, i declared the write pointer like this :

    char xdata * pwrite;

    The space where this pointer is stored isn't defined.
    In small memory model, the compiler put it in data, no problem. But with compact or large model, the compiler put it in xdata, doesn't work.

    I just wrote :
    char xdata * data pwrite;
    and it works.