38
+ − 1 /**
+ − 2 ******************************************************************************
+ − 3 * @file batteryGasGauge.c
+ − 4 * @author heinrichs weikamp gmbh
+ − 5 * @version V0.0.1
+ − 6 * @date 09-Dec-2014
+ − 7 * @brief LTC2942 Battery Gas Gauge
+ − 8 *
+ − 9 @verbatim
+ − 10 ==============================================================================
+ − 11 ##### stm32f4xx_hal_i2c.c modification #####
+ − 12 ==============================================================================
+ − 13 The LTC2942 requires an repeated start condition without stop condition
+ − 14 for data reception.
+ − 15
+ − 16 @endverbatim
+ − 17 ******************************************************************************
+ − 18 * @attention
+ − 19 *
+ − 20 * <h2><center>© COPYRIGHT(c) 2014 heinrichs weikamp</center></h2>
+ − 21 *
+ − 22 ******************************************************************************
+ − 23 */
+ − 24 /* Includes ------------------------------------------------------------------*/
330
+ − 25 #include <string.h> /* memset */
38
+ − 26 #include "batteryGasGauge.h"
+ − 27 #include "baseCPU2.h"
+ − 28 #include "stm32f4xx_hal.h"
+ − 29 #include "i2c.h"
+ − 30
408
+ − 31 static float battery_f_voltage = 6.0; /* max assumed voltage */
228
+ − 32 static float battery_f_charge_percent = 0;
38
+ − 33
+ − 34 #define BGG_BATTERY_OFFSET (26123) //; 65536-(3,35Ah/0,085mAh)
228
+ − 35 #define BGG_BATTERY_DIVIDER (394) //; 3,35Ah/0,085mAh/100 [%]
38
+ − 36
+ − 37 float get_voltage(void)
+ − 38 {
+ − 39 #ifdef OSTC_ON_DISCOVERY_HARDWARE
+ − 40 return 3.0f;
+ − 41 #endif
+ − 42
+ − 43 return battery_f_voltage;
+ − 44 }
+ − 45
+ − 46
+ − 47 float get_charge(void)
+ − 48 {
+ − 49 #ifdef OSTC_ON_DISCOVERY_HARDWARE
+ − 50 return 100.0f;
+ − 51 #endif
+ − 52
+ − 53 return battery_f_charge_percent;
+ − 54 }
+ − 55
+ − 56
+ − 57 void init_battery_gas_gauge(void)
+ − 58 {
+ − 59 #ifdef OSTC_ON_DISCOVERY_HARDWARE
+ − 60 return;
+ − 61 #endif
+ − 62
+ − 63 uint8_t buffer[2];
+ − 64 buffer[0] = 0x01;
228
+ − 65
+ − 66 // F8 = 11111000:
433
+ − 67 // ADC auto mode (11)
228
+ − 68 // Prescale M = 128 (111)
+ − 69 // AL/CC pin disable (0)
+ − 70 // Shutdown (0)
+ − 71 buffer[1] = 0xF8;
+ − 72 I2C_Master_Transmit(DEVICE_BATTERYGAUGE, buffer, 2);
38
+ − 73 }
+ − 74
330
+ − 75 uint8_t battery_gas_gauge_CheckConfigOK(void)
+ − 76 {
+ − 77 #ifdef OSTC_ON_DISCOVERY_HARDWARE
+ − 78 return;
+ − 79 #endif
+ − 80
+ − 81 uint8_t retval = 0;
+ − 82 uint8_t bufferReceive[10];
+ − 83
+ − 84 memset(bufferReceive,0,sizeof(bufferReceive));
+ − 85
+ − 86 I2C_Master_Receive(DEVICE_BATTERYGAUGE, bufferReceive, 10);
+ − 87 if(bufferReceive[1] == 0xf8)
+ − 88 {
+ − 89 retval = 1;
+ − 90 }
+ − 91 return retval;
+ − 92 }
+ − 93
242
+ − 94 static void disable_adc(void)
+ − 95 {
+ − 96 uint8_t buffer[2];
+ − 97 buffer[0] = 0x01;
+ − 98
+ − 99 // according to the datasheet of the LTC2942, the adc shall
+ − 100 // be disabled when writing to the gauge registers
+ − 101
+ − 102 // 0xF9 = 11111001:
+ − 103 // see init_battery_gas_gauge()
+ − 104 // Shutdown (1)
+ − 105 buffer[1] = 0xF9;
+ − 106 I2C_Master_Transmit(DEVICE_BATTERYGAUGE, buffer, 2);
+ − 107 }
+ − 108
38
+ − 109
+ − 110 void battery_gas_gauge_get_data(void)
+ − 111 {
+ − 112 #ifdef OSTC_ON_DISCOVERY_HARDWARE
+ − 113 return;
+ − 114 #endif
+ − 115
+ − 116 float battery_f_voltage_local;
+ − 117 float battery_f_charge_percent_local;
+ − 118
+ − 119 uint8_t bufferReceive[10];
408
+ − 120
+ − 121 if(I2C_Master_Receive(DEVICE_BATTERYGAUGE, bufferReceive, 10) == HAL_OK)
+ − 122 {
+ − 123 battery_f_voltage_local = (float)(bufferReceive[8] * 256);
+ − 124 battery_f_voltage_local += (float)(bufferReceive[9]);
+ − 125 battery_f_voltage_local *= (float)6 / (float)0xFFFF;
+ − 126
+ − 127 // max/full: 0.085 mAh * 1 * 65535 = 5570 mAh
+ − 128 battery_f_charge_percent_local = (float)(bufferReceive[2] * 256);
+ − 129 battery_f_charge_percent_local += (float)(bufferReceive[3]);
433
+ − 130 battery_f_charge_percent_local -= BGG_BATTERY_OFFSET; /* Because of the prescalar 128 the counter assumes a max value of 5570mAh => normalize to 3350mAh*/
+ − 131 battery_f_charge_percent_local /= BGG_BATTERY_DIVIDER; /* transform to percentage */
38
+ − 132
408
+ − 133 if(battery_f_charge_percent_local < 0)
+ − 134 battery_f_charge_percent_local = 0;
+ − 135
+ − 136 battery_f_voltage = battery_f_voltage_local;
+ − 137 battery_f_charge_percent = battery_f_charge_percent_local;
+ − 138 }
38
+ − 139 }
+ − 140
+ − 141
+ − 142 void battery_gas_gauge_set_charge_full(void)
+ − 143 {
242
+ − 144 disable_adc();
38
+ − 145 #ifdef OSTC_ON_DISCOVERY_HARDWARE
+ − 146 return;
+ − 147 #endif
+ − 148
+ − 149 uint8_t bufferSend[3];
+ − 150 bufferSend[0] = 0x02;
+ − 151 bufferSend[1] = 0xFF;
+ − 152 bufferSend[2] = 0xFF;
+ − 153 I2C_Master_Transmit( DEVICE_BATTERYGAUGE, bufferSend, 3);
242
+ − 154 init_battery_gas_gauge();
38
+ − 155 }
+ − 156
+ − 157
+ − 158 void battery_gas_gauge_set(float percentage)
+ − 159 {
242
+ − 160
+ − 161 disable_adc();
38
+ − 162 #ifdef OSTC_ON_DISCOVERY_HARDWARE
+ − 163 return;
+ − 164 #endif
+ − 165
+ − 166 uint16_t mAhSend;
+ − 167
+ − 168 if(percentage >= 100)
+ − 169 mAhSend = 0xFFFF;
228
+ − 170 else {
+ − 171 mAhSend = (percentage * BGG_BATTERY_DIVIDER) + BGG_BATTERY_OFFSET;
+ − 172 }
38
+ − 173
+ − 174 uint8_t bufferSend[3];
+ − 175 bufferSend[0] = 0x02;
+ − 176 bufferSend[1] = (uint8_t)(mAhSend / 256);
+ − 177 bufferSend[2] = (uint8_t)(mAhSend & 0xFF);
+ − 178 I2C_Master_Transmit( DEVICE_BATTERYGAUGE, bufferSend, 3);
242
+ − 179 init_battery_gas_gauge();
38
+ − 180 }
+ − 181
+ − 182
+ − 183 /************************ (C) COPYRIGHT heinrichs weikamp *****END OF FILE****/