2010-09-22 15:10:42 +02:00
|
|
|
/******************************************************************************
|
2013-08-16 10:20:23 +02:00
|
|
|
* Copyright 2008-2009, Freie Universitaet Berlin (FUB). All rights reserved.
|
|
|
|
*
|
|
|
|
* These sources were developed at the Freie Universitaet Berlin, Computer Systems
|
2010-09-22 15:10:42 +02:00
|
|
|
and Telematics group (http://cst.mi.fu-berlin.de).
|
2013-08-16 10:20:23 +02:00
|
|
|
* ----------------------------------------------------------------------------
|
|
|
|
* This file is part of RIOT.
|
|
|
|
*
|
2014-07-31 19:45:27 +02:00
|
|
|
* This file is subject to the terms and conditions of the GNU Lesser
|
|
|
|
* General Public License v2.1. See the file LICENSE in the top level
|
|
|
|
* directory for more details.
|
2013-08-16 10:20:23 +02:00
|
|
|
*
|
2010-09-22 15:10:42 +02:00
|
|
|
*******************************************************************************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @ingroup ltc4150
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* @brief LTC4150 Coulomb Counter
|
|
|
|
*
|
|
|
|
* @author Heiko Will
|
2013-12-04 15:07:56 +01:00
|
|
|
* @author Kaspar Schleiser <kaspar@schleiser.de>
|
2010-09-22 15:10:42 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <hwtimer.h>
|
|
|
|
#include "ltc4150_arch.h"
|
|
|
|
|
|
|
|
static volatile unsigned long int_count;
|
|
|
|
static unsigned int last_int_time;
|
|
|
|
static unsigned int last_int_duration;
|
|
|
|
static unsigned int start_time;
|
|
|
|
|
2013-06-21 22:36:48 +02:00
|
|
|
static double __attribute__((__no_instrument_function__)) int_to_coulomb(int ints)
|
|
|
|
{
|
2010-09-22 15:10:42 +02:00
|
|
|
return ((double)ints) / (_GFH * _R_SENSE);
|
|
|
|
}
|
|
|
|
|
2013-06-21 22:36:48 +02:00
|
|
|
static double __attribute__((__no_instrument_function__)) coulomb_to_mA(double coulomb)
|
|
|
|
{
|
2010-09-22 15:10:42 +02:00
|
|
|
return (coulomb * 1000) / 3600;
|
|
|
|
}
|
|
|
|
|
2013-06-21 22:36:48 +02:00
|
|
|
static double mAh_to_Joule(double mAh)
|
|
|
|
{
|
2011-03-04 16:46:20 +01:00
|
|
|
return (SUPPLY_VOLTAGE * mAh * 3600);
|
|
|
|
}
|
|
|
|
|
2013-06-21 22:36:48 +02:00
|
|
|
uint32_t ltc4150_get_last_int_duration_us(void)
|
|
|
|
{
|
2010-09-22 15:10:42 +02:00
|
|
|
return HWTIMER_TICKS_TO_US(last_int_duration);
|
|
|
|
}
|
|
|
|
|
2013-06-21 22:36:48 +02:00
|
|
|
double ltc4150_get_current_mA(void)
|
|
|
|
{
|
|
|
|
return 1000000000 / (ltc4150_get_last_int_duration_us() * (_GFH * _R_SENSE));
|
2010-09-22 15:10:42 +02:00
|
|
|
}
|
|
|
|
|
2013-06-21 22:36:48 +02:00
|
|
|
double __attribute__((__no_instrument_function__)) ltc4150_get_total_mAh(void)
|
|
|
|
{
|
2010-09-22 15:10:42 +02:00
|
|
|
return coulomb_to_mA(int_to_coulomb(int_count));
|
|
|
|
}
|
|
|
|
|
2013-06-21 22:36:48 +02:00
|
|
|
double ltc4150_get_total_Joule(void)
|
|
|
|
{
|
2011-03-04 16:46:20 +01:00
|
|
|
return mAh_to_Joule(ltc4150_get_total_mAh());
|
|
|
|
}
|
|
|
|
|
2013-06-21 22:36:48 +02:00
|
|
|
double ltc4150_get_avg_mA(void)
|
|
|
|
{
|
|
|
|
return (int_to_coulomb(int_count) * 1000000000) / HWTIMER_TICKS_TO_US(last_int_time - start_time);
|
2010-09-22 15:10:42 +02:00
|
|
|
}
|
|
|
|
|
2013-06-21 22:36:48 +02:00
|
|
|
int ltc4150_get_interval(void)
|
|
|
|
{
|
2010-11-09 18:48:45 +01:00
|
|
|
return HWTIMER_TICKS_TO_US(last_int_time - start_time);
|
|
|
|
}
|
|
|
|
|
2013-06-21 22:36:48 +02:00
|
|
|
unsigned long __attribute__((__no_instrument_function__)) ltc4150_get_intcount(void)
|
|
|
|
{
|
2010-09-22 15:10:42 +02:00
|
|
|
return int_count;
|
|
|
|
}
|
|
|
|
|
2013-06-21 22:36:48 +02:00
|
|
|
void ltc4150_init(void)
|
|
|
|
{
|
2010-09-22 15:10:42 +02:00
|
|
|
ltc4150_arch_init();
|
|
|
|
}
|
|
|
|
|
2013-06-21 22:36:48 +02:00
|
|
|
void ltc4150_start(void)
|
|
|
|
{
|
2010-09-22 15:10:42 +02:00
|
|
|
ltc4150_disable_int();
|
|
|
|
int_count = 0;
|
|
|
|
uint32_t now = hwtimer_now();
|
|
|
|
ltc4150_sync_blocking();
|
|
|
|
start_time = now;
|
|
|
|
last_int_time = now;
|
|
|
|
ltc4150_enable_int();
|
|
|
|
}
|
|
|
|
|
2013-06-21 22:36:48 +02:00
|
|
|
void ltc4150_stop(void)
|
|
|
|
{
|
2010-09-22 15:10:42 +02:00
|
|
|
ltc4150_disable_int();
|
|
|
|
}
|
|
|
|
|
2013-02-08 17:37:02 +01:00
|
|
|
void __attribute__((__no_instrument_function__)) ltc4150_interrupt(void)
|
2010-09-22 15:10:42 +02:00
|
|
|
{
|
|
|
|
uint32_t now = hwtimer_now();
|
2013-06-21 22:36:48 +02:00
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if (now >= last_int_time) {
|
2010-09-22 15:10:42 +02:00
|
|
|
last_int_duration = now - last_int_time;
|
2013-06-21 22:36:48 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
last_int_duration = (0 - 1) - last_int_time + now + 1;
|
2010-09-22 15:10:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
last_int_time = now;
|
|
|
|
int_count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @} */
|