Search code examples
cstm32stm32f4coocox

stm32 c, export prototype to rest of project


I'm developing a little project (c) in stm32f407vg and following the UART tutorial in:

http://letanphuc.net/2015/09/stm32f0-uart-tutorial-5/#comment-346

My problem is with the function prototype:

/* Includes ——————————————————————*/
#include “usart.h”
#include “gpio.h”

/* Private function prototypes ———————————————–*/
#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__ */

/**
* @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 USART */
HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 100);
return ch;
}
/* USER CODE END PFP */

UART_HandleTypeDef huart1;

/* USART1 init function */

void MX_USART1_UART_Init(void)
{
…
..
..

How I have to do the declaration in usart.h so I can use printf() the rest of the project?

Thanks.

EDIT: 2017/01/20 response to Guillaume Michel

I've put in usart.h

#ifndef __usart_H
#define __usart_H

/* Includes ------------------------------------------------------------------*/
#include "stm32f4xx_hal.h"
#include "globals.h"

extern UART_HandleTypeDef huart1;

/* **********************************************
 *
 * **********************************************/
#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__ */

and the PUTCHAR_PROTOTYPE at usart.c:

/* Includes ------------------------------------------------------------------*/
#include "usart.h"
#include "gpio.h"

#include "string.h"


PUTCHAR_PROTOTYPE
{
    /* Place your implementation of fputc here */
    /* e.g. write a character to the USART */
    HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 100);
    return ch;
}
/* USER CODE END PFP */

//UART_HandleTypeDef huart1;

/* USART1 init function */

void MX_USART1_UART_Init(void){
..
..
}

And in main.c:

/* Includes ------------------------------------------------------------------*/
#include "globals.h"
#include "stm32f4xx_hal.h"
#include "syscfg.h"
#include "can.h"
#include "usart.h"
#include "gpio.h"

#include "kernel.h"

#include <stdio.h>

int main(void){
    SysIniCfg();
    printf("Hola");
    while (1){

        //kernelMotor();

        HAL_GPIO_TogglePin(LED_G_GPIO_Port,LED_G_Pin);

    }
}

I have tried other places to put the two sections of the code but this is the only one I don't get warnings or errors


Solution

  • I am pretty sure there is a catch, otherwise you wouldn't ask the question, but I think that if you cut and paste the code below in your uart.h, that should work.

    #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__ */
    

    Don't forget to include uart.h everywhere you call printf