Include Compiler Flag (-fpermissive) PLEASE HELP

I am currently porting an Arduino library to the Photon and am almost completely finished except I am having problems when casting a char * to an uint8_t/unit16_t. I am under the impression that the Photon’s memory addresses are all positive which means the operation is safe. To get the compiler to downgrade the error to a warning a want to add -fpermissive, but cannot figure out how to in Particle Dev (I’ve always used VS before).

Any help is appreciated and if you see an error in my approach by all means please correct me.

You can’t set the compiler flags. But what are you actually trying to accomplish? Can you include a section of the code because casting a char * to a uint8_t is really weird. I can do it, but I don’t see why anyone would want to do it.

I am using it this conversion for memory addresses. I am only doing the code in this way because I have a limited amount of time and don’t want to restructure the entire program or do anything time intensive. I realize that because the Photon, unlike the Arduino, doesn’t require the use of PROGMEM to reference data it might be easier to take the same concepts as the original and start from scratch.

Oh, wait, you’re trying to cast a char * to a uint8_t *. You can’t set the compiler flag, but you just have to insert an explicit cast. Hopefully there aren’t too many.

Fails:

char *p1 = "foo";
uint8_t *p2 = p1;
testfoo.cpp:12:19: error: invalid conversion from 'char*' to 'uint8_t* {aka unsigned char*}' [-fpermissive]

Works:

char *p1 = "foo";
uint8_t *p2 = (uint8_t *)p1;
1 Like
const uint8_t freq = pgm_read_byte(data_ptr++);

This is the original code (one example)
I realized that the flaw in what I was trying to do is I was trying to convert to a unit8_t, but pgm_read_byte returns the value of the byte in memory, not a pointer, so when I changed the code to

const uint8_t freq = (unit8_t)data_ptr;

I changed which kinds of variables were in play. I am working with values, not addresses and that was my mistake. Next time I’ll make sure to read through code carefully and work on a day I got more than 5 hours of sleep. :coffee:

Thanks, not what I needed, but the correct answer to what I asked! Sorry I didn’t know what I wanted.

Okay, that makes some sense. Except pgm_read_byte() is supported on the Particle, and it’s necessary if the data is stored in program memory, so I’m not sure you should have removed that.

Sorry to contradict, but pgm_read_byte() is not needed since the address space for RAM and flash is equally accessable via “standard” pointers (unlike AVRs where you employ a different machine instruction for RAM or flash access).

But pgm_read_byte() is defined in the Particle wiring framework to make porting easier.
In fact it does noting else than taking the pointer and return the dereferenced value (and it can be used for other than byte too).

This is part of spark_wiring_arduino.h

#define PROGMEM
#define PSTR(x) (x)
#define pgm_read_byte(x)  (*(x))
1 Like

I did not know that. I knew it worked, but I didn’t know it wasn’t necessary. You learn something new every day!

1 Like

I was not ware of the ability to port with the use of PROGMEM. What do I need to add to my code to use PROGEM?
Thanks.

You can port Arduino code and leave in all of the PROGMEM, PSTR. and pgm_read_bytes and it will function properly without change on the Particle. I just learned that they’re not actually necessary, but you don’t need to remove/replace them, either.

2 Likes

Arduino libraries do explicitly put static/const variables into flash by use of PROGMEM but since this wouldn’t make sense in the ARM Cortex world we are living, the macro just does nothing, so that the Arduino code would just build.

In our case in order to have global preinitialized static/const variables put into flash only you’d just declare them const (which would be good style for Arduino PROGMEM data anyhow).

int x = 10; // x will live in RAM but will also take flash space to hold 10
const int y = 20; //  y will live in flash only
const int z PROGMEM = 30; // will also build and does the same as y
2 Likes

I saw that it didn’t recognize PGM_P (defined in AVR PGMSpace header) and assumed that particle had no support for it when I read the memory was accessible without it. Perfect example of why you don’t jump to conclusions. Thanks for all the help. I am pleasantly surprised I was able to have my problem solved so quickly on this forum.

1 Like

In the new gcc it is required to be declared as const and no longer just good form but will also cause your program not to compile. Due to some odd uses #define it is now a pain to use anything with arrays in PROGMEM in Arduino IDEs after 1.5.5. Good explanation of where variables are stored.

1 Like

I’ve been able to clear everything else up (Thanks for the help) but am still getting this. What’s puzzling is it only shows up 3 times but I have 9 uses of pgm_read_word(pointer).

I hoped you wouln’t have that in your port, since this is still missing in spark_wiring_arduino.h and I meant to file a pull request, but never got round to it.

But you just add this to your header file

#define pgm_read_word(x) ((uint16_t)(*(x)))

And you should be good :wink:

1 Like

Just what I needed!

Now I’m getting
cast from ‘const char**’ to ‘unit16_t’ {short unsigned int} loses precision [-fpermissive]

Can you show the code line that causes the error and maybe also the error messages before and after.
And also the declaration of the variables involved in this statement.

But since the message complains about a pointer assignment it could well be that this comes from the fact that far pointers on Arduinos usually only are 16bit while on this chip we deal with 32bit addresses.

What is odd is it occurs in

#define pgm_read_word(x) ((uint16_t)(*(x)))

at character 42 and causes 2 identical error messages.