title: Atmel Studio- ATmega128 bootloader
body: <p>I am trying to write a customized boot-loader for ATmega AVR's. I write a code, and it work perfectly in small AVR's like ATmega32A and ATmega8A. But when i want to use it in ATmega128A, it writes nothing in flash segment.
  I'm sure Fuses are correct, ATmega103 mode is disabled, and the program starts at boot section, but it does nothing..
  Before calling function "boot_program_page" I set PORTC and turn some LED's on, and after that I cleared PORTC and LED's goes off. so the code in executing completely too.
  The function I am using is an example provided in avr/boot.h.
  It should write some data ( 0x00 actually ) in page 0 of flash memory..
  here is my code :</p>
  
  <pre><code>#define F_CPU 8000000UL
  
  #include &lt;inttypes.h&gt;
  #include &lt;avr/io.h&gt;
  #include &lt;avr/boot.h&gt;
  #include &lt;avr/interrupt.h&gt;
  #include &lt;avr/pgmspace.h&gt;
  #include &lt;util/delay.h&gt;
  
  void boot_program_page (uint32_t page, uint8_t *buf);
  
  
  int main(void)
  {
      uint8_t data[SPM_PAGESIZE] = {0};
      uint32_t page = 0;
  
      DDRC = 255;
      PORTC = 255;
  
      page *= (uint32_t)SPM_PAGESIZE;
      boot_program_page(page,data);
  
      _delay_ms(1000);
      PORTC = 0;
      while (1)
      {
      }
  }
  
  void boot_program_page (uint32_t page, uint8_t *buf)
  {
      uint16_t i;
      uint8_t sreg;
  
      // Disable interrupts.
  
      sreg = SREG;
      cli();
  
      eeprom_busy_wait ();
  
      boot_page_erase (page);
      boot_spm_busy_wait ();      // Wait until the memory is erased.
  
  
      for (i=0; i&lt;SPM_PAGESIZE; i+=2)
      {
          // Set up little-endian word.
  
          uint16_t w = *buf++;
          w += (*buf++) &lt;&lt; 8;
  
          boot_page_fill (page + i, w);
      }
  
      boot_page_write (page);     // Store buffer in flash page.
      boot_spm_busy_wait();       // Wait until the memory is written.
  
      // Reenable RWW-section again. We need this if we want to jump back
      // to the application after bootloading.
  
      boot_rww_enable ();
  
      // Re-enable interrupts (if they were ever enabled).
  
      SREG = sreg;
  }
  </code></pre>