cancel
Showing results for 
Search instead for 
Did you mean: 

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

T J
Lead
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 ?

7 REPLIES 7
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 Venmo
Up vote any posts that you find helpful, it shows what's working..
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
Posted on January 18, 2017 at 22:59

can you look into my issue ?

I have not been able to make the leap. to connect to STDIN and STDOUT.

I have done what I can in the included files, but there is some syntax that I am not understanding...

:(

Posted on January 19, 2017 at 03:20

The .CPP usage complicates things, and I'm not working with mbed

You need the ♯ pragma to stop the library code from being pulled in because we are trying to replace that with board specific functions.

This is a main.c example for the F4 Nucleo

 

The pieces for the F0 Disco

 
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on January 19, 2017 at 03:26

I am no longer working with mbed.

however, I have a huge project running online, trying to get it offline.

all my new stuff is cubeMX HAL uV5.

can you open that project ? and fix the __FILE syntax issue ?  just link into one of the new putc, puts... if thats ok...

I just cannot make it mesh, spent hours trying different syntax. 

:(

Posted on January 19, 2017 at 04:53

The project 'opens' but it references a lot of stuff I don't have installed.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on January 19, 2017 at 04:59

the project folder compressed to 2.4MB...

I hope it is ok for you.

I have been trying to help online, but so much goes over my head.

________________

Attachments :

Nucleo_printf.zip : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006Hyz9&d=%2Fa%2F0X0000000bEu%2F6yBDlWBY6jSmtNDdIzHSDkfo919ferk1N.oKggJ.lMQ&asPdf=false