0
\$\begingroup\$

I have been studying this for ATmega328, I first want to verify I am correct in the following. And then I have question on page write and page erase.

  1. ATmega328 has 32K byte flash = 16K words. It is organized as 256Pages with 64Words/pages.

  2. So you need 6 address bits for each page(64Words/page), You need 8 address bits to address 256Page.

  3. For ATmega328, you don’t need RAMPZ register. Use Z(6..1) as WORD address within each page. Use Z(14..7) as PAGE address. Z15 and Z0 NOT USED.

I have question about SPM instruction. It supposedly can write ONE WORD or ONE PAGE. It can ERASE ONE PAGE. I really don’t know how it works. I’ve been searching and I cannot find answer.

enter image description here

In the copy above. Can someone post a few lines of code(IN ASSEMBLY LANGUAGE) performing in each case above using SPM instruction? I just want to see the actual code how SPM in action, what is needed to set up to use the SPM instruction.

I have been searching around with no luck.

\$\endgroup\$
5
  • \$\begingroup\$ From where you have searched? The datasheet has a chapter called "Self-Programming the Flash". Have you seen it? \$\endgroup\$
    – Justme
    Commented Aug 20, 2023 at 8:27
  • \$\begingroup\$ I've seen it, they are describing with words saying it can perform page erase. BUT I just want a few lines of code. I am not even 100% that the SPM can repeat itself to put FFFFH into all 64 words in a page. Or do I have to write a loop to write, then increment, then write again. Just show me a few lines of code. thanks. \$\endgroup\$
    – Alan0354
    Commented Aug 20, 2023 at 9:17
  • \$\begingroup\$ Arduino boot loader codes should contain such solutions. I think the sources are public available. \$\endgroup\$
    – Jens
    Commented Aug 20, 2023 at 11:05
  • \$\begingroup\$ I looked very hard for it. All I see is they talk a lot, all the precautions, but when comes to code, very few good example. I bet one page of coding using SPM in the few cases listed in my post will CLEAR everything up. Like how do you set up Z register for single or page write, how do you increment Z and loop 64 times to write a page. AND who points to the temporary page to write to where Z register point to. I spent a lot of time reading, they just TALK. I bet 15 to 20 lines of codes to show all the cases above( 5 lines each) will clarify a lot for me. Thanks. \$\endgroup\$
    – Alan0354
    Commented Aug 20, 2023 at 18:24
  • \$\begingroup\$ I edited the original post and simplify my question. I just want to see the lines of codes using SPM for each case like single word write vs page write, how to set up to use SPM in each case. \$\endgroup\$
    – Alan0354
    Commented Aug 20, 2023 at 18:42

1 Answer 1

1
\$\begingroup\$

You will find such resources from the manufacturer:
https://www.microchip.com/en-us/product/atmega328p

In particular,
https://ww1.microchip.com/downloads/en/Appnotes/Atmel-2575-C-Functions-for-Reading-and-Writing-to-Flash-Memory_ApplicationNote_AVR106.pdf
explains the process and example code (in C).

For some reason the code sample doesn't seem to be included in the documents list; it can still be found on a site search:
https://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-2575-C-Functions-for-Reading-and-Writing-to-Flash-Memory_ApplicationNote_AVR106.zip

I haven't looked at the code samples but they're either using library functions (no need to look at the asm, it's pre-cooked for you), or inline asm; or, if nothing else, you can view the output (with debug symbols included, such luxury!) in the included *.lss file. Or, take code samples and plug them into Compiler Explorer, and observe the asm output, or build your own projects and inspect the *.lss from there (or use binutils to explore the object or binary in various ways).

Also, the instruction set manual itself,
https://ww1.microchip.com/downloads/en/DeviceDoc/AVR-InstructionSet-Manual-DS40002198.pdf
provides ASM on p.134.

\$\endgroup\$
9
  • \$\begingroup\$ Thanks Tim. I am not trying to write in assembly, but I just want to understand the SPM instruction. I know I will use C++ type of coding in the future, I just want to understand how SPM instruction in assembly works. I just need a few lines of assembly codes. \$\endgroup\$
    – Alan0354
    Commented Aug 20, 2023 at 19:09
  • \$\begingroup\$ Were you able to download the ZIP? \$\endgroup\$ Commented Aug 20, 2023 at 19:13
  • \$\begingroup\$ What zip? I open all your links I can open. \$\endgroup\$
    – Alan0354
    Commented Aug 20, 2023 at 20:20
  • 2
    \$\begingroup\$ Honestly, I don't know man. I don't think there's anything missing from what I've linked, they contain the knowledge you seek. I could paste in likely sections from the file myself, but honestly if you can't navigate your way around these documents, what hope do you have to develop on these platforms, but to forever ask others for assistance? \$\endgroup\$ Commented Aug 20, 2023 at 21:55
  • 1
    \$\begingroup\$ @Alan0354 But if you want to code in C/C++, for example avr-libc comes with C functions (or macros) to allow you to erase and write flash pages so do you really need to learn how SPM works, or can you just read the avr-libc code to learn how it works? Any other modern compiler should come with similar feature. \$\endgroup\$
    – Justme
    Commented Aug 21, 2023 at 6:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.