use C++ in Atmel Studio

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

Hi.

 

I would like to use C++ in Atmel Studio, so I write code below:

 

//******Main.CPP*******

#define F_CPU 32000000UL

#include "Settings.h"
#include "ILI9225G.h"

int main(void)
{
    Settings st;
    st.Config32MHzClock();
    InitLCDPins();
    while(1)
    {
    }
}

 

//******Settings.h********

#ifndef __SETTINGS_H__
#define __SETTINGS_H__


class Settings
{
//variables
public:
protected:
private:

//functions
public:
    Settings();
    ~Settings();
    void Config32MHzClock();
protected:
private:
    Settings( const Settings &c );
    Settings& operator=( const Settings &c );

}; //Settings

#endif //__SETTINGS_H__

 

 

//***** Settings.cpp******

#include "avr_compiler.h"
#include "Settings.h"

// default constructor
Settings::Settings()
{
} //Settings

// default destructor
Settings::~Settings()
{
} //~Settings

void Settings::Config32MHzClock()
{
    CCP = CCP_IOREG_gc; //Security Signature to modify clock
    // initialize clock source to be 32MHz internal oscillator (no PLL)
    OSC.CTRL = OSC_RC32MEN_bm; // enable internal 32MHz oscillator
    while(!(OSC.STATUS & OSC_RC32MRDY_bm)); // wait for oscillator ready
    CCP = CCP_IOREG_gc; //Security Signature to modify clock
    CLK.CTRL = CLK_SCLKSEL_RC32M_gc; //select sysclock 32MHz osc
}

 

#ifndef ILI9225G_H_
#define ILI9225G_H_

#include "avr_compiler.h"

#define LCD_DATA_PORT  PORTH
#define LCD_CMD_PORT   PORTJ
   
void InitLCDPins()
{
	LCD_DATA_PORT.DIR=0xFF;
	LCD_CMD_PORT.DIR=0xFF;
	
	LCD_CMD_PORT.OUT=0xFF;
	LCD_DATA_PORT.OUT=0xFF;
}
#endif /* ILI9225G_H_ */

 

#ifndef COMPILER_AVR_H
#define COMPILER_AVR_H

#ifndef F_CPU
/*! \brief Define default CPU frequency, if this is not already defined. */
#define F_CPU 2000000UL
#endif

#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>

uint8_t volatile saved_sreg;
/*! \brief This macro will protect the following code from interrupts. */
#define AVR_ENTER_CRITICAL_REGION( ) saved_sreg = SREG; \
                                     cli();

/*! \brief This macro must always be used in conjunction with AVR_ENTER_CRITICAL_REGION
 *        so the interrupts are enabled again.
 */
#define AVR_LEAVE_CRITICAL_REGION( ) SREG = saved_sreg;

#if defined( __ICCAVR__ )

#include <inavr.h>
#include <ioavr.h>
#include <intrinsics.h>
#include <pgmspace.h>

#ifndef __HAS_ELPM__
#define _MEMATTR  __flash
#else /* __HAS_ELPM__ */
#define _MEMATTR  __farflash
#endif /* __HAS_ELPM__ */

/*! \brief Perform a delay of \c us microseconds.
 *
 *  The macro F_CPU is supposed to be defined to a constant defining the CPU
 *  clock frequency (in Hertz).
 *
 *  The maximal possible delay is 262.14 ms / F_CPU in MHz.
 *
 *  \note For the IAR compiler, currently F_CPU must be a
 *        multiple of 1000000UL (1 MHz).
 */
#define delay_us( us )   ( __delay_cycles( ( F_CPU / 1000000UL ) * ( us ) ) )

/*! \brief Preprocessor magic.
 *
 *  Some preprocessor magic to allow for a header file abstraction of
 *  interrupt service routine declarations for the IAR compiler.  This
 *  requires the use of the C99 _Pragma() directive (rather than the
 *  old #pragma one that could not be used as a macro replacement), as
 *  well as two different levels of preprocessor concetanations in
 *  order to do both, assign the correct interrupt vector name, as well
 *  as construct a unique function name for the ISR.
 *
 *  \note Do *NOT* try to reorder the macros below, as this will only
 *        work in the given order.
 */
#define PRAGMA(x) _Pragma( #x )
#define ISR(vec) PRAGMA( vector=vec ) __interrupt void handler_##vec(void)
#define sei( ) (__enable_interrupt( ))
#define cli( ) (__disable_interrupt( ))

/*! \brief Define the no operation macro. */
#define nop( ) (__no_operation())

/*! \brief Define the watchdog reset macro. */
#define watchdog_reset( ) (__watchdog_reset( ))


#define INLINE PRAGMA( inline=forced ) static

#define FLASH_DECLARE(x) _MEMATTR x
#define FLASH_STRING(x) ((_MEMATTR const char *)(x))
#define FLASH_STRING_T  char const _MEMATTR *
#define FLASH_BYTE_ARRAY_T uint8_t const _MEMATTR *
#define PGM_READ_BYTE(x) *(x)
#define PGM_READ_WORD(x) *(x)

#define SHORTENUM /**/

#elif defined( __GNUC__ )

#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <util/delay.h>

/*! \brief Define the delay_us macro for GCC. */
#define delay_us( us )   (_delay_us( us ))

#define INLINE static inline

/*! \brief Define the no operation macro. */
#define nop()   do { __asm__ __volatile__ ("nop"); } while (0)

#define MAIN_TASK_PROLOGUE int


#define MAIN_TASK_EPILOGUE() return -1;

#define SHORTENUM __attribute__ ((packed))

#else
#error Compiler not supported.
#endif

#endif

Atmel Studio show this error:

"Error    1  multiple definition of `saved_sreg'  " 

which saved_sreg is used in avr_compiler.h

 

could you please guide me how can I fix this error?

This topic has a solution.
Last Edited: Wed. Feb 25, 2015 - 09:33 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Who created avr_compiler.h? Clearly the problem is that it contains a definition rather than just declarations.

 

BTW I edited your post to put the code into code sections. Please use the "<>" icon on the editor toolbar to do this yourself in future.

Last Edited: Wed. Feb 25, 2015 - 09:20 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:

Who created avr_compiler.h? Clearly the problem is that it contains a definition rather than just declarations.

 

BTW I edited your post to put the code into code sections. Please use the "<>" icon on the editor toolbar to do this yourself in future.

 

Thank you for fixing my post and I add avr_compiler.h codes.

 

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

But there is no "saved_sreg" mentioned in that file?

 

Can you grep all the files in the project and find out where "saved_sreg" is being defined? Presumably it's in a .h file that is being included multiple times?

 

Just from the name it sounds like something to do with atomic access protection.

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

clawson wrote:

But there is no "saved_sreg" mentioned in that file?

 

Can you grep all the files in the project and find out where "saved_sreg" is being defined? Presumably it's in a .h file that is being included multiple times?

 

Just from the name it sounds like something to do with atomic access protection.

 

It is in avr-compiler.h just in the first of that:

#ifndef COMPILER_AVR_H
#define COMPILER_AVR_H

#ifndef F_CPU
/*! \brief Define default CPU frequency, if this is not already defined. */
#define F_CPU 2000000UL
#endif

#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>

uint8_t volatile saved_sreg;
/*! \brief This macro will protect the following code from interrupts. */
#define AVR_ENTER_CRITICAL_REGION( ) saved_sreg = SREG; \
                                     cli();

/*! \brief This macro must always be used in conjunction with AVR_ENTER_CRITICAL_REGION
 *        so the interrupts are enabled again.
 */
#define AVR_LEAVE_CRITICAL_REGION( ) SREG = saved_sreg;

 

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

Oh I see. Then again I ask - who wrote this? Putting variables in .h is a recipe for exactly the kind of multiply defined error you are seeing.

 

If you need atomic protection in AVR code you are far better off using <util/atomic.h> which keeps all such saved state variables local within the generated code.

 

EDIT: when I try to locate the source of that file I hit this...

 

http://spaces.atmel.com/gf/proje...

 

That shows two things:

 

1) it appears to come from Atmel themselves

 

2) more importantly perhaps the version I found appears to "fix" this issue:

/*! \brief This macro will protect the following code from interrupts. */
#define AVR_ENTER_CRITICAL_REGION( ) uint8_t volatile saved_sreg = SREG; \
                                     cli();

Notice how that now creates the saved_sreg at the point of invocation, not globally.

 

So I'd suggest the copy of avr_compiler.h you are using is out of date.

 

BTW when I look at you Settings.* files that include the header I am having a hard time spotting exactly WHY you are including the file anyway - what is it from avr_compiler.h that you are using anyway? I cannot help thinking you are using it simply as an alternative to the slightly more obvious:

#include <avr/io.h>

 

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

clawson wrote:

 

1) it appears to come from Atmel themselves

 

2) more importantly perhaps the version I found appears to "fix" this issue:

/*! \brief This macro will protect the following code from interrupts. */
#define AVR_ENTER_CRITICAL_REGION( ) uint8_t volatile saved_sreg = SREG; \
                                     cli();

Notice how that now creates the saved_sreg at the point of invocation, not globally.

 

So I'd suggest the copy of avr_compiler.h you are using is out of date.

 

BTW when I look at you Settings.* files that include the header I am having a hard time spotting exactly WHY you are including the file anyway - what is it from avr_compiler.h that you are using anyway? I cannot help thinking you are using it simply as an alternative to the slightly more obvious:

#include <avr/io.h>

 

 

yes the avr-compiler.h file which I  used in my application was created by atmel and now I changed that with this:

http://spaces.atmel.com/gf/project/asf/scmgit/?p=asf;a=blob_plain;f=thirdparty/qtouch/generic/avr8/qtouch/examples/example_qt_qt600/avr_compiler.h;hb=HEAD

 

and every thing works fine :)

 

thank you for helping me :)

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

While that fixes it, it's not the "right fix". If the avr_compiler.h you had is an old one with a bug what else do you have from the same source that is also old, buggy and out-dated? Also nothing says the one I found is "newer" anyway. I would go back to your own source of avr_compiler.h (ASF?) and see if there is an updated version of the entire thing available so you can benefit from any other problems that might have been fixed.