jone31
March 4, 2017, 8:03pm
1
Hi there, im' facing a big problem my life is sad,
i did a program on arduino uno that i have pointer in my function, i want to compile the same program on arduino due but i have a crazy result.
after deep search, i recognize the problem, the problem is the data type in arduino uno and arduino due not the same.
in arduino uno Serial.println(sizeof(uint32_t*)); give 2
in arduino due Serial.println(sizeof(uint32_t*)); give 4
i want to get 2 in arduino due is there a solution for that
please help
Why would you want 16 bit pointers on 32 bit cpu?
With 96 KBytes of SRAM and 512 KBytes of Flash memory that would not work.
The difference in size should have no impact on 'clean' c++ code.
Can you show a short example?
jone31
March 4, 2017, 8:28pm
3
Here is the code.
is there a way to change the pointer with other stuf ??
uint8_t update() {
uint32_t* rawVal;
// lines...
read(rawVal);
return 1;
}
static void read(uint32_t* val) {
union { uint32_t val; uint8_t raw[4]; } data;
Wire.beginTransmission(0x87);
Wire.write(0);
Wire.endTransmission();
Wire.requestFrom(0x87, 3);
while (Wire.available() < 3);
data.raw[2] = Wire.read();
data.raw[1] = Wire.read();
data.raw[0] = Wire.read();
*val = data.val;
}
There is no problem with pointer sizes, the difference could be exception on the due/don't care on Arduino.
You seem to write to an uninitialized pointer, changing size would not change anything.
uint32_t* rawVal;
read(rawVal);
static void read(uint32_t* val) {
*val = data.val;
}
Why do you pass the address of the uint32 to a void routine to be set?
I would return an uint32 and pass no address.
Why is the function static?
Why should other linked modules not see it?
jone31
March 4, 2017, 8:51pm
6
uint8_t update() {
uint32_t* rawVal;
uint8_t comm;
if (func1.sta == 2) {
func1.sta = 0;
Calculate();
return 1;
}
if (func1.state == 0) {
rawVal = &data1;
}
else {
rawVal = &data2;
}
func1.sta++;
Read(rawVal);
return 1;
}
can you help me to write the code without pointer please ?
Maybe somthing like this:
uint8_t update() {
uint32_t rawVal = read();
// do something with the read value
return 1;
}
uint32_t read() {
union { uint32_t val; uint8_t raw[4]; } data;
Wire.beginTransmission(0x87);
Wire.write(0);
Wire.endTransmission();
Wire.requestFrom(0x87, 3);
while (Wire.available() < 3);
data.raw[2] = Wire.read();
data.raw[1] = Wire.read();
data.raw[0] = Wire.read();
data.raw[3] = 0;
return data.val;
}
jone31
March 4, 2017, 9:05pm
8
Why you did this rawVal = read(); ??
To read a value into the variable?
jone31
March 4, 2017, 9:11pm
10
I tried your code, but it d'oesn"t work
Greetings from snippets are us.
My code at least does not trigger exceptions/write to somewhere.
"does not work" has no information.
jone31
March 4, 2017, 9:27pm
12
the code here it is
uint8_t update() {
uint32_t* rawVal;
uint8_t comm;
if (func1.sta == 2) {
func1.sta = 0;
Calculate();
return 1;
}
if (func1.state == 0) {
rawVal = &data1;
}
else {
rawVal = &data2;
}
func1.sta++;
Read(rawVal);
return 1;
}
static void read(uint32_t* val) {
union { uint32_t val; uint8_t raw[4]; } data;
Wire.beginTransmission(0x87);
Wire.write(0);
Wire.endTransmission();
Wire.requestFrom(0x87, 3);
while (Wire.available() < 3);
data.raw[2] = Wire.read();
data.raw[1] = Wire.read();
data.raw[0] = Wire.read();
*val = data.val;
}
on uno i get correct value but on due i have crazy valu that jump
uint8_t update() {
if (func1.sta == 2) {
func1.sta = 0;
Calculate();
return 1;
}
if (func1.state == 0) {
data1 = Read();
} else {
data2 = Read();
}
func1.sta++;
return 1;
}
uint32_t read() {
union { uint32_t val; uint8_t raw[4]; } data;
Wire.beginTransmission(0x87);
Wire.write(0);
Wire.endTransmission();
Wire.requestFrom(0x87, 3);
while (Wire.available() < 3);
data.raw[2] = Wire.read();
data.raw[1] = Wire.read();
data.raw[0] = Wire.read();
data.raw[3] = 0;
return data.val;
}
And these are still only parts, no definition of func1 which has the strange members 'sta' and 'state', ...
that forces wild guessing and there is no possibility to let the compiler check for typos.
Why is it so hard to post the complete code, or at least a compiling subset?
Passing a valid pointer to your read function is another way.
uint8_t update() {
uint32_t rawVal;
read(&rawVal);
// do something with the read value
return 1;
}
You can use your current read function.
But if Whandall's code does not do what you expect, the above will also not do.
Is there a difference in endianness between due and uno? It might explain the vague 'does not work'.
His Read routine injects a random value in the highest byte of the set value, a simple print would not ignore that.
There is insufficient context.
jone31
March 4, 2017, 9:48pm
16
now it works, but always i have a crazy valu, i see no diferance with the old code. so the size of the pointer does't afecte, i don't understand what do you mean by
His Read routine injects a random value in the highest byte of the set value
could you explain more please
union { uint32_t val; uint8_t raw[4]; } data;
data.raw[2] = Wire.read();
data.raw[1] = Wire.read();
data.raw[0] = Wire.read();
What value will data.raw[3] (the highest byte) get?
You did not initialize data, it contains junk.
BTW the Due is little endian, like the 8 bit Arduinos
manages all data memory accesses as little-endian.
Instruction memory and Private Peripheral Bus (PPB) accesses are always little-endian.
See “Memory regions, types and attributes” on page 62 for more information.
jone31
March 4, 2017, 10:07pm
18
so this is how that i have crazy value ?? the randow value that affect the data.raw[3] ??
but why on the uno i have not this problem ??
On the Uno the junk happens to be zero?
jone31
March 4, 2017, 10:15pm
20
so what the solution on the due ? i'm verry sad