This example demonstrates how buffers can be used, an UART communication is configured to pass data received to the configured buffer to after be passed to a registered task in charge of receiving all the data from the buffer after being processed.

Code Example:

#include "bsp.h"
#include <string.h>

#define RESET_VALUE 0               /* Value to reset an element */
#define BUFFER_SIZE 100             /* Size of stream buffer */
#define TRIGGER_LEVEL 1             /* Set the triger level to start reception */

UART_HandleTypeDef UartHandle;      /* UART handler structure */
StreamBufferHandle_t xStreamBuffer; /* define stream buffer handle */

uint8_t RxBuffer[ 10 ];             /* buffer for message received */
uint8_t RxByte;                     /* temporary reception buffer */

void Task1( void *pvParameters );   /* Task1 To Receive the stream buffer */

int main( void )
{
    HAL_Init();

    SEGGER_SYSVIEW_Conf( );
    SEGGER_SYSVIEW_Start( );

    xStreamBuffer = xStreamBufferCreate( BUFFER_SIZE, TRIGGER_LEVEL );          /* Create the stream buffer with a size of 100 bytes */

    /* --- --- UART configuration options --- --- */
    UartHandle.Instance         = USART2;
    UartHandle.Init.BaudRate    = 9600;
    UartHandle.Init.WordLength  = UART_WORDLENGTH_8B;
    UartHandle.Init.StopBits    = UART_STOPBITS_1;
    UartHandle.Init.Parity      = UART_PARITY_NONE;
    UartHandle.Init.HwFlowCtl   = UART_HWCONTROL_NONE;
    UartHandle.Init.Mode        = UART_MODE_TX_RX;
    UartHandle.Init.OverSampling = UART_OVERSAMPLING_16;
    HAL_UART_Init( &UartHandle );                            /* Init the UART */
    HAL_UART_Receive_IT( &UartHandle, &RxByte, 1 );          /* Enable and set the reception using Interruption */   

    xTaskCreate( Task1, "T1_Reception", 240, NULL, 2, NULL ); /* Register Task1 to Receive stream buffer */
    
    vTaskStartScheduler();                                   /* Init the Kernel */
}

void Task1( void *pvParameters )
{
    uint8_t receiver_data;                          /* Variable to receive the data */
    size_t bytes_read;                              /* Variable to know if there are bytes to read on the buffer */
    char process_data[BUFFER_SIZE];                 /* Variable to link the data to be printed on SystemView Terminal */
    uint32_t iter = 0;                              /* Iteration value to set each data in array position */

    for(;;)
    {
        do{                                                     /* loop for read all the buffer */
            bytes_read = xStreamBufferReceive(xStreamBuffer, &receiver_data, BUFFER_SIZE, portMAX_DELAY);   /* Read data from buffer */

            if ('\r' == receiver_data)                          /* if the received data match with the termination character */
            {
                SEGGER_SYSVIEW_PrintfHost( process_data );      /* Print on terminal the data received */
                iter = RESET_VALUE;                                       /* Reset the iteration value of array position */
                memset(process_data, RESET_VALUE, sizeof(process_data));  /* Reset the array to print a new message */
            }
            else
            {
                process_data[iter] = receiver_data;           /* Store the data received to be linked with the other data */
                iter++;                                       /* increment the array psoition */
            }

        }while (bytes_read > 0);                              /* Ends the loop when the buffer is empty */

        vTaskDelay(1000);                                     /* Period of the task */
    }
}

/* Callback of Serial data reception */
void HAL_UART_RxCpltCallback( UART_HandleTypeDef *huart )
{
    RxBuffer[10] = RxByte;                                                      /* store data */

    HAL_UART_Transmit_IT( &UartHandle, &RxByte, sizeof(RxByte));                /* Transmit the data to terminal */
    xStreamBufferSendFromISR( xStreamBuffer, &RxByte, sizeof( RxByte ), NULL ); /* Send the data using stram bufer to other task */

    HAL_UART_Receive_IT( &UartHandle, &RxByte, 1 );                             /*Set reception through interrupt of one character again*/
}

Observe the code, notice that UART callback is configured to send each data to the stream buffer. The task registered is in charge of receiving elements to the buffer and processing it, the task gets all the data until a “\r“ parameter is received, this means that all the data received going to be stored in an array to be printed to read all the data together.

SystemView Output:

observe that the message is received correctly until the parameter '\r' is received. Each letter is a data sent by UART and stored in the stream buffer, the task is in charge of putting together the letters and simulating a message received.

Document

TimeLine:

Document

The firsts elements are readed and printed on terminal, observe the time and compare it with the next capture.

Observe the next execution of the task, 7 seconds elapsed since the last message, this means that data is not sent in this period of time. Therefore the buffer is empty and task enters in the blocked state until a data is received.

After all the elements is received and processed in task, print the message and clear the array to process the new data.