This is a FileHandle interface implementation and retarget registration of the stdio interface to the Segger Real-Time Terminal (RTT) instead of the default serial port. Retargeting will automatically take place if this library is added to the project.
Dependents: 3_Test_AFE 1_Test_Flash_ADC_RTT
retarget_segger_rtt.cpp@0:7fca1bf48117, 2019-05-18 (annotated)
- Committer:
- 0x6d61726b
- Date:
- Sat May 18 22:41:10 2019 +0000
- Revision:
- 0:7fca1bf48117
updated to Segger RTT version 6.44i
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
0x6d61726b | 0:7fca1bf48117 | 1 | //---------------------------------------------------------------------------- |
0x6d61726b | 0:7fca1bf48117 | 2 | // Namespace: mbed |
0x6d61726b | 0:7fca1bf48117 | 3 | // Class: SeggerRTT |
0x6d61726b | 0:7fca1bf48117 | 4 | // Description: Alternative stdio read/write interface function for Segger RTT |
0x6d61726b | 0:7fca1bf48117 | 5 | // Copyright: (c) 2017-2019 Mark <0x6d61726b@gmail.com> |
0x6d61726b | 0:7fca1bf48117 | 6 | // License: MIT License |
0x6d61726b | 0:7fca1bf48117 | 7 | // SVN: $Id: retarget_segger_rtt.cpp 382 2019-05-18 22:37:59Z 0x6d61726b $ |
0x6d61726b | 0:7fca1bf48117 | 8 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
0x6d61726b | 0:7fca1bf48117 | 9 | // Permission is hereby granted, free of charge, to any person obtaining a |
0x6d61726b | 0:7fca1bf48117 | 10 | // copy of this software and associated documentation files (the "Software"), |
0x6d61726b | 0:7fca1bf48117 | 11 | // to deal in the Software without restriction, including without limitation |
0x6d61726b | 0:7fca1bf48117 | 12 | // the rights to use, copy, modify, merge, publish, distribute, sublicense, |
0x6d61726b | 0:7fca1bf48117 | 13 | // and/or sell copies of the Software, and to permit persons to whom the |
0x6d61726b | 0:7fca1bf48117 | 14 | // Software is furnished to do so, subject to the following conditions: |
0x6d61726b | 0:7fca1bf48117 | 15 | // |
0x6d61726b | 0:7fca1bf48117 | 16 | // The above copyright notice and this permission notice shall be included in |
0x6d61726b | 0:7fca1bf48117 | 17 | // all copies or substantial portions of the Software. |
0x6d61726b | 0:7fca1bf48117 | 18 | // |
0x6d61726b | 0:7fca1bf48117 | 19 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
0x6d61726b | 0:7fca1bf48117 | 20 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
0x6d61726b | 0:7fca1bf48117 | 21 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
0x6d61726b | 0:7fca1bf48117 | 22 | // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
0x6d61726b | 0:7fca1bf48117 | 23 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
0x6d61726b | 0:7fca1bf48117 | 24 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
0x6d61726b | 0:7fca1bf48117 | 25 | // DEALINGS IN THE SOFTWARE. |
0x6d61726b | 0:7fca1bf48117 | 26 | //---------------------------------------------------------------------------- |
0x6d61726b | 0:7fca1bf48117 | 27 | #include "retarget_segger_rtt.h" |
0x6d61726b | 0:7fca1bf48117 | 28 | #include "platform/mbed_retarget.h" |
0x6d61726b | 0:7fca1bf48117 | 29 | #include "platform/mbed_poll.h" |
0x6d61726b | 0:7fca1bf48117 | 30 | |
0x6d61726b | 0:7fca1bf48117 | 31 | namespace mbed { |
0x6d61726b | 0:7fca1bf48117 | 32 | |
0x6d61726b | 0:7fca1bf48117 | 33 | // retarget stdin/stdout/stderr to Segger RTT |
0x6d61726b | 0:7fca1bf48117 | 34 | static SeggerRTT fhSeggerRtt; |
0x6d61726b | 0:7fca1bf48117 | 35 | FileHandle *mbed_target_override_console(int fd) { |
0x6d61726b | 0:7fca1bf48117 | 36 | return &fhSeggerRtt; |
0x6d61726b | 0:7fca1bf48117 | 37 | } |
0x6d61726b | 0:7fca1bf48117 | 38 | |
0x6d61726b | 0:7fca1bf48117 | 39 | ssize_t SeggerRTT::write(const void *buffer, size_t size) { |
0x6d61726b | 0:7fca1bf48117 | 40 | // write the buffer content to the terminal |
0x6d61726b | 0:7fca1bf48117 | 41 | return SEGGER_RTT_Write(0, buffer, size); |
0x6d61726b | 0:7fca1bf48117 | 42 | } |
0x6d61726b | 0:7fca1bf48117 | 43 | |
0x6d61726b | 0:7fca1bf48117 | 44 | ssize_t SeggerRTT::read(void *buffer, size_t size) { |
0x6d61726b | 0:7fca1bf48117 | 45 | // wait until a character becomes available |
0x6d61726b | 0:7fca1bf48117 | 46 | while (!SEGGER_RTT_HasKey()) {} |
0x6d61726b | 0:7fca1bf48117 | 47 | |
0x6d61726b | 0:7fca1bf48117 | 48 | // read the characters from the terminal into the buffer |
0x6d61726b | 0:7fca1bf48117 | 49 | return SEGGER_RTT_Read(0, buffer, size); |
0x6d61726b | 0:7fca1bf48117 | 50 | } |
0x6d61726b | 0:7fca1bf48117 | 51 | |
0x6d61726b | 0:7fca1bf48117 | 52 | short SeggerRTT::poll(short events) const { |
0x6d61726b | 0:7fca1bf48117 | 53 | short revents = 0; |
0x6d61726b | 0:7fca1bf48117 | 54 | if ((events & POLLIN) && SEGGER_RTT_HasKey()) { |
0x6d61726b | 0:7fca1bf48117 | 55 | revents |= POLLIN; |
0x6d61726b | 0:7fca1bf48117 | 56 | } |
0x6d61726b | 0:7fca1bf48117 | 57 | if (events & POLLOUT) { |
0x6d61726b | 0:7fca1bf48117 | 58 | revents |= POLLOUT; |
0x6d61726b | 0:7fca1bf48117 | 59 | } |
0x6d61726b | 0:7fca1bf48117 | 60 | return revents; |
0x6d61726b | 0:7fca1bf48117 | 61 | } |
0x6d61726b | 0:7fca1bf48117 | 62 | } |