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

This is an example about how to set and manage the watchdogs.

/* @file main.c
* @author TST
* @version 0.1
* @date 20/06/2012
* @brief This is an example about how to use a watchdog.
*
* This example uses a Watchdog and a debug UART. A watchdog is
* very useful tool for critical applications. User can check
* the reset carried out by the watchdog using the debug UART.
*
* This application makes an exception copying data in a null
* pointer, then the watchdog can't reset its counter and makes
* a hardware reset.
*
* @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"
/* Choose WDG to activate */
#define IWDG_ENABLE
//#define WWDG_ENABLE
/* @brief vWatchdog task
*
* This task start the watchdog and reset its counter.
*
* @param pvParameters
*/
void vWatchdog(void *pvParameters) {
/* Debug message */
TSMART_UART_Send(&tsmart_uart1, "Starting Watchdog example\r\n", strlen("Starting Watchdog example\r\n"), 1000/portTICK_RATE_MS);
#ifdef IWDG_ENABLE
/* Start Watchdog */
if(TSMART_WDG_Start() != TSMART_PASS){
for(;;);
}
while(1){
/* Debug message */
TSMART_UART_Send(&tsmart_uart1, "Reset counter\r\n", strlen("Reset counter\r\n"), 1000/portTICK_RATE_MS);
/* Reset counter */
if( TSMART_WDG_Reset() != TSMART_PASS){
for(;;);
}
/* Wait to reset the counter again */
vTaskDelay(1000/portTICK_RATE_MS);
}
#endif
for(;;);
}
/* @brief vException task
*
* This task wait for 10 second to make an exception, copying data in a null pointer.
*
* @param pvParameters
*/
void vException(void *pvParameters) {
/* Varaibles */
uint8_t buffer[5];
uint32_t temp;
/* Wait for 10 seconds */
vTaskDelay(10000/portTICK_RATE_MS);
/* Debug message */
TSMART_UART_Send(&tsmart_uart1, "Exception!!\r\n", strlen("Exception!!\r\n"), 1000/portTICK_RATE_MS);
/* Make an exception */
memcpy(0, buffer, 5);
}
/* @brief "Init()" function.
*
* This is the first thing that the user must do for using TSmarT.
* It initializes 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 */
portBASE_TYPE xReturn;
tsmart_uart_config_t tsmart_uart_config;
static uint8_t uart_buf[50];
/* ************************************************************************* */
/* Debug Mode */
/* ************************************************************************* */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
/* ************************************************************************* */
/* Initialize resources */
/* ************************************************************************* */
/* The specific hardware for this application: Watchdog and UART and Dled */
/* Initialize Debug LED */
#ifdef WWDG_ENABLE
if( TSMART_WDG_INIT() == 1){
}else{
}
#endif
#ifdef IWDG_ENABLE
/* The timebomb parameter will be set to 6.5 seconds.*/
if (TSMART_WDG_Config(TSMART_WDG_6500) != TSMART_PASS){
return TSMART_FAIL;
}
#endif
/* UART configuration. */
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 = 50;
tsmart_uart_config.rx_buf = uart_buf;
tsmart_uart_config.use_dma = TSMART_UART_DMA_RX_TX;
tsmart_uart_config.use_tim = TSMART_UART_TIM_SW;
TSMART_UART_Init(&tsmart_uart1, &tsmart_uart_config);
/* ************************************************************************* */
/* Application task */
/* ************************************************************************* */
/* This examples will need two tasks:
*
* - watchdog task: This task refesh the independent watchdog counter.
* - Exception task: this task wait for 10 seconds to make an exception.
*/
xReturn = xTaskCreate(vWatchdog, "Watchdog", 128, NULL, 7, NULL);
if (xReturn != pdPASS) {
return TSMART_FAIL;
}
xReturn = xTaskCreate(vException, "Exception", 128, NULL, 7, NULL);
if (xReturn != pdPASS) {
return TSMART_FAIL;
}
/* Everything OK */
return TSMART_PASS;
}