Microchip® Advanced Software Framework

Transmitting using the FreeRTOS UART driver in

fully asynchronous mode

This example demonstrates using the FreeRTOS UART API to send data using the fully asynchronous operation mode. See Initializing the FreeRTOS UART

The example below implements a function that transmits two strings, and then returns.

Refer to the FreeRTOS peripheral control projects in the Atmel ASF distribution for complete working examples, and the FreeRTOS web site for information on getting started with FreeRTOS.

// This examples assumes freertos_uart has already been set by a successful
// call to freertos_uart_serial_init(), and that freertos_uart_serial_init()
// configured the FreeRTOS ASF driver for standard mode operation.
// xSemaphoreHandle is a FreeRTOS type used to store a handle to a semaphore.
// In this example, the semaphore has already been created using a call to
// the FreeRTOS vSemaphoreCreateBinary() API function, and is being passed in
// as a function parameter.
status_code_t write_two_strings(freertos_uart_if freertos_uart,
xSemaphoreHandle notification_semaphore){
uint8_t write_buffer[5];
status_code_t result;
// Send a string to the UART. The string must be in RAM, so copy it
// into an array. The array must exist for the entire time taken to
// transmit the string. This can be ensured by making it global, static,
// or by allocating it on the stack and then ensuring the stack frame
// does not change until the transmission is complete.
strcpy(write_buffer, "one");
// notification_semaphore, passed into the function, is used by the
// FreeRTOS ASF driver to signal that the transmission has finished.
// Using a block time of 100 / portTICK_RATE_MS means "don't block any
// longer than 100ms" to get access to the UART.
result = freertos_uart_write_packet_async(freertos_uart, write_buffer,
strlen("one"), 100 / portTICK_RATE_MS,
notification_semaphore);
if(result == STATUS_OK) {
// Transmission of the string was started successfully.
}
// .. other processing can be performed here, while the string is being
// transmitted ..
// Another string is going to be sent, but the write_buffer array must
// not be altered until the original transmission is complete. If the
// notification semaphore is available, then the transmission is
// complete and the following function call will return immediately. If
// the notification semaphore is not available, then the following
// function call will place this task into the Blocked state for a
// maximum of 200ms to wait for it to become available (other tasks will
// execute during the wait).
xSemaphoreTake(notification_semaphore, 200 / portTICK_RATE_MS);
strcpy(write_buffer, "two");
result = freertos_uart_write_packet_async(freertos_uart, write_buffer,
strlen("two"), 100 / portTICK_RATE_MS,
notification_semaphore);
// .. other processing can be performed here, while the string is being
// transmitted ..
// In this example, the array being transmitted is declared on the
// stack. If this function exits, the array will no longer exist, and
// if it was still being transmitted, the transmitted data can be
// corrupted. Therefore, xSemaphoreTake() is used again to ensure the
// transmission has completely finished before allowing the function to
// return.
xSemaphoreTake(notification_semaphore, 200 / portTICK_RATE_MS);
return result;
}