1.2.4.2 Using The Library

The Console System Service allows the application/middleware to route messages/debug information to a console running on a host computer.

  • Depending on the application need, the size of the transmit and receive ring buffers can be configured

  • The read/write API called by the calling program returns immediately. For read requests, the read API returns the available bytes from the receive buffer. For write requests, the write API copies the application data to the transmit buffer.

  • For read, APIs are provided to query the number of bytes present in the receive buffer, the amount of free space available in the receive buffer and the size of the receive buffer.

  • Similarly, for writes, APIs are provided to query the number of bytes pending transmission in the transmit buffer, the amount of free space in the transmit buffer and the size of the transmit buffer.

Example Application to Read and Write from UART Console

#define UART_CONSOLE_NUM_BYTES_READ              10
#define UART_CONSOLE_READ_BUFFER_SIZE            10

uint8_t uart_console_read_buffer[UART_CONSOLE_READ_BUFFER_SIZE];

APP_DATA appData;

void APP_Initialize ( void )
{
    /* Place the App state machine in its initial state. */
    appData.state = APP_STATE_WAIT_UART_CONSOLE_CONFIGURED;
}

void APP_Tasks ( void )
{
    switch ( appData.state )
    {
        case APP_STATE_WAIT_UART_CONSOLE_CONFIGURED:
            if (SYS_CONSOLE_Status(SYS_CONSOLE_INDEX_0) == SYS_STATUS_READY)
            {
                appData.state = APP_STATE_GET_CONSOLE_HANDLE;
            }
            break;

        case APP_STATE_GET_CONSOLE_HANDLE:
            /* Get handles to both the USB console instances */
            appData.consoleHandle = SYS_CONSOLE_HandleGet(SYS_CONSOLE_INDEX_0);

            if (appData.consoleHandle != SYS_CONSOLE_HANDLE_INVALID)
            {
                appData.state = APP_STATE_READ_FROM_CONSOLE;
            }
            else
            {
                appData.state = APP_STATE_ERROR;
            }
            break;

        case APP_STATE_READ_FROM_CONSOLE:
            SYS_CONSOLE_PRINT("\n\rFree Space in RX Buffer = %d bytes", SYS_CONSOLE_ReadFreeBufferCountGet(appData.consoleHandle));
            SYS_CONSOLE_Print(appData.consoleHandle, "\n\rEnter %d characters:", UART_CONSOLE_NUM_BYTES_READ);
            appData.state = APP_STATE_WAIT_READ_COMPLETE;
            break;

        case APP_STATE_WAIT_READ_COMPLETE:
            /* Demonstrate SYS_CONSOLE_ReadCountGet() and SYS_CONSOLE_Read() APIs */

            if (SYS_CONSOLE_ReadCountGet(appData.consoleHandle) >= UART_CONSOLE_NUM_BYTES_READ)
            {
                SYS_CONSOLE_PRINT("\n\rFree Space in RX Buffer = %d bytes", SYS_CONSOLE_ReadFreeBufferCountGet(appData.consoleHandle));

                /* UART_CONSOLE_NUM_BYTES_READ or more characters are available. Read the data in the application buffer. */
                if (SYS_CONSOLE_Read(appData.consoleHandle, uart_console_read_buffer, UART_CONSOLE_NUM_BYTES_READ) == UART_CONSOLE_NUM_BYTES_READ)
                {
                    appData.state = APP_STATE_WRITE_RECEIVED_DATA;
                }
                else
                {
                    appData.state = APP_STATE_ERROR;
                }
            }
            break;

        case APP_STATE_WRITE_RECEIVED_DATA:
            /* Demonstrate SYS_CONSOLE_WriteFreeBufferCountGet() and SYS_CONSOLE_Write() APIs */
            SYS_CONSOLE_MESSAGE("\n\rReceived Characters:");
            SYS_CONSOLE_Write(appData.consoleHandle, uart_console_read_buffer, UART_CONSOLE_NUM_BYTES_READ);
            appData.state = APP_STATE_WAIT_WRITE_BUFFER_EMPTY;
            break;

        case APP_STATE_WAIT_WRITE_BUFFER_EMPTY:
            if (SYS_CONSOLE_WriteCountGet(appData.consoleHandle) == 0)
            {
                SYS_CONSOLE_PRINT("\n\rFree Space in TX Buffer = %d", SYS_CONSOLE_WriteFreeBufferCountGet(appData.consoleHandle));
                appData.state = APP_STATE_ECHO_TEST;
            }
            break;

        case APP_STATE_ECHO_TEST:

            SYS_CONSOLE_Message(appData.consoleHandle, "\n\r\n\r***Echo Test*** \n\rEnter a character and it will be echoed back \n\r");
            appData.state = APP_STATE_CONSOLE_READ_WRITE;
            break;

        case APP_STATE_CONSOLE_READ_WRITE:
            if (SYS_CONSOLE_Read(appData.consoleHandle, uart_console_read_buffer, 1) >= 1)
            {
                SYS_CONSOLE_Write(appData.consoleHandle, uart_console_read_buffer, 1);
            }
            break;

        case APP_STATE_ERROR:
        default:
            break;

    }
}