2

////// EDIT: SOLVED, read solution below

I'm trying to use the fpu with the stm32f4Discovery board, programmed with Keil 4 (free version) but, when trying to use it, enters in an infinite loop.

I don't know exactly why, I'm using a very simple code in C and the debugger:

#include "stm32f4_discovery.h"
#include <stdio.h>

float a = 1.332, b = 2.994;

int main(void)
{
   printf("Hola");
     printf("%f",a*b);
   return(0);
}

Here is the results from the debugger: nothing in printf viewer and infinite loop because of "Hard Fault exception occurs" (image here, imgur)

Without line printf("%f",a*b) the debugger shows perfectly "Hola" and ends the program.

I've been searching a possible solution in google since I used this board for a project in university months ago, but anyone knows how to fix it.

I know I can disable fpu and use libraries but that's not the point...

Thank you for your help

/////////////// SOLUTION I had to change the code in startup_stm32f4xx.s and the function SystemInit() from system_stm32f4xx.c

In startup_stm32f4xx.s, search for the Reset Handler, the code should look like this, but the part below ";FPU settings" is mostly not in the original:

; Reset handler Reset_Handler PROC EXPORT Reset_Handler [WEAK] IMPORT SystemInit IMPORT __main

             ;FPU settings
             LDR     R0, =0xE000ED88           ; Enable CP10,CP11
             LDR     R1,[R0]
             ORR     R1,R1,#(0xF << 20)
             STR     R1,[R0]

             LDR     R0, =SystemInit
             BLX     R0
             LDR     R0, =__main
             BX      R0
             ENDP

And then add in system_stm32f4xx.c, function SystemInit(void), this lines:

void SystemInit(void) {

/* FPU settings ------------------------------------------------------------*/

#if (__FPU_PRESENT == 1) && (__FPU_USED == 1) SCB->CPACR |= ((3UL << 10*2)|(3UL << 11*2)); /* set CP10 and CP11 Full Access */ #endif

The debugger will show now the operation result in printf viewer. I don't really know if it's using FPU, but I will test it tomorrow (should be faster now).

Source here

jmth
  • 41
  • 3
  • Are you enabling access to the FPU in your startup code? – Notlikethat Sep 04 '15 at 20:10
  • I have configured the use of FPU in options and that changes "compiler control string" adding --cpu Cortex-M4.fp I'm not using SystemInit() in the code because compiler includes it for me, maybe this function isn't enabling access to the FPU? – jmth Sep 04 '15 at 20:27
  • Yeah, configuring the compiler to actually generate FP instructions is only half the story (specifically, the _second_ half ;)). I don't know anything about the Keil/ST software stack, but I'd hope there's a function somewhere that nicely abstracts [the necessary hardware poking](http://infocenter.arm.com/help/topic/com.arm.doc.dui0553a/BEHBJHIG.html) - hope that gives you an avenue of investigation at least! – Notlikethat Sep 04 '15 at 20:34
  • Okay I did a thing and it's working, I will edit the first post with the solution, thank you very much – jmth Sep 04 '15 at 21:39

0 Answers0