Arduino ports registers in Eclipse are not working - c++

I am building a Arduino application in C with Eclipse and the Arduino plugin. In my old code I used pinMode and digitalWrite. But as you all know, this uses more space. So I am re-building my code now with port manipulation. If you don't know what that is, you can see it here: http://www.arduino.cc/en/Reference/PortManipulation
I will explain what I did.
Where there stands pinMode, I changed it to something like this: DDRD = 0b11111111;
And where there stands digitalWrite, I changed it to PORTD = 0b10000000;
You can see it in my code below.
Eclipse is now giving me the error (highlighting the words DDRD and PORTD with a red line) of symbol not resolved for DDRD and PORTD, but the program is building and running normal. How do I solve this?
#include <avr/io.h>
#include <util/delay.h>
int main()
{
UCSR0B = 0; // disconnect pins 0 and 1 from USART (Serial)
DDRD = 0b11111111; // all pins of port D as output
for(;;)
{
PORTD = 0b10000000; // Pin 7 on
_delay_ms(500); // Wait
}
}

These are multi-level macros which encode direct volatile access to the SFR locations.
They are individually defined in one of an assortment of chip-specific header files which avr/io.h will include when it is informed of the specific CPU variant you are using.
Normally this is done with the -mmcu flag to avr-gcc, for example
-mmcu=atmega328p
However, if the Eclipse plugin does it's own pass through the project sources to try to give you advisory errors, it may not be smart enough to turn that into a define (you can get Eclipse claiming errors even when gcc is happy). To work around that, you may need to explicitly define the cpu type above the include in your code, or in some configuration for Eclipse. For example:
#ifndef __AVR_ATmega328P__
#define __AVR_ATmega328P__
#endif
#include <avr/io.h>
Note that this may cause problems if you later change the processor type! And even as is, it's a little iffy as there are two potential names for each processor.

I have faced the same problem. This is what solved for me in Eclipse.
From project explorer right click on the active project > Index > Rebuild

Related

exit status 1 expected primary-expression before '.' token

I'm trying to get the temperature and humidity every 60 seconds, but my code won't work. Whenever I compile it, there's this error "expected primary-expression before '.' token"
This line gets highlighted.
Serial.print(DHT.humidity,0);//prints humidity in serial
Here's my entire code:
#include <DHT_U.h>
#define DHT12PIN 7// sets pin 7 for DHT11 signal connection
void setup(){
Serial.begin(9600);//opens serial
}
void loop()
{
int chk = DHT.read12(DHT12PIN);//reads DHT12
Serial.print(DHT.temperature,0);//prints temp in serial
Serial.print(",");//pints comma in serial
Serial.print(DHT.humidity,0);//prints humidity in serial
Serial.println(); //carraiage return
delay(2000);//wait 2 seconsds
}
Wow there was a lot to unpack in this question.
I have used the DHT library by Adafruit a lot of times and thought that you just ended up using the class name instead of instantiating a variable and that's why you were facing the issue.
But then I realized that you are using the following function which is not a part of the Adafruit library:
int chk = dht.read12(DHT12PIN);//reads DHT12
So, I did a bit of digging on the internet and realized that someone has made a library for DHT with that specific function. (A lot of libraries to be specific)
Based on my analysis of this library, and the example code that was given, you have 3 issues.
You have included the wrong header file. The header file dht.h from RobTillaart's library should replace the header file DHT_U.h of Adafruit's library, in your code.
You have to instantiate the variable named DHT of the dht class. This can be done as follows, above your setup function
dht DHT;
Given that you had the code and the header files mixed up from TWO DIFFERENT LIBRARIES, I am guessing that you have installed the Adafruit library, in the place of RobTillaart's library. To fix this, you will have to remove the DHT-sensor-library-1.3.4 from your Documents/Arduino/libraries, create a new folder named DHTStable in it's place, and place all of the files listed here in the new folder.
Some words of experience-based wisdom, don't use RobTillaart's library, I can tell from a glance of it's folder structure that you will face more problems then you can solve if you use it. Instead use Adafruit's library, with their example. You will also have to install Adafruit's sensor library, but it is definitely worth it.

Debugging C++ queues in an IDE

I want to debug this code segment:
#include <iostream>
#include <queue>
#include <random>
#include <time.h>
using namespace std;
int main()
{
cout << "Hello, World!" << endl;
queue<int> q;
srand(time(NULL));
for(int i = 0; i < 10; i++)
{
q.push(rand() % 100);
}
int a = q.front();
q.pop();
int b = q.front();
q.pop();
cout << "a: " << a << ", b: " << b << endl;
return 0;
}
I tried to debug it on 2 IDEs - CLion (my personal favorite) and VS2015. However, both of those did not show the queue items (as they would if I'd use an array, for instance):
CLion
VS2015
As I continued investigating, I noticed that if I remove the upper breakpoint in CLion, it does show the queue elements:
CLion - good version
Any ideas about why does it happen, and if there's a way to see the queue elements in the "bad" cases?
Removing the upper break point and switching between 32 and 64 bit compilations won't affect this. The 32/64 bits have to do with the generated assembly code. Once the code compiles correctly, the 32 and 64 bit assembly codes won't be the same but the program itself will still retain equivalent functionality. That is, 64 bit programs can't "do more" than 32 big programs. This is an ultra watered-down definition of Turing-completeness, but the upshot here is that it doesn't matter for the purposes of what you're trying to do right now whether you set your build target to 32 or 64 bits.
The IDE that you use will have a minimal effect here though, because they use different Debuggers. Since both debuggers did the same thing in your case, I'd say we can safely chalk it up to user error (see below), but like I said in my afterword, if you will, keep working with the debugger. It's an absolutely essential skill to master. Props to you for getting started early.
As for your debug problem, here is my debug of your program. Notice the breakpoints I used. Like Jesus Christ said before me, the debug worked correctly for both of us. The usual suspect in these cases is trying to debug the release build. When you compile a debug build, the compiler does not perform as many optimizations to allow you to trace your code through the variables and see exactly what's going on. Once your code functions correctly, you can switch to the release version and your compiler will optimize away a lot of the variables for maximum performance.
If you did debug under the Debug build as you said, then I'd say just chalk it up to debugger error. If you're a C++ newbie, there's a chance you just might not be experienced enough to navigate the intricacies of the debugger. No disrespect intended, but debugging is just as much an art as it is a science, and a new developer would not be faulted for not knowing how exactly to maneuver the tool.
I found a solution for the CLion problem - simply press on the variable in the variables tab, then press on Duplicate Watch (marked in red circle bellow, hotkey: ctrl+D) and you're done:
Did you compile with debug selected in the project?
I can clearly see queue values.
Make sure that
1) you are debugging Debug build (that is debug information is present and no optimization is done)
Project Properties -> C/C++ -> General -> Debug information format is set to "Program Database"
Project Properties -> Linker -> Generate Debug info is set to "/DEBUG")
2) raw structure mode is disabled
Tools -> Options -> Debugging -> Show raw structure of objects is not checked

C++ FatFs undefined reference to functions

I have a code written in Atmel Studio to read/write data from a SD card. I am using FatFs here. My problem is the code doesn't compile when I use some of the functions (f_chdir, f_getcwd...) in FatFs. Some functions works fine (f_puts, f_open, f_mount, f_mkdir...). All these functions are located in same header file (ff.h, ff.c)
The error says "undefined reference to -function-, ld returned 1 exit status". When I go to the error it shows the end of the code while it is suppose to show where the error is.
I cannot understand the problem with my code. Any help is appreciated.
Just ran into this using the SD card for a project using SAM4S Xplained Pro (Atmel 7, ASF 3.20).
Make sure you have all the asf projects (fatfs, sd_mmc, memory access control, and then the other basics e.g. pmc, gpio, and maybe a few more). My asf did NOT include sd_mmc_mem.c and sd_mmc_mem.h for some reason, so I had to include those myself. Also remember to do the sd_mmc_init() at the top of your main loop. As for the configuration...
If you look closely at ffconf.h, the first thing it does is include conf_fatfs.h, which (wait for it!) is EXACTLY the same file line by line as ffconf.h. All the variables are defined there first and foremost (and guarded by an #ifndef FFCONF and NOT an CONF_FATFS) aka that's where it counts..
Go into conf_fatfs.h and change _USE_STRFUNC to 1 or 2 et voila.
Also note that in the places where you use it, you'll have to #include the ff.h preceded by either ffconf.h or conf_access.h
ASF is a real snake pit if you don't know what you're looking for.
Enjoy.
By default, the memory control access interface is disabled in the ASF wizard. In order to enable the memory control access, please follow the steps below.
Open the ASF wizard (Alt + W).
Enable the Memory Control Access as follows.
ASF SD sd_mmc_mem.h memory access enable
Finally, click the “Apply” option to make the changes.
This adds sd_mmc_mem.h /.c files
Open the ffconf.h in your favorite editor and set _FS_RPATH to 2. From ffconf.h:
#define _FS_RPATH 0
/* This option configures relative path feature.
/
/ 0: Disable relative path feature and remove related functions.
/ 1: Enable relative path feature. f_chdir() and f_chdrive() are available.
/ 2: f_getcwd() function is available in addition to 1.
/
/ Note that directory items read via f_readdir() are affected by this option. */
Which features of the fatfs library are included in your build is configurable, so that you don't have to lose valuable ROM space (as well as a few bytes of RAM) for functions you're not using.
For versions of the FatFS library prior to 0.8a, _FS_RPATH supports only values 0 and 1; f_getcwd is not available in these versions.
Additionally, in versions prior to 0.8, it is necessary for C++ code to explicitly include its headers as C headers to avoid name mangling:
extern "C" {
#include "ff.h"
}
From version 0.8 onwards, it does this internally. You can find the new versions here if you're still working with an old one -- the comment you left leads me to believe that this might be the case.
Check if _FS_MINIMZE in ffconf.h is 0 to have all functions available.
In my version that I downloaded from elm-chan it was by default set to 3 and lead to the compiler error: undefined reference.
In file ffconf.h, set #define _USE_FIND to 1.
/* This option switches filtered directory read functions, f_findfirst() and
/ f_findnext(). (0:Disable, 1:Enable 2:Enable with matching altname[] too) */
I needed to use f_findfirst() and f_findnext() functions and i was getting undefined reference errors.
Now this solved my problem.
Drive/directory handling functions are under #if _FS_RPATH >= 1 (or similar preprocessors) .

Assigning value to float data type crashes program

I'm losing my mind here. Please someone help me understand what is going on.
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <windows.h>
int main(int argc, char *argv[])
{
float test;
printf("You see me\n");
test = 3;
printf("Wont get here\n");
return(0);
}
You see me is printed out then the app crashes before the Wont get here is printed.
Important to note that this compiles and runs fine on my system, but when this exe is transferred to a 32 bit, Windows XP machine it crashes.
Ints, Bools, char data formats work fine, but when I try to use floats/doubles the app just crashes with no error.
Am I not compiling this correctly in Visual Studio Express 2013 in some way that anyone can think of? Should I check myself into the local loony ward?
Okay found the issue with the help of a colleague.
The windows machine has an older processor, Geode Integrated Processor.
Found the answer here: http://msdn.microsoft.com/en-us/library/7t5yh4fd.aspx
Open the Property Pages dialog box for the project.
Select the C/C++ folder.
Select the Code Generation property page.
Modify the Enable Enhanced Instruction Set property.
In my case I needed to change this to /arch:IA32. Bam! Works! Thank you all for the brainstorming session.

Eclipse Invalid arguments error when using gstreamer

Ok, so I want to use gstreamer library.
1. Situation
I have some code:
#include <gst/gstpipeline.h>
#include <gst/gst.h>
...
GstElement* pipe = gst_pipeline_new("PipeName");
Where gst_pipeline_new is declared in gstpipeline.h:
GstElement* gst_pipeline_new (const gchar* name) G_GNUC_MALLOC;
where non obvious "things" :) are defined somewhere in the system:
typedef struct _GstElement GstElement; // gstelement.h
typedef char gchar; // gtypes.h
#define G_GNUC_MALLOC __attribute__((__malloc__)) // gmacros.h
2. Problem
Since I use make for building I have no errors during compilation and linking. Program itself runs OK as well. However...
In Eclipse IDE I have the following error:
Description Resource Path Location Type
Invalid arguments '
Candidates are:
_GstElement * gst_pipeline_new(const ? *)
' file.cc /path/to/file line 106 Semantic Error
I added all include directories which are specified in Makefile to eclipse project configuration (Project->Properties->C/C++ General->Paths and Symbols->Includes->C++). Of course it's a C++ project.
3. Question
How to get rid of that Eclipse error? I have no clue how to do this... And it drives me mad since now I use some legacy code and I have around 100 errors like this one.
So far I've tried:
casting either by reinterpret_cast<>() or C-like casting to const gchar*
adding typedef char gchar at the beginning of the file - before any other include!
including gtypes.h (gchar is defined there) - also before any other include
redeclaring `_GstElement gst_pipeline_new(const gchar* name)'
Nither of those helped...
To me it looks like Eclipse does not see the gchar type since it says that the candidate is _GstElement * gst_pipeline_new(const ? *) Where ? substitutes the real type. But I have no idea how to make (or event force :)) Eclipse to see it...
Most probably eclipse just doesn't know about your include paths (for this specific library) and complains about the unindexed types and declarations.
You can add them under 'Project->Properties->C++ General->Paths and Symbols'
If this doesn't help, you can also switch off semantic error checking (see Code Analysis), either in whole or for particular error types.
As g-maulik suggested, It seems that it was really an indexer problem. After increasing the indexer cache limits everything works fine.
Go to Window->Preferences->C/C++->Indexer tab cache limits and increase (might be machine dependent):
Index Database cache:
Limit relative to the maximum heap size: 15%
Absolute limit: 128 MB
Header file cache:
Absolute Limit: 128 MB