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

This example shows how to manage the timers

/*
* @file timer.c
* @author TST
* @version V0.0
* @date 09/10/2012
* @brief TIMER example. In this example shows how to use the timers:
* - Chrono (to count time).
* - Delay (to wait for).
*
* This application waits for a specific programming time and counts a time to
* execute the vTaskDelay()intruccion.
*
* @note to use the debug UART, user must set the next configuration in the serial communication program:
* - Baud rate: 19200
* - Word length: 8 bits
* - Parity: none
* - Stop bit: 1
* - Flow control: none
*
*/
/* Platform headers */
#include "tsmart.h"
/* Defines */
#define DEBUG_UART tsmart_uart1
#define APP_RX_BUFFER_SZ 100 /* UART buffer size */
/* Reception buffer UART */
static uint8_t app_rx_buf[APP_RX_BUFFER_SZ];
/* This task is waiting for a timeout. When that happens the task counts
* the time of the delay instruction and shows the count and it compares
* with the expected count. The different between them is due to FreeRTOS
* system operating.
*
* @param pvParameters
*/
void vTIMER_C(void *pvParameters){
/* Variables */
static uint8_t debug_buffer[200];
ts_tim_cfg_t ts_tim_cfg;
uint32_t time;
uint16_t count;
/* Wait for interruption */
sprintf(debug_buffer, "IRQ Timer delay\r\n");
TSMART_UART_Send(&DEBUG_UART, debug_buffer, strlen(debug_buffer), 1000/portTICK_RATE_MS);
/* Start timer chrono */
vTaskDelay(12700/portTICK_RATE_MS);
sprintf(debug_buffer, "timer crono1 = %d expected = 127000\r\n", time);
TSMART_UART_Send(&DEBUG_UART, debug_buffer, strlen(debug_buffer), 1000/portTICK_RATE_MS);
/* Start */
vTaskDelay(121/portTICK_RATE_MS);
sprintf(debug_buffer, "timer crono2 = %d expected = 1210\r\n", time);
TSMART_UART_Send(&DEBUG_UART, debug_buffer, strlen(debug_buffer), 1000/portTICK_RATE_MS);
for(;;);
}
/* @brief This task configures the timers as crono and delay and it
* starts a timer delay.
*
* @param pvParameters
*/
void vTIMER_D(void *pvParameters){
/* Variables */
tsmart_timer_config_t timer_config;
uint16_t count;
/* Configure Timer delay*/
timer_config.counter_mode = TS_TIM_MODE_DELAY;
timer_config.timeout = 50000;
/* Configure TIMER */
/* Configure Timer Chrono */
timer_config.counter_mode = TS_TIM_MODE_CHRONOMETER;
/* Configure TIMER */
/* Configure Timer Chrono */
timer_config.counter_mode = TS_TIM_MODE_CHRONOMETER;
/* Configure TIMER */
TSMART_UART_Send(&DEBUG_UART, "Waiting for 5 sec\r\n", strlen("Waiting for 5 sec\r\n"), 1000/portTICK_RATE_MS);
/* Start timer delay */
vTaskDelay(portMAX_DELAY);
}
/*
* @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;
/* ************************************************************************* */
/* Debug Mode */
/* ************************************************************************* */
/* ************************************************************************* */
/* Initialize resources */
/* ************************************************************************* */
/* Initialize Delay timers */
/* Initialize Chrono timers */
/* Initialize Chrono timers */
/* Initialize Debug UART */
tsmart_uart_config.baud_rate = 19200;
tsmart_uart_config.flow_control = TSMART_UART_FC_NONE;
tsmart_uart_config.parity_bit = TSMART_UART_PARITY_NONE;
tsmart_uart_config.word_length = TSMART_UART_WORDLENGTH_8B;
tsmart_uart_config.stop_bit = TSMART_UART_STOPBITS_1;
tsmart_uart_config.rx_buf_sz = APP_RX_BUFFER_SZ;
tsmart_uart_config.rx_buf = app_rx_buf;
tsmart_uart_config.use_dma = TSMART_UART_DMA_RX_TX;
tsmart_uart_config.use_tim = TSMART_UART_TIM_SW;
TSMART_UART_Init(&DEBUG_UART, &tsmart_uart_config);
/* ************************************************************************* */
/* Application task */
/* ************************************************************************* */
/* Create the Timer delay */
if(xTaskCreate(vTIMER_D, "TIMER_D", 512, NULL, 7, NULL) != pdPASS){
return TSMART_FAIL;
}
/* Create the Timer chrono */
if(xTaskCreate(vTIMER_C, "TIMER_C", 512, NULL, 9, NULL) != pdPASS){
return TSMART_FAIL;
}
/* Everything OK */
return TSMART_PASS;
}