Issue with SPM in Bootloader

Go To Last Post
3 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello, this is my first post.

 

Anyway, I've been trying to write to flash with the SPM from a bootloader with an ATmega328p, and so far I've tried multiple examples, including the example function in the boot.h header file, without any luck. To boil the problem down, I wrote the code below, which in theory should write something to the first couple bytes of flash:

 

#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <avr/boot.h>
//F_CPU is defined in the toolchain settings as F_CPU=1000000UL

int main(void)
{
        cli();
	boot_page_erase_safe(0);
	boot_page_fill_safe(0,0xAAAA);
	boot_page_write_safe(0);
	boot_rww_enable_safe();
	DDRC = 0b00000001;
	PORTC = 0b00000001;
	while (1);
}

Here's what the .hex file looks like that Atmel Studio generates:

:103800000C94341C0C943E1C0C943E1C0C943E1CDA
:103810000C943E1C0C943E1C0C943E1C0C943E1CC0
:103820000C943E1C0C943E1C0C943E1C0C943E1CB0
:103830000C943E1C0C943E1C0C943E1C0C943E1CA0
:103840000C943E1C0C943E1C0C943E1C0C943E1C90
:103850000C943E1C0C943E1C0C943E1C0C943E1C80
:103860000C943E1C0C943E1C11241FBECFEFD8E0DC
:10387000DEBFCDBF0E94401C0C946E1C0C94001C3B
:10388000F89407B600FCFDCFF999FECFE0E0F0E038
:1038900083E080935700E89507B600FCFDCFF999C7
:1038A000FECF2AEA3AEAE0E0F0E081E00901809305
:1038B0005700E895112407B600FCFDCFF999FECF1B
:1038C000E0E0F0E085E080935700E89581E18093A7
:1038D0005700E89581E087B988B9FFCFF894FFCF0A
:0400000300003800C1
:00000001FF

Here's my fuse bit settings:

Fuse bit settings image

 

Alright, so the problem is that nothing is being written to flash. The LED attached to PC0 will light up just fine, so the bootloader code is finishing, it's just that nothing is being written, and all the memory outside of the bootloader is 1's. To prove this, I ran a flash dump with avrdude and got the following:

 

Raw Data Dump

I pretty new to writing bootloaders, so I'm assuming that there's something obvious that I'm missing. Any help is greatly appreciated. Thanks.

 

This topic has a solution.
Last Edited: Sun. Jul 26, 2020 - 12:02 AM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

That code is at 3800. You made the classic byte/word error. I'd suggest you'd get a whole lot more mileage at 7000

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks so much. Setting the start address to 0x7000 like you said fixed things, and now the code runs fine. Problem solved.