APPLICATION NOTE Atmel AVR145: USB HID Lithium-Ion Battery Charging via USB with ATmega16/32U4 16-/32-bit Atmel Microcontrollers Features • • • • • Fully functional design for charging Lithium-Ion batteries High accuracy measurement with 10-bit A/D converter Modular “C” source code Analog inputs for reading battery ID and temperature USB HID class for user interface Introduction This application note describes how to use the Atmel® EVK527 evaluation kit to charge Lithium-Ion (Li-Ion) batteries using USB connection as power supply. The USB HID class is used to display battery parameters on PC. This application note is a merge between two application notes: • AVR®328: USB Generic HID Implementation Battery charge parameters are updated on PC with using HID report messages. A battery task is added in generic HID firmware. • AVR146: USB Lithium-Ion Battery Charging via USB with ATmega16/32U4 The battery task added in generic HID firmware comes from this application note. AVR146 implements the USB CDC class. 8208A−AVR−10/2012 1. Description This document describes an application running on the Atmel EVK527 evaluation kit. The EVK527 is dedicated to the Atmel ATmega16/32U4. The USB offers a 5V power supply on the VBUS pin. The available current range is from 100mA to 500mA. This is enough to charge a Li-Ion battery cell. A Li-Ion cell needs an accurate control of voltage and current during charge. ATmega16/32U4 offers a USB full speed interface, PWM channels and 10-bit ADC channels. All these features are used to perform a Li-Ion battery charger via USB. Figure 1-1. Hardware description. EVK527 PC USB Link Battery For a user-friendly interface, all charging parameters (charging status, battery voltage, charge current, battery temperature…) are displayed on the PC without the use of measurement tools. After the USB enumeration, a USB Human Interface Device appears (see Figure 1-2). The user may launch the dedicated USB battery charging PC application. Battery charging parameters are updated with HID report messages. AVR145: USB HID Lithium-Ion Battery Charging via USB [APPLICATION NOTE] 8208A−AVR−10/2012 2 Figure 1-2. Device Manager window. Figure 1-3. Properties window. AVR145: USB HID Lithium-Ion Battery Charging via USB [APPLICATION NOTE] 8208A−AVR−10/2012 3 2. Theory of operation Battery charging is made possible by a reversible chemical reaction that restores energy in a chemical system. Depending on the chemicals used, the battery will have certain characteristics. A detailed knowledge of these characteristics is required in order to avoid inflicting damage to the battery. 2.1 Li-Ion battery technology Lithium-Ion batteries have the highest energy/weight and energy/space ratios of modern rechargeable batteries (See Reference 1 on page 20). It is currently the fastest growing battery system on the market, with end applications such as notebook computers, cell phones, portable media players, Personal Digital Assistants (PDA), power tools and medical devices. Compared to traditional rechargeable batteries, Li-Ion batteries have low internal resistance, high cycle life, fast charge time, low self-discharge, low toxicity and no maintenance requirements. For example, Lithium-Ion cells with cobalt cathodes hold twice the energy of a nickel-based battery and four times that of lead acid. Lithium-Ion is a low maintenance system, an advantage that most other chemistries cannot claim. There is no memory effect with LithiumIon and the battery does not require scheduled cycling to prolong its life. Lithium-Ion has a low self-discharge and is environmentally friendly. Disposal causes minimal harm. Drawbacks of Li-Ion batteries include low tolerance of overcharge and the need for embedded protection circuitry. An electrical short can result in a large current flow, a temperature rise and thermal runaway in which flaming gases are vented. 2.1.1 Safety Lithium-Ion batteries are safe, provided that certain precautions are met when charging and discharging. In addition, battery manufacturers ensure a high level of reliability by adding three layers of protection, as follows: 1. 2. 3. The amount of active material is limited to achieve a workable equilibrium of energy density and safety. Various safety mechanisms are included within each cell. An electronic protection circuit is added inside the battery pack. Cell protection devices work as follows: • A PTC/NTC (positive/negative temperature coefficient) device acts as a protection to inhibit high current surges • The CID (circuit interrupt device) opens the electrical path if an excessively high charge voltage raises the internal cell pressure • The safety vent allows a controlled release of gas in the event of a rapid increase in cell pressure The electronic protection circuit works as follows: • • • A solid-state switch is opened if the charge voltage of any cell reaches a given threshold A fuse cuts the current flow if the skin temperature of the cell approaches 90°C (194°F) The current path is cut when cell voltage drops below a given threshold. This is in order to prevent the battery from over-discharging Today, Lithium-Ion is one of the most successful and safe battery chemistries available with billions of cells being produced every year. AVR145: USB HID Lithium-Ion Battery Charging via USB [APPLICATION NOTE] 8208A−AVR−10/2012 4 2.2 Charging Li-Ion batteries There is only one way to charge lithium-based batteries. Manufacturers of Lithium-Ion cells have very strict guidelines in charge procedures and the packs should be charged as per the manufacturers "typical" charge technique. Li-Ion batteries are charged using constant voltage (after having reached the nominal charge voltage), with current limiter to avoid overheating in the initial stage of the charging process. Charging is terminated when the charge current drops below a threshold set by the manufacturer. Several parameters are monitored during the charge: charge time, battery temperature… The battery takes damage from overcharging and may explode if overcharged. 2.2.1 Safety Static electricity or a faulty charger may destroy the battery's protection circuit and turn solid-state switches to a permanent ON position. This may happen without the user knowing. A battery with a faulty protection circuit may function normally but does not provide protection against abuse. Consumer grade Lithium-Ion batteries cannot be charged below 0°C (32°F). If charged at cold temperatures, battery packs may appear to be charging normally but chemical reactions inside the cells may cause permanent damage and can compromise the safety of the pack. The battery will become more vulnerable to failure if subjected to impact, crush or high rate charging. The battery must remain cool. A battery pack that gets hot during charge should not be used. 2.2.2 Priming and charge intervals Unlike many other types of rechargeable batteries, Lithium-Ion batteries do not need priming. The first charge of a Li-Ion battery is no different than the 10th or the 100th charge. Lithium-Ion batteries may be – and should be – charged often. The battery lasts longer with partial rather than full discharges. Full discharges should be avoided because of wear. The battery loses capacity due to aging, whether used or not. 2.2.3 Charge stages Lithium-Ion battery charge follows three stages: 1. 2. 3. Prequalification current. Charging of a Li-Ion battery starts with a test of battery voltage. If the voltage is under a defined threshold (PREQUAL_VOLTAGE), the charge starts with a fixed low current. Constant current. The charge continues with applying constant current to the battery. The size of the charge current is batterydependent and given by the manufacturer. This stage is complete when battery voltage has reached the threshold given by the manufacturer. Constant voltage. After battery threshold voltage has been reached the charger will switch from supplying constant current to supplying constant voltage. This stage is complete when charge current has dropped below the threshold given by the manufacturer. Figure 2-1 illustrates voltage and current of a Lithium-Ion battery during charging. AVR145: USB HID Lithium-Ion Battery Charging via USB [APPLICATION NOTE] 8208A−AVR−10/2012 5 Figure 2-1. Charge stages and limits of a VARTA EasyPack 550mAh. Voltage/Current OVERCHARGE VOLTAGE LIMIT Vbatt 4.2V 3V OVERDISCHARGE VOLTAGE LIMIT 260mA Ibatt 100mA 10mA ICUTOFF t CONSTANT CURRENT CHARGE CONSTANT VOLTAGE CHARGE In Figure 2-1, “Overcharge” is the level at which cell protection circuitry cuts in and opens a solid-state switch and discontinues the charge current path. After this, battery voltage typically needs to drop several hundred millivolts before the current path is restored. “Overdischarge” is the level at which the current path is cut in order to prevent the battery from over-discharging. 2.3 VARTA battery 2.3.1 Typical charge characteristics Battery specifications should always be verified from manufacturer’s data sheets. Table 2-1 shows a summary of typical Lithium-Ion battery charge characteristics is to be found in. Actual parameters may vary. Table 2-1. Typical charge characteristics. Parameter Typical value Charge time 3 hours Charge current 1 C (1) Charge efficiency 99.9% 0.03 C (1) Charge current threshold Charge voltage 4.20V Charge voltage tolerance (per cell) ±0.05V Temperature range 0 … +45°C Humidity range 65 ±20 RH Notes: 1. C corresponds to the typical rated capacity value (see Table 2-2). AVR145: USB HID Lithium-Ion Battery Charging via USB [APPLICATION NOTE] 8208A−AVR−10/2012 6 2.3.2 Typical battery characteristics Table 2-2 summarises manufacturer’s data for the batteries types used in this application. Other types of batteries may be used, but may require adjustments to software and/or hardware. Table 2-2. Manufacturer’s data for VARTA EasyPack range of Lithium-Ion batteries. Parameter Rated capacity (typical) EZPack S-3.7V EZPack M-3.7V EZPack L-3.7V EZPack XL-3.7V Unit 550 750 1000 2000 mAh Nominal voltage 3.70 V 2.75 … 4.20 V Charge voltage 4.20 V Charge voltage tolerance ±50 mV Operating voltage range Charge current 520 720 955 955 mA Charge cut-off time 3 3 3 4 hours Charge cut-off current 10 14 19 38 mA 3.9 6.8 10 24 kΩ RID (1) (resistor ID) NTC 10 kΩ B-value (2) 3435 K Overcharge detection 4.35 V Overdischarge detection 2.20 V Notes: 2.3.3 1. RID: Battery internal resistor identifies the capacity of battery connected. 2. B value is used in temperature formula. Electrical pinout This application uses a particular type of Lithium-Ion batteries and all configurations presented here are based on manufacturer’s data. Other Lithium-Ion batteries may naturally be used but it is up to the user to look up battery data from manufacturer’s data sheets and make sure that necessary adjustments are made to firmware and hardware. Figure 2-2 illustrates connection pads of the Lithium-Ion batteries used in this application. Figure 2-2. Connection pads of a VARTA EasyPack cell. AVR145: USB HID Lithium-Ion Battery Charging via USB [APPLICATION NOTE] 8208A−AVR−10/2012 7 The battery is connected to the battery charger as follows. Table 2-3. 2.4 Connecting battery to charger. Battery connector Charger connector Note - (minus) BATTERY- NTC NTC/RID Battery temperature measurement ID SCL RID, Battery identification resistor + (plus) BATTERY+ VBUS supply voltage USB powered applications fall into one of the three following categories: • Low-Power Bus The low-power bus powered functions derive all their power from VBUS and must not draw more than one unit load (100mA) according to the USB standard. It must also be able to work between the VBUS voltage of 4.40V and 5.25V. • High-Power Bus The high-power bus powered functions derive all their power from VBUS and cannot draw more than 100mA until it has been configured. Once configured, it can draw up to five unit loads (500mA) by requesting it in its descriptor. At full load, it must be able to work between the VBUS voltage of 4.75V and 5.25V. • Self-Power Self powered functions can draw up to 100mA from VBUS and the rest from another source. The current to power the Atmel EVK527 and to charge the battery comes from VBUS. The EVK527 must limit the charge current if needed. An easy solution is to modify the I-charge parameter in the lookup table. For example, a 550mAh battery allows a 260mA charging current. A modification of this parameter to 90mA (for example) allows connecting the charger on a Low-Power Bus, knowing that the EVK527 consumption with an 8MHz oscillator is about 10mA. In this case, the prequalification current must also be limited to 90mA. 2.5 EVK527 revision The EVK527 Rev1.0.0 is provided with several updates: • The shunt resistor is connected between PF0 and PF1 to use the differential input mode. PF0 must replace PD4 and SP6 must be “without solder” (default configuration) • • • Gate pin and Source pins of Q1A are disconnected R6 and R7 new values are 13kΩ R3 new value is 1Ω These modifications are implemented in schematics given in §6. 2.6 Buck converter A buck converter is integrated on EVK527 to control the battery voltage and the battery current. The switch is controlled by the High speed PWM output. AVR145: USB HID Lithium-Ion Battery Charging via USB [APPLICATION NOTE] 8208A−AVR−10/2012 8 Figure 2-3. Buck converter schematic. 2.6.2 PWM frequency The PWM speed for the PWM is programmed to the maximum (64MHz). The source clock is the PLL output (96MHz) used both by USB and PWM. A postscaler offers a 1.5 division for the PLL signal: 96MHz/1.5 = 64MHz (see PLLTM1 and PLLTM0 in PLLFREQ register). The result on the PWM output signal is a 250kHz frequency: 64MHz / 256 = 250kHz Where 256 is the size in bit of the in OCR4A compare register used in Timer 4. Figure 2-4. PWM output signal. PWM output ton toff t The software controls the battery voltage/current in modifying the duty cycle of PWM output. If ton increases, the battery voltage/current receives a more important energy load. AVR145: USB HID Lithium-Ion Battery Charging via USB [APPLICATION NOTE] 8208A−AVR−10/2012 9 3. Battery charger firmware 3.1 Scheduler A scheduler is implemented to call indefinitely defined tasks. Before starting this infinite loop, init functions are called. There are three tasks. Each task is called after the end of the previous one (no pre-emption). Figure 3-1. Scheduler. Scheduler Init: Usb_task_init() HID_task_init() Batt_task_init() Scheduler task: Usb_task() HID_task() Batt_task() 3.2 List of files The firmware is written in C language using IAR Embedded Workbench®, version 5.10. Since the firmware has been written entirely in C, it should not be a difficult task to port it to other AVR C-compilers. Some compiler specific details may, however, need to be rewritten. In Table 3-1 are listed the files that are relevant to the compiler project. Table 3-1. Project files for HID application (see IAR EW workspace file). File Type hid_task.c C source code hid_task.h Header file main.c C source code main.h Header file power_drv.c C source code power_drv.h Header file scheduler.c C source code scheduler.h Header file start_boot.c C source code start_boot.h Header file Note HID task and HID task init functions Main program / Program entry point Power management low level driver Scheduler routines Boot functions AVR145: USB HID Lithium-Ion Battery Charging via USB [APPLICATION NOTE] 8208A−AVR−10/2012 10 File Type time.c C source code time.h Header file usb_descriptor.c C source code usb_descriptor.h Header file usb_device_task.c C source code usb_device_task.h Header file usb_drv.c C source code usb_drv.h Header file usb_standard_request.c C source code usb_standard_request.h Header file usb_specific_request.c C source code usb_specific_request.h Header file usb_task.c C source code usb_task.h Header file Table 3-2. Note Functions for timing USB parameters that identify the application USB device controller USB driver routines USB device enumeration requests User call-back functions Usb task and Usb init task functions Project files for battery module (see IAR EW workspace file). File Type ADC.c C source code ADC.h Header file Batt_task.c C source code Batt_task.h Header file battery.c C source code battery.h Header file chargefunc.c C source code chargefunc.h Header file LIIONcharge.c C source code LIIONcharge.h Header file menu.c C source code menu.h Header file PWM.c C source code PWM.h Header file statefunc.c C source code statefunc.h Header file Note Functions related to A/D converter Batt task and Batt init task functions Battery-specific definitions and functions related to battery control and data acquisition Charge functions Charge state function for Li-Ion batteries State machine definitions Functions related to generating pulse-width modulated output Functions related to the states defined in menu file AVR145: USB HID Lithium-Ion Battery Charging via USB [APPLICATION NOTE] 8208A−AVR−10/2012 11 3.3 Battery task A complete description of battery task is available in the AVR146: Lithium-Ion Battery Charging via USB with ATmega16/32U4 application note. Battery modules are identical in CDC and HID implementations. 3.4 HID task The AVR328 USB Generic HID reports are modified to add battery charge parameters. The OUT reports are not used for battery charge (LEDs management for Generic HID). The size of IN reports is the same (8 bytes). Table 3-3. HID IN report description. HID IN report Value Description BYTE 0 0 Reserved for HID Generic Demo 1 Reserved for HID Generic Demo Battery Type BYTE 1 0 No battery detected 1 VARTA EASYPACK 550mA 2 VARTA EASYPACK 750mA 3 VARTA EASYPACK 1000mA 4 VARTA EASYPACK 2000mA Charge State BYTE 2 0 No charge 1 Constant current charge 2 Constant voltage charge 3 Full charge detected Battery voltage (16 bits) BYTE 3 Value Batt Voltage LSB BYTE 4 Value Batt Voltage MSB Battery current (16 bits) BYTE 5 Value Batt Current LSB BYTE 6 Value Batt Current MSB Battery temperature (8 bits) BYTE 7 Value Batt Temperature AVR145: USB HID Lithium-Ion Battery Charging via USB [APPLICATION NOTE] 8208A−AVR−10/2012 12 3.5 Memory requirements The firmware integrates all functions required to charge a Lithium-Ion battery. Table 3-4. Memory requirements of firmware (GCC without optimization, -O0). Build option Debug Memory Approximate value CODE (Flash) 13272 bytes DATA (SRAM) 274 bytes XDATA (EEPROM) 130 bytes AVR145: USB HID Lithium-Ion Battery Charging via USB [APPLICATION NOTE] 8208A−AVR−10/2012 13 4. Battery charger software 4.1 Overview A PC application is launched and detects automatically the HID device. Battery voltage, current, temperature, battery type and charge state are displayed. The actual firmware only works with VARTA EASYPACK batteries. To support another type of battery, the battery parameters table (Flash memory) needs to be modified (new firmware revision). Caution: “Enter DFU” button needs to have a bootloader loaded in Flash memory. If not, it appears as a reset of the application firmware. Figure 4-1. PC screen shot. AVR145: USB HID Lithium-Ion Battery Charging via USB [APPLICATION NOTE] 8208A−AVR−10/2012 14 5. Atmel EVK527 Rev1.0.0 schematics Figure 5-1. Page 1/5. AVR145: USB HID Lithium-Ion Battery Charging via USB [APPLICATION NOTE] 8208A−AVR−10/2012 15 Figure 5-2. Page 2/5. AVR145: USB HID Lithium-Ion Battery Charging via USB [APPLICATION NOTE] 8208A−AVR−10/2012 16 Figure 5-3. Page 3/5. AVR145: USB HID Lithium-Ion Battery Charging via USB [APPLICATION NOTE] 8208A−AVR−10/2012 17 Figure 5-4. Page 4/5. AVR145: USB HID Lithium-Ion Battery Charging via USB [APPLICATION NOTE] 8208A−AVR−10/2012 18 Figure 5-5. Page 5/5 AVR145: USB HID Lithium-Ion Battery Charging via USB [APPLICATION NOTE] 8208A−AVR−10/2012 19 6. References 1. “What’s the best battery?”. Retrieved April 3, 2007, from Battery University: http://www.batteryuniversity.com/partone-3.htm 2. “Lithium-Ion safety concerns”. Retrieved April 3, 2007, from Battery University: http://www.batteryuniversity.com/partone-5B.htm 3. “Charging Lithium-Ion batteries”. Retrieved April 3, 2007, from Battery University: http://www.batteryuniversity.com/partone-12.htm 4. “VARTA EasyPack” datasheet: • 550mAh http://www.varta-microbattery.com/applications/mb_data/documents/product_information/PI_56445701099.pdf • 1000mAh http://www.varta-microbattery.com/applications/mb_data/documents/product_information/PI_56446701099.pdf • 2000mAh http://www.varta-microbattery.com/applications/mb_data/documents/product_information/PI_56446702099.pdf • 5. 750mAh Not for sale anymore “ATmega32U4”. Available from Atmel web site: http://www.atmel.com/products/avr/ AVR145: USB HID Lithium-Ion Battery Charging via USB [APPLICATION NOTE] 8208A−AVR−10/2012 20 7. Revision history Doc. Rev. Date Comments 8208A 10/2012 Initial document release AVR145: USB HID Lithium-Ion Battery Charging via USB [APPLICATION NOTE] 8208A−AVR−10/2012 21 Atmel Corporation Atmel Asia Limited Atmel Munich GmbH Atmel Japan G.K. 1600 Technology Drive Unit 01-5 & 16, 19F Business Campus 16F Shin-Osaki Kangyo Bldg. San Jose, CA 95110 BEA Tower, Millennium City 5 Parkring 4 1-6-4 Osaki, Shinagawa-ku USA 418 Kwun Tong Road D-85748 Garching b. Munich Tokyo 141-0032 Tel: (+1)(408) 441-0311 Kwun Tong, Kowloon GERMANY JAPAN Fax: (+1)(408) 487-2600 HONG KONG Tel: (+49) 89-31970-0 Tel: (+81)(3) 6417-0300 www.atmel.com Tel: (+852) 2245-6100 Fax: (+49) 89-3194621 Fax: (+81)(3) 6417-0370 Fax: (+852) 2722-1369 © 2012 Atmel Corporation. All rights reserved. / Rev.: 8208A−AVR−10/2012 Atmel®, Atmel logo and combinations thereof, AVR®, Enabling Unlimited Possibilities®, and others are registered trademarks or trademarks of Atmel Corporation or its subsidiaries. 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.