why write flash failed? read flash is ok

i use atemga2560 mcu,want write some data to the flash,and the program regin not locked.this is my code,could you please help me how to write flash in the shetch?

void write_one_page(unsigned int FlashAddr,unsigned char *buf)
{
  boot_page_erase(FlashAddr);                  //擦除一个Flash页
  boot_spm_busy_wait();
  for(pagptr = 0; pagptr < SPM_PAGESIZE; pagptr += 2) //数据填入Flash缓冲页
  {
    boot_page_fill(pagptr, buf[pagptr] + (buf[pagptr + 1] << 8));
  }
  boot_page_write(FlashAddr);                  //将缓冲页数据写入一个Flash页
  boot_spm_busy_wait();                        //等待页编程完成
}

i read the flash successful,the returned data is right.
i compared to the flash data which read out by the AVR STUDIO 6.2.

The Set Program Memory (SPM) instruction is the only instruction that can write into FLASH. The SPM instruction can only be executed in the Boot Loader Segment. That means only the bootloader can write into FLASH.

If you need to write into FLASH you will have to modify the bootloader so you can call a function in the bootloader to do the actual write.

johnwasser:
The Set Program Memory (SPM) instruction is the only instruction that can write into FLASH. The SPM instruction can only be executed in the Boot Loader Segment. That means only the bootloader can write into FLASH.

If you need to write into FLASH you will have to modify the bootloader so you can call a function in the bootloader to do the actual write.

ok,we just found the truth,thanks.
call a function from the bootloader seems to use the assemble code, do someone have the experience on it?

Why do you want to write to the flash? Why not the EEPROM?

because i need to update the program via Ethernet on time.the board is far away to get. it's a project application.

also,the mcu2560 has 256Kbytes FLASH,the code just about 60Kbytes,whether i need to update program via ethernet in purpose or not,its just a big waist...why not we use it for some data storage instead of some parallel or SPI FLASH application?

waysleehh:
also,the mcu2560 has 256Kbytes FLASH,the code just about 60Kbytes,whether i need to update program via ethernet in purpose or not,its just a big waist…why not we use it for some data storage instead of some parallel or SPI FLASH application?

sorry,“waist” is waste

So you are writing a bootloader, is that it?

no.at the beginning,i want to update my program by these steps:

  1. receive the updated program data from the ethernet,then store these data on a special defined address of the 256KByte flash.
  2. then make a update flag in eeprom or flash,reset the device.
  3. modify the bootloader to check that update flag,if is true,then bootloader make a memory copy from the special defined address to the beginning of the program 0x0.
    4.reset when finished the memory writing.

if those steps success,i think the program must be updated successfully

To achieve step 1, you must be executing code in the area that is capable of writing to program memory, so yes, you are writing a bootloader

waysleehh:
no.at the beginning,i want to update my program by these steps:

You are updating your program in the flash memory, but you are not writing a bootloader. Is that your position?

I'm inclined to agree with johnwasser in reply #2. What is your problem with that?

i should write my bootloader,but i don't know how to call the flash write function resides in the bootloader area from the application area

A fixed vector?

AWOL:
A fixed vector?

i think it should be use assemble code or some special jump to do that, but i'm so weak in it...