Commit mthomas changes to GCC and port those changes to IAR
[avr_bc100.git] / BaseTinyFirmware / IAR / battery.c
index 4486197e7f3957d222568d05d6106238b99b5c82..406e49492c50cee96fa8708be051abccf51d1496 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
@@ -129,7 +129,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
@@ -137,14 +137,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
@@ -170,7 +170,7 @@ unsigned char BatteryStatusRefresh(void)
 {\r
        // Assume the worst..\r
        unsigned char success = FALSE;\r
-       \r
+\r
        BattData.Present = FALSE;\r
        BattData.Charged = FALSE;\r
        BattData.Low = TRUE;\r
@@ -196,7 +196,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
@@ -238,13 +238,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
@@ -274,14 +274,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
                                                        BattEEPROM[page][i] = temp;\r
                                                }\r
-                                               \r
+\r
                                                if (OWI_ComputeCRC8(OWI_ReceiveByte(OWIBUS),crc) == 0) {\r
                                                                success = TRUE;  // Data read OK\r
                                                }\r
@@ -289,7 +289,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
@@ -297,7 +297,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
-                               BattEEPROM[page][i] = 0;\r
+                          // mthomas: avoid unneeded writes\r
+                          if ( eeprom_read_byte( &BattEEPROM[page][i] ) != 0 ) {\r
+                            BattEEPROM[page][i] = 0;\r
+                          }\r
                        }\r
                }\r
        }\r
@@ -326,10 +329,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
@@ -340,7 +343,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
@@ -356,7 +359,7 @@ void DisableBatteries(void)
 unsigned char RIDLookUp (void)\r
 {\r
        unsigned char i, found = FALSE;\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
@@ -366,12 +369,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
@@ -379,7 +382,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
@@ -399,21 +402,21 @@ void NTCLookUp (void)
 {\r
        unsigned char i;\r
        unsigned char found = FALSE;\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
        for (i=0 ; (i < NTC_TABLE_SIZE) && (!found); i++)       {\r
                if (ADCS.rawNTC >= NTC[i].ADC) {\r
                        BattData.Temperature = (i<<2) ;\r
-                       BattData.ADCSteps = NTC[i].ADCsteps;  \r
+                       BattData.ADCSteps = NTC[i].ADCsteps;\r
                        BattData.Temperature -= ((ADCS.rawNTC - NTC[i].ADC)<<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