Mega0/xTiny Self programming flash

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

On the Mega0 and xTiny, one achieves writes to Application flash by writing to the page buffer at RAM address aliases for flash, and then writing a command to the NVMCTRL registers to actually write the page buffer to the flash.

 

Also, you can only write to Application flash from the boot section.

 

Does anyone know whether the application can write to the page buffer and then call code in the bootloader to do the NVMCTRL commands, or whether both are restricted to the bootloader section?

 

 

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

Surely the rule is simply about the SPM opcode?

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

I plan to explore many of these questions when I get my chip "HotWired".  Many of the data sheets are a little different between the tinys and megas and may shed some light on some of your questions. It helped me understand the UPDI instruction set a little better. A good person to ask is probably El Tangas. I'm curious myself to twist this chip's code to my advantage.

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

These chips don't have the SPM instruction.  It's all done through the NVM controller.

Letting the smoke out since 1978

 

 

 

 

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

Plexd wrote:
I plan to explore many of these questions when I get my chip "HotWired".
Might try the cases in the AVR simulator.

Reason : most of the NVM controller registers are present

Atmel Packs

 

"Dare to be naïve." - Buckminster Fuller

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

My reading of the datasheet would suggest that you can do what you propose provided that the bootloader does not set BOOTLOCK bit in the NVMCTRL.CTRLB register before passing control to the application:

Letting the smoke out since 1978

 

 

 

 

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

Well, I did some experiments...

 

  1. You can't issue nvmctrl.ctrla commands from the application section (well, you can do NoCmd)   This is expected.
  2. You also can't write to the flash alias addresses from the application section.  Doing so results in the nvmctrl.status showing a write error (even before you write to ctrla.)  (this was the main question.)
  3. So you have to call code in the bootloader section to write memory in the flash alias area of the address space.
  4. fortunately, this is more-or-less compatible with the parameters needed to write to older AVR flash; you have a command, an address, and a value.  One of the commands can be "copy from value to *address.  Easy Peasy.

 

optiboot_x has been updated to support this, and I've added a test_nvmctrl.ino sketch example to test it (actually, the sketch allows relatively free-form poking and peeking at memory as well.)