view Small_CPU/Src/batteryGasGauge.c @ 805:dd7ce655db26

Adds a simple countdown timer, available as a custom view in surface and dive mode. This can be used to time safety stops, or to prebreathe a CCR (or to boil your breakfast eggs if you are so inclined). The duration of the timer is configurable from 1 second to 9:59 minutes in the System menu. The timer is started by switching to the custom view, and remaining on it until a 10 second delay has elapsed. Once the timer has started the custom view can be changed and the timer will continue running in the background. After the timer has run out 'Finished' will be shown for 10 seconds in the timer custom view, and then automatic switching of custom views (if configured) resumes. In surface mode the dive computer will not go to sleep while the timer is running, and a mini timer will be shown when the timer custom view is not showing. (mikeller)
author heinrichsweikamp
date Mon, 21 Aug 2023 17:20:07 +0200
parents 079bb5b22c06
children
line wrap: on
line source

/**
  ******************************************************************************
  * @file    batteryGasGauge.c 
  * @author  heinrichs weikamp gmbh
  * @version V0.0.1
  * @date    09-Dec-2014
  * @brief   LTC2942 Battery Gas Gauge
  *           
  @verbatim                 
  ============================================================================== 
                ##### stm32f4xx_hal_i2c.c modification #####
  ============================================================================== 
	The LTC2942 requires an repeated start condition without stop condition
	for data reception.
	
  @endverbatim
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT(c) 2014 heinrichs weikamp</center></h2>
  *
  ******************************************************************************
  */ 
/* Includes ------------------------------------------------------------------*/
#include <string.h>	/* memset */
#include "batteryGasGauge.h"
#include "baseCPU2.h"
#include "stm32f4xx_hal.h"
#include "i2c.h"

static float battery_f_voltage = BATTERY_DEFAULT_VOLTAGE;		/* max assumed voltage */
static float battery_f_charge_percent = 0;
static uint8_t chargeValueKnown = 0;							/* indicator if the charge of the battery is known (for example after a full charge cycle) */


#define BGG_BATTERY_OFFSET          (26123)  //; 65536-(3,35Ah/0,085mAh)
#define BGG_BATTERY_DIVIDER         (394)    //; 3,35Ah/0,085mAh/100 [%]

float get_voltage(void)
{
#ifdef OSTC_ON_DISCOVERY_HARDWARE
	return 3.0f;
#endif

	return battery_f_voltage;
}


float get_charge(void)
{
	#ifdef OSTC_ON_DISCOVERY_HARDWARE
		return 100.0f;
	#endif
	
	return battery_f_charge_percent;
}


void init_battery_gas_gauge(void)
{
	#ifdef OSTC_ON_DISCOVERY_HARDWARE
		return;
	#endif
	
	uint8_t buffer[2];
	buffer[0] = 0x01;

	// F8 = 11111000:
	// ADC auto mode (11)
	// Prescale M = 128 (111)
	// AL/CC pin disable (00)
	// Shutdown (0)
	buffer[1] = 0xF8;
	I2C_Master_Transmit(DEVICE_BATTERYGAUGE, buffer, 2);
}

uint8_t battery_gas_gauge_CheckConfigOK(void)
{
	#ifdef OSTC_ON_DISCOVERY_HARDWARE
		return;
	#endif

	uint8_t retval = 0;
	uint8_t bufferReceive[10];

	memset(bufferReceive,0,sizeof(bufferReceive));

	I2C_Master_Receive(DEVICE_BATTERYGAUGE, bufferReceive, 10);
	if(bufferReceive[1] == 0xf8)
	{
		retval = 1;
	}
	return retval;
}

static void disable_adc(void)
{
	uint8_t buffer[2];
	buffer[0] = 0x01;

	// according to the datasheet of the LTC2942, the adc shall
	// be disabled when writing to the gauge registers

	// 0xF9 = 11111001:
	// see init_battery_gas_gauge()
	// Shutdown (1)
	buffer[1] = 0xF9;
	I2C_Master_Transmit(DEVICE_BATTERYGAUGE, buffer, 2);
}


void battery_gas_gauge_get_data(void)
{
	#ifdef OSTC_ON_DISCOVERY_HARDWARE
		return;
	#endif
	
	float battery_f_voltage_local;
	float battery_f_charge_percent_local;
	
	uint8_t bufferReceive[10];
	
	if(I2C_Master_Receive(DEVICE_BATTERYGAUGE, bufferReceive, 10) == HAL_OK)
	{
		battery_f_voltage_local =  (float)(bufferReceive[8] * 256);
		battery_f_voltage_local += (float)(bufferReceive[9]);
		battery_f_voltage_local *= (float)6 / (float)0xFFFF;
	
		// max/full: 0.085 mAh * 1 * 65535 = 5570 mAh
		battery_f_charge_percent_local =  (float)(bufferReceive[2] * 256);
		battery_f_charge_percent_local += (float)(bufferReceive[3]);
		battery_f_charge_percent_local -= BGG_BATTERY_OFFSET;		/* Because of the prescalar 128 the counter assumes a max value of 5570mAh => normalize to 3350mAh*/
		battery_f_charge_percent_local /= BGG_BATTERY_DIVIDER;		/* transform to percentage */

		if(battery_f_charge_percent_local < 0)
			battery_f_charge_percent_local = 0;

		battery_f_voltage = battery_f_voltage_local;
		battery_f_charge_percent = battery_f_charge_percent_local;
	}
}


void battery_gas_gauge_set_charge_full(void)
{
	disable_adc();
	#ifdef OSTC_ON_DISCOVERY_HARDWARE
		return;
	#endif
	
	uint8_t bufferSend[3];
	bufferSend[0] = 0x02;
	bufferSend[1] = 0xFF;
	bufferSend[2] = 0xFF;
	I2C_Master_Transmit(  DEVICE_BATTERYGAUGE, bufferSend, 3);
	init_battery_gas_gauge();
	chargeValueKnown = 1;
}


void battery_gas_gauge_set(float percentage)
{

	disable_adc();
	#ifdef OSTC_ON_DISCOVERY_HARDWARE
		return;
	#endif

	uint16_t mAhSend;
	
	if(percentage >= 100)
		mAhSend = 0xFFFF;
	else {
		mAhSend = (percentage * BGG_BATTERY_DIVIDER) + BGG_BATTERY_OFFSET;
	}
	
	uint8_t bufferSend[3];
	bufferSend[0] = 0x02;
	bufferSend[1] = (uint8_t)(mAhSend / 256);
	bufferSend[2] = (uint8_t)(mAhSend & 0xFF);
	I2C_Master_Transmit(  DEVICE_BATTERYGAUGE, bufferSend, 3);
	init_battery_gas_gauge();
	chargeValueKnown = 1;
}

uint8_t battery_gas_gauge_isChargeValueValid(void)
{
	return chargeValueKnown;
}

void battery_gas_gauge_setChargeValueValid(void)
{
	chargeValueKnown = 1;
}

/************************ (C) COPYRIGHT heinrichs weikamp *****END OF FILE****/