Author Topic: ESP32 Arithmetic Error  (Read 2256 times)

0 Members and 1 Guest are viewing this topic.

Offline Quarlo KlobrigneyTopic starter

  • Frequent Contributor
  • **
  • Posts: 967
  • Country: pt
  • This Space For Rent
ESP32 Arithmetic Error
« on: February 07, 2023, 11:03:23 pm »
Can anybody tell me just what is wrong with this code posted as working but won't compile without this error?

"suggest parentheses around arithmetic in operand of '|' [-Werror=parentheses]"

I tried several parentheses in the normal way I would use them but nope.
   The line in question:
cmd_si5351(60,(R<<4)&0x70|(P1>>16)&0x03);

Here's a photo.
« Last Edit: February 07, 2023, 11:09:14 pm by Quarlo Klobrigney »
Voltage does not flow, nor does voltage go.
 

Offline langwadt

  • Super Contributor
  • ***
  • Posts: 4392
  • Country: dk
Re: ESP32 Arithmetic Error
« Reply #1 on: February 07, 2023, 11:12:10 pm »
cmd_si5351(60,((R<<4)&0x70)|((P1>>16)&0x03));

?
 
The following users thanked this post: Quarlo Klobrigney

Offline Quarlo KlobrigneyTopic starter

  • Frequent Contributor
  • **
  • Posts: 967
  • Country: pt
  • This Space For Rent
Re: ESP32 Arithmetic Error
« Reply #2 on: February 07, 2023, 11:22:13 pm »
Thanks, it seems to get past that bit but now it's on to something else:


sprintf(str, "%3d, %03d, %02d",  frq/1000000, (frq/1000)%1000, (frq/10)%100 );



format '%d' expects argument of type 'int', but argument 3 has type 'long int' [-Werror=format=]

And this was shown to be working with the download link.


Any advice?
I've been in hospital for the last 2 months and the thinker box is lagging behind by 4.
« Last Edit: February 10, 2023, 10:00:38 am by Quarlo Klobrigney »
Voltage does not flow, nor does voltage go.
 

Online SiliconWizard

  • Super Contributor
  • ***
  • Posts: 14305
  • Country: fr
Re: ESP32 Arithmetic Error
« Reply #3 on: February 08, 2023, 12:06:44 am »
The original statement was already too readable "cmd_si5351(60,(R<<4)&0x70|(P1>>16)&0x03);", which is probably why your latest post uses a minuscule font. ;D
 

Online ejeffrey

  • Super Contributor
  • ***
  • Posts: 3686
  • Country: us
Re: ESP32 Arithmetic Error
« Reply #4 on: February 08, 2023, 01:01:28 am »
frq must be declared 'long int' outside of the posed code snippet.  The sprintf format specifier %d expects an integer.  In C, these are not the same type even if they are the same size.  The solution is to change the format specifier to %ld.

I'm guessing that frq is actually declared with something like int32_t, or another fixed width type which is typedef'ed by stdint.h into 'long int' but would be different on another platform.  If you want to be cross platform you can fix this a couple of ways. 

1) You can use the "PRId32" or similar marcros which are defined to have the correct type for fixed width types.  This works fine, but dramatically decreases readability, and doesn't provide a solution for other typedefs (such as user or platform defined ones).
2) You can use the format specifier %jd and cast the value to intmax_t.  This is more readable, and works for any signed integral type, but likely carries runtime overhead.

 

Offline Quarlo KlobrigneyTopic starter

  • Frequent Contributor
  • **
  • Posts: 967
  • Country: pt
  • This Space For Rent
Re: ESP32 Arithmetic Error
« Reply #5 on: February 08, 2023, 01:19:28 pm »
It was declared early on as a Global, so I don't understand why except for!!

long frq=init_freq;

I can provide the code from the author and the associated page if anyone can make it work.


I'm still learning to walk all over again and a nice project would distract me from my troubles, 2 months and more to come, but I can't figure it out, Perhaps someone here would be so kind.

I was going to make 2 units, with a friend of mine (my age)  receiving one unit for free. He helped me with a lot of things while I was in hospital.

So, the project is a signal generator/VFO that would I'm sure he would like if he knew he was getting it.

The code is a strange mish-mash of a Japanese ham's work and has a lot of undeclared and unused variables.

Anybody like to help the aged and infirmed while in recovery, set this code straight where it will compile?
;D  
« Last Edit: February 08, 2023, 01:28:41 pm by Quarlo Klobrigney »
Voltage does not flow, nor does voltage go.
 

Offline paulca

  • Super Contributor
  • ***
  • Posts: 4003
  • Country: gb
Re: ESP32 Arithmetic Error
« Reply #6 on: February 09, 2023, 10:53:10 am »
My advice would be to delete the obfuscated Martian and write it in human readable code instead.

cmd_si5351(60,(R<<4)&0x70|(P1>>16)&0x03);

Split it up.

int aNamedThing = 60;

#define A_CONSTANT 4
#define ANOTHER_CONSTANT 0x70
#define YET_ANOTHER_CONSTANT 0x03
#define AND_ANOTHER_CONSTANT 16

long anotherThing = (R<<A_CONSTANT)&ANOTHER_CONSTANT;
long aThing = (P1>>AND_ANOTHER_CONSTANT)&YET_ANOTHER_CONSTANT;

Then

cmd_si5351(aNamedThing , anotherThing | a Thing );

Now you can actually read it, assuming Thing and Another thing have actual readible names.

The compiler will optimise it and remove most of it, so worry not.

Complex expressions like that are obsfucation and it is a sign of a non-profressional programmer, most likely a Solo hacker who doesn't care if anyone, including himself can read and understand it in 6 weeks.
"What could possibly go wrong?"
Current Open Projects:  STM32F411RE+ESP32+TFT for home IoT (NoT) projects.  Child's advent xmas countdown toy.  Digital audio routing board.
 
The following users thanked this post: Tom45, newbrain

Offline ledtester

  • Super Contributor
  • ***
  • Posts: 3032
  • Country: us
Re: ESP32 Arithmetic Error
« Reply #7 on: February 09, 2023, 03:58:56 pm »
My advice would be to delete the obfuscated Martian and write it in human readable code instead.
...

I think adding a comment like

Code: [Select]
// form the byte with bits: 0 | R_2 | R_1 | R_0 | 0 | 0 | P1_17 | P1_16
// i.e. R[2:0] in the top nybble and P1[17:16] in the lower nybble
...

would be more useful.
 

Offline Quarlo KlobrigneyTopic starter

  • Frequent Contributor
  • **
  • Posts: 967
  • Country: pt
  • This Space For Rent
Re: ESP32 Arithmetic Error
« Reply #8 on: February 09, 2023, 04:15:17 pm »
I'm afraid you're going faster than my brain can handle right now. The stay in hospital took a lot out of me mostly physical and mental.
I get what you are trying to tell me but the application of it feels impossible right now.


Please let me show you all the original article and code and please just attempt to compile it for a standard ESP32 and let me know if I am that addled or is it the code.


Here are the necessary links for this riddle. This has nothing to do with me or anyone else's post.
I just found it interesting as a signal generator and that display!!




https://www.hackster.io/mircemk/universal-vfo-10-khz-160-mhz-with-retro-analog-scale-c7c5b6

Thanks for everyone's help so far here.
Voltage does not flow, nor does voltage go.
 

Offline paulca

  • Super Contributor
  • ***
  • Posts: 4003
  • Country: gb
Re: ESP32 Arithmetic Error
« Reply #9 on: February 09, 2023, 06:13:10 pm »
Code: [Select]
// form the byte with bits: 0 | R_2 | R_1 | R_0 | 0 | 0 | P1_17 | P1_16
// i.e. R[2:0] in the top nybble and P1[17:16] in the lower nybble
...

would be more useful.

Aye.  Then you later need to go look up what those bits are.  Why not do everyone a favour and just name them with constants?

Someone else reading your code is going to ask, "What does that do?"

Making your code completely unreadable and full of magic numbers just makes it harder for them.  Most people who have been on the receiving end of this in their career go out of their way not to do it to others.

Yes, YOU know what they are NOW, but you in 6 months might not.  Someone else might not.  So they literally have to go get the reference manual out to understand what the code does.  Just to save a few hundred characters of text.
"What could possibly go wrong?"
Current Open Projects:  STM32F411RE+ESP32+TFT for home IoT (NoT) projects.  Child's advent xmas countdown toy.  Digital audio routing board.
 

Offline Quarlo KlobrigneyTopic starter

  • Frequent Contributor
  • **
  • Posts: 967
  • Country: pt
  • This Space For Rent
Re: ESP32 Arithmetic Error
« Reply #10 on: February 26, 2023, 03:33:15 am »
Forget the math errors. The author did not mention only the ESP-32-S2 variant will work due to size constraints. |O


I compiled all of the Arduino sketches that did not work from the original Japanese all the wat through the latest versions found on the Intertoobs. All of them finished compiling.
Whether they work or not we'll see within the week. I don't have an S2 on hand right now.


I'm looking for a 4M Flash & 2M PSRAM version.
The big problem is that Amazon, Digi-Key, Mouser etc do not explain which S2's PSRAM equals what by a specific part number. :-//
I even see several listings that say ESP-32-S2 but then describe an 8286 down below for the item and photo above :--
Now I have the old issue of the seller having no clue what thy are selling.
Stand by..............................................................................



Voltage does not flow, nor does voltage go.
 

Offline tooki

  • Super Contributor
  • ***
  • Posts: 11341
  • Country: ch
Re: ESP32 Arithmetic Error
« Reply #11 on: February 26, 2023, 01:10:44 pm »
Can anybody tell me just what is wrong with this code posted as working but won't compile without this error?

Assuming that you are simply attempting to compile this:
Please let me show you all the original article and code and please just attempt to compile it for a standard ESP32 and let me know if I am that addled or is it the code.
...
https://www.hackster.io/mircemk/universal-vfo-10-khz-160-mhz-with-retro-analog-scale-c7c5b6
...then there's nothing wrong with the code. It compiles without issue for me using the Arduino IDE 1.8.16 with default settings, set up for an ESP32 dev module. If you change the settings to give "more" or "all" compiler warnings, then you see these warnings.

It could be as simple as you needing to reset your build environment.

Preferences:
- Compiler warnings: default.
- Additional boards manager URLs: https://dl.espressif.com/dl/package_esp32_index.json

ESP32 board support v.1.0.6

Tools:
- Board: ESP32 Dev Module
The rest should be the defaults.


Forget the math errors. The author did not mention only the ESP-32-S2 variant will work due to size constraints. |O
Where did you get that idea?!?

It only uses a fraction of the standard ESP32's resources:
Quote
Sketch uses 273882 bytes (20%) of program storage space. Maximum is 1310720 bytes.
Global variables use 120256 bytes (36%) of dynamic memory, leaving 207424 bytes for local variables. Maximum is 327680 bytes.


Also, FYI, PSRAM isn't even used automatically, so if you run out of regular RAM, you can't just get a module with extra PSRAM and then upload your code. You have to modify the code to push things into PSRAM.


I can't test the code since I don't have the correct display nor the si5351, but the code does compile.
 

Offline Quarlo KlobrigneyTopic starter

  • Frequent Contributor
  • **
  • Posts: 967
  • Country: pt
  • This Space For Rent
Re: ESP32 Arithmetic Error
« Reply #12 on: February 27, 2023, 09:17:07 am »


Well you are making a lot of assumptions.


Who told me that it needed an S2, one of the latter authors who confirmed that the original project with an ESP32, ran out of memory.
The code still doesn't compile for a non S2 for all the versions of the original code.
I don't care if the code fits or not, it doesn't compile. Iv'e still yet to program anything because I do not have an S2.


Of course the json file was installed long ago.


The compiler warnings had never been turned off until I kept getting errors weeks ago.
Why would any good programmer ever turn off errors anyway it is counter intuitive to doing the job right.
Yes, the settings are exactly the same as yours which are still the same as when I started.

Any way I want more memory and PSRAM just because, not because it will fix this. I want the max because I don't like to buy things twice. More PSRAM equates to spiffs not running short of space.


Expressif's site is all over the map and still doesn't give a correct part number for 2M or 4M (if available) to order from a vendor-distributor.



Quote from: tooki on Yesterday at 08:10:44

>
« Last Edit: February 27, 2023, 09:20:36 am by Quarlo Klobrigney »
Voltage does not flow, nor does voltage go.
 

Offline m98

  • Frequent Contributor
  • **
  • Posts: 613
  • Country: de
Re: ESP32 Arithmetic Error
« Reply #13 on: February 27, 2023, 07:46:02 pm »
The ESP32 should have more than enough resources for this application. Maybe just ditch that code and implement it yourself with ESP-IDF and LVGL.
 

Offline tooki

  • Super Contributor
  • ***
  • Posts: 11341
  • Country: ch
Re: ESP32 Arithmetic Error
« Reply #14 on: February 27, 2023, 10:49:14 pm »
Well you are making a lot of assumptions.


Who told me that it needed an S2, one of the latter authors who confirmed that the original project with an ESP32, ran out of memory.
The code still doesn't compile for a non S2 for all the versions of the original code.
I don't care if the code fits or not, it doesn't compile. Iv'e still yet to program anything because I do not have an S2.
I am not assuming anything!!!  I did not speculate that it would work. I tested it.  I downloaded the code YOU linked, and then SUCCESSFULLY COMPILED IT FOR ESP32 (NOT S2) on the first try.

I just can’t test whether it’s actually working because I don’t have the peripherals in question.

Is the project you linked to NOT the “original code”? That project didn’t use an S2, either.


Also: Any assumptions the reader has to make is because YOU didn’t provide the actual information. That’s your fault, not mine.

The compiler warnings had never been turned off until I kept getting errors weeks ago.
Why would any good programmer ever turn off errors anyway it is counter intuitive to doing the job right.
I said nothing about what is and isn’t good practice. I only said that those warnings (not errors!) do not occur with the default warnings level.

If you don’t like what the default warnings are, please argue with the Arduino authors about it, not me. I didn’t decide them.

Yes, the settings are exactly the same as yours which are still the same as when I started.
Well clearly not, since you have the warnings set to a different level.
 

Offline Quarlo KlobrigneyTopic starter

  • Frequent Contributor
  • **
  • Posts: 967
  • Country: pt
  • This Space For Rent
Re: ESP32 Arithmetic Error
« Reply #15 on: February 28, 2023, 04:37:41 am »

As I have already said, "I've been in hospital for the last 2+ months and the thinker box is lagging behind by 4 months (or years)".
I'm not capable of that level of thinking at this time.
I was hoping for advice-help for code correction, not criticism of my abilities at this time.
I do wish more have read the entire thread.


Edited because garbage is posted along with post such as errant tags and font issues.
Only this browser on this site does it.

Quote from: m98 on Today at 14:46:02>The ESP32 should have more than enough resources for this application. Maybe just ditch that code and implement it yourself with ESP-IDF and LVGL.

« Last Edit: February 28, 2023, 04:43:32 am by Quarlo Klobrigney »
Voltage does not flow, nor does voltage go.
 

Offline tooki

  • Super Contributor
  • ***
  • Posts: 11341
  • Country: ch
Re: ESP32 Arithmetic Error
« Reply #16 on: February 28, 2023, 06:35:49 am »

As I have already said, "I've been in hospital for the last 2+ months and the thinker box is lagging behind by 4 months (or years)".
I'm not capable of that level of thinking at this time.
I am sorry you’re not feeling well, and I hope you feel better soon.

I was hoping for advice-help for code correction, not criticism of my abilities at this time.
But the code doesn’t need correction. I tested the code and it compiled without error on the first try. So something is wrong with your build environment.

Do you have another computer to try on?

Also, a thought: try temporarily uninstalling the libraries in your Arduino IDE, just to make sure there isn’t some library versioning conflict.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf