Unable to set registers

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

Hello, first time here so apologies if I make posting mistakes.

I'm currently trying to program an Atmega1280 with an AVR Dragon (EDIT: using Atmel Studio 7).

When I'm debugging and stepping through the code some registers wouldn't get set even after stepping over code that should change them. 

So far I've found that this happens with SPCR and TIMSK0.

This happens in both when using a simulator and when debugging with the dragon. Can anyone tell me why this happens and how to fix it?

attached is a picture of the situation.

Attachment(s): 

This topic has a solution.

SO

Last Edited: Tue. Sep 26, 2017 - 01:02 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It makes life a whole lot easier if you embed the image in the post - so we can see it:

 

Instructions here: http://www.avrfreaks.net/comment...

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

 

The result of shifting zero by any amount is always zero.

 

Therefore, the end result of those 2 lines is simply to set SPSR to zero - which is exactly what you got.

 

The program is doing what you asked.

 

To clear a bit, you need:

 

SPSR &= ~(1<<SPI2X);

 

See: http://www.avrfreaks.net/forum/tut-c-bit-manipulation-aka-programming-101?skey=programming%20101 for a tutorial on bit manipulation.

 

#ClearBits #BitManipulation

Last Edited: Wed. Sep 20, 2017 - 07:21 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The question is about SPCR.
The cursor position is not yet executed.

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

Depending on the optimization, it may not be setting registers at that point yet.
What happens if you put a breakpoint outside the function?

  SPI_MasterInit ();
  _NOP (); // set break point

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

kabasan wrote:
The question is about SPCR

Oops so it is.

 

blush

 

But the comment about clearing a bit still applies; also kabasan's comment about optimisation.

 

Note also that all except bit 0 of SPSR are read-only - so it will not necessarily read-back what's written to it ...

 

 

 ATmega640/V-1280/V-1281/V-2560/V-2561/V Datasheet wrote:

The Power Reduction SPI bit, PRSPI, in “PRR0 – Power Reduction Register 0” on page 55 on page 50 must be written to zero to enable SPI module.

 

http://www.atmel.com/Images/Atme... - immediately above Figure 21-1 on page 190

Have you done that?

 

EDIT

 

page reference

Last Edited: Wed. Sep 20, 2017 - 07:35 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

SPSR is not a problem.
He does not want to clear SPI2X, he is writing a setting value of 0.

And the default of the PRR0 register is 0, it is not necessary to clear it unless it is intentionally set.

Of course there is a possibility that the questioner is operating PRR 0 and forgetting it.

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

From the picture just highlighting the fact that it says:

 

 

So this is not in the silicon but in the virtual world of the PC - could well be a fault in the simulator - likely suspect is that either the simulation dll for 1280 is faulty or the .adtf that describes the part is wrong in some way.

 

In the OP he said:

SleepyOak wrote:
with an AVR Dragon
which is curious - as clearly that is not what is being used to debug here.

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

SleepyOak wrote:
This happens in both when using a simulator and when debugging with the dragon. 

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

kabasan wrote:
He does not want to clear SPI2X, he is writing a setting value of 0.

So why has he written

SPSR = (0<<SPI2X);

and not just

SPSR = 0;

then?

 

It certainly looks like he has misunderstood the bit manipulation ...

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

It is not a solution of the issue, but it is also worth to mention, that there should be

SPCR = (1<<SPE)|(1<<MSTR)|(0<<SPR1)|(1<<SPR0)

instead of

SPCR = (1<<SPE)|(1<<MSTR)|(0<SPR1)|(1<<SPR0)

assuming, of course, that it is better than

SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR0)

 

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

MarekJ71 wrote:
instead of
Is this one of those "spot the difference" competitions? On the surface I cannot actually see how the two lines you quote differ?!?

 

(I used a diff program to compare them and it cannot see a difference either - so it must be well hidden!!)

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

Looks like a copy-and-paste error - he forgot to make the change in the "instead of" version ... ?

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

clawson wrote:
Is this one of those "spot the difference" competitions? On the surface I cannot actually see how the two lines you quote differ?!?

SPCR = (1<<SPE)|(1<<MSTR)|(0<SPR1)|(1<<SPR0)
                            ^
SPCR = (1<<SPE)|(1<<MSTR)|(0<<SPR1)|(1<<SPR0)
                            ^^

 

Stefan Ernst

Last Edited: Wed. Sep 20, 2017 - 08:58 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Oh rats - the diff actually showed that but my eyes missed it:

 

methinks I need new glasses or to change Meld to highlight diffs in a brighter colour!!

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

Thank you for all the responses

 

awneil wrote:

 ATmega640/V-1280/V-1281/V-2560/V-2561/V Datasheet wrote:

The Power Reduction SPI bit, PRSPI, in “PRR0 – Power Reduction Register 0” on page 55 on page 50 must be written to zero to enable SPI module.

 

http://www.atmel.com/Images/Atme... - immediately above Figure 21-1 on page 190

Have you done that?

 

 

I don't use the power reduction register at any point so it should be default 0, should I write 0 to PRSPI it to make sure?

 

kabasan wrote:

Depending on the optimization, it may not be setting registers at that point yet.
What happens if you put a breakpoint outside the function?

  SPI_MasterInit ();
  _NOP (); // set break point

 

clawson wrote:

In the OP he said:

SleepyOak wrote:
with an AVR Dragon
which is curious - as clearly that is not what is being used to debug here.

 

 

Here's the same situation when debugging with the dragon.

Added nop and breakpoint but it hasn't made a difference

 

awneil wrote:

kabasan wrote:
He does not want to clear SPI2X, he is writing a setting value of 0.

So why has he written

SPSR = (0<<SPI2X);

and not just

SPSR = 0;

then?

 

It certainly looks like he has misunderstood the bit manipulation ...

 

Ah that, it was originally 1<<SPI2X and I reflex changed it to zero instead of commenting it out

 

MarekJ71 wrote:

It is not a solution of the issue, but it is also worth to mention, that there should be

SPCR = (1<<SPE)|(1<<MSTR)|(0<<SPR1)|(1<<SPR0)

instead of

SPCR = (1<<SPE)|(1<<MSTR)|(0<SPR1)|(1<<SPR0)

assuming, of course, that it is better than

SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR0)

 

 

Nice spot, fixed it.

 

 

Could the problem be because of the software? should I try to reinstall atmel or perhaps use another computer?

SO

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

Here's the same problem but with a timer0 register

 

SO

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

So it looks like a faulty ADTF file then.

 

Just checking - this is .1417 and your DFPs are up to date ?

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

SleepyOak wrote:
Could the problem be because of the software? should I try to reinstall atmel or perhaps use another computer?

It is totally unlikely for me that this could help. Currently I have two suggestions:

 

1. Check what happens to SPCR with the code like this:

#include <what has to be included>

volatile int n;

int main ()
{
    SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR0);
    n = 1; // this line is only to allow you to enter here with debugger
}

2. Check what happens to SPCR with your current code built with all optimizations turned off.

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

clawson wrote:

So it looks like a faulty ADTF file then.

 

Just checking - this is .1417 and your DFPs are up to date ?

 

Yes version is 7.0.1417

 

How do I check if my DFPs are up to date? I only recently installed Atmel Studio

 

 

MarekJ71 wrote:

SleepyOak wrote:
Could the problem be because of the software? should I try to reinstall atmel or perhaps use another computer?

It is totally unlikely for me that this could help. Currently I have two suggestions:

 

1. Check what happens to SPCR with the code like this:

#include <what has to be included>

volatile int n;

int main ()
{
    SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR0);
    n = 1; // this line is only to allow you to enter here with debugger
}

2. Check what happens to SPCR with your current code built with all optimizations turned off.

 

Started a new project and wrote the code in, still doesn't appear to do anything even with optimizations off

 

 

SO

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

Does the code actually work on the chip?

 

What happens if you add code to read the register into a variable, then examine that variable in the debugger? or send it to the serial port?

 

Do you have access to another debugger; eg, AtmelICE ... ?

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

awneil wrote:

Does the code actually work on the chip?

 

What happens if you add code to read the register into a variable, then examine that variable in the debugger? or send it to the serial port?

 

Do you have access to another debugger; eg, AtmelICE ... ?

 

Ignoring the register problem and the parts of the code that need it, other parts of the code seems to work. Example it correctly sets the direction and level of pins.

 

By adding a variable to read did you mean something like this? It still reads as 0

 

I don't have any other debugger that's compatible with Atmega1280

SO

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

And what about PRSPI? What if you remove sysclk_init() and/or board_init() calls from main()?

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

Well, this is a bit embarrassing. seems PRSPI was the problem, the entire PRR0 seemed to have been the problem. Clearing it fixed everything

 

I just used the default board_init() and sysclk_init(), I guess inside one of those it set things to power reduction.

 

Thank you for all the help everyone, really appreciate it!

 

Is there some sort of Marked as Solved I need to do for this thread?

SO

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

I have a feeling that the 0x4C it shows as the address for SPCR is wrong. Looking at:

 

it is true that SPCR is at RAM address 0x4C but that is IO address 0x2C so I think the debugger maybe should be showing the contents of 0x2C not 0x4C, depending on whether (below 0x3F) it switches to IO addressing or not.

 

So I am guessing this probably is an ATDF problem. May have to wait for Morten or someone else from Atmel to come by to verify that suspicion.

Last Edited: Wed. Sep 20, 2017 - 01:10 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

SleepyOak wrote:
Is there some sort of Marked as Solved I need to do for this thread?

 

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

This is why "we" ask for complete programs.  Even when OP did the "minimal" it was not the complete suggested posted program.

 

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.