Table des matières

"Printf" sur STM32

Selon les environnements de développement les “printf” ne sont pas actif pour les STM32.

Tuto

1. Mapper les pins de USART à l'aide de “CubeMX”

2. Ajouter le fichier source “syscalls.c” et des options de linkage.

 /* Placeholder to list other libraries required by the application. GROUP() */
 GROUP(
   libgcc.a
   libg.a
   libc.a
   libm.a
   libnosys.a
 )

3. Modifier le fichier de configuration de UART (USART.c):

#ifdef __GNUC__
/* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
   set to 'Yes') calls __io_putchar() */
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */

et coller ceci entre /* USER CODE BEGIN 1 */ et /* USER CODE END 1 */ :

/**
  * @brief  Retargets the C library printf function to the USART.
  * @param  None
  * @retval None
  */
PUTCHAR_PROTOTYPE
{
  /* Place your implementation of fputc here */
  /* e.g. write a character to the USART2 and Loop until the end of transmission */
  HAL_UART_Transmit(&huart2, (uint8_t *)&ch, 1, 0xFFFF);
 
  return ch;
}

Vérifier qu'il n'y est pas d'erreur au niveau de HAL_UART_Transmit(&huart2, (uint8_t *)&ch, 1, 0xFFFF);
Si erreur il y a il faut récupérer la variable en haut du fichier nomé UART_HandleTypeDef huart2;

Finir en ajoutant #include <stdio.h> dans le “main.c”.

4. Activer les options de compilation et linker:

5. Test.

exemple :

printf("salut \n");
      HAL_Delay(1000);
 
    Le retour chariot et important!!!