This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Weird things happening with floats in nRF51, GCC, Eclipse

Hello, I wanted to show here two cases that made me crazy today and I fixed using workarounds, but still have no idea why they are happening.

In main context I call a function that returns a float, I print this float with RTT and it is always fine, with range aprox [-200,200], so not a big number.

Case A:

a = myfunction();  //"a" variable is a global defined as static float at the beggining of the file
static float b = 0.0;
b = b + a;

When I print b with RTT same way as I printed a before, it says "nan" everytime.

Case B:

a = myfunction();  //"a" variable is a global defined as static float at the beggining of the file
int b = a * 100;  //Also tried doing (int)(a*100)

When I print b with RTT, this case with d formatting, it prints fine, but once in a while, at random times, printed b is wrong, takes very high values or very small, it is like the cast is failing sometimes.

What am I missing? I am pretty sure I am doing something wrong.

I am using nRF51822, Eclipse IDE, attached at the end all the flags in makefile. I tried restarting computer just in case. My code is quite long, using app_timer, scheduler, TWI, PWM (hardware and software), S130, SDK 9 (some things taken from SDK10), BLE connection to master (not connected yet in these cases. The called function is in another file, in case that matters somehow. Please ask if you need more details, code or anything.

Thank you very much for your time. :)

CFLAGS  = -DBOARD_FELIX21
CFLAGS += -DSWI_DISABLE0
CFLAGS += -DSOFTDEVICE_PRESENT
CFLAGS += -DNRF51
CFLAGS += -DS130
CFLAGS += -DBLE_STACK_SUPPORT_REQD
CFLAGS += -mcpu=cortex-m0
CFLAGS += -mthumb -mabi=aapcs --std=gnu99
CFLAGS += -Wall -g3 -Og #Og es el unico nivel de optimizacion que admite inline functions y no peta TWI MPU, investigar en futuro
#CFLAGS += -Wall -Werror -O3
CFLAGS += -mfloat-abi=soft
# keep every function in separate section. This will allow linker to dump unused functions
CFLAGS += -ffunction-sections -fdata-sections -fno-strict-aliasing
#CFLAGS += -fno-builtin --short-enums 
CFLAGS += --short-enums 

# keep every function in separate section. This will allow linker to dump unused functions
LDFLAGS += -Xlinker -Map=$(LISTING_DIRECTORY)/$(OUTPUT_FILENAME).map
LDFLAGS += -mthumb -mabi=aapcs -L $(LINKER_PATH) -T$(LINKER_SCRIPT) 
LDFLAGS += -mcpu=cortex-m0
# let linker to dump unused sections
LDFLAGS += -Wl,--gc-sections
# use newlib in nano version
LDFLAGS += --specs=nano.specs -lc -lnosys
# accept floats in sprintf
LDFLAGS += -u _printf_float

# Assembler flags
ASMFLAGS += -x assembler-with-cpp

#ASMFLAGS += -DBOARD_PCA10028
ASMFLAGS += -DBOARD_FELIX21

ASMFLAGS += -DSWI_DISABLE0
ASMFLAGS += -DSOFTDEVICE_PRESENT
ASMFLAGS += -DNRF51
ASMFLAGS += -DS130
ASMFLAGS += -DBLE_STACK_SUPPORT_REQDe here
Parents Reply Children
  • Thanks for the attention Vidar. I discovered that these errors I am seeing could be related to timing as well, because I have seen they disappear when addding / removing unrelated things in those functions... So now I have moved along but I will come back to this later in order to sort it out using GDB as you mentioned. I will update once I test it again. Thank you

Related