//-------------------------------------------------------------------------- // // This module uses the RC battery circuit to compute a battery level // between 1 and 10. // //-------------------------------------------------------------------------- // $Archive: /WirelessUSB/WUSB Kits/CY4632 LS KBM RDK/DocSrc/CD_Root/Firmware/Source Code/RDK Mouse/battery.c $ // $Modtime: 6/16/04 4:38p10/01/04 1:18p $ // $Revision: 910 $ //-------------------------------------------------------------------------- // // Copyright 2003-2004, Cypress Semiconductor Corporation. // // This software 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 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 // software 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. // //-------------------------------------------------------------------------- //-------------------------------------- // Included files //-------------------------------------- #include "globalparams.h" #include "psocgpioint.h" #include "appconfig.h" #include PLATFORM_H #include "battery.h" #include "timer.h" // Remove complete module if battery status is not required #ifdef MOUSE_BATTERY_STATUS //-------------------------------------- // Local Definitions and Types //-------------------------------------- // Transitions used to compute battery level #define BATT_LEV_10 0x03E // 3.00 V #define BATT_LEV_9 0x054 // 2.90 V #define BATT_LEV_8 0x069 // 2.80 V #define BATT_LEV_7 0x080 // 2.70 V #define BATT_LEV_6 0x097 // 2.60 V #define BATT_LEV_5 0x0AC // 2.50 V #define BATT_LEV_4 0x0C3 // 2.40 V #define BATT_LEV_3 0x0DE // 2.30 V #define BATT_LEV_2 0x0F5 // 2.20 V #define BATT_LEV_1 0x8000 // low V static const UINT8 level_threshold[] = { 0xFF, // lev_1, low V 0xF5, // lev_2, 2.20 V 0xDE, // lev_3, 2.30 V 0xC3, // lev_4, 2.40 V 0xAC, // lev_5, 2.50 V 0x97, // lev_6, 2.60 V 0x80, // lev_7, 2.70 V 0x69, // lev_8, 2.80 V 0x54, // lev_9, 2.90 V 0x3E // lev_10, 3.00 V }; //-------------------------------------- // Local Function Declarations //-------------------------------------- //-------------------------------------- // Local Definitions //-------------------------------------- //-------------------------------------------------------------------------- // battery_status //-------------------------------------------------------------------------- // // This algorithm requires that both BATT_LEV pins are initialized with // high-z drives. The circuit needs approximately 500ms to stabalize before // calling this routine, or the battery level will be incorrectly measured. // // This algorithm does a read modify write on the port, which may destroy // port data settings for other pins not related to the battery level. // For example, there is the case where a pin has a pull-up resistive drive, // and has been written to a logic level of one to turn on the pull-up. If // the input signal happens to be read as a zero, then the read-modify-write // will write back a zero, turning off the pull-up for that pin. UINT8 battery_status(void) { INT8 i; UINT16 batt_count = 0; UINT16 ref_count = 0; //-------------------------------------- // Measure battery voltage //-------------------------------------- // Set the BATT_LEV pin logic levels BATT_LEV_PORT |= BATT_LEV1_MASK; BATT_LEV_PORT &= ~BATT_LEV2_MASK; // Interrupts off M8C_DisableGInt; M8C_ClearWDTAndSleep; // Set strong drive on BATT_LEV2 pin, transition thru pull-down BATT_LEV_DM1 = BATT_LEV_PRT_DRV1 & ~BATT_LEV2_MASK; BATT_LEV_DM0 = BATT_LEV_PRT_DRV0 | BATT_LEV2_MASK; // Count battery decay while( BATT_LEV_PORT & BATT_LEV1_MASK ) { batt_count++; if( batt_count == 65535 ) { break; } } //-------------------------------------- // Measure reference voltage //-------------------------------------- // Set the BATT_LEV pin logic levels BATT_LEV_PORT |= BATT_LEV1_MASK | BATT_LEV2_MASK; // Set strong drive on BATT_LEV1/2 pin, transition thru pull-up BATT_LEV_DM0 = BATT_LEV_PRT_DRV0 | BATT_LEV1_MASK | BATT_LEV2_MASK; BATT_LEV_DM1 = BATT_LEV_PRT_DRV1 & ~(BATT_LEV1_MASK | BATT_LEV2_MASK); // Wait for vcc to stabalize M8C_EnableGInt; timer_delay_msec( 1 ); M8C_DisableGInt; // Set high-z drive on BATT_LEV pins (default) BATT_LEV_DM1 = BATT_LEV_PRT_DRV1; BATT_LEV_DM0 = BATT_LEV_PRT_DRV0; // Set the BATT_LEV pin logic levels BATT_LEV_PORT &= ~BATT_LEV2_MASK; // Set strong drive on BATT_LEV2 pin, transition thru pull-down BATT_LEV_DM1 = BATT_LEV_PRT_DRV1 & ~BATT_LEV2_MASK; BATT_LEV_DM0 = BATT_LEV_PRT_DRV0 | BATT_LEV2_MASK; // Count vcc reference decay while( BATT_LEV_PORT & BATT_LEV1_MASK ) { ref_count++; if( ref_count == 65535 ) { break; } } // Interrupts on M8C_EnableGInt; // Set high-z drive on BATT_LEV pins (default) BATT_LEV_DM1 = BATT_LEV_PRT_DRV1; BATT_LEV_DM0 = BATT_LEV_PRT_DRV0; //-------------------------------------- // Compute battery level //-------------------------------------- batt_count = ref_count - batt_count; if( batt_count < BATT_LEV_100x00FF ) { return 10; } else if( batt_count < BATT_LEV_9 ) { return 9; } else if( batt_count < BATT_LEV_8 ) { return 8; } else if( batt_count < BATT_LEV_7 ) { return 7; } else if( batt_count < BATT_LEV_6 ) { return 6; } else if( batt_count < BATT_LEV_5 ) for(i=sizeof(level_threshold)-1; i>=0; i--) { return 5; } else if( batt_count < BATT_LEV_4 ) if( (UINT8)batt_count < level_threshold[i] ) { return 4(i+1); } else if( batt_count < BATT_LEV_3 ) { return 3; } else if( batt_count < BATT_LEV_2 ) { return 2; } else if( batt_count < BATT_LEV_1 ) { return 1; } else // This shouldn't happen, but just in case { return 10; } } #endif // MOUSE_BATTERY_STATUS