SAMV71 Xplained Ultra Software Package 1.5

main.c

Go to the documentation of this file.
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 rtt RTT Example
00032  *
00033  * \section Purpose
00034  *
00035  * This example demonstrates the Real-Time Timer (RTT) provided on
00036  * SAMV7/E7 micro-controllers. It enables the user to set an alarm and watch
00037  * it being triggered when the timer reaches the corresponding value.
00038  *
00039  * You can configure the rtt_module "RTT" by following steps
00040  * - SetPrescaler the RTT to 1s (32768)
00041  * - Initialize the ISR routine which refresh it when 1s is counted down
00042  * - Enable the RTT Interrupt of the vector
00043  *
00044  * \section Requirements
00045  *
00046  * This package can be used with SAMV71 Xplained Ultra board or SAME70 Xplained board.
00047  *
00048  * \section Description
00049  *
00050  * When launched, this program displays a timer count and a menu on the terminal,
00051  * enabling the user to choose between several options.
00052  *
00053  * \section Usage
00054  *
00055  *  -# Build the program and download it inside the board. 
00056  *     Please refer to the Getting Started with SAM V71/E70 Microcontrollers.pdf
00057  * -# On the computer, open and configure a terminal application
00058  *    (e.g. HyperTerminal on Microsoft Windows) with these settings:
00059  *   - 115200 baud rate
00060  *   - 8 bits of data
00061  *   - No parity
00062  *   - 1 stop bit
00063  *   - No flow control
00064  * -# Start the application.
00065  * -# In the terminal window, the following text should appear:
00066  *    \code
00067  *     -- RTT Example xxx --
00068  *     -- xxxxxx-xx
00069  *     -- Compiled: xxx xx xxxx xx:xx:xx --
00070  *     Time: 0
00071  *     Menu:
00072  *     r - Reset timer
00073  *     s - Set alarm
00074  *     Choice?
00075  *    \endcode
00076  *
00077  * The user can then choose any of the available options to perform
00078  * the described action.
00079  *
00080  * \section References
00081  * - rtt/main.c
00082  * - rtt.c
00083  * - rtt.h
00084  */
00085 
00086 /**
00087  * \file
00088  *
00089  * This file contains all the specific code for the rtt example.
00090  *
00091  */
00092 
00093 /*----------------------------------------------------------------------------
00094  *        Headers
00095  *----------------------------------------------------------------------------*/
00096 
00097 #include "board.h"
00098 
00099 /* These headers were introduced in C99 by working group ISO/IEC JTC1/SC22/WG14. */
00100 #include <stdint.h>
00101 #include <stdio.h>
00102 
00103 /*----------------------------------------------------------------------------
00104  *        Local definitions
00105  *----------------------------------------------------------------------------*/
00106 
00107 /** Device state: in the main menu. */
00108 #define STATE_MAINMENU      0
00109 /** Device state: user is setting an alarm time */
00110 #define STATE_SETALARM      1
00111 
00112 /*----------------------------------------------------------------------------
00113  *        Local variables
00114  *----------------------------------------------------------------------------*/
00115 
00116 /** Current device state. */
00117 volatile unsigned char state;
00118 
00119 /** New alarm time being currently entered. */
00120 volatile unsigned int newAlarm;
00121 
00122 /** Indicates if an alarm has occurred but has not been cleared. */
00123 volatile unsigned char alarmed;
00124 
00125 /*----------------------------------------------------------------------------
00126  *        Local functions
00127  *----------------------------------------------------------------------------*/
00128 
00129 /**
00130  * \brief Refresh display on terminal.
00131  *
00132  * Updates the terminal display to show the current menu and the current time
00133  * depending on the device state.
00134  */
00135 static void _RefreshDisplay(void)
00136 {
00137     printf("%c[2J\r", 27);
00138     printf("Time: %u\n\r", (unsigned int)RTT_GetTime(RTT));
00139 
00140     /* Display alarm */
00141     if (alarmed)
00142         printf("!!! ALARM !!!\n\r");
00143 
00144     /* Main menu */
00145     if (state == STATE_MAINMENU) {
00146         printf("Menu:\n\r");
00147         printf(" r - Reset timer\n\r");
00148         printf(" s - Set alarm\n\r");
00149         if (alarmed)
00150             printf(" c - Clear alarm notification\n\r");
00151         printf("\n\rChoice? ");
00152 #if defined (__GNUC__)
00153         fflush(stdout);
00154 #endif
00155     }
00156     /* Set alarm */
00157     else {
00158         if (state == STATE_SETALARM) {
00159             printf("Enter alarm time: ");
00160             if ( newAlarm != 0 ) {
00161                 printf("%u", newAlarm);
00162 #if defined (__GNUC__)
00163                 fflush(stdout);
00164 #endif
00165             }
00166         }
00167     }
00168 }
00169 
00170 /**
00171  * \brief Interrupt handler for the RTT.
00172  *
00173  * Displays the current time on the terminal.
00174  */
00175 void RTT_Handler(void)
00176 {
00177     uint32_t status;
00178 
00179     /* Get RTT status */
00180     status = RTT_GetStatus(RTT);
00181 
00182     /* Time has changed, refresh display */
00183     if ((status & RTT_SR_RTTINC) == RTT_SR_RTTINC)
00184         _RefreshDisplay();
00185 
00186     /* Alarm */
00187     if ((status & RTT_SR_ALMS) == RTT_SR_ALMS) {
00188         alarmed = 1;
00189         _RefreshDisplay();
00190     }
00191 }
00192 
00193 /**
00194  * \brief RTT configuration function.
00195  *
00196  * Configures the RTT to generate a one second tick, which triggers the RTTINC
00197  * interrupt.
00198  */
00199 static void _ConfigureRtt(void)
00200 {
00201     uint32_t previousTime;
00202 
00203     /* Configure RTT for a 1 second tick interrupt */
00204     RTT_SetPrescaler(RTT, 32768);
00205     previousTime = RTT_GetTime(RTT);
00206     while (previousTime == RTT_GetTime(RTT));
00207 
00208     /* Enable RTT interrupt */
00209     NVIC_DisableIRQ(RTT_IRQn);
00210     NVIC_ClearPendingIRQ(RTT_IRQn);
00211     NVIC_SetPriority(RTT_IRQn, 0);
00212     NVIC_EnableIRQ(RTT_IRQn);
00213     RTT_EnableIT(RTT, RTT_MR_RTTINCIEN);
00214 }
00215 
00216 /*----------------------------------------------------------------------------
00217  *         Global functions
00218  *----------------------------------------------------------------------------*/
00219 
00220 /**
00221  * \brief Application entry point for RTT example.
00222  *
00223  * Initializes the RTT, displays the current time and allows the user to
00224  * perform several actions: clear the timer, set an alarm, etc.
00225  *
00226  * \return Unused (ANSI-C compatibility).
00227  */
00228 extern int main( void )
00229 {
00230     unsigned char c;
00231 
00232     /* Disable watchdog */
00233     WDT_Disable(WDT);
00234     
00235     SCB_EnableICache();
00236     SCB_EnableDCache();
00237     
00238     /* Output example information */
00239     printf("-- RTT Example %s --\n\r", SOFTPACK_VERSION);
00240     printf("-- %s\n\r", BOARD_NAME);
00241     printf("-- Compiled: %s %s With %s--\n\r", __DATE__, __TIME__, COMPILER_NAME);
00242 
00243     /* Configure RTT */
00244     _ConfigureRtt();
00245 
00246     /* Initialize state machine */
00247     state = STATE_MAINMENU;
00248     alarmed = 0;
00249     _RefreshDisplay();
00250 
00251     /* User input loop */
00252     while (1) {
00253         /* Wait for user input */
00254         c = DBG_GetChar();
00255 
00256         /* Main menu mode */
00257         if (state == STATE_MAINMENU) {
00258             /* Reset timer */
00259             if (c == 'r') {
00260                 _ConfigureRtt();
00261                 _RefreshDisplay();
00262             }
00263             /* Set alarm */
00264             else
00265                 if (c == 's') {
00266                     state = STATE_SETALARM;
00267                     newAlarm = 0;
00268                     _RefreshDisplay();
00269                 }
00270             /* Clear alarm */
00271                 else {
00272                     if ((c == 'c') && alarmed) {
00273                         alarmed = 0;
00274                         _RefreshDisplay();
00275                     }
00276                 }
00277         }
00278         /* Set alarm mode */
00279         else {
00280             if (state == STATE_SETALARM) {
00281                 /* Number */
00282                 if ((c >= '0') && (c <= '9')) {
00283                     newAlarm = newAlarm * 10 + c - '0';
00284                     _RefreshDisplay();
00285                 }
00286                 /* Backspace */
00287                 else {
00288                     if (c == 8) {
00289                         newAlarm /= 10;
00290                         _RefreshDisplay();
00291                     }
00292                     /* Enter key */
00293                     else {
00294                         if (c == 13) {
00295                             /* Avoid newAlarm = 0 case */
00296                             if (newAlarm != 0) {
00297                                 RTT_SetAlarm(RTT, newAlarm);
00298                             }
00299                             state = STATE_MAINMENU;
00300                             _RefreshDisplay();
00301                         }
00302                     }
00303                 }
00304             }
00305         }
00306     }
00307 }
00308 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines