title: Stm32 printf over usb cdc
body: <p>Currently i am developing a software and i am stuck. I need some help.</p>
  
  <p>I am using stm32f103, hal and library freertos.</p>
  
  <p>I wrote a little serial debugger fucntion that sends chars over uart or usb cdc.</p>
  
  <p>Here is my definetions:</p>
  
  <p>in the header file:</p>
  
  <pre><code>#if ( DEBUG == DEBUG_ENABLE )
      /* printf definetion */
      #define DEBUG_MSG( FLAG, fmt, ...)                                                                          
      do
      {
          if( FLAG )
              printf( "[%08d]%s:%d:%s(): " fmt "\r\n", HAL_GetTick(), __FILE__, __LINE__, __func__, ##__VA_ARGS__);
      }while( 0 );
  #else
      #define DEBUG_MSG( FLAG, fmt, ... )
  #endif
  </code></pre>
  
  <p>and in the c file:</p>
  
  <pre><code>#define PUTCHAR_PROTOTYE int fputc( int ch, FILE *f )
  
  PUTCHAR_PROTOTYE
  {
  #if DEBUG_PORT == USB_DEBUG
      CDC_Transmit_FS((uint8_t *)&amp;ch, 1);
  #elif DEBUG_PORT == UART_DEBUG
      HAL_UART_Transmit( pDEBUG_PORT, ( uint8_t * )&amp;ch, 1, 0xFFFF );
  #else
      #error "Define a debug port!"
  #endif
      return ch;
  }
  </code></pre>
  
  <p>I am using this like: <code>DEBUG_MSG(MAIN_DEBUG, "Gsm manager thread started!");</code>
  When debugger port is uart_debug there is no problem at all. Everything runs perfectly.</p>
  
  <p>But whenever i select usb_debug port, there some thing happing.</p>
  
  <p>While debug port is usb cdc, i am getting some text like below:</p>
  
  <blockquote>
    <p>"[0[0[0[0[6"</p>
  </blockquote>
  
  <p>But in the debug mode, in putc function i am putting a break point at <code>CDC_Transmit_FS((uint8_t *)&amp;ch, 1);</code> line, and pressing F5 and i am getting:</p>
  
  <blockquote>
    <p>[00010046]../Src/main.c:633:gsmmanager_handler(): Gsm manager thread started!</p>
  </blockquote>
  
  <p>string just like as i expecting. And if i put <code>CDC_Transmit_FS((uint8_t *)"test", strlen("test"));</code> lines right after <code>CDC_Transmit_FS((uint8_t *)&amp;ch, 1);</code> and  i am getting:</p>
  
  <blockquote>
    <p>[0[0test</p>
  </blockquote>
  
  <p>string.</p>
  
  <p>So i dont understand anything. What could it be?</p>