Application Note AN2361 Power Management - USB-Powered Battery Charger for NiCd/NiMH Batteries Author: Svyatoslav Paliy Associated Project: Yes Associated Part Family: CY8C24794 GET FREE SAMPLES HERE (CY Sample Request Form for all Product Lines) PSoC Designer Version: 4.2 + SP2 Associated Application Notes: AN2041, AN2107, AN2203, AN2260, AN2267, AN2267a Abstract This Application Note describes a USB-powered battery charger for NiCd/NiMH batteries that permits rapid recharging. Dedicated PC-based software has been developed to monitor and control the charging process in real time, and display data in a graphical user interface. The charger can be embedded into consumer, office and industrial applications. It needs no drivers and starts working immediately when plugged into a USB port. A battery can be left in the charger for any length of time without risk of overcharge. Introduction o PC Utilities and Debugging The growing popularity of wireless computer peripherals such as the wireless mouse, keyboard, and similar devices is likely to spur demand for a device that can be recharged rapidly. Rechargeable batteries are also widely used in peripheral and non-computer related appliances. o Design Modifications o Appendixes The charger described in this Application Note can easily be incorporated into computer peripherals. For example, one battery can be located in a mouse or another wireless device, and a second battery inserted into a combination host/charger that is connected to the USB port of a computer. When the battery in the peripheral device is drained, the user can simply swap the two batteries. Moreover, this charger can charge or discharge user batteries under manual charge control. The user can control the charger from PC software, immediately stopping or starting the charge process. As an added option, it is possible to discharge the battery before charging. This lets the user train batteries using a discharge-charge cycle. In the following sections, this Application Note describes how the charger works, its various aspects, and how it can be modified: o Rapid Battery Charging o Charger Hardware o Charger Firmware o USB Connection May 5, 2006 Rapid Battery Charging This charger uses the pulse method for nickel-based batteries, which makes rapid battery charging possible and preserves battery health. The battery is charged by means of constant current impulses of 5-second duration, alternated with 0.5-second latent periods during which no charge is applied. Then the charger measures the battery voltage. When the battery voltage stops increasing or starts decreasing, the battery is fully charged. The charge termination criteria and the battery voltage upon full charge are shown in Figure 1. A more detailed description of NiCd/NiMH battery charge termination criteria is available in AN2260, “Rapid NiCd/NiMH Battery Charger and DC Brushed Motor Controller for Autonomous Appliances.” Document No. 001-17400 Rev. ** -1- [+] Feedback AN2361 Figure 1. NiCd/NiMH Battery Pack Voltage in Time Graph V BAT Stop charge point Battery is fully charged The charge stage should be limited by a time-out that should extend for approximately 120-150 percent of the estimated time required to charge NiCd/NiMH batteries with expected maximal capacity. Time Charge Timeout Table 1. Main Charger Characteristics No. Parameter Name Two Cells Three Cells 1 Voltage indicating battery presence, V 0.5 0.75 2 Re-charge voltage level, V 1.8 2.7 3 Emergency charger stop voltage, V 3.5 5.25 4 Discharge stop voltage, V 1.0 1.5 Charger Hardware The charger structure is shown in Figure 2. The acronyms in the figure are defined as follows: USB – Full-speed universal serial bus engine used for receiving control data from the PC and sending data about charger status and battery states to the PC. CPU – Central processor core for implementing charge algorithms and performing charge control functions. INT – Differential integrator built on the base of a switched capacitor block. DSM – Delta-sigma modulator used for current regulator control output. ADC – Incremental analog-to-digital converter for digitizing the analog signals. AMUX – Analog multiplexer used for switching between two analog signals for measurement. INA – Switched capacitor-based instrumentation amplifier. Figure 2. Charger Structure The main task of the charger is efficient use of the maximum allowed current from the USB bus (0.5A) in order to minimize charge time. Because the current from a USB is insufficient for a fast battery charge, the battery is charged with a fixed current. It is the current drawn from the USB that is controlled rather than the charge current. The battery May 5, 2006 charge current is determined by the battery voltage; regulator input power is maintained at a constant level. The current regulator uses a hardware control loop. The hardware control loop consists of an instrumentation amplifier (INA) to handle a current sense resistor (R9) signal, a differential integrator (INT), which acts as a Document No. 001-17400 Rev. ** -2- [+] Feedback AN2361 difference amplifier, and a delta-sigma modulator (DSM) as a voltage-to-duty cycle converter. This loop controls the MOSFET gate duty cycle to keep the input current constant. This method enables the greatest use of available USB power. This implementation uses three configurable switched capacitor blocks: one for the instrumentation amplifier, one for the differential integrator and one for the delta-sigma modulator. The configuration of parameters of the switched capacitor block is described in AN2041, “Understanding Switched Capacitor Analog Blocks.” The following equations describe the operation of the regulator: VINA = VINA C AINA C INA V − BINA VP 2[3] ; INA P 2[1] CF CF Figure 3. Integrator Output (Upper Trace) and Delta-Sigma Modulator Output (Lower Trace) (1) C INA = AINA Rsense I bus ; C AINA = CBINA ; CF ⎡C VINT = ∫ ⎢ ⎣C INT A INT F Vref − C C INT B INT F ⎤ VINA ⎥dt; ⎦ a) When Battery Voltage is 3.9V (2) VINA is instrumentation amplifier output, and C AINA , CBINA , CFINA , C AINT , CBINT , and CFINT are values of the capacitors that correspond to generic switched capacitor blocks. For correct operation, C AINA must be equal to CBINA . The VP2[1] and VP2[3] values are voltage on the PSoC® pins P2[1] and P2[3], respectively. The value Rsense is the current sense resistor value. The value Ibus is the current drawn from the USB. The value VINT is the differential integrator output. The value Vref is the bandgap reference voltage. In the steady state, the integrated (VINT) average value should be equal to zero. Thus, the target value of the regulator current can be calculated as follows: I bus = C FINA ⋅ C AINT ⋅ Vref C BINT ⋅ C AINA ⋅ Rsense ; b) When Battery Voltage is 1.3V The battery charge current is determined by input current, battery voltage, and regulator efficiency. Regulator efficiency based on real measurements is shown in Figure 4. (3) If CFINA = 16, C AINT = 2, Vref = 1.3V, CBINT = 14, C AINA = CBINA = 31, and Rsense = 0.2Ω, the USB drawing current is equal to 0.479A. By modifying the capacitor values, the user can alter the current consumption from the USB supply. However, the USB specification enforces a 0.5A current limitation. Note that low efficiency at low voltage output is caused by voltage drops on diodes D3 and D4. Figure 4. Regulator Efficiency 85 Operation of the regulator is shown in Figure 3. Effi, % 80 75 70 65 60 1,0 1,5 2,0 2,5 3,0 3,5 4,0 4,5 Uout, V The battery voltage is measured by an ADC using a resistive divider to condition the signals to the allowed level. This is combined with a correlated double sampling method. ADC and input amplifier offsets are thus eliminated. The May 5, 2006 Document No. 001-17400 Rev. ** -3- [+] Feedback AN2361 ADC first measures voltage on pin P0[1] and then on pin P0[5]. The battery voltage is calculated as the difference between the voltages of P0[1] and P0[5]. The derived voltage measurement is shown in the following equation: ΔnV = nmax ( GV VP 0[1] − VP 0[5] Vref ) =n max GV βV Vbat (4) Vref VP0[1] and VP0[5] are the voltages on pins P0[1] and P0[5], respectively. The value ∆nV is the ADC value without the amplifier and ADC offset voltages. The value nmax is the maximum ADC code, with a value equal to 2047 for an 11bit incremental ADC. The value Vbat is battery voltage, and GV is the input amplifier gain, equal to 1. The value Vref is the bandgap reference voltage (1.3V), and βV is the resistive divider coefficient, which is equal to 0.25 for R3=150 kΩ, R5=100 kΩ, and R8=150 kΩ divider resistors: βV = 1 (5) R3 + R8 1+ R5 The ADC and regulator share Comparator Bus 1. Because the regulator and ADC never work simultaneously, the comparator bus reconnects by firmware to the ADC to provide measurement. The comparator bus reconnects to the delta-sigma modulator for hardware control of the charge state. See Charger Firmware later in this Application Note. The MOSFET Q4 allows the battery to be discharged before a charge. If Q2 is activated, the current from the battery flows though R11, R12, and R13, which are connected in parallel to provide greater thermal dissipation. This batterydischarge feature can be activated only from the PC software as an option for battery training; it is not automatic. The charger can be used in applications that require leaving the battery in the charger after the charge cycle, even if the PC power is turned off. Overcharging the battery can cause permanent damage or decrease battery capacity. To prevent battery overcharge, battery charging is only initiated after PC start when the battery voltage falls below a May 5, 2006 predefined limit. This limit is called the charge restart condition. But if the user inserts the battery in the charger, the charger starts immediately, without testing the voltage. To detect that a battery was changed when the charger was not powered from a USB, a simple RS-trigger, powered from the battery, is used. The RS-trigger automatically enters the reset state after the power goes on. Thus, changing the battery automatically resets the trigger. If the battery is not replaced in the charger, the recharge starts only if the battery voltage has fallen below a predefined limit. When the battery is changed, a charge is initiated regardless of battery voltage. The RS-trigger is built from two bipolar transistors, Q3 and Q4. As a result of nonsymmetrical resistances in bases (R4 vs. R5) and collector circuits (R1 vs. R2), a reset state is always triggered upon power-up. Refer to Appendix 1. One digital block and interrupt-based firmware are used for charge-time control. The PSoC Timer8 User Module generates an interrupt with a frequency of 20 Hz and clocks from VC3 (5,859 kHz). Two LEDs, green and red, are used to indicate the charger state. When the charger is connected to the USB, both LEDs are switched on. After device enumeration, the LEDs indicate the charger state. A green LED means a charge is in process. When both LEDs are turned off, everything is functioning properly but no charge is being applied to the battery at this time, possibly because the battery is fully charged or there is no battery in the charger. A red light indicates an error condition. More detailed charger state information is provided with the PC software. Charger Firmware The firmware is constructed as a state machine. The state diagram is shown in Figure 5. The active charge states are marked in green. Error states are marked in red. Document No. 001-17400 Rev. ** -4- [+] Feedback AN2361 Figure 5. Charger State Diagram Stop i Discharge g m Power On Initialization k h j a Wait After Discharge l Charge p o b f No Battery Wait After Charge m Voltage Error c e Timeout Error Charge Complete d n In Figure 5, state transitions are indicated by lines and squares labeled with letters. See State Transition Descriptions ahead for explanations of the transitions. The states in circles in Figure 5 are defined as follows: Initialization – Both LEDs are off. Test for battery presence and state based on battery voltage. Timeout Error – Red LED is on. Charging is stopped. Charger has attempted to charge the battery for a predefined period but detects that the battery has failed to take a full charge. The battery may be damaged. Wait for the battery to be removed. State Transition Descriptions Charge – Green LED is on. Charge uses hardware current regulator for 5 seconds. The state transitions shown in Figure 5 are described as follows: Wait After Charge – Green LED is on. No charge, wait 0.5 seconds. a) IF VNO-BATT < VBATT ≤ VRE-CH or fBatteryChanged flag is set Charge Complete – Both LEDs are off. No charge. Battery is fully charged. THEN Charge Stop – Both LEDs are off. No charge. This state occurs when the device gets a stop command from the PC. Wait for the next command. If a battery is detected and the battery is not already charged, then the green LED is turned on, and transition to the Charge state occurs. Discharge – Green LED is on. Discharge is in progress for 5 seconds. b) Wait After Discharge – Green LED is on. No charge, no discharge, wait 0.5 seconds. Charge the battery for 5 seconds. No Battery – Both LEDs are off. Charge is turned off. Wait for battery to be inserted. Voltage Error – Red LED is on. Stop charging. Battery voltage has reached the permitted safe level. The battery or charger may be damaged. Wait for battery to be removed. May 5, 2006 IF 5 sec has elapsed THEN Wait after charge c) IF battery voltage increasing THEN Charge Complete The charge termination criteria are that the voltage is decreasing during a charge, or that the voltage remains constant (no longer increasing) during a charge. If either criterion is not met, then the battery charge continues. Document No. 001-17400 Rev. ** -5- [+] Feedback AN2361 d) IF battery voltage not increasing If the battery charge exceeds a predefined limit, the charge stops. The battery may be damaged. THEN Charge Complete If termination criteria are met, the green LED is turned off and the charge is stopped. Transition to the Charge Complete state occurs. e) THEN Charge p) IF VRE-CH < VBATT If a battery is detected and is fully charged, transition to the Charge Complete state occurs. IF Stop command received THEN Stop If a stop command has been received from the PC, a stop occurs immediately, regardless of the current state. h) IF Charge command received If a charge command has been received from the PC, then charging starts immediately, regardless of the battery voltage. A time-based interrupt handler is used to implement sleep mode (Suspend state of the USB). The interrupt handler checks the bus activity bit. If the bus enters USB Suspend mode, the charger goes into CPU sleep mode. The CPU stays in sleep mode until it receives a wake-up event from the USB. Sleep mode support is required for full compliance with USB standards. The charger uses USB for two purposes: • As a power source for the charger. THEN Discharge Logically, the charger is represented as an HID device. The charger requires only a low data rate. An HID is very useful in this case. The advantage of an HID is that no special driver is needed and software support is simplified. The supporting documents (see supporting files in the project .zip) contain tables of the USB descriptors and HID report descriptors. Both descriptors are represented in PSoC style, corresponding to the USB Setup Wizard tables. IF TBATT ≤ TCOLD_STOP IF VBATT > VDISCH PC Utilities and Debugging THEN Discharge Dedicated charger control software has been developed to monitor the charge process. The user interface (UI) is very simple, as shown in Figure 6. If batteries are not discharged, the discharge continues. IF VBATT ≤ VDISCH THEN Charge If the battery is fully discharged, transition to the Charge state occurs. m) Note that if a battery is removed, then the charger goes to the No Battery state from any other state except the Stop state. IF Discharge command received Permits discharge of the battery for 5 seconds. l) If a newly inserted battery is detected, a charge starts. A charge always starts after a battery is inserted. To receive and manually control data that relates to the charge process. THEN Wait after discharge k) THEN Charge • If the discharge command is received from the PC, then discharging starts immediately. This is the only way to discharge the battery. j) IF VBATT > VNO-BATT USB Connection THEN Charge i) THEN No Battery Exit from the current state and transition to No Battery if the battery has been removed. THEN Charge Complete g) IF VBATT ≤ VNO-BATT IF VNO-BATT < VBATT ≤ VRE-CH If the battery voltage has fallen below the re-charge voltage level (possibly because the battery is self-discharging), the battery charge is resumed. f) o) The UI consists of a chart that displays battery voltage, labels indicating the present charger state (Stopped, in Figure 6), battery voltage (2.79V), and four buttons to manually control the charge process. The software is optional and not required for charger operation. IF VBATT > VERR THEN Voltage Error If battery voltage exceeds the VERR level, transition to the Voltage Error state occurs regardless of the present state. n) IF duration of charge exceeds predefined limit THEN Timeout Error May 5, 2006 Document No. 001-17400 Rev. ** -6- [+] Feedback AN2361 Figure 6. Charger Monitoring Software Charger State Battery Chart with Charge Control Buttons Battery Voltage The data packets that are sent from the charger to the PC and from the PC to the charger are shown in Table 2. The data packet from charger to PC contains the charger state and battery voltage. Using this data, the software builds a chart and prints the actual battery voltage. This package has a length of 3 bytes. The battery voltage is calculated in Equation (6) from the returned ADC code by the inverse function of Equation (4): VBATT = ΔnV BATT Vref ΔnV BATT GV βV nmax ; (6) is the battery voltage in ADC code from the data packet. The data packet from PC to charger contains a single 1-byte command. Table 2. Data Packets Item # Length in Bytes Reference Description a) Data Packet from Charger to PC 1 2 1 2 State ΔnVBATT Charger State • INITIALIZATION = 0 • CHARGE = 1 • WAIT AFTER CHARGE = 2 • CHARGER COMPLETE = 3 • NO BATTERY = 4 • DISCHARGE = 5 • WAIT AFTER DISCHARGE = 6 • STOP = 7 • VOLTAGE ERROR = 8 • TIMEOUT ERROR = 9 Battery voltage in ADC code b) Data Packet from PC to Charger 1 1 Cmd Command to Charger • STOP = 1 • CHARGE = 2 • RECHARGE = 3 • DISCHARGE = 4 • SET TIMEOUT = 5 May 5, 2006 Document No. 001-17400 Rev. ** -7- [+] Feedback AN2361 Design Modifications It is possible to decrease the cost of the charger by eliminating the coil and Schottky diode and substituting a pulse regulator with a linear current source. However, this alternative increases the charge time because if a linear regulator is used, the charge current is not greater than 0.5A. The constant current source consists of an instrumentation amplifier to sense the current and a differential integrator. The current source is similar to that described in AN2203 “Programmable Analog High Current Source. PSoC Style.” The structure of a charger with a linear current source is shown in Figure 7. The delta-sigma modulator (DSM) is eliminated and the differential integrator directly drives the MOSFET gate. The differential amplifier inputs are swapped to obtain an inverted signal on the output. The inverted signal is required to form negative feedback in order to drive a P-channel MOSFET. In contrast to a switching regulator, the MOSFET in a linear regulator operates in linear mode. As a result, the power dissipation on the MOSFET is higher, so a small heat sink may be necessary. A PCB plane is suitable for this role. Figure 7. Charger Structure with Linear Regulator The charge current vs. battery voltage for the switching regulator is shown in Figure 8 Figure 8. Charge Current vs. Battery Voltage 1200 However, the overall benefit is not great when a switching regulator is used to charge three NiCd/NiMH batteries. So in this case, a linear regulator can be used with a very small loss of efficiency. Switching and linear regulator efficiencies against battery voltage are shown in Figure 9. Figure 9. Switching and Linear Regulator Output Power vs. Battery Voltage 1100 1000 , mA 900 800 2,0 600 1,8 500 1,6 400 1,0 Switching reg Linear reg. 2,2 700 1,5 2,0 2,5 3,0 3,5 4,0 4,5 Uout , V Power, W out I 1,4 1,2 1,0 0,8 For the linear regulator, the current is constant and does not exceed 0.5A. So when a switching regulator is used to charge two batteries (2.6V in a charged state) the charge current is significantly greater than the charge current when a linear current source is used. For three batteries with 3.9V in a charged state, the charge current is about the same for both kinds of regulators. For deeply discharged batteries (2.4V), the charge current is greater. May 5, 2006 0,6 1,0 1,5 2,0 2,5 3,0 3,5 4,0 4,5 Uout,V The time required to charge different batteries when the two regulator types are used is shown in Table 3. Document No. 001-17400 Rev. ** -8- [+] Feedback AN2361 Table 3. Charge Time Examples with Different Regulator Types No. Case Time 1 Two cells by 1400 mAh using switching regulator 189 min 2 Two cells by 1400 mAh using linear regulator 323 min 3 Three cells by 1800 mAh using switching regulator 384 min 4 Three cells by 1800 mAh using linear regulator 426 min The USB charger is designed to be part of a more complex system. Additional resources can be allocated to the unused digital blocks. For example, the SPIM User Module can be added for communication with the WUSB User Module. The size of the firmware code is small, about 4.5 KB (less than 30 percent of Flash). A total of 104 bytes of SRAM is used for variables (13 percent using the large memory model (LMM), 40 percent without). Resources are thus available to combine this charger onto one PSoC device with the user’s own design. May 5, 2006 Document No. 001-17400 Rev. ** -9- [+] Feedback AN2361 Appendix 1. Charger Schematic DISCH VN VP VDD 48 47 46 45 44 43 50 49 R6 24R +5V Vdd -D +D Gnd P0[6] P0[4] P0[2] P0[0] P2[6] P2[4] R1 330 R2 330 D1 RED T_read T_Set D2 GREEN P7[7] P7[0] P1[0] P1[2] P1[4] P1[6] Vbat R3 150K R4 VDD ISSP/DEBUG J2 42 41 40 39 38 37 36 35 34 33 32 31 30 29 23 24 25 26 27 28 19 20 21 22 15 16 17 18 1 2 3 4 5 P2[2] P2[0] P4[6] P4[4] P4[2] P4[0] P3[6] P3[4] P3[2] P3[0] P5[6] P5[4] P5[2] P5[0] CY 8C24794 P1[7] P1[5] P1[3] P1[1] +5V J1 P2[3] P2[1] P4[7] P4[5] P4[3] P4[1] P3[7] P3[5] P3[3] P3[1] P5[7] P5[5] P5[3] P5[1] Vss D+ DVdd 1 2 3 4 5 6 7 8 9 10 11 12 13 14 IN IP Vss Vdd P2[5] P2[7] P0[1] P0[3] P0[5] P0[7] U1 56 55 54 53 52 51 DRIVE C1 1u R7 24R +5V 10R VP R5 100K C2 100u + VN 1 2 3 4 R8 150K USB-B Switch-mode Regulator IP +5V + IN Q1 IRLML6402 R9 0.2R 1% C3 100u C5 0.1u + Vbat D3 L1 MBR320 C4 100u R10 1M J3 47uH* D4 MBR320* 1 2 C6 1u BATTERY DRIVE Linear Regulator IP +5V + IN Q1 MMSF3P03HD R9 0.2R 1% C3 100u C5 0.1u R10 100K R20 1.5K Vbat D3 J3 1 2 C6 0.1u MBR320 BATTERY DRIVE Discharge Switch RS-trigger Vbat R11 30R Vbat R12 30R R14 47k R13 30R R15 330k T_read R16 1M Q2 IRML2502 DISCH Q3 BC817 R19 1M R17 1M R18 180k Q4 BC817 D5 T_Set LL4148 May 5, 2006 Document No. 001-17400 Rev. ** - 10 - [+] Feedback AN2361 Appendix 2. Board Photo May 5, 2006 Document No. 001-17400 Rev. ** - 11 - [+] Feedback AN2361 About the Author Name: Svyatoslav Paliy Title: Application Engineer Background: Svyatoslav earned a Master of Science degree from Lviv Polytechnic National University (Ukraine) in 2000. His interests include the various aspects of embedded systems design, and Windows and Linux programming. Contact: [email protected] In March of 2007, Cypress recataloged all of its Application Notes using a new documentation number and revision code. This new documentation number and revision code (001-xxxxx, beginning with rev. **), located in the footer of the document, will be used in all subsequent revisions Cypress Semiconductor 198 Champion Court San Jose, CA 95134-1709 Phone: 408-943-2600 Fax: 408-943-4730 http://www.cypress.com © Cypress Semiconductor Corporation, 2006-2007. The information contained herein is subject to change without notice. Cypress Semiconductor Corporation assumes no responsibility for the use of any circuitry other than circuitry embodied in a Cypress product. Nor does it convey or imply any license under patent or other rights. Cypress products are not warranted nor intended to be used for medical, life support, life saving, critical control or safety applications, unless pursuant to an express written agreement with Cypress. Furthermore, Cypress does not authorize its products for use as critical components in life-support systems where a malfunction or failure may reasonably be expected to result in significant injury to the user. The inclusion of Cypress products in life-support systems application implies that the manufacturer assumes all risk of such use and in doing so indemnifies Cypress against all charges. This Source Code (software and/or firmware) is owned by Cypress Semiconductor Corporation (Cypress) and is protected by and subject to worldwide patent protection (United States and foreign), United States copyright laws and international treaty provisions. Cypress hereby grants to licensee a personal, non-exclusive, non-transferable license to copy, use, modify, create derivative works of, and compile the Cypress Source Code and derivative works for the sole purpose of creating custom software and or firmware in support of licensee product to be used only in conjunction with a Cypress integrated circuit as specified in the applicable agreement. Any reproduction, modification, translation, compilation, or representation of this Source Code except as specified above is prohibited without the express written permission of Cypress. Disclaimer: CYPRESS MAKES NO WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, WITH REGARD TO THIS MATERIAL, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. Cypress reserves the right to make changes without further notice to the materials described herein. Cypress does not assume any liability arising out of the application or use of any product or circuit described herein. Cypress does not authorize its products for use as critical components in life-support systems where a malfunction or failure may reasonably be expected to result in significant injury to the user. The inclusion of Cypress’ product in a life-support systems application implies that the manufacturer assumes all risk of such use and in doing so indemnifies Cypress against all charges. Use may be limited by and subject to the applicable Cypress software license agreement. May 5, 2006 Document No. 001-17400 Rev. ** - 12 - [+] Feedback