title: STM32F4 microcontroller serial wire debug not working
body: <p>I am using the STM32F4 discovery board - <a href="http://www.st.com/st-web-ui/static/active/en/resource/technical/document/data_brief/DM00037955.pdf" rel="nofollow noreferrer">http://www.st.com/st-web-ui/static/active/en/resource/technical/document/data_brief/DM00037955.pdf</a></p>
  <p>And I am trying to debug via &quot;printf&quot;-like statements using the Serial Wire Viewer in the ST Micro STLink software: <a href="http://www.st.com/st-web-ui/static/active/en/resource/technical/document/user_manual/CD00262073.pdf" rel="nofollow noreferrer">http://www.st.com/st-web-ui/static/active/en/resource/technical/document/user_manual/CD00262073.pdf</a></p>
  <p>However I cannot see any results in my SWO Viewer despite setting the system clock to 168000000 Hz and stimulus port to 'All'. The (relevant) software I have running on the chip is below. This demo is set up to change the LED lights based on pressing the user button.</p>
  <pre><code>static uint8_t lastButtonStatus = RESET;
  
  int main() {
      init();
  
      do {
          loop();
      } while (1);
  }
  
  void init() {
      initLeds();
      initButton();
  }
  
  void loop() {
      static uint32_t counter = 0;
  
      uint8_t currentButtonStatus = GPIO_ReadInputDataBit(GPIOA, USER_BUTTON);
  
      if (lastButtonStatus != currentButtonStatus
              &amp;&amp; currentButtonStatus != RESET) {
          ++counter;
          GPIO_ResetBits(GPIOD, LEDS);
          GPIO_SetBits(GPIOD, LED[counter % 4]);
          // Test SWD output
          SWV_puts(&quot;hello from stm32f4\n&quot;);
          SWV_printfloat(1.98254, 2);
      }
      lastButtonStatus = currentButtonStatus;
  }
  </code></pre>
  <p>Here are the <code>SWV_</code> printing functions:</p>
  <pre><code>void SWV_puts(const char *s )
  {
      while (*s) ITM_SendChar(*s++);
  
  }
  
  /**
    * @brief   This function sends numbers to the serial wire viewer.
    * @param  number: number to be displayed on SWV
    * @retval None
    */
  void SWV_printnum(long number)
  {
   unsigned char buf[8 * sizeof(long)]; // Assumes 8-bit chars.
   unsigned int i = 0;
  
   //if number is 0
   if (number == 0)
   {
    ITM_SendChar('0'); //if number is zero
       return;
   }
   //account for negative numbers
   if (number &lt; 0)
   {
    ITM_SendChar('-');
    number = number * -1;
   }
   while(number &gt; 0)
   {
    buf[i++] = number % 10; //display in base 10
    number = number / 10;
    //NOTE: the effect of i++ means that the i variable will be at number of digits + 1
   }
   for(; i &gt; 0; i--)
   {
    ITM_SendChar((char)('0' + buf[i-1]));
   }
  }
  
  /**
    * @brief  This function sends numbers to the serial wire viewer.
    * @param  number: number to be displayed on SWV
    * @param  digits: number of digits after decimal point
    * @retval None
    */
  void SWV_printfloat(double number, int digits)
  {
   int i = 0;
   //handle negative numbers
   if(number &lt; 0.0)
   {
    ITM_SendChar('-');
    number = -number;
   }
   //round correctly so that uart_printfloat(1.999, 2) shows as &quot;2.00&quot;
   double rounding = 0.5;
   for(i = 0; i &lt; digits; ++i) rounding = rounding / 10.0;
   number = number + rounding;
  
   //extract the integer part of the number and print it
   unsigned long int_part = (unsigned long) number;
   double remainder = (double)(number - (double)int_part);
   SWV_printnum(int_part); //print the integer part
   if(digits &gt; 0) ITM_SendChar('.'); //print decimal pint
   int toprint;
   while(digits-- &gt; 0)
   {
    remainder = remainder * 10.0;
    toprint = (int)remainder;
    SWV_printnum(toprint);
    remainder = remainder - toprint;
   }
  
  }
  </code></pre>
  <p>I can confirm that the code compiles with no errors or warnings.</p>