IO Stream CPC device usage#

Configuration Parameters#

The IO Stream CPC component behavior can be customized through the following configurable parameters:

Parameter

Default

Description

SL_IOSTREAM_CPC_TX_BUFFER_SIZE

128

Size of the transmit buffer for outgoing data

SL_IOSTREAM_CPC_RX_BUFFER_SIZE

256

Size of the receive buffer for incoming data

SL_IOSTREAM_CPC_TX_RETRY_TIMEOUT

2 ms

Time to wait before retrying a failed transmission

SL_IOSTREAM_CPC_DISCONNECT_TIMEOUT

500 ms

Time to wait before attempting to reconnect after disconnect

These parameters can be configured in the sl_iostream_cpc_config.h file within your project. When a project is generated using Silicon Labs tools [SLC], this configuration file is copied to your project directory where you can modify the values according to your application's requirements. The buffer sizes are particularly important for applications that need to handle larger amounts of data or have specific memory constraints.

Initialization#

#include "sl_iostream.h"
#include "sl_iostream_cpc.h"

void app_init(void)
{
  // Initialize the CPC iostream
  sl_iostream_cpc_init();

  // Note: sl_iostream_cpc_init() will set itself as the default stream
  // If you have multiple streams, you may need to explicitly set your default:
  // sl_iostream_set_default(sl_iostream_cpc_handle);
}

When initialized, iostream_cpc will set itself as the default system stream. If you have multiple streams in your project, you may need to explicitly set your preferred default stream after initialization to ensure the correct stream is used.

Reading and Writing#

#include "sl_iostream.h"
#include "sl_iostream_cpc.h"

void app_process_action(void)
{
  // Method 1: Write using the CPC handle directly
  sl_iostream_write(sl_iostream_cpc_handle, "Hello, world!\r\n", 15);

  // Method 2: Write using the default stream (if CPC is set as default)
  sl_iostream_write(SL_IOSTREAM_STDOUT, "Hello, world!\r\n", 15);

  // Method 3: Use formatted output
  sl_iostream_printf(sl_iostream_cpc_handle, "Value: %d\r\n", 42);

  // Read from the CPC iostream
  uint8_t buffer[64];
  size_t bytes_read;
  sl_status_t status = sl_iostream_read(sl_iostream_cpc_handle, buffer, sizeof(buffer), &bytes_read);

  if (status == SL_STATUS_OK && bytes_read > 0) {
    // Process the read data
  }
}

Default Stream Macros#

The IO Stream framework provides these macros for accessing default streams:

SL_IOSTREAM_STDIN  // Default input stream
SL_IOSTREAM_STDOUT // Default output stream
SL_IOSTREAM_STDERR // Default error stream

Using with RTOS#

In RTOS environments, each task can have its own default stream. By default, a task's stream is set to the system-wide default, but each task can change its default stream without affecting other tasks.

#include "sl_iostream.h"
#include "sl_iostream_cpc.h"

void my_task(void *p_arg)
{
  (void)p_arg;

  // Set this task's default stream to CPC
  sl_iostream_set_default(sl_iostream_cpc_handle);

  while (1) {
    // This will use the task's default stream (CPC)
    sl_iostream_printf(SL_IOSTREAM_STDOUT, "Task message\r\n");

    // Task processing
  }
}