8 years, 8 months ago.

call mbed from C library

Dear mbed community,

I am trying to use mbed with STM32Cube_FW_F4_V1.7.0. I have downloaded mbed-src and inserted the required files. Digital_IO-s and Serial already working! Now I would like to write out an error message if a hard fault is happened by using the Serial class. In the file stm32f4xx_it.c I found the empty function:

/**
  * @brief  This function handles Hard Fault exception.
  * @param  None
  * @retval None
  */
void HardFault_Handler()
{

  while (1);
}

The program is compiles and working until this point. If I generate hardfault I find myself in the infinite loop. I can also write some message to the terminal from the main.cpp.

I would like to use Serial class with printf to write some error message. First step is to include mbed.h to stm32f4xx_it.c.

#include "mbed.h"

After this the program won't compile the error message is the following:

Quote:

In file included from C:\...\src\mbed\api/mbed.h:21:0, from ../src/stm32f4xx_it.c:43: C:\...\src\mbed\api/platform.h: At top level: C:\...\src\mbed\api/platform.h:25:19: fatal error: cstddef: No such file or directory

  1. include <cstddef> ^ compilation terminated. make: * [src/stm32f4xx_it.o] Error 1

Of course the file is there and if I delete the line #include "mbed.h" everything is fine again. From main.cpp I can #include "mbed.h" without a problem.

Maybe there is a problem with calling C++ from C library? Why the error came up in platform.h?

platform.h contains this

#ifndef MBED_PLATFORM_H
#define MBED_PLATFORM_H

#define MBED_OPERATORS    1

#include "device.h"
#include "PinNames.h"
#include "PeripheralNames.h"

#include <cstddef>
#include <cstdlib>
#include <cstdio>
#include <cstring>

#endif

Thank you in advance! Dave

2 Answers

8 years, 8 months ago.

You should not be using mbed.h in an STM file. The mbed lib already has the STM HAL files. Just use the mbed lib as normal and use STMcube to generate the drivers you require. Place the generated files together with your mbed program files and call them when needed. I would have expected the function "HardFault_Handler()" to have a weak link so that you can write your own, maybe in main.cpp, where you should have no problem using "printf".

Accepted Answer

mbed using Cube library 1.4.0. I would like to use the newest cube library and for that reason I separated mbed and stm driver files and connect them step by step. It is working however I am currently facing this problem... By the way we are using STM32F429 which is not directly supported by mbed as far as I know so using mbed lib is not trivial. Do you think I should use only stm driver library and forget mbed?

posted by David Molnar 13 Aug 2015

You should have offered the above information in your question. I would not say forget mbed, but as you are using at present an unsupported uC, you have 2 choices, either complete your port to the F429 or use the STM lib.

posted by David Fletcher 13 Aug 2015
8 years, 8 months ago.

You cannot include a C++ library in a C file.

You can use NVIC_SetVector to relocate the Hardfault handler to any function you like it to be in.

For your requirements here I think it should work if you just include <stdio.h> in that file, then you can use printf, and that should be handled by the default serial port. If printf is actually going to work when it is in a hardfault situation is not something I am sure about.