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 low_power Low-power Example
00032  *
00033  * \section Purpose
00034  * This example allows to measure the consumption of the core in different modes
00035  * (sleep mode, wait mode, backup mode).
00036  *
00037  * \section Requirements
00038  *
00039  *  This package can be used with SAMV71 Xplained Ultra board or SAME70 Xplained board.
00040  *  Note: the package should run on flash.
00041  *
00042  * \section Description
00043  *
00044  * At start-up, the program configures all the PIOs as input to avoid parasite
00045  * consumption. Then a menu is displayed. It allows user to enter in a mode to
00046  * measure consumption.
00047  *
00048  * \section Usage
00049  *
00050  * -# Build the program and download it inside the board.
00051  * Please refer to the Getting Started with SAM V71/E70 Microcontrollers.pdf
00052  * -# On the computer, open and configure a terminal application
00053  *    (e.g. HyperTerminal on Microsoft Windows) with these settings:
00054  *   - 115200 baud rate
00055  *   - 8 bits of data
00056  *   - No parity
00057  *   - 1 stop bit
00058  *   - No flow control
00059  * -# Start the application.
00060  * -# In the terminal window, the following text should appear:
00061  *    \code
00062  *     -- Low_power Example xxx --
00063  *     -- xxxxxx-xx
00064  *     -- Compiled: xxx xx xxxx xx:xx:xx --
00065  *    \endcode
00066  * -# Press one of the keys listed in the menu to perform the corresponding action.
00067  *
00068  * \section References
00069  * - low_power/main.c
00070  */
00071 
00072 /**
00073  * \file
00074  *
00075  * This file contains all the specific code for the low-power example.
00076  */
00077 
00078 /*----------------------------------------------------------------------------
00079  *        Headers
00080  *----------------------------------------------------------------------------*/
00081 
00082 #include "board.h"
00083 
00084 #include <stdint.h>
00085 #include <stdio.h>
00086 
00087 /*----------------------------------------------------------------------------
00088  *        Local definitions
00089  *----------------------------------------------------------------------------*/
00090 
00091 /* (SCR) Sleep deep bit */
00092 #define SCR_SLEEPDEEP   (0x1 <<  2)
00093 
00094 #define CONSOLE_EDBG
00095 
00096 /*----------------------------------------------------------------------------
00097  *        Local variables
00098  *----------------------------------------------------------------------------*/
00099 /* Wakeup PIN Index definition */
00100 const uint32_t gWakeUpPinId = (1 << WKUP_IDX);
00101 
00102 /*----------------------------------------------------------------------------
00103  *        Local functions
00104  *----------------------------------------------------------------------------*/
00105 
00106 #if defined CONSOLE_EDBG
00107 /**
00108  * \brief USART ISR for wakeup from sleep mode
00109  */
00110 void USART1_Handler(void)
00111 {
00112     USART_GetChar(USART1);
00113 }
00114 #else
00115 void UART0_Handler(void)
00116 {
00117     UART_GetChar(UART0);
00118 }
00119 #endif
00120 
00121 /**
00122  * \brief Initialize the chip.
00123  */
00124 static void _InitChip(void)
00125 {
00126     uint32_t tmp;
00127 
00128     /* All PIO in input mode */
00129     PIOA -> PIO_ODR = 0xFFFFFFFF;
00130     PIOB -> PIO_ODR = 0xFFFFFFFF;
00131     PIOC -> PIO_ODR = 0xFFFFFFFF;
00132     PIOD -> PIO_ODR = 0xFFFFFFFF;
00133     PIOE -> PIO_ODR = 0xFFFFFFFF;
00134 
00135     PIOA ->PIO_PPDDR = 0xFFFFFFFF;     // pulldown disable
00136     PIOA ->PIO_PUDR = 0xFFFFFFFF;      // pullup   disable
00137     PIOA ->PIO_PPDER = 0;              // pulldown Enable
00138     PIOA ->PIO_PUER = 0xFFFFFFFF;      // pullup   Enable
00139 
00140     PIOB ->PIO_PPDDR = 0xFFFFFFFF;     // pulldown disable
00141     PIOB ->PIO_PUDR = 0xFFFFFFFF;      // pullup   disable
00142     PIOB ->PIO_PPDER = 0x0;            // pulldown Enable
00143     PIOB ->PIO_PUER = 0xFFFFFFFF;      // pullup   Enable
00144 
00145     PIOC ->PIO_PPDDR = 0xFFFFFFFF;     // pulldown disable
00146     PIOC ->PIO_PUDR = 0xFFFFFFFF;      // pullup   disable
00147     PIOC ->PIO_PPDER = 0;              // pulldown Enable
00148     PIOC ->PIO_PUER = 0xFFFFFFFF;      // pullup   Enable
00149 
00150     PIOD ->PIO_PPDDR = 0xFFFFFFFF;     // pulldown disable
00151     PIOD ->PIO_PUDR = 0xFFFFFFFF;      // pullup   disable
00152     PIOD ->PIO_PPDER = 0;              // pulldown Enable
00153     PIOD ->PIO_PUER = 0xFFFFFFFF;      // pullup   Enable
00154 
00155     PIOE ->PIO_PPDDR = 0xFFFFFFFF;     // pulldown disable
00156     PIOE ->PIO_PUDR = 0xFFFFFFFF;      // pullup   disable
00157     PIOE ->PIO_PPDER = 0;              // pulldown Enable
00158     PIOE ->PIO_PUER = 0xFFFFFFFF;      // pullup   Enable
00159 
00160     tmp = SUPC_MR_KEY_PASSWD;
00161     tmp |=  ((SUPC -> SUPC_MR) &( ~(1u << 17)));
00162     SUPC->SUPC_MR |= tmp;
00163 
00164     /* Disable UTMI PLL clock */
00165     PMC->CKGR_UCKR &= ~CKGR_UCKR_UPLLEN;
00166 
00167     /* Disable PCK */
00168     PMC->PMC_SCDR = 0xFFFFFFFF;
00169 
00170     /* Disable all the peripheral clocks */
00171     PMC_DisableAllPeripherals();
00172 }
00173 
00174 /**
00175  * \brief Sets the wake-up inputs for fast start-up mode registers.
00176  *
00177  * \param inputs  Wake up inputs to enable.
00178  */
00179 static void _SetFastStartupInput(uint32_t dwInputs)
00180 {
00181     PMC->PMC_FSMR &= (uint32_t)~0xFFF00000;
00182     PMC->PMC_FSMR |= dwInputs;
00183 }
00184 
00185 /**
00186  * \brief Enter Wait Mode.
00187  * Enter condition: WFE + (SLEEPDEEP bit = 0) + (LPM bit = 1)
00188  */
00189 static void enterWaitMode(void)
00190 {
00191     printf("-I- Enter in Wait Mode\n\r");
00192     printf("-I- Press WAKE UP button to wakeup\n\r");
00193     /* Configure 4Mhz fast RC oscillator */
00194     /* First switch MCK to Slow clock  */
00195     PMC_SetMckSelection(PMC_MCKR_CSS_SLOW_CLK, PMC_MCKR_PRES(0));
00196     /* Then, enable Fast RC */
00197     PMC_EnableIntRC4_8_12MHz(CKGR_MOR_MOSCRCF_4_MHz);
00198     /* Then, Switch MCK to main clock  */
00199     PMC_SetMckSelection(PMC_MCKR_CSS_MAIN_CLK, PMC_PCK_PRES(0));
00200     /* Disable unused clock to save power (optional) */
00201     PMC_SetPllaClock(0, 0);
00202 
00203     /* Set wakeup input for fast start-up */
00204     _SetFastStartupInput( gWakeUpPinId );
00205     /* Configure the FLPM field in the PMC Fast Start-up Mode Register(PMC_FSMR).*/
00206     PMC->PMC_FSMR |= PMC_FSMR_FLPM_FLASH_DEEP_POWERDOWN | PMC_FSMR_LPM;
00207     /* Set Flash Wait State at 0*/
00208     EFC->EEFC_FMR = EEFC_FMR_FWS(0);
00209     /*  No SLEEPDEEP */
00210     SCB->SCR &= (uint32_t)~SCR_SLEEPDEEP;
00211     /* Set the WAITMODE bit in PMC Clock Generator Main Oscillator
00212         Register (CKGR_MOR)*/
00213     PMC->CKGR_MOR = CKGR_MOR_KEY_PASSWD | CKGR_MOR_WAITMODE;
00214     /* Wait for MCKRDY = 1 in the PMC Status Register (PMC_SR) */
00215     while (!(PMC->PMC_SR & PMC_SR_MCKRDY));
00216     __WFE();
00217 
00218     /* Restore previous clock and DBG */
00219     PMC_SetMckSelection(PMC_MCKR_CSS_SLOW_CLK, PMC_MCKR_PRES_CLK_1);
00220     LowLevelInit();
00221     DBG_Configure(115200, BOARD_MCK);
00222 
00223     printf("-I- Exit Wait Mode\n\r");
00224 }
00225 
00226 /**
00227  * \brief Test Sleep Mode
00228  */
00229 static void enterSleepMode(void)
00230 {
00231     printf("-I- Enter in Sleep Mode\n\r");
00232     printf("-I- Press any key to wakeup\n\r");
00233     /* The purpose of sleep mode is to optimize power consumption of the
00234         device versus response time.
00235        In this mode, only the core clock is stopped. The peripheral clocks can
00236        be enabled.
00237        The current consumption in this mode is application-dependent.*/
00238     PMC->PMC_FSMR &= (uint32_t)~PMC_FSMR_LPM;
00239     SCB->SCR &= (uint32_t)~SCR_SLEEPDEEP;
00240 
00241     /* Processor wake-up is triggered by an interrupt if the WFI instruction
00242         of the Cortex-M processor is used.*/
00243 #if defined CONSOLE_EDBG
00244     NVIC_ClearPendingIRQ(USART1_IRQn);
00245     NVIC_EnableIRQ(USART1_IRQn);
00246     USART_EnableIt(USART1,UART_IER_RXRDY);
00247 #else
00248     NVIC_ClearPendingIRQ(UART0_IRQn);
00249     NVIC_EnableIRQ(UART0_IRQn);
00250     UART_EnableIt(UART0,UART_IER_RXRDY);
00251 #endif
00252 
00253     /* This mode is entered using the instruction Wait for Interrupt (WFI).*/
00254     __WFI();
00255     printf("-I- Processor wake-up is triggered by an interrupt \n\r");
00256     printf("-I- Exit Sleep Mode\n\r" );
00257 }
00258 
00259 /**
00260  * \brief Test Backup Mode.
00261  */
00262 static void enterBackupMode(void)
00263 {
00264     printf( "-I- Enter in Backup Mode\n\r" );
00265     printf( "-I- Press WAKE UP button to wakeup\n\r");
00266     /* Enable the PIO for wake-up */
00267     SUPC->SUPC_WUIR = (gWakeUpPinId << 16) | gWakeUpPinId;
00268     PMC->PMC_FSMR   = PMC_FSMR_FLPM_FLASH_DEEP_POWERDOWN | gWakeUpPinId;
00269     /* Set the SLEEPDEEP bit of Cortex-M processor. */
00270     SCB->SCR |= SCR_SLEEPDEEP;
00271     /* Set the VROFF bit of SUPC_CR. */
00272     SUPC->SUPC_CR = SUPC_CR_KEY_PASSWD | SUPC_CR_VROFF_STOP_VREG;
00273     SUPC->SUPC_WUMR = (1 << 12);
00274 
00275     /* Wake Up Input 2 and 4 (L_CLICK and R_CLICK) Enable + (0) high to low
00276         level Transition. Exit from Backup mode occurs as a result of one of
00277         the following enabled wake-up events:
00278         WKUPEN0-13 pins (level transition, configurable denouncing)
00279         Supply Monitor alarm
00280         RTC alarm
00281         RTT alarm */
00282     printf("-I- Processor wake-up is triggered by WAKEUP button \n\r");
00283     printf("-I- Exit backup Mode\n\r");
00284     while (1);
00285 }
00286 
00287 /**
00288  * \brief Display test Core menu.
00289  */
00290 static void _DisplayMenuCore( void )
00291 {
00292     printf("\n\r");
00293     printf("===========================================================\n\r");
00294     printf("Menu: press a key to select low power mode.\n\r");
00295     printf("===========================================================\n\r");
00296     printf("Configure:\n\r");
00297     printf("  S : Sleep mode\n\r");
00298     printf("  W : Wait mode\n\r");
00299     printf("  B : Backup mode\n\r");
00300 }
00301 
00302 /**
00303  * \brief Test Core consumption
00304  */
00305 static void lowPowerMode(void)
00306 {
00307     uint8_t ucKey;
00308 
00309     while (1) {
00310         _DisplayMenuCore();
00311         ucKey = DBG_GetChar();
00312         switch (ucKey) {
00313         case 's':
00314         case 'S':
00315             enterSleepMode();
00316             break;
00317 
00318         case 'w':
00319         case 'W':
00320             enterWaitMode();
00321             break;
00322 
00323         case 'b':
00324         case 'B':
00325             enterBackupMode();
00326             break;
00327 
00328         default:
00329             printf("This menu does not exist !\n\r");
00330             break;
00331         } /* switch */
00332     }
00333 }
00334 
00335 /**
00336  * \brief Application entry point for low-power example.
00337  *
00338  * \return Unused (ANSI-C compatibility).
00339  */
00340 int main(void)
00341 {
00342     /* Disable watchdog */
00343     WDT_Disable(WDT);
00344 
00345     /* Enable I and D cache */
00346     SCB_EnableICache();
00347     SCB_EnableDCache();
00348 
00349     /* initialize the chip for the power consumption test */
00350     _InitChip();
00351 
00352     /* Output example information */
00353     printf("\n\r\n\r\n\r");
00354     printf("-- Low-power Example %s --\n\r", SOFTPACK_VERSION );
00355     printf("-- %s\n\r", BOARD_NAME);
00356     printf("-- Compiled: %s %s With %s--\n\r", __DATE__, __TIME__, COMPILER_NAME);
00357 
00358     /* Test core consumption */
00359     lowPowerMode();
00360     return 0;
00361 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines