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

Using float with sprintf generates hardware fault

nRF51 SDK_v5.2.0.39364 GNU Compiler version 4.7 2013q1 Eclipse Juno SR2 nRF51822 Development Kit

uint8_t str[128]; //Declared globally

float tempval; tempval = measure_temperature(); memset(&str[0], 0, 128); sprintf((char*)&str[0], "%3.2f", tempval); <<- Hardware Fault here

I have attached the snapshot of the callstack. All source files compiled with "-mfloat-abi=soft" and linked with "-u _printf_float"

Can someone please help?

Best regards, Karthik

Capture.PNG

Parents
  • FormerMember
    0 FormerMember

    Hi Karthik, Its working for me. I've added this code in the timer handler called from the scheduler.

    
     float tempval;
     uint32_t n;
     tempval = 128.54;
     memset(&str[0], 0, 128);
     n = sprintf((char*)&str[0], "%3.2f", tempval);
     printf ("[%s] is a string %d chars long\n",str,n);
    
    

    where str is a global uint8_t variable of 128 octet length. The string > [128.54] is a string 6 chars long is printed every second, from the timer handler. The linker flags used are

    LDFLAGS = -Xlinker
    LDFLAGS += -Map=$(BIN_DIR)/$(OUTPUT_NAME).map
    LDFLAGS += --specs=nano.specs
    LDFLAGS += -u _printf_float
    LDFLAGS += -mcpu=$(CPU) 
    LDFLAGS += -mthumb 
    LDFLAGS += -mabi=aapcs 
    LDFLAGS += -T$(LD_SCRIPT)
    LDFLAGS	+= -D$(DEVICE)
    LDFLAGS	+= -D$(BOARD)
    LDFLAGS += -D$(BLE_REQD)
    LDFLAGS	+= $(patsubst %,-L%, $(LDDIRS))
    

    The difference that I see are that i am not specifying the mfloat-abi option, while I am using the nano library for printf and sprintf. See if this works. Cheers, Prithvi

Reply
  • FormerMember
    0 FormerMember

    Hi Karthik, Its working for me. I've added this code in the timer handler called from the scheduler.

    
     float tempval;
     uint32_t n;
     tempval = 128.54;
     memset(&str[0], 0, 128);
     n = sprintf((char*)&str[0], "%3.2f", tempval);
     printf ("[%s] is a string %d chars long\n",str,n);
    
    

    where str is a global uint8_t variable of 128 octet length. The string > [128.54] is a string 6 chars long is printed every second, from the timer handler. The linker flags used are

    LDFLAGS = -Xlinker
    LDFLAGS += -Map=$(BIN_DIR)/$(OUTPUT_NAME).map
    LDFLAGS += --specs=nano.specs
    LDFLAGS += -u _printf_float
    LDFLAGS += -mcpu=$(CPU) 
    LDFLAGS += -mthumb 
    LDFLAGS += -mabi=aapcs 
    LDFLAGS += -T$(LD_SCRIPT)
    LDFLAGS	+= -D$(DEVICE)
    LDFLAGS	+= -D$(BOARD)
    LDFLAGS += -D$(BLE_REQD)
    LDFLAGS	+= $(patsubst %,-L%, $(LDDIRS))
    

    The difference that I see are that i am not specifying the mfloat-abi option, while I am using the nano library for printf and sprintf. See if this works. Cheers, Prithvi

Children
No Data
Related