SAMV71 Xplained Ultra Software Package 1.4

main.c

Go to the documentation of this file.
00001 /* ----------------------------------------------------------------------------
00002  *         SAM Software Package License
00003  * ----------------------------------------------------------------------------
00004  * Copyright (c) 2011, Atmel Corporation
00005  *
00006  * All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions are met:
00010  *
00011  * - Redistributions of source code must retain the above copyright notice,
00012  * this list of conditions and the disclaimer below.
00013  *
00014  * Atmel's name may not be used to endorse or promote products derived from
00015  * this software without specific prior written permission.
00016  *
00017  * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00019  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
00020  * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00022  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
00023  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00024  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00025  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
00026  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027  * ----------------------------------------------------------------------------
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 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 SAM V71 Xplained Ultra 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 SAM V71 Xplained Ultra board. 
00056  *     Please refer to the Getting Started with SAM V71 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
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     {
00143         printf("!!! ALARM !!!\n\r");
00144     }
00145 
00146     /* Main menu */
00147     if ( state == STATE_MAINMENU )
00148     {
00149         printf("Menu:\n\r");
00150         printf(" r - Reset timer\n\r");
00151         printf(" s - Set alarm\n\r");
00152         if ( alarmed )
00153         {
00154             printf(" c - Clear alarm notification\n\r");
00155         }
00156         printf("\n\rChoice? ");
00157 #if defined (  __GNUC__  )
00158         fflush(stdout);
00159 #endif
00160     }
00161     /* Set alarm */
00162     else
00163     {
00164         if (state == STATE_SETALARM)
00165         {
00166             printf("Enter alarm time: ");
00167             if ( newAlarm != 0 )
00168             {
00169                 printf("%u", newAlarm);
00170 #if defined (  __GNUC__  )
00171                 fflush(stdout);
00172 #endif
00173             }
00174         }
00175     }
00176 }
00177 
00178 /**
00179  * \brief Interrupt handler for the RTT.
00180  *
00181  * Displays the current time on the terminal.
00182  */
00183 void RTT_Handler( void )
00184 {
00185     uint32_t status ;
00186 
00187     /* Get RTT status */
00188     status = RTT_GetStatus( RTT ) ;
00189 
00190     /* Time has changed, refresh display */
00191     if ((status & RTT_SR_RTTINC) == RTT_SR_RTTINC)
00192     {
00193         _RefreshDisplay();
00194     }
00195 
00196     /* Alarm */
00197     if ((status & RTT_SR_ALMS) == RTT_SR_ALMS)
00198     {
00199         alarmed = 1;
00200         _RefreshDisplay();
00201     }
00202 }
00203 
00204 /**
00205  * \brief RTT configuration function.
00206  *
00207  * Configures the RTT to generate a one second tick, which triggers the RTTINC
00208  * interrupt.
00209  */
00210 static void _ConfigureRtt( void )
00211 {
00212     uint32_t previousTime ;
00213 
00214     /* Configure RTT for a 1 second tick interrupt */
00215     RTT_SetPrescaler( RTT, 32768 ) ;
00216     previousTime = RTT_GetTime( RTT ) ;
00217     while ( previousTime == RTT_GetTime( RTT ) ) ;
00218 
00219     /* Enable RTT interrupt */
00220     NVIC_DisableIRQ( RTT_IRQn ) ;
00221     NVIC_ClearPendingIRQ( RTT_IRQn ) ;
00222     NVIC_SetPriority( RTT_IRQn, 0 ) ;
00223     NVIC_EnableIRQ( RTT_IRQn ) ;
00224     RTT_EnableIT( RTT, RTT_MR_RTTINCIEN ) ;
00225 }
00226 
00227 /*----------------------------------------------------------------------------
00228  *         Global functions
00229  *----------------------------------------------------------------------------*/
00230 
00231 /**
00232  * \brief Application entry point for RTT example.
00233  *
00234  * Initializes the RTT, displays the current time and allows the user to
00235  * perform several actions: clear the timer, set an alarm, etc.
00236  *
00237  * \return Unused (ANSI-C compatibility).
00238  */
00239 extern int main( void )
00240 {
00241     unsigned char c ;
00242 
00243     /* Disable watchdog */
00244     WDT_Disable( WDT ) ;
00245     
00246     SCB_EnableICache();
00247     SCB_EnableDCache();
00248     
00249     /* Output example information */
00250     printf( "-- RTT Example %s --\n\r", SOFTPACK_VERSION ) ;
00251     printf( "-- %s\n\r", BOARD_NAME ) ;
00252     printf( "-- Compiled: %s %s With %s--\n\r", __DATE__, __TIME__ , COMPILER_NAME) ;
00253 
00254     /* Configure RTT */
00255     _ConfigureRtt() ;
00256 
00257     /* Initialize state machine */
00258     state = STATE_MAINMENU ;
00259     alarmed = 0;
00260     _RefreshDisplay() ;
00261 
00262     /* User input loop */
00263     while (1)
00264     {
00265         /* Wait for user input */
00266         c = DBG_GetChar() ;
00267 
00268         /* Main menu mode */
00269         if ( state == STATE_MAINMENU ) {
00270             /* Reset timer */
00271             if ( c == 'r') {
00272                 _ConfigureRtt() ;
00273                 _RefreshDisplay() ;
00274             }
00275             /* Set alarm */
00276             else
00277                 if (c == 's') {
00278                     state = STATE_SETALARM;
00279                     newAlarm = 0;
00280                     _RefreshDisplay();
00281                 }
00282             /* Clear alarm */
00283                 else {
00284                     if ((c == 'c') && alarmed) {
00285                         alarmed = 0;
00286                         _RefreshDisplay();
00287                     }
00288                 }
00289         }
00290         /* Set alarm mode */
00291         else {
00292             if (state == STATE_SETALARM) {
00293                 /* Number */
00294                 if ((c >= '0') && (c <= '9')) {
00295                     newAlarm = newAlarm * 10 + c - '0';
00296                     _RefreshDisplay();
00297                 }
00298                 /* Backspace */
00299                 else {
00300                     if ( c == 8 ) {
00301                         newAlarm /= 10;
00302                         _RefreshDisplay();
00303                     }
00304                     /* Enter key */
00305                     else {
00306                         if ( c == 13 ) {
00307                             /* Avoid newAlarm = 0 case */
00308                             if (newAlarm != 0) {
00309                                 RTT_SetAlarm( RTT, newAlarm ) ;
00310                             }
00311                             state = STATE_MAINMENU;
00312                             _RefreshDisplay();
00313                         }
00314                     }
00315                 }
00316             }
00317         }
00318     }
00319 }
00320 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines