cancel
Showing results for 
Search instead for 
Did you mean: 

problem with casting between float and int

fmiklasz
Associate II
Posted on June 04, 2010 at 19:09

problem with casting between float and int

12 REPLIES 12
t1
Associate II
Posted on May 17, 2011 at 13:53

I don't know anything about the

codesourcery compiler but you should not need to cast.  The compiler should generate the code to convert int to float without it.

 

 

What happens if you remove the cast?

 

fmiklasz
Associate II
Posted on May 17, 2011 at 13:53

the same hardfault exception

 

Andrew Neil
Evangelist III
Posted on May 17, 2011 at 13:53

Do you actually have floating point enabled and properly configured?

Many embedded toolsets default to not providing floating point support because it can be quire resource hungry - see my 1st reply in this thread:

[DEAD LINK /public/STe2ecommunities/mcu/Lists/ARM%20CortexM3%20STM32/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/ARM CortexM3 STM32/usart printf float&FolderCTID=0x01200200770978C69A1141439FE559EB459D758000626BE2B829C32145B9EB5739142DC17E&TopicsView=https://my.st.com/public/STe2ecommunities/mcu/Lists/ARM%2520CortexM3%2520STM32/AllItems.aspx&currentviews=231]https://my.st.com/public/STe2ecommunities/mcu/Lists/ARM%20CortexM3%20STM32/Flat.aspx?RootFolder=%2fpublic%2fSTe2ecommunities%2fmcu%2fLists%2fARM%20CortexM3%20STM32%2fusart%20printf%20float&FolderCTID=0x01200200770978C69A1141439FE559EB459D758000626BE2B829C32145B9EB5739142DC17E&TopicsView=https%3A%2F%2Fmy%2Est%2Ecom%2Fpublic%2FSTe2ecommunities%2Fmcu%2FLists%2FARM%2520CortexM3%2520STM32%2FAllItems%2Easpx&currentviews=231

t1
Associate II
Posted on May 17, 2011 at 13:53

What assembly code is the compiler generating for the assignment?

Can you put a breakpoint on this assignment, switch to disassembly mode in the debugger and step through the code instruction by instruction?

fmiklasz
Associate II
Posted on May 17, 2011 at 13:53

hmm unfortunately I'm new in the arm/cortex world and have prepared by someone a Makefile. As I see there is -mthumb option during compiling and I didn't see any implicite linking oder than library for m3 core... Could you look at this and say where is a mistake?

TOOLCHAIN_BASE = /opt/CodeSourcery/bin/arm-none-eabi-

COMPILE_OPTS = -mcpu=cortex-m3 -mthumb -Wall -g -O0 -mfloat-abi=soft

PROJECT_NAME = plytka

#====================================================

# Paths and directories names

#=====================================================

WORKSPACE_DIR = workspace

PROJECT_DIR = /home/$(USER)/$(WORKSPACE_DIR)/$(PROJECT_NAME)

FIRMWARE_DIR = ./hardware/firmware

SPD_INC_DIR = $(FIRMWARE_DIR)/STM32F10x_StdPeriph_Driver/inc

CMSIS_INC_DIR = $(FIRMWARE_DIR)/CMSIS/Core/CM3

INCLUDE_DIRS = -I. -I$(SPD_INC_DIR) -I$(CMSIS_INC_DIR)

OUTPUT_DIR = $(PROJECT_DIR)/out

DOCS_DIR = $(PROJECT_DIR)/doc

SUBDIRS = hardware

#=====================================================

# Input files

#=====================================================

C_SRC = $(wildcard *.c)

CPP_SRC = $(wildcard *.cpp)

ASM_SRC = $(wildcard *.s)

#===============================================================================

# Output files created from input files

#===============================================================================

CPP_OBJS = $(addprefix $(OUTPUT_DIR)/,$(CPP_SRC:.cpp=.o))

C_OBJS = $(addprefix $(OUTPUT_DIR)/,$(C_SRC:.c=.o))

ASM_OBJS = $(addprefix $(OUTPUT_DIR)/,$(ASM_SRC:.s=.o))

OBJS = $(ASM_OBJS) $(C_OBJS) $(CPP_OBJS) $(USER_OBJS)

#===============================================================================

# tools for compiling, linking and other

#===============================================================================

CC = $(TOOLCHAIN_BASE)gcc

CFLAGS = $(COMPILE_OPTS) $(INCLUDE_DIRS)

CXX = $(TOOLCHAIN_BASE)g++

CXXFLAGS = $(COMPILE_OPTS) $(INCLUDE_DIRS)

AS = $(TOOLCHAIN_BASE)gcc

ASFLAGS = $(COMPILE_OPTS) -c

LD = $(TOOLCHAIN_BASE)gcc

LDFLAGS = -Wl,--gc-sections,-Map=$@.map,-cref,-u,Reset_Handler $(INCLUDE_DIRS) -T $(FIRMWARE_DIR)/stm32.ld

OBJCP = $(TOOLCHAIN_BASE)objcopy

OBJCPFLAGS = -O binary

AR = $(TOOLCHAIN_BASE)ar

ARFLAGS = cr

SIZE = $(TOOLCHAIN_BASE)size

#===============================================================================

# Destination files

#===============================================================================

MAIN_OUT = $(PROJECT_NAME)

MAIN_OUT_ELF = $(MAIN_OUT).elf

MAIN_OUT_BIN = $(MAIN_OUT).bin

#===============================================================================

# main Makefile rule

#===============================================================================

all:

rm -f $(MAIN_OUT_ELF) $(MAIN_OUT_BIN) $(MAIN_OUT_ELF).map

$(shell mkdir $(OUTPUT_DIR) 2>/dev/null)

cd hardware; make all

make this

this: $(OBJS) $(MAIN_OUT_ELF) $(MAIN_OUT_BIN) print_size

#===============================================================================

# compiling - C source -> objects

#===============================================================================

$(OUTPUT_DIR)/%.o : %.c

@echo 'Compiling file: $<'

$(CC) -c $(CFLAGS) $< -o $@

@echo ' '

#===============================================================================

# assembling - ASM source -> objects

#===============================================================================

$(OUTPUT_DIR)/%.o : %.s

@echo 'Assembling file: $<'

$(AS) -c $(ASFLAGS) $< -o $@

@echo ' '

#===============================================================================

# creating destination files

#===============================================================================

$(MAIN_OUT_ELF): $(OBJ)

$(LD) $(LDFLAGS) $(OUTPUT_DIR)/*.o --output $@

$(MAIN_OUT_BIN): $(MAIN_OUT_ELF)

$(OBJCP) $(OBJCPFLAGS) $< $@

fmiklasz
Associate II
Posted on May 17, 2011 at 13:53

as I said I can use floating point variables. I.e. function Sin(float) from math.h goes correct and everything its ok.

I disassemble the code and simplifing it looks like:

a = aaa();

val = a+bbb();

     =>

 8001c40: f7ff ffc4 bl 8001bcc <aaa>

 8001c44: 4603       mov r3, r0

 8001c46: 607b       str r3, [r7, #4]

 8001c48: f001 f944 bl 8002ed4 <bbb>

 8001c4c: 4603       mov r3, r0

 8001c4e: 4618       mov r0, r3

 8001c50: f008 ef0a blx 800aa68 <__aeabi_ui2f>

 8001c54: 4603       mov r3, r0

 8001c56: 4618       mov r0, r3

 8001c58: 6879       ldr r1, [r7, #4]

 8001c5a: f008 ee28 blx 800a8ac <__addsf3>

    

    

    

    

....

0800aa68 <__aeabi_ui2f>:

 800aa68: e3a03000 mov r3, #0

 800aa6c: ea000001 b 800aa78 <__aeabi_i2f+0x8>

'a' is simply just this :

float a = aaa(); // float aaa() {return 2.5;}

and funciton bbb is:

uint32_t bbb();

The exception occurred in the line 8001c5. It didn't jump to <__aeabi_ui2f>...

Posted on May 17, 2011 at 13:53

The Cortex-M3 does not support ARM code, only THUMB(2), you are including an ARM library/function and that is why the code traps.

 8001c50: f008 ef0a blx 800aa68 <__aeabi_ui2f>

0800aa68 <__aeabi_ui2f>: <= 32 BIT ARM CODE

 800aa68: e3a03000 mov r3, #0

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on May 17, 2011 at 13:53

Make sure it is pulling the libraries from a lib/thumb2 directory, I don't use GNU/GCC

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
sjo2
Associate II
Posted on May 17, 2011 at 13:53

You CFLAGS contains the correct -mcpu=cortex-m3 -mthumb but the LDFLAGS will also need this info to pull in the correct cortex libs.

Adding COMPILE_OPTS to the LDFLAGS should solve the problem.

Cheers

sjo