Commit mthomas changes to GCC and port those changes to IAR
[avr_bc100.git] / BaseTinyFirmware / GCC / battery.c
index b3617118cd562fd4f29df63ad09e3ec1bcfb9861..97c9f3bbce6ba8409460cc56e9483d385e1bf29b 100644 (file)
 \r
  *\r
  * \par Documentation\r
- *      For comprehensive code documentation, supported compilers, compiler \r
+ *      For comprehensive code documentation, supported compilers, compiler\r
  *      settings and supported devices see readme.html\r
  *\r
  * \author\r
  *      Atmel Corporation: http://www.atmel.com \n\r
  *      Support email: avr@atmel.com\r
  *\r
- * \r
+ *\r
  * $Name$\r
  * $Revision: 2299 $\r
  * $RCSfile$\r
@@ -124,7 +124,7 @@ const NTC_Lookup_t NTC[NTC_TABLE_SIZE] = {
 };\r
 */\r
 \r
\r
+\r
 //******************************************************************************\r
 // Functions\r
 //******************************************************************************\r
@@ -132,7 +132,7 @@ const NTC_Lookup_t NTC[NTC_TABLE_SIZE] = {
  *\r
  * Stores current capacity, then attempts to refresh battery status.\n\r
  * If the refresh is successful, old capacity is compared with the new one.\r
- * \r
+ *\r
  * \retval FALSE Battery is disconnected, or capacity has changed.\r
  * \retval TRUE All OK.\r
  */\r
@@ -140,14 +140,14 @@ unsigned char BatteryCheck(void)
 {\r
        unsigned char success = TRUE;\r
        unsigned int  oldCapacity;\r
-       \r
+\r
        // Save to see if battery data has changed.\r
-       oldCapacity = BattData.Capacity;  \r
-       \r
+       oldCapacity = BattData.Capacity;\r
+\r
        if (!BatteryStatusRefresh()) {\r
                success = FALSE;              // Battery not present or RID was invalid.\r
        }\r
-       \r
+\r
        if (oldCapacity != BattData.Capacity) {\r
                success = FALSE;              // Battery configuration has changed.\r
        }\r
@@ -176,7 +176,7 @@ unsigned char BatteryStatusRefresh(void)
        unsigned char sreg_saved;\r
        unsigned int adcs_VBAT_tmp;\r
        signed int adcs_avgIBAT_tmp;\r
-       \r
+\r
        BattData.Present = FALSE;\r
        BattData.Charged = FALSE;\r
        BattData.Low = TRUE;\r
@@ -208,7 +208,7 @@ unsigned char BatteryStatusRefresh(void)
 \r
        // If we are not charging, yet VBAT is above safe limit, battery is present.\r
        // If we are charging and there's a current flowing, the battery is present.\r
-       \r
+\r
        /*! \todo If ABORT_IF_PWM_MAX is defined this last check battery presence\r
        * check is redundant since charging will be aborted due to low current at\r
        * max duty cycle. That is preferrable since the charge current reading is\r
@@ -250,13 +250,13 @@ unsigned char BatteryDataRefresh(void)
        unsigned char offset;\r
        unsigned char i, crc, family, temp, page;\r
        unsigned char success;\r
-       \r
+\r
        // Look for EPROM and read 4 pages of 32 bytes each worth of data, if found.\r
        for (page = 0; page < 4; page++)        {\r
                success = FALSE;\r
-       \r
+\r
                if (OWI_DetectPresence(OWIBUS) == OWIBUS) {\r
-                       \r
+\r
                        // Presence detected, check type and CRC.\r
                        OWI_SendByte(OWI_ROM_READ, OWIBUS);\r
                        family = OWI_ReceiveByte(OWIBUS);\r
@@ -286,14 +286,14 @@ unsigned char BatteryDataRefresh(void)
                                        // and writing it to EEPROM.\r
                                        if (OWI_ComputeCRC8(OWI_ReceiveByte(OWIBUS),crc) == 0) {\r
                                                crc = 0;\r
-                                               \r
+\r
                                                // Fill page with data.\r
                                                for (i=0; i<32; i++) {\r
                                                        temp = OWI_ReceiveByte(OWIBUS);\r
                                                        crc = OWI_ComputeCRC8(temp, crc);\r
                                                        eeprom_write_byte(&BattEEPROM[page][i], temp);\r
                                                }\r
-                                               \r
+\r
                                                if (OWI_ComputeCRC8(OWI_ReceiveByte(OWIBUS),crc) == 0) {\r
                                                                success = TRUE;  // Data read OK\r
                                                }\r
@@ -301,7 +301,7 @@ unsigned char BatteryDataRefresh(void)
                                        }\r
                                } else { // Wrong device type.\r
                                }\r
-                       } else { // No device found.             \r
+                       } else { // No device found.\r
                        }\r
                } else { // No presence detected on one-wire bus.\r
                }\r
@@ -309,7 +309,10 @@ unsigned char BatteryDataRefresh(void)
          // Erase local EEPROM page if there were any errors during transfer.\r
                if (!success) {\r
                        for (i=0; i<32; i++) {\r
-                               eeprom_write_byte(&BattEEPROM[page][i], 0);\r
+                          // mthomas: avoid unneeded writes\r
+                          if ( eeprom_read_byte( &BattEEPROM[page][i] ) != 0 ) {\r
+                            eeprom_write_byte(&BattEEPROM[page][i], 0);\r
+                          }\r
                        }\r
                }\r
        }\r
@@ -338,10 +341,10 @@ void EnableBattery(unsigned char bat)
        PORTB |= (1 << (PB4+bat));\r
 \r
        // Disconnect other battery.\r
-       PORTB &= ~(1<<(PB5-bat));     \r
+       PORTB &= ~(1<<(PB5-bat));\r
 \r
        do { // Let port switch settle.\r
-       } while (Time_Left(TIMER_GEN));  \r
+       } while (Time_Left(TIMER_GEN));\r
 }\r
 \r
 \r
@@ -352,7 +355,7 @@ void EnableBattery(unsigned char bat)
 void DisableBatteries(void)\r
 {\r
        // Turn off LEDs and disconnect batteries.\r
-       PORTB &= ~((1<<PB4)|(1<<PB5));  \r
+       PORTB &= ~((1<<PB4)|(1<<PB5));\r
 }\r
 \r
 \r
@@ -370,7 +373,7 @@ unsigned char RIDLookUp (void)
        unsigned char i, found = FALSE;\r
        unsigned int adcs_rawRID_tmp;\r
        unsigned char sreg_saved;\r
-               \r
+\r
        // Lookup in the RID-table. If measured RID is within the limits\r
        // of an entry, those data are used, and TRUE is returned.\r
        for (i = 0 ; i < RID_TABLE_SIZE; i++) {\r
@@ -384,12 +387,12 @@ unsigned char RIDLookUp (void)
                                BattData.MaxCurrent = RID[i].Icharge;\r
                                BattData.MaxTime = RID[i].tCutOff;\r
                                BattData.MinCurrent = RID[i].ICutOff;\r
-                               \r
+\r
                                found = TRUE;\r
                        }\r
                }\r
        }\r
-       \r
+\r
        // If no valid entry is found, use defaults and return FALSE.\r
        if (!found) {\r
                BattData.Capacity = DEF_BAT_CAPACITY;\r
@@ -397,7 +400,7 @@ unsigned char RIDLookUp (void)
                BattData.MaxTime = DEF_BAT_TIME_MAX;\r
                BattData.MinCurrent = DEF_BAT_CURRENT_MIN;\r
        }\r
-       \r
+\r
        return(found);\r
 }\r
 \r
@@ -417,9 +420,9 @@ void NTCLookUp (void)
 {\r
        unsigned char i;\r
        unsigned char found = FALSE;\r
-       unsigned char adcs_rawNTC_tmp;\r
+       unsigned int adcs_rawNTC_tmp;\r
        unsigned char sreg_saved;\r
-       \r
+\r
        // Lookup in the NTC-table. Use the first entry which is equal or below\r
        // sampled NTC. Calculate temperature by using the index number, and the\r
        // difference between the measured NTC value and the one in the entry.\r
@@ -430,14 +433,14 @@ void NTCLookUp (void)
                SREG = sreg_saved;\r
                if (adcs_rawNTC_tmp >= NTC[i].ADCV) {\r
                        BattData.Temperature = (i<<2) ;\r
-                       BattData.ADCSteps = NTC[i].ADCsteps;  \r
+                       BattData.ADCSteps = NTC[i].ADCsteps;\r
                        BattData.Temperature -= ((adcs_rawNTC_tmp - NTC[i].ADCV)<<1) / BattData.ADCSteps;\r
-                       \r
+\r
                        found = TRUE;  // Could be done with a break, but that violates MISRA.\r
                }\r
        }\r
-       \r
-       // For safety, is temperature is greater than the NTC \r
+\r
+       // For safety, is temperature is greater than the NTC\r
        if (!found) {\r
                BattData.Temperature = 80;\r
        }\r