SPM - how many clocks to execute?

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

Hello.
My code collected some data and wrote it to the flash. I need to know how much time (clock cyles) it takes to execute the SPM instruction.

The purpose of that application was to measure some frequency, so it is essential for me to know the periods of time between the data...
I already searched the forum and found that you can't tell how much it lasts. How is that possible? 

(I am not sure if it is understandable, sorry for my english.)

This topic has a solution.

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You start a timer.   Issue the SPM.   Wait for SPMEN (SELFPRGEN) to clear.   Read the Timer.

 

Alternatively,   you wiggle a PORT pin.    Capture your wiggles with Logic Analyser or scope.

 

I think you will find that it takes about 5ms to write a page of Flash.     The 'other' SPM operations will be far quicker.

 

Untested.   You can do the measuring.

 

In practice,   you wait for SPMEN to be clear before issuing a SPM instruction.     This is more efficient than waiting to complete.     OTOH,   the 'completion' will give you the time that you wanted to know.

 

David.

Last Edited: Tue. Oct 14, 2014 - 04:59 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The purpose of that application was to measure some frequency, so it is essential for me to know the periods of time between the data...

???  Surely, you are measuring frequency with a mechanism that is independent of other AVR operations--input capture perhaps, or n cycles per a given time period.

 

Surely, you aren't trying to write an entire page of flash for each cycle of your frequency?!?

 

Tell more about this frequency range, how you are measuring, and what you are logging.  Tell also why the SPM time is important.

 

[To David:  You can say >>that<< again. ;) ]

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

theusch wrote:

The purpose of that application was to measure some frequency, so it is essential for me to know the periods of time between the data...

???  Surely, you are measuring frequency with a mechanism that is independent of other AVR operations--input capture perhaps, or n cycles per a given time period.

 

Surely, you aren't trying to write an entire page of flash for each cycle of your frequency?!?

 

Tell more about this frequency range, how you are measuring, and what you are logging.  Tell also why the SPM time is important.

 

[To David:  You can say >>that<< again. ;) ]

I have a feeling from your message that I'm doing some bullshit (I don't realize that yet) and you are going to tell me about it. :D

So I was just trying to get the frequency of a remote control. I have one IR diode attached to mega8. I poll the diode and save it's state to the flash.
The flash looks like "1,0,0,0...1". And I want to calculate how much time it takes between two "1"s. That should be 1/freq of the remote control.
And for that I need to know how much time takes the algorithm with polling and saving, including SPM.
 

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

Flash memory needs to start with 0xFFs.    And you can only program a whole page at a time.

Of course you can continue operation of the AVR while SPM is working (on Mega).    e.g. copying data to SRAM.

 

It is just like any other Flash device.    You start a program-page or erase-page and then get on with other tasks.    With luck,   the first SPM has completed before you start the next SPM.

 

This is why you check for EEPROM completion and SPM completion before you issue the next SPM.     With a well designed app,   you never need to wait!

 

Oh,   if you want to read the 38kHz of a IR diode,   you measure it with a Timer.

 

David.

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

Why in the world would you want to write every sample to flash?

 

If polling a pin's logic level, what does the time between two "1" samples tell you?  Next to nothing--that would be the time that the pin fell to logic 0 level until it rose to logic 1 level.  Surely you meant "the time between rising edges (0=>1 transitions)".

 

38kHz is about 30us.  If you have a 1us polling loop that would be about 30 samples for one period of the signal?

 

There are many better ways to determine a frequency with an AVR; nearly all involve a timer and nearly none involve cycle-counting.  And I'd wager that only your approach involved flash or SPM.

 

I assume that you are able to fire the signal for a period of time--perhaps holding down a button?  At least for a fraction of a second?  So you can set up a timer-as-counter and have it count the "clocks".  Then another timer generates a convenient time base such as 0.1 seconds.  Then every 0.1 seconds the counts represent frequency/10.

 

The other common method is to use ICP set for rising edge.  Each time it "hits", the timer count difference from the previous is the period on timer ticks.  From that the frequency can be calculated.  Perhaps several "hits" are gathered and averaged; two overflows without a hit is "no signal".

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

Yeah, yeah, as david.prentice mentioned the timer, I realized that saving it to flash is an absolutely retarded method.
Thanks for your help.