Search code examples
cstm32newlibgnu-arm

STM32 CubeMX/GNU/Eclipse-Toolchain Retargeting (newlib and libgloss)


I set up a Windows-based tool-chain for a STM32F401C Discovery Board using Eclipse CDT 4.5.1 (Mars), ARM GCC 4.9 2015q3, GnuArmEclipse-Plugin by Liviu Ionescu, OpenOCD 0.9.0 and STM32CubeMX 4.11.0.

So basically, I generated an SW4STM32 project using STM32CubeMX, imported everything into a new Cross ARM GCC Eclipse C Project and adapted some parameters, i.e. symbols, CPU type, FPU and link file. A simple LED blink example is built successfully and can be executed by OpenOCD, breakpoints work as well.

In order to provide the system calls required by newlib or newlib nano, e.g., _sbrk and _write, I started with --specs=nosys.specs linker option. This way dynamic memory allocation works just fine. However, I would like to implement the _write function myself in order to print to a display or send data via UART.

In libgloss the _write function has got a weak symbol, so it should be sufficient to re-implement it. I did this in my main.c:

int _write (int fd, char *ptr, int len)
{
  HAL_GPIO_TogglePin(LD5_GPIO_Port, LD5_Pin);
  HAL_Delay(300);
  return len;
}

int main(void)
{
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* Configure the system clock */
  SystemClock_Config();

  /* Initialize all configured peripherals */
  MX_GPIO_Init();

  HAL_GPIO_TogglePin(LD5_GPIO_Port, LD5_Pin);
  printf("Blink");

  while(1)
  {
    // do nothing
  }
}

I expected the LED to blink, but only the toggle command within the main function itself showed an effect.

Why is my write function not called? Did I miss something?


Solution

  • You must either flush the buffer explicitly to the output (fflush(stdout)) or send a line feed ('\n').