cancel
Showing results for 
Search instead for 
Did you mean: 

stm32CubeMX stm32f407 discovery printf doesn't work

nikola1010
Associate II
Posted on December 15, 2015 at 10:46

Hi all,

trying to retarget printf on stm32f407 discovery with cubemx generated code isn't working.

Inside main.c i've put:

/**

  * @brief  Retargets the C library printf function to the USART.

  * @param  None

  * @retval None

  */

int fputc(int ch, FILE *f){

  /* Place your implementation of fputc here */

  /* e.g. write a character to the USART */

  HAL_UART_Transmit(&huart6,(uint8_t*)&ch,1,1);// 1 tick waiting (1ms) enough for 87us/byte at 115200

  return ch;

}

HAL_UART_Transmit

works without problem on it's own, printf doesn't output.

Could anyone help  please?

3 REPLIES 3
markb
Associate II
Posted on December 15, 2015 at 11:03

Hi,

Perhaps printf() doesn't use fputc() for its output?

I am providing an implementation of _write(fd, buf, count) which sends the output to USB CDC and printf works fine.

Cheers,

Mark

nikola1010
Associate II
Posted on December 15, 2015 at 13:56

Thanks for hint!

I did the same and it works.

Here is the code for others:

int _write( int file, char *ptr, int len ){

    int i;

    for(i = 0 ; i < len ; i++){

        HAL_UART_Transmit(&huart6,(uint8_t*)&ptr[i],1,10);

    }

    char ch='\r';

    HAL_UART_Transmit(&huart6,(uint8_t*)&ch,1,10);

}

gaurav2
Associate II
Posted on June 08, 2016 at 19:07

Hello all,

I am trying to get the UART output (send some character strings over UART via USB CDC class). My code is based upon CubeF0 codebase and HAL and USB_Device CDC class.

USBD_HandleTypeDef USBD_Device;

extern UART_HandleTypeDef UartHandle;

void main(void)

{

   uint8_t ch = 'a';

    HAL_Init();

    SystemClock_Config();

  

    /* Init Device Library */

    USBD_Init(&USBD_Device, &VCP_Desc, 0);

  

    /* Add Supported Class */

    USBD_RegisterClass(&USBD_Device, &USBD_CDC);

  

    /* Add CDC Interface Class */

    USBD_CDC_RegisterInterface(&USBD_Device, &USBD_CDC_fops);

  

    /* Start Device Process */

    USBD_Start(&USBD_Device);

    

    while (1) {

        status = HAL_UART_Transmit(&UartHandle, (uint8_t *)&ch, 1, 10);

    }

}

However, nothing comes out on the Hyper-terminal connected on Windows. The HAL_Transmit_UART is going through but I fear that somehow the UART transmit data struct is not connected to the USB CDC class buffer and/or some DMA issue. 

I don't want to use DMA but not sure how to just be in polling mode and if the UART (huart->Instance->TDR) in stmf0xx_hal_uart.c is really giving the character to the CDC.

I think that USB CDC class transmits a packet in the USBD_CDC_TransmitPacket() function where it calls

      USBD_LL_Transmit(pdev, CDC_IN_EP, hcdc->TxBuffer, hcdc->TxLength);

Do I instead need to use HAL_UART_Transmit_IT() to transmit in interrupt mode for USB to recognize that data is pending? Otherwise I don't see how the HAL_UART_Transmit() connects to USB CDC. The _IT() version seems to connect via the UserTxBuffer[].

Any pointers in making progress will help me. Thanks.