SPI error: "was not declared in this scope"

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

Hello,

 

I'm using Atmel studio 7 and I started a new project with the device Atmega324A.

All SPI registers are mentioned as undeclared. What is wrong?

The "iom324a.h" included in the dependencies folder.

In older project with Atmega1284 the same SPI library is working normally.

 

Thank you in advance.

 

 

 

Attachment(s): 

This topic has a solution.
Last Edited: Fri. Sep 25, 2020 - 10:49 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Where is your statement for #include <avr/io.h> ? By this, I mean, where, physically, is it within the code?

 

Jim

 

 

 

Until Black Lives Matter, we do not have "All Lives Matter"!

 

 

Last Edited: Thu. Sep 24, 2020 - 10:36 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

BTW screenshots are not the best way to distribute text.

Use copy and paste.

Also, .jpg is not the best for screenshots of text.

.png and .gif are both better for something with a small number of distinct colors.

Iluvatar is the better part of Valar.

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

Where is your statement for #include <avr/io.h> ? By this, I mean, where, physically, is it within the code?

 Yes #include <avr/io.h> has been included to the header file SPI.h

 

BTW screenshots are not the best way to distribute text.

Use copy and paste.

Also, .jpg is not the best for screenshots of text.

.png and .gif are both better for something with a small number of distinct colors.

Sorry but I can't copy paste the error list.

This is my initialization code:

void spi_init(unsigned char speed)
{
	SPI_DDR |= _BV(MOSI);
	SPI_DDR |= _BV(SCK);
	SPI_DDR |= _BV(SS);
	SPCR=(0<<SPIE) | (1<<SPE) | (0<<DORD) | (1<<MSTR) | (0<<CPOL) | (0<<CPHA) | (0<<SPR1) | (0<<SPR0);
	SPSR=(0<<SPI2X);
}

 

However in a new project with Atmega1284 the SPI library is compiling without issues.

According to the datasheet Atmega324 &1284 are using the same names for SPI registers.

I don't understand what is wrong.

 

 

Last Edited: Thu. Sep 24, 2020 - 11:34 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Is the SPI header file included in the SPI C file?

 

better post the content of both the header and Code file.

In this editor there is a symbol " <>"  click that and then add the code there in that way it is nicely formatted and a lot easier to read for us.

And also make separate blocks for the H and C file

Last Edited: Fri. Sep 25, 2020 - 05:23 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

kobogia wrote:

Sorry but I can't copy paste the error list.

Well you can; by selecting all the rows in the Error List (Ctrl-A works) - Right Click mouse and select copy (or Ctrl-C).

 

There is more detail however in the Output tab when "Showing output from: Build". Again Ctrl-A Ctrl-C works here also.

 

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

N.Winterbottom wrote:
There is more detail however in the Output tab

Absolutely!

And it will also be in the correct order;  unlike the 'Error List' - which will often disconnect messages that need to be read together. angry

 

For illustrated instructions on where to find the 'Output' window, see: https://www.avrfreaks.net/commen...

 

Please also take a look at my signature, below, for other tips on how to effectively use the forum - including how to put images into your post, and how to post source code ...

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Fri. Sep 25, 2020 - 08:20 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


kobogia wrote:
was not declared in this scope

Is is a fundamental rule of the 'C' programming language that an identifier (the name of a function, variable, etc) must be declared before it can be used.

 

So this message is telling you that you have broken that rule - you have used those things without having first declared them:

 

 

As the others have suggested, these are things that would normally be expected to be defined by #including the  avr/io.h header file.

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1


 

Just an idea but did you stop to look at the 324A datasheet? ...

 

 

So as a test:

 

 

yes, I get the same errors you saw but even as I was typing it:

 

 

Not only were all those symbols underlined in red (which means they weren't recognised) but the IDE was even offering to auto-complete and for something like DORD as soon as I typed DOR it auto-completed DORD0. So now when I fix things:

 

 

Not only has all the red underlining gone but this builds to completion without error.

 

What you learned today is that you MUST read the datasheet to know how to program these things!

 

PS: this is not unusual. In many later models Atmel added ordinal suffixes to register/bitnames to open the possibility (perhaps in a later derived model) of having more than one of these things so not just SPI but SPI0, SPI1, SPI2 etc.

Last Edited: Fri. Sep 25, 2020 - 08:33 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


 

kobogia wrote:
In older project with Atmega1284 the same SPI library is working normally.

clawson wrote:
In many later models Atmel added ordinal suffixes to register/bitnames to open the possibility (perhaps in a later derived model) of having more than one of these things so not just SPI but SPI0, SPI1, SPI2 etc.

Sure enough, the Atmega1284 datasheet shows register names with no suffix:

 

EDIT

 

and:

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Fri. Sep 25, 2020 - 08:51 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

 

kobogia wrote:
In older project with Atmega1284 the same SPI library is working normally

I guess if you want to keep using that old library unmodified, you could do something like this:

 

New chip:

 

Old chip:

 

The list of chip defines is here: https://www.nongnu.org/avr-libc/user-manual/using_tools.html

 

It might be more effective to use the 'Architecture' define than the specific chip define ?

 

This, of course, means that your library is still limited to only using the first SPI instance on chips which have more that one ...

 

 

EDIT

 

For completeness, this is what would happen in the above  if you tried to use a chip other than the 1284 or 324A:

 

#ErrorDirective

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Fri. Sep 25, 2020 - 09:25 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you for your replies.

I will check and revert.

 

 

Last Edited: Fri. Sep 25, 2020 - 09:53 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

angry

I was expecting something more complex... smiley

In my datasheet the registers are written without zero SPCR(0) and I didn't observe the "auto correction".

Thank you everybody for your replies and valuable advice.

 

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

If the question is now resolved, please see Tip #5:

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:
still limited to only using the first SPI instance on chips which have more that one

there don't actually seem to be any "classic" ATmega which do have more than one instance of the SPI controller?

 

so purely a "for future expansion" thing?

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I haven't looked at "packs" but in the "traditional" header installation I only see:

C:\Program Files (x86)\Atmel\Studio\7.0\toolchain\avr8\avr8-gnu-toolchain\avr\include\avr>grep SPCR0 * .h
iom164pa.h:#define SPCR0   _SFR_IO8(0x2C)
iom324a.h:#define SPCR0   _SFR_IO8(0x2C)
iom324p.h:#define SPCR0   _SFR_IO8(0x2C)
iom324pa.h:#define SPCR0 _SFR_IO8(0x2C)
grep: .h: No such file or directory

C:\Program Files (x86)\Atmel\Studio\7.0\toolchain\avr8\avr8-gnu-toolchain\avr\include\avr>

so 164 and 324 seem fairly unusual in this. Wonder why I'm not seeing it in 644 and 1284 - you'd have thought they'd be consistent?

 

In fact looking at recent Mega packs...

C:\Program Files (x86)\Atmel\Studio\7.0\packs\atmel\ATmega_DFP\1.4.346\atdf>grep SPCR0 *
ATmega164A.atdf:        <register caption="SPI Control Register" name="SPCR0" offset="0x4C" size="1">
ATmega164P.atdf:        <register caption="SPI Control Register" name="SPCR0" offset="0x4C" size="1">
ATmega164PA.atdf:        <register caption="SPI Control Register" name="SPCR0" offset="0x4C" size="1">
ATmega324A.atdf:        <register caption="SPI Control Register" name="SPCR0" offset="0x4C" size="1">
ATmega324P.atdf:        <register caption="SPI Control Register" name="SPCR0" offset="0x4C" size="1">
ATmega324PA.atdf:        <register caption="SPI Control Register" name="SPCR0" offset="0x4C" size="1">
ATmega324PB.atdf:        <register caption="SPI Control Register" name="SPCR0" offset="0x4C" size="1">
ATmega328PB.atdf:        <register caption="SPI Control Register" name="SPCR0" offset="0x4C" size="1">

So also a couple of "PB"s too - does 328PB really have more that one ?

 

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

clawson wrote:
does 328PB really have more that one ?

I think not

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Wouldn't it help with the speculation to repeat the search with spcr1?

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

I tried searching for "SPCR" - and only saw hits for SPRC0 and SPCR

 

I also looked at some datasheets for parts alleged to have "2 SPI", and none of them had a second SPI controller - I guess the 2nd "SPI" is either a mode of the USART or some other peripheral ?

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Fri. Sep 25, 2020 - 03:59 PM