Difference between revisions of "Embedded Studio Library IO"

From SEGGER Wiki
Jump to: navigation, search
(STD)
Line 36: Line 36:
 
Recommended for minimum size.
 
Recommended for minimum size.
   
==== SEMIHOST buffered ====
+
=== None ===
   
  +
No I/O mechanism implemented.
Output is written to a small buffer in target RAM and flushed to the debugger via semihosting when the buffer is filled or on end of output.
 
  +
Use user-supplied I/O mechanisms, such as output on a UART.
   
==== SEMIHOST unbufferd ====
+
==== User-supplied I/O ====
  +
''Note: This section applies to Embedded Studio V6.10 and later.''
   
  +
When no standard I/O mechanism is implemented, the user can supply an implementation of the low-level routines to do output and optionally input.
Output is immediately flushed to the debugger instead of buffered, which requires less RAM but is slower because the target is halted for every output operation.
 
   
  +
The functions to be implemented are:
=== STD ===
 
  +
int __SEGGER_RTL_X_file_write(__SEGGER_RTL_FILE *stream, const char *s, unsigned len); // Write data to file stream. >= 0: OK. < 0: Error.
  +
int __SEGGER_RTL_X_file_read(__SEGGER_RTL_FILE * stream, char * s, unsigned len); // Read data from file stream. >= 0: OK. < 0: Failed to read.
  +
int __SEGGER_RTL_X_file_bufsize(__SEGGER_RTL_FILE *stream); // Get stream buffer size. == 1: Unbuffered I/O. > 1: Size of buffer.
  +
int __SEGGER_RTL_X_file_unget(__SEGGER_RTL_FILE *stream, int c); // Push character back to stream. >= 0: OK. < 0: Error.
  +
int __SEGGER_RTL_X_file_stat(__SEGGER_RTL_FILE *stream); // Get file status. >= 0: OK. < 0: Invalid file stream.
   
  +
The default implementations are:
Use user-supplied I/O mechanisms, such as output on a UART.
 
  +
  +
int __SEGGER_RTL_X_file_write(__SEGGER_RTL_FILE *stream, const char *s, unsigned len) {
  +
while (len > 0) {
  +
UART_SendByte(*s++); // Output any stream to UART.
  +
--len;
  +
}
  +
return 0;
  +
}
   
  +
int __SEGGER_RTL_X_file_read(__SEGGER_RTL_FILE * stream, char * s, unsigned len) {
When STD is selected as Library I/O, the user has to supply an implementation of <code>int __SEGGER_RTL_X_file_write(__SEGGER_RTL_FILE *__stream, const char *__s, unsigned __len);</code> for output and <code>int __SEGGER_RTL_X_file_read(__SEGGER_RTL_FILE *__stream, char *__s, unsigned __len);</code> for input which can be used to route character output/input to any resource, such as a UART or other terminal.
 
  +
return EOF; // Read not implemented
  +
}
   
  +
int __SEGGER_RTL_X_file_bufsize(__SEGGER_RTL_FILE *stream) {
Return values are:
 
  +
return 1; // 1 for unbuffered I/O. Otherwise 80.
* >=0: OK
 
  +
}
* < 0: Error
 
   
  +
int __SEGGER_RTL_X_file_unget(__SEGGER_RTL_FILE *stream, int c) {
====STD Deprecated====
 
  +
return EOF; // Unget not implemented.
'''---'''''The following implementation was changed in ES V5.50. So for that a newer versions use the implementation above. For older versions the one below.'''''---'''
 
  +
}
   
  +
int __SEGGER_RTL_X_file_stat(__SEGGER_RTL_FILE *stream) {
When STD is selected as Library I/O, the user has to supply an implementation of <code>int __SEGGER_RTL_stdout_putc_std(int c);</code>
 
  +
return 0; // Assume only stdout/stdin/stderr is used and always valid.
which can be used to route character output to any resource, such as a UART or other terminal.
 
  +
}

Revision as of 14:03, 24 January 2022

The debugger and the Runtime Library of Embedded Studio support different mechanisms for debug input and output. The mechanism to be used in a project can easily be selected in the project options at Library -> Library I/O and does not require any additional file or configuration.

Library I/O Mechanisms

There are different mechanisms for different application requirements.

RTT

Use SEGGER Real-Time Transfer (RTT), which enables super fast output without halting the system. The target application writes the output string to a buffer in RAM. The J-Link reads the buffer while the target is running.

Suitable for applications with real-time requirements.

Available on all Cortex-M based systems.

SWO

The target application writes the output string to the ITM stimulus ports. The J-Link reads the SWO pin while the target is running.

Suitable for applications with real-time requirements.

Available on all Cortex-M based systems with a SWO pin.

SEMIHOST

Halt the target CPU for I/O operations. On halt, the debugger reads and executes the operation command and parameters.

Available on all targets (Cortex-M, Cortex-A, Cortex-R, RISC-V)

SEMIHOST host-formatted

With host formatting printf output is processed by the debugger instead of on the target. The debugger reads the format string and the parameters from the target and feeds it to its formatter to be printed. No code for formatting is required on the target, saving 1 - 3 kiloBytes of ROM.

Recommended for minimum size.

None

No I/O mechanism implemented. Use user-supplied I/O mechanisms, such as output on a UART.

User-supplied I/O

Note: This section applies to Embedded Studio V6.10 and later.

When no standard I/O mechanism is implemented, the user can supply an implementation of the low-level routines to do output and optionally input.

The functions to be implemented are:

 int __SEGGER_RTL_X_file_write(__SEGGER_RTL_FILE *stream, const char *s, unsigned len);  // Write data to file stream.     >= 0: OK. < 0: Error.
 int __SEGGER_RTL_X_file_read(__SEGGER_RTL_FILE * stream, char * s, unsigned len);       // Read data from file stream.    >= 0: OK. < 0: Failed to read.
 int __SEGGER_RTL_X_file_bufsize(__SEGGER_RTL_FILE *stream);                             // Get stream buffer size.        == 1: Unbuffered I/O. > 1: Size of buffer.
 int __SEGGER_RTL_X_file_unget(__SEGGER_RTL_FILE *stream, int c);                        // Push character back to stream. >= 0: OK. < 0: Error.
 int __SEGGER_RTL_X_file_stat(__SEGGER_RTL_FILE *stream);                                // Get file status.               >= 0: OK. < 0: Invalid file stream.

The default implementations are:

 int __SEGGER_RTL_X_file_write(__SEGGER_RTL_FILE *stream, const char *s, unsigned len) {
   while (len > 0) {
     UART_SendByte(*s++); // Output any stream to UART.
     --len;
   }
   return 0;
 }
 int __SEGGER_RTL_X_file_read(__SEGGER_RTL_FILE * stream, char * s, unsigned len) {
   return EOF; // Read not implemented
 }
 int __SEGGER_RTL_X_file_bufsize(__SEGGER_RTL_FILE *stream) {
   return 1; // 1 for unbuffered I/O. Otherwise 80.
 }
 int __SEGGER_RTL_X_file_unget(__SEGGER_RTL_FILE *stream, int c) {
   return EOF; // Unget not implemented.
 }
 int __SEGGER_RTL_X_file_stat(__SEGGER_RTL_FILE *stream) {
   return 0;  // Assume only stdout/stdin/stderr is used and always valid.
 }