[SOLVED] How to use SPM instruction with ATmega328

Status
Not open for further replies.

Alan8947

Full Member level 4
Joined
Sep 21, 2016
Messages
215
Helped
9
Reputation
18
Reaction score
11
Trophy points
1,298
Activity points
3,746
Hi, I am still reading the datasheet of ATmega328. I am stuck on page erase, page write of program memory(flash) using instruction SPM. I want to find ASSEMBLY CODES performing each of those below.


I just want to see the assembly codes how to perform on the memory shown in (i) to (iv).

I know C code can do it transparent to all these. I just want to know how to use SPM instruction.

Thanks.

Alan
 

Hi,

in the instruction set manual there are assembler examples for using SPM.

****
Do you want to write your own bootloader, or why do you ask?
In either case: try to read bootloader source code.

Klaus
 

Hi,

in the instruction set manual there are assembler examples for using SPM.

****
Do you want to write your own bootloader, or why do you ask?
In either case: try to read bootloader source code.

Klaus
I don't want to write my own code, I just want to see example. Funny I don't see any code, the only one I saw is this.


1) How do they set up the address of page write into Z registers?
2) How do they set up address pointer to temporary location of the page to be written?
3) you see, they only wrote "SPM"? How does SPM know whether it's a page erase, page write or write ONLY ONE WORD, not a whole page?

This really doesn't help. I spent so much time looking and I cannot find info. In the data sheet, they talk so much in words, BUT not a single coding example except this one.

I know this is NOT important in real life coding, I just want to understand what the processor can and cannot do from the basic SPM assembly instruction.

Thanks
--- Updated ---

I know I should have moved on. After I got over the 3 separate bus systems, it was easy and smooth reading and understanding over 200 pages until this. I thought I could finish this in no time, this is about the last topic, last 30 pages or so.

I have been stuck for a while. I really want to understand the heart of the processor. I am the crazy kind that I actually used logic analyzer and scope to look at the instruction and all before. I used to be able to troubleshoot glitches in waveform by reading the program of FPGA. Those present so much intermittent problem in circuit using FPGA where programmer never know.

I just want to understand this one time through so I can move on.
 
Last edited:

Hi,

I refer to this document:
Atmel AVR Instruction Set Manual [OTHER]
Atmel-0856L-AVR-Instruction-Set-Manual_Other-11/2016

Page 169 f

Even the Atmeta328p Datasheet shows this:
25.2.5 Simple Assembly Code Example for a Boot Loader

Both sections are easy to find:
* Open the document in the PDF reader
* use the PDF reader´s search function for "SPM"
* step through the different "SPM" loactions

Klaus
 

Thanks Klaus

I went through all that many times. Can you just screen shot and post the few lines of codes for me. I really really cannot find it, I have all the datasheet, Assembly code and all that. I would not come here to ask stupid question like this if I have not spent days already. Just please post the screen shot of the assembly codes to do the few I asked.

Thanks
 

Hi,

I clearly referred to the according documents and page / section within the documents.
You still don´t see more code than that snippet of post#3.
And you can´t find the example code in section 25.2.5 of the ATMEGA328P datasheet.

Don´t get me wrong, I don´t want to be "your datasheet reader and copy and paste the according sections in the forum for you".

So let´s make a deal:
* Either I post the assembler code of the according documents and leave this thread
* or you post the screenshot of the according pages/sections (to show that there is no (more) code). And I will stay for a couple of posts.

Klaus
 
Hi Klaus

Your data sheet is different. But when you mentioned, I discover I was reading cpt 27 all along. I just discovered 26 is talking about the similar thing, I have to read it first.

I have been reverse engineering the program you mentioned. It's the SAME PROGRAM IN CPT26 AS CPT27. I print it out to read, somehow, I skipped cpt26 and start with 27. I might have missed something in 26. I'll read it first.

I have not been here for two days because I was reversing the program you talked about. I learn a lot, I have no question yet. Believe it or not, I spent time looking on line, this is THE ONLY PROGRAM THAT CAN ANSWER MY QUESTIONS. So I am working on it slowly. If I have question, I'll be back.

The assembly instruction datasheet on SPM is really bad, it never mention that SPM can do loops for whole page in some cases, or just write to SINGLE location all depends on how to programming the SPMCSR register. Combination of SPMEN(bit0) with other bits will make SPM perform completely different things. I wish the instruction datasheet would mention that.

Also, on page memory write ,It's clear Z registers are used to point to the program memory to be written. I yet to see any mention WHAT REGISTER USED TO POINT TO THE TEMPORARY PAGE. From the program, it used Y registers.

ALL my problems is in reading the data sheet. Just like from the beginning, I had a lot of problem getting the 3 separate buses, after that, I flew through like 200 pages fast. Now I am stuck with this and I have yet to find a decent explanation on SPM.

thanks, let me work on this, if I have question, I'll be back. I don't mean to have you read the datasheet. It is just BADLY WRITTEN. On top, most people don't get into this like me. call me strange, I just want more when I learn. Getting fast is not my goal, I want to know the MPU more. I have no plan to do assembly, but I still want to understand more.

thanks
--- Updated ---

EDIT
.
.
.
.
.
.
.
.
I went and read cpt26. It's for ATmega48 ONLY, it dose not help me in anything. It looked similar at a glance, but it's not. So I am back to the original situation. Too late to edit the last post as it won't allow me to do that.

I am going to work through the program and see.
 
Last edited:

Hi,
Your data sheet is different.
Or: your datasheet is different.
You are the thread starter, so if you refer to a document, you should provide a link to it, so we can discuss about the same source of information.

To get the most up to date information, I always recommend to use the latest version of a document, directly from the according manufacturer's site.

The assembly instruction datasheet on SPM is really bad,
SPM is an instruction with different features on different AVRs. Thus you can find "general" information in the AVR general instruction set....while you need to read the 328p datasheet for the features that belong to the 328p microcontroller.
(If you put all in one document you get very big documents ... and the documents are hard to maintain)

it never mention that SPM can do loops
It surely mentions that it can do a "page write". Indeed 48 times in the 328p datasheet.
It says: "The program memory is updated in a page by page fashion.".... and that's the only way! You can't update the program flash byte by byte, nor word by word.
But it's not a software loop. Still the programmer is free to write a software loop on any instruction.

The datasheet also says "This allows one page of program data to be programmed simultaneously". Indeed the word "simultaneously" tells it even is not a hardware loop. But for a programmer it does not matter whether it is a bytewise hardware loop, a wordwise hardware loop, or all data in a page simultaneously ... writing a page takes it's time, and that is what the programmer must care of.

Klaus
 

I am extracting the program one by one. This is page erase. Looks like SPM is looping itself to complete the whole page erase in one SPM execution.

I think I understand to set Z for starting memory address. Only thing is I am missing where to set the page size. Here is the program I extracted.


I did not see in the original program where it set the page size. Something SPM got to know to erase the correct amount.

thanks
 

I know for ATmega328, page size is 64 words or 128byte. But how to set it for SPM in Page Erase?

I did search of "page size". I saw all that. I saw page size of different chips. BUT in the program, HOW DOES THE PROGRAM KNOW WHAT CHIP I USE?

I went through all the places that have "page size", I don't see there is anything the set the page size for the program.

Actually if you look at the program you referred to, in the later part where SPM write one word at a time from RAM to Flash. It actually use r24 and r25 to tell the size of page in bytes.


Thanks
 
Last edited:

Hi,

There is an Atmega328p silicon chip. It includes:
* the program Flash .... with it's pages
* the SPM instruction for deleting a page

Why do you think you need to tell the SPM instruction what's the page size?

See it this way: Let there be 5 people, each with a purse with money. Each with a different amount of money.
Now tell each of these persons to empty their purse. I guess each single one knows what to do, without you telling them how much money they have in their purse.

Klaus
 

Do you mean when you do programming, you specify it's ATmega328, that would be good enough? That's all I need to know. Nothing on the datasheet said that it's automatic. I have a little suspicion, but I want to confirm.

So in this case with page erase, it only need to execute SPM instruction one time, the MPU will do the loop to write FFFFH into all 64 locations(128bytes) according to the start address in Z registers? I just want to confirm this.

Thanks
 

Hi,

You don't even have to specify it in assembler. The silicon chip "knows" that it's an ATMEGA328p. It has no reason to "think" it's a different microcontroller.

Assembler depends on the silicon chip. Every single assembler code directly relates to an machine instruction.

This is different with "C". C is machine independent. A C instruction is compiled to one or many machine instructions. Thus the compiler needs to know for what silicon chip it has to translate.

Nothing on the datasheet said that it's automatic
I don't understand your thinking. Every instruction is performed automatically.
If the instruction is:
* ADD two registers .... the microcontroller does exactly this. Automatically. Almost immediately
* Erase a page .... the microcontroller does exactly this. Automatically. It takes some time until this job is finished.
* send a byte via UART ... the microcontroller does exactly this. Automatically. It takes some time until this job is finished.
The UART send: It automatically checks whether there still are bits of the previous byte in the queue, then sends the START bit, then the 8 data bits, then the STOP bit. It may even calculate the parity bit and send it. All automatically. This may take thousands of clock cycles ... while the microcontroler can perform hundreds of other instructions the same time.

There is no need to state that a "UART send" will be performed automatically.
There is no need to state that a "page erase" will be performed automatically.
There is no need to state that any instruction is performed automatically.

So in this case with page erase, it only need to execute SPM instruction one time, the MPU will do the loop to write FFFFH into all 64 locations(128bytes) according to the start address in Z registers? I just want to confirm this.
Again: Why do you call it "loop", while the datasheet says it is performed simultaneously. The datasheet does not talk about a loop, it´s just you.
In the end it does not matter "how" it is done. You know "what" the instruction does, and you know "how long it takes", and you know how to "check the state". That´s all you need to know.

Did you ever care whether the bottles of a six pack of beer are filled simulataneously or one after the other?
It does not matter. The bottles need to be filled, that´s the task.

***
Now you read these lines in your browser. Did you care that these lines are transferred from the edaboard server to your computer in packets? Did you care about the packet size? Do you know that these packets usually are sent in ascending order ... but the packets may travel via different paths through the internet, so they may be received in random order? Some packets may even be received corrupt and thus requested a second time.
Does it matter? .. as long as the page is displayed correctly...

What if the ATMEGA328P silicon manufactured in 2015 does the "page erase" sequentially and the ATMEGA328P manufactured in 2020 does it simultaneously? As long as they "erase the page" and do this within the specified time...

Klaus
 

Hi Klaus


I thought about what you said about what do I care how the MPU do the job. respectfully, I disagree with you.

I know people try to separate hardware and software. Software people should treat hardware as a blackbox and you just trust what it said and don't worry about it.

As a hardware designer more and novice in in software or firmware. IT IS VERY IMPORTANT TO KNOW HOW THE HARDWARD PERFORM WITH A SOFTWARE COMMEND.

I said SPM perform in a loop to erase 64 words, IT HAS TO BE. memory is accessed by address, data bus AND with Rd and Wr signal. IT HAS TO DO IT ONE LOCATION AFTER THE OTHER. IT HAS TO BE A LOOP. From my years of experience, some issue happens when ONE SIDE DO NOT UNDERSTAND THE OTHER SIDE. This might not happen often, once in a while. BUT when that ever happen, it LIKELY takes a long time(if ever) to resolve the problem as both sides only think of it in their own boundary, not thinking cross boundary to the other side. That's the reason I spend my time learning the datasheet even though I HAVE NO INTENTION TO WRITE IN ASSEMBLY. Just to get the feel of how the MPU actually operate.

I can tell you my personal experience, Like FPGA. THE BIGGEST MISTAKE PEOPLE MAKE IS THE PROGRAMMER ACTUALLY THINK OF IT LIKE SOFTWARE WHEN WRITING THE VHDL OR AHDL. THAT'S WHERE IT BECOMES DEADLY. Most that wrote VHDL/AHDL do not think of the hardware, signal waveform timing. THEY DON’T THINK OF IT AS REAL TIME. They sometimes create GLITCHES that is NOT supposed to be. That can take a long time if ever to troubleshoot as neither side think outside the boundary, they stay in their own boundary. It takes someone that actually understands both sides to see that.

As a real example. I was retired 18yrs. 10yrs ago, I was in Gold’s Gym. I had a friend that was in very high position in Lockheed, He's a PhD, in charge of a whole design team. We were just casually talking about problems. I was telling him about my experience in trouble shooting FPGA that sometimes is very hard to spot. I was telling him how I look at VHDL as actually circuit and look at the timing. HE ACTUALLY OFFERED ME A JOB ON THE SPOT because he had problem with his project and cannot be resolved. I just laugh, I don't want a job!!! I could get a job any time if I want to.

Simple example, for a D-flipflop, Let reset signal /R=/A and /B. So as long as one input is high, there will be no reset. BUT what happen there is a propagation different between /A and /B, that /A going low before /B goes up. You create a LOW GOING GLITCH at the transition and reset the DFF. This kind of things happens in real life and I actually troubleshoot by reading the VHDL.

It’s very important to know both sides, that’s the reason I spent over a month slowly reading the ATmega328 datasheet even though EVERYONE told me it’s a waste of time.

Not just hardware/software. Even one has only hardware knowledge can run into all sort of problems. As circuit speed getting faster and faster, one cannot think of it as hardware logic only, it's transmission line, RF, cross talk etal. I was manager of EE, I actually LAYOUT some critical mixed signal pcb for my engineers because That involve RF, signal integrity knowledge to do the layout. Boundary gets blur in the big picture. You need someone that can see the big picture.

Thanks
 

I disagree with you.
I´m fine with this.

As a hardware designer more and novice in in software or firmware. IT IS VERY IMPORTANT TO KNOW HOW THE HARDWARD PERFORM WITH A SOFTWARE COMMEND.
No need to shout.
I´m a hardware designer, too. Mainly analog/digital measurement equippment using analog circuitry, ADC, DAC, PLDs, microcontroller...
I fully agree with "how it performs". But not down to the timing of each single signal line on the chip. You would have many thousands of datasheet pages (and respectfully, it´s you who already complains about the datasheet and how to find informations. It will be much more difficult if you had a much more bigger datasheet)
What´s important for the "PAGE ERASE" command:
* the mnemonic, parameters..
* that it erases a FLASH page
* how long it takes.
* how to check state

The chip designer is free to change the hardware (silicon) from one revision to the other in how the FLAS is being erased - as long as it keeps on datasheet specifications.

I said SPM perform in a loop to erase 64 words, IT HAS TO BE. memory is accessed by address, data bus AND with Rd and Wr signal.
This is your thinking like an external FLASH chip is wired to a microprocessor.
But AVR include all on one silicon chip. They are free to design an extra logic with extra wiring for a dedicated FLASH PAGE ERASE.
On the one side you expect the datasheet to give informations. But on the other side you refuse to accept that they wrote "a FLASH page is erased simultaneously" and follow your own ideas. If you don´t believe it - you don´t believe it. No one can make you believe it. Honestly - who´s problem is this?

You take the words right of my mouth!
Yes the programmers always think it is performed "sequentially" while it can (not must) be performed "simultaneously".
Do you see the analogy? But now it´s you who thinks it has to be done "sequentially".
But it´s hardware, it can be done simultaneously.

Be honest: How often did you decide to change parts of your FPGA code from a parallel to a sequential signal flow - or back?
To reduce gate count or to improve timing.

Klaus
 

Hi Klaus

I have not read your whole reply yet, I just want to say that I use capital letters not to shout. I just try to mean it's important points I like to make. I guess I should put it in bold instead of capital. Sorry.

I respect you and thanks for all the help.

Thanks
 

I cut out part of the program, I have a question on who is pointing at the temporary buffer for SPM to do page write into flash pointed by Z registers? you can see the question in RED on the part of the program.


Thanks
--- Updated ---

.
.
.
.
.
Also there's something I don't like about the program. This is the part where I question where's the pointer


In the ADIW instruction, Z registers points to the last element of the page. Instead of loading the starting address of the page, stupid program SUBTRACT page size from the Z register to get back to the initial value. This will make it so confusing and easily making mistake when people try to modify the program.

Just use LDI to reload the initial address of the page and would be much cleaner.

Also I don't get the BRNE comparing the value after the value of Z adding 2 with Loophi:looplo subtract 2.
 
Last edited:

no, with the ADIW instruction you are pointing to the next element, once you get to fill the last element of the page, the Z pointer will point to the next page (the first element at x00) so, to finalize the page write you need to point again to the previous page we just got out. the easiest way is to substract a pagesize to the pointer.
why you say? you can squeeze 2 bytes by only having the Zpointer for all the operations, so no need to have a Start_L and Start_H registers to keep track of the start of each page. (remember, you have many pages, but most of the time you'll write them sequentially)

who is pointing the the data buffer? Y registers(R28:R29), you can see loading from Y pointer (and increment Y+) to R0 and R1 to proceed to the writing
 

I understand the reason because after the SPM Z is pointing to the beginning of next page. So they subtract the page size to get back to the starting of the last page that was being written.

But my point is why not load back the starting address to Z like before writing operation. You guaranty you got it right? Why play a trick of subtracting the page size to get back the original address?

Problem is when someone later on come in, accidentally add some lines that change the Z, the program will be screwed because it only subtract the page size. Loading the original address to Z will avoid all the potential problem.


I believe in doing it in the cleanest way. might not look "wise", but cleanest and safest.

Notice a lot of the new products involve firmware getting more buggy in the last few years? Be it cars, printers, satellite tv boxes or any electronics runs slower, constantly need to turn off power to restart, doing funny things? I went through at least 5 all-in-one printers in less than 5yrs, my 2018 car was in the dealer over a month in the first 5 months and still cannot resolve the computer problems. My 2014 of same brand never been to dealer to fix anything. I thought the Direct TV box was buggy.....until I switched to Dishnet. How many times I had to unplug the AC cord and let it reset? Things just got slower, intermittent and don't last. My 3yrs old 65" tv is doing funny things also.

I blame this on firmware/software. That people stay within their boundary and treat other's like a black box. Nobody understand the other part. I just hope people put in the effort to just make it WORK.
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…