PrintF via SWO

Post here first, or if you can't find a relevant section!
DrBanana
Posts: 51
Joined: Fri Apr 22, 2016 11:15 am

PrintF via SWO

Post by DrBanana » Thu Oct 06, 2016 1:19 pm

I usually program stm32 board with ST Link, but problem is, I cannot debug as its not connected through Serial. Now Im wondering if there is method that can send strings from MCU to PC through SWO, so I can use ST Link Utility and its "PrintF via SWO" ?

stevestrong
Posts: 1500
Joined: Mon Oct 19, 2015 12:06 am
Location: Munich, Germany

Re: PrintF via SWO

Post by stevestrong » Thu Oct 06, 2016 2:40 pm

Well, if you just want to debug, you have already the right tool (STlink adapter) in your hand. You just need to mix it with a bit of SW on you PC (Eclipse + OpenOCD) and there you have it.

The SWO feature would be interesting for me as well.

User avatar
RogerClark
Posts: 6662
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: PrintF via SWO

Post by RogerClark » Thu Oct 06, 2016 8:53 pm

There is a way to send back debug strings STLink ( or any SWD programmer), but from what I have seen, it doesnt seem to be widely used.

Why not just hook up a USB to Serial adaptor to USART1 ( PA9 and PA10),

User avatar
Slammer
Posts: 241
Joined: Tue Mar 01, 2016 10:35 pm
Location: Athens, Greece

Re: PrintF via SWO

Post by Slammer » Thu Oct 06, 2016 9:39 pm

stdio through SWO requires some initialization during startup and a function based on CMSIS, the ITM_SendChar(), with this it is very easy to expand a stream class to use it as putchar. The extra code for using SWO is more or less the same as UART and you need a special program to read it (openocd, ST Link Utility, etc).
I have tested with the arm eclipse plugin (it can be enabled during creation of the project), but finally I think that using a uart for this is a better option. Personally for difficult points I prefer to debug with ST-Link directly (with breakpoints, step execution, etc)
While SWO is working with Nucleo boards, this is not working with ST-Link V2 clones as the SWO does not exist.

User avatar
martinayotte
Posts: 1213
Joined: Mon Apr 27, 2015 1:45 pm

Re: PrintF via SWO

Post by martinayotte » Fri Oct 07, 2016 1:50 pm

There someone who did hacked STLink clones to add a SWO line on them :
http://www.eevblog.com/forum/microcontr ... nk-clones/

Ollie
Posts: 183
Joined: Thu Feb 25, 2016 7:27 pm

Re: PrintF via SWO

Post by Ollie » Fri Oct 07, 2016 3:10 pm

Personally, I like debugging with ST-Link. There are many use cases where the tracking print statements do not work.

If one day, Arduino or stm32duino will have the debugger support, I hope that it would go one step further and leverage all the capabilities built in the M3 platform. Please, take a look of the following blog explaining and demonstrating these capabilities.

http://essentialscrap.com/tips/arm_trace/theory.html

Cheers, Ollie

User avatar
Pito
Posts: 1494
Joined: Sat Mar 26, 2016 3:26 pm
Location: Rapa Nui

Re: PrintF via SWO

Post by Pito » Fri Oct 07, 2016 4:24 pm

There someone who did hacked STLink clones to add a SWO line on them :

interesting the last post in the thread :)
I've figured it out, but still waiting with pulling the trigger :) (as I have currently only a single stlink dongle handy..)
Pukao Hats Cleaning Services Ltd.

User avatar
RogerClark
Posts: 6662
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: PrintF via SWO

Post by RogerClark » Fri Oct 07, 2016 8:07 pm

Ollie

There is an active thread by @stevestrong where he now uses Eclipse + an Arduino plugin.

This may be the best of both worlds, but it looks like the install is a little tricky.

I dont think the Arduino IDE is going to get debugger support any time in the near future, as for AVR Arduino now recommends the use of Atmel Studio ( aka AVR Studio) if you want debugger support, and surprisingly the Arduino team seem to think debug support is not something beginners would want ( but I think debug support makes things easier for beginners not harder, even web browsers now come with Javascript debuggers by default)

victor_pv
Posts: 1599
Joined: Mon Apr 27, 2015 12:12 pm

Re: PrintF via SWO

Post by victor_pv » Sun Jan 15, 2017 2:57 am

DrBanana wrote:I usually program stm32 board with ST Link, but problem is, I cannot debug as its not connected through Serial. Now Im wondering if there is method that can send strings from MCU to PC through SWO, so I can use ST Link Utility and its "PrintF via SWO" ?
I decided to write today a short swo driver based on those pieces from mbed, opencm3, cmsis, and the printf implementation from Slammer from another thread. SWO inherits from the print class so can also use the usual println and so on.

I have not tested it cause I dont have a probe with SWO at hand, so if anyone has one and can test it, would be great. All I did was compile it.
My understanding is that stlink and jlink enable swo in the mcu by themselves, so currently that's not set from the code, but the libopen implementation does it and we just need to copy or write a similar function. I used the mbed swo library one as template, which in turn seems to be based on the libopencm3 one, and includes a commented out setup() from it, but would likely need more registers to be definded.
swo.zip
(6.58 KiB) Downloaded 51 times
This is an example sketch, the dac test sketch Andy passed me to test the DAC using SWO.

Code: Select all

/*
 * Simple DAC test.
 *
 * Author: Marti Bolivar <mbolivar@leaflabs.com>
 *
 * This file is released into the public domain.
 */
//# define STM32_HAVE_DAC    1
// #include <wirish/wirish.h>
#include <libmaple/dac.h>
#include "swo.h"
#define BOARD_LED_PIN PC13



SWO_Channel SWO;
#define  SWO_ErrLog(...) SWO.printf(__VA_ARGS__);

//#define  SWO_ErrLog(...)

uint16 count = 0;

void setup() {
    pinMode(BOARD_LED_PIN, OUTPUT);
    digitalWrite(BOARD_LED_PIN, HIGH);

    Serial.begin(115200);
    Serial.println("**** Beginning DAC test");

    Serial.print("Init... ");
    pinMode(PA4,OUTPUT);
    pinMode(PA5,OUTPUT);
    
    dac_init(DAC, DAC_CH1 | DAC_CH2);
    Serial.println("Done.");
}

void loop() {
    
    delay(10);

    count += 1;
    if (count > 4095) {
        count = 0;
        toggleLED();
    }
    Serial.println(count);
    SWO_ErrLog("Count is %d \r\n", count);
    dac_write_channel(DAC, 1, 4095 - count);
    dac_write_channel(DAC, 2, count);
}


void toggleLED() {
  digitalWrite(BOARD_LED_PIN,(!digitalRead(BOARD_LED_PIN)));
}

victor_pv
Posts: 1599
Joined: Mon Apr 27, 2015 12:12 pm

Re: PrintF via SWO

Post by victor_pv » Sun Feb 19, 2017 6:11 pm

I just had some time to test the SWO class with a j-link and an st-link, works fine on both.
It nicely prints information at about 2Mhz in stlink, and 4.5Mhz in swo viewer.

Now I do have a problem with #define and ifdef in the arduino IDE.

This works fine in both Eclipse and Arduino IDE if I define SWODEBUG, but if I comment it out "#define SWODEBUG" to eliminate the debug output, then in Arduino it doesn't compile:

Code: Select all

#define SWODEBUG

#ifdef SWODEBUG
  SWO_Channel SWO;
  #define  SWO_ErrLog(...) SWO.printf(__VA_ARGS__); 

#else /* Not DEBUG */
  #define  SWO_ErrLog(...)
#endif
Anyone has any idea why it doesn't work in Arduino?

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests