Skip to main content
T J
Senior III
January 13, 2017
Question

How to fix the mbed-printf Keil uV5 hardfault issue ?

  • January 13, 2017
  • 1 reply
  • 1332 views
Posted on January 13, 2017 at 21:35

Hi,

I have finally dragged everything offline into Keil and it now compiles

but instantly goes into hardfault caused by the printf.

How do we fix this issue ?

    This topic has been closed for replies.

    1 reply

    Tesla DeLorean
    Guru
    January 14, 2017
    Posted on January 14, 2017 at 04:36

    You'd have to ensure the data goes some where rather than trap with a BKPT or SWI

    //****************************************************************************
    // Hosting of stdio functionality through USART2
    //****************************************************************************
     
    #include <rt_misc.h>
     
    #pragma import(__use_no_semihosting_swi)
     
    struct __FILE { int handle; /* Add whatever you need here */ };
    FILE __stdout;
    FILE __stdin;
     
    int fputc(int ch, FILE *f)
    {
     while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
     
     USART_SendData(USART2, ch);
     
     return(ch);
    }
     
    int fgetc(FILE *f)
    {
     char ch;
     
     while(USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET);
     
     ch = USART_ReceiveData(USART2);
     
     return((int)ch);
    }
     
    int ferror(FILE *f)
    {
     /* Your implementation of ferror */
     return EOF;
    }
     
    void _ttywrch(int ch)
    {
     while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
     
     USART_SendData(USART2, ch);
    }
     
    void _sys_exit(int return_code)
    {
    label: goto label; /* endless loop */
    }
     
    //****************************************************************************
    �?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?

    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
    T J
    T JAuthor
    Senior III
    January 15, 2017
    Posted on January 15, 2017 at 00:31

    Hi Clive, excellent.

    I am trying to implement the same now, but the syntax kills me.

    The resulting code is attached but still hard faults because I attempt to write to STDOUT.

    Compile Errors

    [FILE __stdout] warning: #2681-D: name linkage conflicts with previous declaration of variable 'std::__stdout' (declared at line 138 of 'C:\Keil_v5demo\ARM\ARMCC\Bin\..\include\stdio.h')

    error: #734: function 'fputc(int, std::FILE *)' conflicts with using-declaration of function 'std::fputc(int, std::FILE *)'

    should there be a .h declaration of FILE ? is it nStream ?

    I have implemented mostly of what you suggested, but I cannot work out the syntax to redirect stdin/stdout.

    which is required to fix the printf hardfault issue in Keil when using mbed.

    Please see attached skeleton uV5 project without the target or mbed

    my target is a STM32F091

    ________________

    Attachments :

    Nucleo_printf.uvguix.zip : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006HysO&d=%2Fa%2F0X0000000bF1%2Fg3UhhshhqGQeO5bXWAi7HxCnc1RGWOWsqgXAjrQwB6U&asPdf=false