API  2.2
TSmarT Software Library
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
uart.c

Example showing how to type and read data using the UART device

/*
* @file uart.c
* @author TST
* @version V0.0
* @date 19/06/2012
* @brief UART example.
*
* In this example user types characters using an hyperterminal tool and it prints an echo.
* We use the hyperterminal communication tool for writing and reading.
* It is necessary to configure the hyperterminal. After choosing a name for the connection and selecting the
* correct communication port, we need to configure it with the following values:
*
* @arg Bits per second: the selected baud rate. For this example 19200.
* @arg Data bits: 8
* @arg Parity: None
* @arg Stop bits: 1
* @arg Flow Control: the selected flow control. In this example None.
*
*/
#include "tsmart.h"
#define UART tsmart_uart1
/*
* @brief Task function. User writes data through the hyperterminal. The UART sends the data and
* read the same values that were written (echo), printing them in the hyperterminal
*
* @param pvParameters
*
*/
void vUART_EXAMPLE(void *pvParameters){
/* Variables */
uint8_t echo;
uint8_t title[64]="\r\n\r\n\t\t\t\tUART EXAMPLE\r\n\t\t\t Prints echo from user\r\n\r\n";
/* A title is sent through the UART and printed on the hyperterminal */
TSMART_UART_Send(&UART, title, strlen(title), 1000/portTICK_RATE_MS);
for(;;){
/* User types any character with the keyboard through the hyperterminal */
TSMART_UART_Receive(&UART, &echo, 1, portMAX_DELAY);
/* The UART sends the character previously written and is printed on the hyperterminal */
TSMART_UART_Send(&UART, &echo, 1, 1000/portTICK_RATE_MS);
}
}
/*
* @brief init() function
*
* This is the first thing that the user must do for using TSmarT.
* It initializes the specific hardware resources (GPRS, GPS, AI, DIO, MODBUS, MSA...)
* and software resources (queues, mutex, tasks...) for the user application.
*
* The way to fill in this function properly is to initialize first
* hardware resources and after that software resources.
*
* This function must return: TSMART_PASS when every thing is OK or
* TSMART_FAIL when a failure happened.
*
* @return
* @arg TSMART_PASS
* @arg TSMART_FAIL
*/
int32_t init() {
/* Variables */
tsmart_uart_config_t tsmart_uart_config;
/* Reception buffer UART. Size must be the same as rx_buf_sz */
static uint8_t app_rx_buf[50];
/* ************************************************************************* */
/* Debug Mode */
/* ************************************************************************* */
/* ************************************************************************* */
/* Initialize resources */
/* ************************************************************************* */
/* UART configuration. */
/* Baud rate */
tsmart_uart_config.baud_rate = 19200;
/* Flow control */
tsmart_uart_config.flow_control = TSMART_UART_FC_NONE;
/* Parity bit */
tsmart_uart_config.parity_bit = TSMART_UART_PARITY_NONE;
/* Word length */
tsmart_uart_config.word_length = TSMART_UART_WORDLENGTH_8B;
/* Stop bit */
tsmart_uart_config.stop_bit = TSMART_UART_STOPBITS_1;
/* Buffer size for our UART */
tsmart_uart_config.rx_buf_sz = 50;
/* Buffer for our UART */
tsmart_uart_config.rx_buf = app_rx_buf;
/* UART DMA */
tsmart_uart_config.use_dma = TSMART_UART_DMA_RX_TX;
/* UART time out */
tsmart_uart_config.use_tim = TSMART_UART_TIM_SW;
/* Initialize UART. UART 1 is selected */
TSMART_UART_Init(&UART, &tsmart_uart_config);
/* ************************************************************************* */
/* Application task */
/* ************************************************************************* */
/* Create the UART task */
if(xTaskCreate(vUART_EXAMPLE, "UART_EXAMPLE", 512, NULL, 7, NULL) != pdPASS){
return TSMART_FAIL;
}
/* Everything OK */
return TSMART_PASS;
}