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.h@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.h 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 "platform/FileHandle.h" |
0x6d61726b | 0:7fca1bf48117 | 28 | #include "SeggerRTT/SEGGER_RTT.h" |
0x6d61726b | 0:7fca1bf48117 | 29 | |
0x6d61726b | 0:7fca1bf48117 | 30 | namespace mbed { |
0x6d61726b | 0:7fca1bf48117 | 31 | |
0x6d61726b | 0:7fca1bf48117 | 32 | /** Class SeggerRTT |
0x6d61726b | 0:7fca1bf48117 | 33 | * |
0x6d61726b | 0:7fca1bf48117 | 34 | * An interface to perform file-like operations for Segger RTT. |
0x6d61726b | 0:7fca1bf48117 | 35 | * The "file" in this implementation is the Segger RTT terminal itself. |
0x6d61726b | 0:7fca1bf48117 | 36 | */ |
0x6d61726b | 0:7fca1bf48117 | 37 | class SeggerRTT : public FileHandle { |
0x6d61726b | 0:7fca1bf48117 | 38 | public: |
0x6d61726b | 0:7fca1bf48117 | 39 | /** Write the contents of a buffer to the terminal |
0x6d61726b | 0:7fca1bf48117 | 40 | * |
0x6d61726b | 0:7fca1bf48117 | 41 | * @param buffer The buffer to write from |
0x6d61726b | 0:7fca1bf48117 | 42 | * @param size The number of bytes to write |
0x6d61726b | 0:7fca1bf48117 | 43 | * @return The number of bytes written, negative error on failure |
0x6d61726b | 0:7fca1bf48117 | 44 | */ |
0x6d61726b | 0:7fca1bf48117 | 45 | virtual ssize_t write(const void *buffer, size_t size); |
0x6d61726b | 0:7fca1bf48117 | 46 | |
0x6d61726b | 0:7fca1bf48117 | 47 | /** Read the contents from the terminal into a buffer (blocking access) |
0x6d61726b | 0:7fca1bf48117 | 48 | * |
0x6d61726b | 0:7fca1bf48117 | 49 | * @param buffer The buffer to read in to |
0x6d61726b | 0:7fca1bf48117 | 50 | * @param size The number of bytes to read |
0x6d61726b | 0:7fca1bf48117 | 51 | * @return The number of bytes read, 0 at end of file, negative error on failure |
0x6d61726b | 0:7fca1bf48117 | 52 | */ |
0x6d61726b | 0:7fca1bf48117 | 53 | virtual ssize_t read(void *buffer, size_t size); |
0x6d61726b | 0:7fca1bf48117 | 54 | |
0x6d61726b | 0:7fca1bf48117 | 55 | /** Move the file position to a given offset from a given location |
0x6d61726b | 0:7fca1bf48117 | 56 | * (this operation is not supported by SeggerRTT) |
0x6d61726b | 0:7fca1bf48117 | 57 | * |
0x6d61726b | 0:7fca1bf48117 | 58 | * @return -ESPIPE (illegal seek) |
0x6d61726b | 0:7fca1bf48117 | 59 | */ |
0x6d61726b | 0:7fca1bf48117 | 60 | virtual off_t seek(off_t offset, int whence = SEEK_SET) { |
0x6d61726b | 0:7fca1bf48117 | 61 | return -ESPIPE; |
0x6d61726b | 0:7fca1bf48117 | 62 | } |
0x6d61726b | 0:7fca1bf48117 | 63 | |
0x6d61726b | 0:7fca1bf48117 | 64 | /** Get the size of the file |
0x6d61726b | 0:7fca1bf48117 | 65 | * (this operation is not supported by SeggerRTT) |
0x6d61726b | 0:7fca1bf48117 | 66 | * |
0x6d61726b | 0:7fca1bf48117 | 67 | * @return -EINVAL (invalid argument) |
0x6d61726b | 0:7fca1bf48117 | 68 | */ |
0x6d61726b | 0:7fca1bf48117 | 69 | virtual off_t size() { |
0x6d61726b | 0:7fca1bf48117 | 70 | return -EINVAL; |
0x6d61726b | 0:7fca1bf48117 | 71 | } |
0x6d61726b | 0:7fca1bf48117 | 72 | |
0x6d61726b | 0:7fca1bf48117 | 73 | /** Check if the file in an interactive terminal device |
0x6d61726b | 0:7fca1bf48117 | 74 | * |
0x6d61726b | 0:7fca1bf48117 | 75 | * @return true (SeggerRTT is a terminal) |
0x6d61726b | 0:7fca1bf48117 | 76 | */ |
0x6d61726b | 0:7fca1bf48117 | 77 | virtual int isatty() { |
0x6d61726b | 0:7fca1bf48117 | 78 | return true; |
0x6d61726b | 0:7fca1bf48117 | 79 | } |
0x6d61726b | 0:7fca1bf48117 | 80 | |
0x6d61726b | 0:7fca1bf48117 | 81 | /** Close the file to the terminal |
0x6d61726b | 0:7fca1bf48117 | 82 | * |
0x6d61726b | 0:7fca1bf48117 | 83 | * @return 0 on success (no explicit open/close possible) |
0x6d61726b | 0:7fca1bf48117 | 84 | */ |
0x6d61726b | 0:7fca1bf48117 | 85 | virtual int close() { |
0x6d61726b | 0:7fca1bf48117 | 86 | return 0; |
0x6d61726b | 0:7fca1bf48117 | 87 | } |
0x6d61726b | 0:7fca1bf48117 | 88 | |
0x6d61726b | 0:7fca1bf48117 | 89 | /** Check for poll event flags |
0x6d61726b | 0:7fca1bf48117 | 90 | * Call is non-blocking - returns instantaneous state of events. |
0x6d61726b | 0:7fca1bf48117 | 91 | * |
0x6d61726b | 0:7fca1bf48117 | 92 | * @param events bitmask of poll events we're interested in - POLLIN/POLLOUT etc. |
0x6d61726b | 0:7fca1bf48117 | 93 | * |
0x6d61726b | 0:7fca1bf48117 | 94 | * @returns bitmask of poll events that have occurred. |
0x6d61726b | 0:7fca1bf48117 | 95 | */ |
0x6d61726b | 0:7fca1bf48117 | 96 | virtual short poll(short events) const; |
0x6d61726b | 0:7fca1bf48117 | 97 | }; |
0x6d61726b | 0:7fca1bf48117 | 98 | } |