00001 /* ---------------------------------------------------------------------------- */ 00002 /* Atmel Microcontroller Software Support */ 00003 /* SAM Software Package License */ 00004 /* ---------------------------------------------------------------------------- */ 00005 /* Copyright (c) 2015, Atmel Corporation */ 00006 /* */ 00007 /* All rights reserved. */ 00008 /* */ 00009 /* Redistribution and use in source and binary forms, with or without */ 00010 /* modification, are permitted provided that the following condition is met: */ 00011 /* */ 00012 /* - Redistributions of source code must retain the above copyright notice, */ 00013 /* this list of conditions and the disclaimer below. */ 00014 /* */ 00015 /* Atmel's name may not be used to endorse or promote products derived from */ 00016 /* this software without specific prior written permission. */ 00017 /* */ 00018 /* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR */ 00019 /* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF */ 00020 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE */ 00021 /* DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, */ 00022 /* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT */ 00023 /* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, */ 00024 /* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF */ 00025 /* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING */ 00026 /* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, */ 00027 /* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ 00028 /* ---------------------------------------------------------------------------- */ 00029 00030 /** 00031 * \page wdt Watchdog with IRQ Interrupt Example 00032 * 00033 * \section Purpose 00034 * 00035 * This example demonstrates user to trigger a watchdog interrupt 00036 * if the software becomes trapped in a deadlock. 00037 * 00038 * \section Requirements 00039 * 00040 * This package can be used with SAMV71 Xplained Ultra board or SAME70 Xplained board. 00041 * 00042 * \section Description 00043 * 00044 * When launched, this program reloads the watchdog at regular intervals 00045 * before the timer underflow occurs, a LED is blinked. User could press 00046 * button1 to make the program run in a infinite loop without 00047 * reloading the watchdog. So a watchdog interrupt will be triggered, and 00048 * "Enter watchdog interrupt." will print to terminal. 00049 * 00050 * \note 00051 * -# User can enable a watchdog reset instead of an interrupt by setting 00052 * WDRSTEN bit in WDT_MR register. 00053 * 00054 * \section Usage 00055 * 00056 * -# Build the program and download it inside the board. 00057 * Please refer to the Getting Started with SAM V71/E70 Microcontrollers.pdf 00058 * -# On the computer, open and configure a terminal application 00059 * (e.g. HyperTerminal on Microsoft Windows) with these settings: 00060 * - 115200 baud rate 00061 * - 8 bits of data 00062 * - No parity 00063 * - 1 stop bit 00064 * - No flow control 00065 * -# Start the application. 00066 * -# In the terminal window, the following text should appear: 00067 * \code 00068 * -- Watchdog with IRQ Interrupt Example xxx -- 00069 * -- xxxxxx-xx 00070 * -- Compiled: xxx xx xxxx xx:xx:xx -- 00071 * \endcode 00072 * 00073 * The user could press the button1 to trigger a watchdog interrupt. 00074 * 00075 * \section References 00076 * - wdt/main.c 00077 * - wdt.c 00078 * - wdt.h 00079 */ 00080 00081 /** 00082 * \file 00083 * 00084 * This file contains all the specific code for the wdt example. 00085 */ 00086 00087 /*---------------------------------------------------------------------------- 00088 * Headers 00089 *----------------------------------------------------------------------------*/ 00090 00091 #include "board.h" 00092 00093 /* These headers were introduced in C99 by working group ISO/IEC JTC1/SC22/WG14. */ 00094 #include <stdbool.h> 00095 #include <stdio.h> 00096 00097 /*---------------------------------------------------------------------------- 00098 * Local definitions 00099 *----------------------------------------------------------------------------*/ 00100 00101 /** LED used in this program */ 00102 #define LED_ID 1 00103 00104 /** LED blink time, in ms */ 00105 #define BLINK_PERIOD 300 00106 00107 /** Watchdog period, in ms */ 00108 #define WDT_PERIOD 3000 00109 /** Watchdog restart period, in ms */ 00110 #define WDT_RESTART_PERIOD 2000 00111 00112 /*---------------------------------------------------------------------------- 00113 * Local variables 00114 *----------------------------------------------------------------------------*/ 00115 00116 /** Pushbutton \#1 pin instance. */ 00117 const Pin pinPB1 = PIN_PUSHBUTTON_0; 00118 00119 /** Pushbutton \#1 pin event flag. */ 00120 volatile bool button1Evt = false; 00121 00122 volatile uint32_t gSystick = 0; 00123 /*---------------------------------------------------------------------------- 00124 * Local functions 00125 *----------------------------------------------------------------------------*/ 00126 00127 00128 /** 00129 * \brief Handler for Button 1 rising edge interrupt. 00130 * 00131 * Set button1 event flag (button1Evt). 00132 */ 00133 static void _Button1_Handler(const Pin* pPin) 00134 { 00135 if (pPin == &pinPB1) 00136 button1Evt = true; 00137 } 00138 00139 /** 00140 * \brief Handler for watchdog interrupt. 00141 */ 00142 void WDT_Handler(void) 00143 { 00144 Wdt *pWdt = WDT; 00145 volatile uint32_t dummy; 00146 00147 /* Clear status bit to acknowledge interrupt */ 00148 dummy = pWdt->WDT_SR; 00149 00150 printf("Enter watchdog interrupt.\n\r"); 00151 #ifdef sram 00152 WDT_Restart(WDT); 00153 printf("The watchdog timer was restarted.\n\r"); 00154 #else 00155 printf("Processor reset\n\n\n\r"); 00156 RSTC_ExtReset(); 00157 #endif 00158 } 00159 00160 /** 00161 * \brief Configure the Pushbuttons 00162 * 00163 * Configure the PIO as inputs and generate corresponding interrupt when 00164 * pressed or released. 00165 */ 00166 static void _ConfigureButtons(void) 00167 { 00168 /* Configure PIO as inputs. */ 00169 PIO_Configure(&pinPB1, 1); 00170 00171 /* Adjust PIO debounce filter parameters, uses 10 Hz filter. */ 00172 PIO_SetDebounceFilter(&pinPB1, 10); 00173 00174 /* Initialize PIO interrupt handlers, see PIO definition in board.h. */ 00175 PIO_ConfigureIt(&pinPB1, _Button1_Handler); /* Interrupt on rising edge */ 00176 00177 /* Enable PIO controller IRQs. */ 00178 NVIC_EnableIRQ((IRQn_Type)pinPB1.id); 00179 00180 /* Enable PIO line interrupts. */ 00181 PIO_EnableIt(&pinPB1); 00182 } 00183 00184 /** 00185 * \brief Configure LEDs 00186 * 00187 * Configures LED (cleared by default). 00188 */ 00189 static void _ConfigureLeds(void) 00190 { 00191 LED_Configure(LED_ID); 00192 } 00193 00194 /*---------------------------------------------------------------------------- 00195 * Global functions 00196 *----------------------------------------------------------------------------*/ 00197 00198 /** 00199 * \brief Application entry point for wdg_irq example. 00200 * 00201 * \return Unused (ANSI-C compatibility). 00202 */ 00203 extern int main( void ) 00204 { 00205 uint32_t dwPeriod; 00206 uint32_t startTime; 00207 00208 SCB_EnableICache(); 00209 SCB_EnableDCache(); 00210 00211 /* Output example information */ 00212 printf("-- Watchdog with IRQ Interrupt Example %s --\n\r", SOFTPACK_VERSION); 00213 printf("-- %s\n\r", BOARD_NAME); 00214 printf("-- Compiled: %s %s With %s--\n\r", __DATE__, __TIME__, COMPILER_NAME); 00215 00216 /* Sys tick configuration. */ 00217 printf("Configure sys tick to get 1ms tick period.\n\r"); 00218 00219 TimeTick_Configure(); 00220 00221 /* PIO configuration for LEDs and Buttons. */ 00222 PIO_InitializeInterrupts(0); 00223 _ConfigureLeds(); 00224 _ConfigureButtons(); 00225 00226 /* Configure WDT to trigger a interrupt (or reset) */ 00227 printf("Enable watchdog with %u millisecond period\n\r", 00228 (unsigned int)WDT_PERIOD); 00229 dwPeriod = WDT_GetPeriod(WDT_PERIOD); 00230 00231 #if 1 /* trigger a watchdog interrupt */ 00232 WDT_Enable(WDT, WDT_MR_WDFIEN | WDT_MR_WDDBGHLT 00233 | WDT_MR_WDIDLEHLT | (dwPeriod << 16) | dwPeriod); 00234 NVIC_DisableIRQ(WDT_IRQn); 00235 NVIC_ClearPendingIRQ(WDT_IRQn); 00236 NVIC_SetPriority(WDT_IRQn, 0); 00237 NVIC_EnableIRQ(WDT_IRQn); 00238 #else /* trigger a watchdog reset */ 00239 WDT_Enable(WDT, WDT_MR_WDRSTEN | WDT_MR_WDDBGHLT 00240 | WDT_MR_WDIDLEHLT | (dwPeriod << 16) | dwPeriod); 00241 #endif 00242 startTime = GetTicks(); 00243 printf("Press USRPB0 to simulate a deadlock loop.\n\r"); 00244 00245 while (1) { 00246 if ((gSystick != GetTicks()) && 00247 ((GetDelayInTicks( startTime, GetTicks())) < 0xFFFFFFFF)) { 00248 gSystick = GetTicks(); 00249 /* Toggle led at given period */ 00250 if ((GetTicks() % BLINK_PERIOD) == 0) 00251 LED_Toggle(LED_ID); 00252 /* Restart watchdog at given period */ 00253 if ((GetTicks() % WDT_RESTART_PERIOD) == 0) 00254 WDT_Restart(WDT); 00255 } 00256 00257 /* Simulate deadlock when button be pressed */ 00258 if (button1Evt == true) { 00259 printf( "Program enter infinite loop for triggering watchdog \ 00260 interrupt.\n\r" ); 00261 while (1); 00262 } 00263 } 00264 } 00265