AVR XMEGA Microcontrollers AVR42772: Data Logger Demo Application on XMEGA A1U Xplained Pro APPLICATION NOTE Introduction This application note covers some of the power saving features of the Atmel XMEGA microcontrollers based on a data logger example. The goal for this application note is to make users familiar with the picopower technology used in the XMEGA MCUs, and show how to implement the power-saving features of the AVR microcontrollers. This data logger example can also be the base of a real product. The following items will be covered: Clock gating ADC Sleep modes Event system DMA Features ADC conversion and memory copy without CPU intervention Low power mode Firmware example project on Atmel START
Table of Contents Introduction...1 Features... 1 1. Hardware Requirements...3 2. Application... 4 2.1. Clock Setup... 4 2.2. Event System... 5 2.3. DMA...5 2.4. Low Power...6 2.4.1. Disable the clocks to unused peripheral modules... 6 2.4.2. Put the EEPROM into Power Reduction Mode...8 2.4.3. Add Idle Sleep Mode... 8 3. Revision History...9 2
1. Hardware Requirements The Data Logging example application requires the following hardware: Figure 1-1. XMEGA A1U Xplained Pro Evaluation Kit XMEGA A1U Xplained Pro Evaluation Kit which includes: Atmel ATxmega128A1U microcontroller One mechanical reset button One mechanical user push-button One yellow user LED 32.768kHz crystal External 512KB SRAM USB interface, device mode Three Xplained Pro extension headers Xplained Pro LCD extension connector Embedded Debugger USB powered For more details of XMEGA A1U Xplained Pro Evaluation Kit, refer to http://www.atmel.com/tools/ atxmegaa1u-xpro.aspx and XMEGA A1U Xplained Pro User Guide. This example can also be easily ported to other XMEGA devices. 3
2. Application This application is implemented as a simple data logger measuring an analog signal every 5 th millisecond, using RTC to trigger the ADC sampling periodically. As the XMEGA A1U Xplained Pro board does not have any external components added to the analog input pins, a floating input pin will be used in this demo sample. In this application the CPU is only needed to setup the peripherals and to perform the data processing (does nothing in this example, users can add their own routines). In order to reduce power consumption we can put the CPU in sleep whenever it is not in use, idle mode will be used to do this. In Idle mode the CPU and Flash program memory is stopped, while the peripheral clock and peripheral modules are kept running. When an RTC overflows, it raises a event to trigger an ADC conversion. When the ADC conversion is complete, the DMA will transfer the ADC conversion result to the data buffer. For simplicity this operation is called a "measurement cycle". After 64 measurement cycles, when the last ADC conversion is complete, the CPU is woke up by DMA complete interrupt and performs the data processing. This cycle (64 measurement cycles plus the data processing) is called a "complete cycle". DMA complete interrupt service route will re-assign destination address and reload DMA. The following peripherals and functions are used: Clock system Real time counter (RTC) Analog to Digital Converter (ADC) Event System DMA The current consumption waveform is illustrated below: The source project can be downloaded from Atmel START by browsing examples. Atmel START is an innovative online tool for intuitive and graphical configuration of embedded software projects. For more information, refer to the Getting Started guide. 2.1. Clock Setup For this application the ATxmega128A1U is running at 8MHz. The ATxmega128A1U has an internal 32MHz RC oscillator and an internal 2MHz RC oscillator. By default the 2MHz RC oscillator is selected as the main clock source at start-up. The different clocks can be selected by software, and in this application it selects the 32MHz RC oscillator, which is divided by 4 using the system prescaler. The 2MHz oscillator 4
is disabled. The XMEGA Real Time Counter (RTC) is set up to trigger a new ADC conversion at regular intervals by using event system, approximately every five milliseconds. The clock setup will be configured by Atmel START drivers, as well as the GPIO configuration, which means that the clock setup and GPIO PINMUX can be configured on the Atmel START web page. For details, refer to the codes from the example project. 2.2. Event System The XMEGA event system is an inter-peripheral hardware communication network enabling peripherals to directly signal each other without using the CPU. Traditionally, an interrupt with an interrupt service routine would be used when a peripheral needs to trigger an action in another peripheral. However, the event system peripherals can directly trigger an action (event) in other peripherals via the event system. The CPU can be put in Idle sleep mode because the event system operates totally independent from the CPU. ATxmega128A1U has eight independent event channels. In this application, event channel 7 is used to trigger a conversion on Channel 0 of the ADCA module. The trigger source used for channel 7 will be the RTC overflow flag. By using the event system, the MCU does not need to wake up from sleep mode to execute Interrupt Servicing Routines (when executing ISR the CPU needs to be in active mode). Therefore, the event system can help you to save power by reducing the amount of time the CPU needs to stay in active mode. Note that although the extra power consumption used by the event system is very small, it still adds a little bit to the total power consumption. So in applications where the interrupts do not happen very frequently or the interrupt functions are relatively short, the total reduction in current consumption by using the event system will be smaller than in applications where there are frequently occurring interrupts, or interrupts with long execution time. In this application the event system removes 64 relatively short RTC interrupt routine calls per program cycle, and give us approximately 1% lower power consumption by using the event system. In other applications where many interrupts or longer interrupt servicing routines can be removed, the reduction in power consumption could be even larger. In addition, using the event system will reduce the CPU workload, freeing the CPU for other tasks, and thus increasing your total system performance. It may also allow you to operate the system at a lower frequency and thus saving power. Here are the event system initialization codes: // Enable event system clock domain PR.PRGEN &= ~PR_EVSYS_bm; // Configure event channel 7 source (RTC overflow) EVSYS.CH7MUX = EVSYS_CHMUX_RTC_OVF_gc Below line configures the trigger source of ADC to use RTC overflow event: //Enable event system ADCA.EVCTRL = ADC_SWEEP_0_gc ADC_EVACT_CH0_gc ADC_EVSEL_7_gc; 2.3. DMA The DMA system is used to transfer the ADC conversion results to the ADC result buffer. The DMA system can work in IDLE sleep mode, and together with the event system the CPU can be left in idle mode without wake-up by RTC or ADC interrupt service routines. 5
When an ADC conversion is completed, the ADC signals to the DMA system, and the DMA system transfers the ADC conversion result to the buffer directly without using the CPU. The CPU will stay in idle sleep mode until the DMA transfer is completed, and then wakes up to perform the data processing. Although the extra power consumption used by the DMA module is relatively small, it still consumes some power. Therefore, in applications where the amount of data to be moved is small, interrupts do not happen very frequently, the current reduction achieved by the DMA might not be significant. In this application the addition of DMA removes only 64 relatively short ADC ISRs in one complete cycle. The more data transferred in the application, the greater the power reduction will be by using DMA. In addition, the DMA will reduce the CPU workload, freeing the CPU for other tasks, and thus increasing your total system performance. It may also allow you to operate the system at a lower frequency and thus saving power. In this application the amount of data to be transferred is so small that the reduction of power by enabling the DMA is not very significant. The initialization routine is as below: // Enable DMA clock domain PR.PRGEN &= ~PR_DMA_bm; /* Reset DMA controller just to make sure everything is from scratch */ DMA.CTRL = DMA_RESET_bm; // Enable DMA module DMA.CTRL = DMA_ENABLE_bm DMA_DBUFMODE_CH01_gc DMA_PRIMODE_RR0123_gc; // Clear interrupt flags DMA.INTFLAGS = 0; // Configure address handle mode DMA.CH0.ADDRCTRL = DMA_CH_SRCRELOAD_BURST_gc DMA_CH_SRCDIR_INC_gc DMA_CH_DESTRELOAD_BLOCK_gc DMA_CH_DESTDIR_INC_gc; // Configure trigger source DMA.CH0.TRIGSRC = DMA_CH_TRIGSRC_ADCA_CH0_gc; // Transfer 2 byte for every trigger. the total transfer byte will be 64*2 = 128 DMA.CH0.TRFCNT = 128; // Assign source address DMA.CH0.SRCADDR0 = (uint8_t)((uint16_t)&(adca.ch0.res)); DMA.CH0.SRCADDR1 = (uint8_t)((uint16_t)&(adca.ch0.res) >> 8); DMA.CH0.SRCADDR2 = 0; // Assign destination address DMA.CH0.DESTADDR0 = (uint8_t)((uint16_t)adc_result[0]); DMA.CH0.DESTADDR1 = (uint8_t)((uint16_t)adc_result[0] >> 8); DMA.CH0.DESTADDR2 = 0; // Enable interrupt DMA.CH0.CTRLB = DMA_CH_TRNINTLVL_LO_gc; // Set transfer mode and enable transfer DMA.CH0.CTRLA = DMA_CH_ENABLE_bm DMA_CH_BURSTLEN_2BYTE_gc DMA_CH_SINGLE_bm; 2.4. Low Power 2.4.1. Disable the clocks to unused peripheral modules The XMEGA devices have a very rich set of peripherals available, and each peripheral will add a small contribution to the total current consumption. The clock domain of most peripherals can be disabled 6
independently, and by removing the clock signal to an unused peripheral, the dynamic power consumption contribution of that peripheral will be removed. There are seven PR registers available on the ATxmega128A1U (no PORT F), see section "Register Description Power Reduction" in the XMEGA AU Manual. Note: To disable the clock of a peripheral module, the corresponding bit in the PR register need to be written to "1". Note: Each port might have its own instance of a peripheral. For instance USART0 is found in PORT C, PORT D, PORT E, and PORT F. Each port has its own PR register to disable the peripherals of that port. As an example, disabling the AES and EBI module is done by setting the corresponding bits in the PR.PRGEN register like this: PR.PRGEN = PR_AES_bm PR_EBI_bm. The following modules can be disabled: PR.PRGEN: AES EBI USB PR.PRPA: AC DAC PR.PRPB: AC ADC DAC PR.PRPC: TWI USART1 USART0 SPI HIRES TC1 TC0 PR.PRPD: TWI USART1 USART0 SPI TC1 TC0 PR.PRPE: TWI USART1 USART0 7
SPI HIRES TC1 TC0 PR.PRPF: TWI USART1 USART0 SPI TC1 TC0 2.4.2. Put the EEPROM into Power Reduction Mode In this application the XMEGA EEPROM block is not used, so it can be put into power reduction mode. The register description for the NVM module can be found in section "Register Description NVM Controller" of the XMEGA AU Manual. The NVM.CTRLB register, which will be used to disable the EEPROM is protected by the Configuration Change Protection (CCP) mechanism. Special care regarding timing needs to be taken when writing to this register. This project includes a function that handles this for you. Below is the code: // Disable EEPROM in NVM module. ccp_write_io((void*)&nvm.ctrlb, NVM_EPRM_bm); 2.4.3. Add Idle Sleep Mode In this application the CPU is needed only to set up the peripherals and to perform the data processing. In order to reduce power consumption the CPU can be put into sleep whenever it is not in use. In Idle mode the CPU and Flash program memory is stopped, while the peripheral clock and peripheral modules are kept running. To enter Idle sleep mode, the following steps need to be performed: Select the desired sleep mode Enable sleep function Execute sleep instruction After wake-up, disable the sleep function Strictly speaking it does not need to disable the sleep function. It is however good coding practice to do this to ensure that software bugs leading to runaway code do not accidentally put the CPU back in sleep mode. The AVR GCC Toolchain has predefined macros and functions available to control the sleep modes of the AVR. In this application the following macro and functions are used: set_sleep_mode(mode) void sleep_enable(void) void sleep_cpu(void) void sleep_disable(void) To use these functions, include the "avr/sleep.h" header file in your project. 8
3. Revision History Doc Rev. Date Comments 42772A 09/2016 Initial document release 9
Atmel Corporation 1600 Technology Drive, San Jose, CA 95110 USA T: (+1)(408) 441.0311 F: (+1)(408) 436.4200 www.atmel.com 2016 Atmel Corporation. / Rev.: Atmel, Atmel logo and combinations thereof, Enabling Unlimited Possibilities, AVR, picopower, XMEGA, and others are registered trademarks or trademarks of Atmel Corporation in U.S. and other countries. Other terms and product names may be trademarks of others. DISCLAIMER: The information in this document is provided in connection with Atmel products. No license, express or implied, by estoppel or otherwise, to any intellectual property right is granted by this document or in connection with the sale of Atmel products. EXCEPT AS SET FORTH IN THE ATMEL TERMS AND CONDITIONS OF SALES LOCATED ON THE ATMEL WEBSITE, ATMEL ASSUMES NO LIABILITY WHATSOEVER AND DISCLAIMS ANY EXPRESS, IMPLIED OR STATUTORY WARRANTY RELATING TO ITS PRODUCTS INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, CONSEQUENTIAL, PUNITIVE, SPECIAL OR INCIDENTAL DAMAGES (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS AND PROFITS, BUSINESS INTERRUPTION, OR LOSS OF INFORMATION) ARISING OUT OF THE USE OR INABILITY TO USE THIS DOCUMENT, EVEN IF ATMEL HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. Atmel makes no representations or warranties with respect to the accuracy or completeness of the contents of this document and reserves the right to make changes to specifications and products descriptions at any time without notice. Atmel does not make any commitment to update the information contained herein. Unless specifically provided otherwise, Atmel products are not suitable for, and shall not be used in, automotive applications. Atmel products are not intended, authorized, or warranted for use as components in applications intended to support or sustain life. SAFETY-CRITICAL, MILITARY, AND AUTOMOTIVE APPLICATIONS DISCLAIMER: Atmel products are not designed for and will not be used in connection with any applications where the failure of such products would reasonably be expected to result in significant personal injury or death ( Safety-Critical Applications ) without an Atmel officer's specific written consent. Safety-Critical Applications include, without limitation, life support devices and systems, equipment or systems for the operation of nuclear facilities and weapons systems. Atmel products are not designed nor intended for use in military or aerospace applications or environments unless specifically designated by Atmel as military-grade. Atmel products are not designed nor intended for use in automotive applications unless specifically designated by Atmel as automotive-grade.