test
Fork of SWO by
Diff: SWO.cpp
- Revision:
- 4:53de8ef789f3
- Parent:
- 3:e5af2e131b95
diff -r e5af2e131b95 -r 53de8ef789f3 SWO.cpp --- a/SWO.cpp Tue Dec 23 21:05:52 2014 +0000 +++ b/SWO.cpp Thu Aug 24 18:15:02 2017 +0000 @@ -1,6 +1,7 @@ /* mbed SWO Library * Copyright (c) 2014, v01: WH. Ported from Segger example (www.segger.com) * v02: WH. Added Class with Stream support + * 2017, v03: WH,PS. Added stream claim for stdout, proposed by Pavel Sorejs * * Simple implementation for tracing via Serial Wire Output(SWO) for Cortex-M processors. * It can be used with Host PC software such as ST-LINK Utility or Segger J-Link SWO viewer. @@ -35,9 +36,10 @@ /** Create and SWO interface for debugging that supports Stream * @brief Currently works on nucleo ST-LINK using ST-Link Utility and other devices that support SWD/SWO using Segger SWO viewer */ -SWO_Channel::SWO_Channel () { +SWO_Channel::SWO_Channel (const char *name) : Stream(name) { //May want to add initialisation stuff here } + /** Write a single character (Stream implementation) * @@ -59,6 +61,35 @@ return -1; } + /** + * Claim and redirect a stream to this SWO object + * Important: A name parameter must have been added when creating the SWO object. + * + * @param FILE *stream The stream to redirect (default = stdout) + * @return true if succeeded, else false + */ +bool SWO_Channel::claim (FILE *stream) { + if ( FileBase::getName() == NULL) { + error("claim requires a name to be given in the instantiator of the SWO instance!\r\n"); + } + + //Add '/' before name: + char *path = new char[strlen(FileBase::getName()) + 2]; + sprintf(path, "/%s", FileBase::getName()); + + if (freopen(path, "w", stream) == NULL) { + // Failed, should not happen + return false; + } + + delete(path); + + //No buffering + setvbuf(stream, NULL, _IONBF, 32); + return true; +} + + // //This is the classic implementation