1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00
RIOT/drivers/ltc4150/ltc4150.c

124 lines
2.9 KiB
C
Raw Normal View History

/******************************************************************************
* Copyright 2008-2009, Freie Universitaet Berlin (FUB). All rights reserved.
*
* These sources were developed at the Freie Universitaet Berlin, Computer Systems
and Telematics group (http://cst.mi.fu-berlin.de).
* ----------------------------------------------------------------------------
* This file is part of RIOT.
*
2013-11-22 20:47:05 +01:00
* This file is subject to the terms and conditions of the GNU Lesser General
* Public License. See the file LICENSE in the top level directory for more
* details.
*
*******************************************************************************/
/**
* @ingroup ltc4150
* @{
*/
/**
* @file
* @brief LTC4150 Coulomb Counter
*
* @author Freie Universität Berlin, Computer Systems & Telematics
* @author Heiko Will
* @author Kaspar Schleiser <kaspar.schleiser@fu-berlin.de>
*/
#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)
{
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)
{
return (coulomb * 1000) / 3600;
}
2013-06-21 22:36:48 +02:00
static double mAh_to_Joule(double mAh)
{
return (SUPPLY_VOLTAGE * mAh * 3600);
}
2013-06-21 22:36:48 +02:00
uint32_t ltc4150_get_last_int_duration_us(void)
{
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));
}
2013-06-21 22:36:48 +02:00
double __attribute__((__no_instrument_function__)) ltc4150_get_total_mAh(void)
{
return coulomb_to_mA(int_to_coulomb(int_count));
}
2013-06-21 22:36:48 +02:00
double ltc4150_get_total_Joule(void)
{
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);
}
2013-06-21 22:36:48 +02:00
int ltc4150_get_interval(void)
{
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)
{
return int_count;
}
2013-06-21 22:36:48 +02:00
void ltc4150_init(void)
{
ltc4150_arch_init();
}
2013-06-21 22:36:48 +02:00
void ltc4150_start(void)
{
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)
{
ltc4150_disable_int();
}
void __attribute__((__no_instrument_function__)) ltc4150_interrupt(void)
{
uint32_t now = hwtimer_now();
2013-06-21 22:36:48 +02:00
if (now >= last_int_time) {
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;
}
last_int_time = now;
int_count++;
}
/** @} */