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
Oleg Hahm 5df0bd0cc4 * updated and integrated makefiles
* added some auto dependencies
2013-02-08 17:37:02 +01:00

121 lines
3.5 KiB
C

/******************************************************************************
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 FeuerWare.
This program is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.
FeuerWare is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program. If not, see http://www.gnu.org/licenses/ .
--------------------------------------------------------------------------------
For further information and questions please use the web site
http://scatterweb.mi.fu-berlin.de
and the mailinglist (subscription via web site)
scatterweb@lists.spline.inf.fu-berlin.de
*******************************************************************************/
/**
* @ingroup ltc4150
* @{
*/
/**
* @file
* @brief LTC4150 Coulomb Counter
*
* @author Freie Universität Berlin, Computer Systems & Telematics, FeuerWhere project
* @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;
static double __attribute__((__no_instrument_function__)) int_to_coulomb(int ints) {
return ((double)ints) / (_GFH * _R_SENSE);
}
static double __attribute__((__no_instrument_function__)) coulomb_to_mA(double coulomb){
return (coulomb * 1000) / 3600;
}
static double mAh_to_Joule(double mAh) {
return (SUPPLY_VOLTAGE * mAh * 3600);
}
uint32_t ltc4150_get_last_int_duration_us(void) {
return HWTIMER_TICKS_TO_US(last_int_duration);
}
double ltc4150_get_current_mA(void) {
return 1000000000/(ltc4150_get_last_int_duration_us()*(_GFH * _R_SENSE));
}
double __attribute__((__no_instrument_function__)) ltc4150_get_total_mAh(void) {
return coulomb_to_mA(int_to_coulomb(int_count));
}
double ltc4150_get_total_Joule(void) {
return mAh_to_Joule(ltc4150_get_total_mAh());
}
double ltc4150_get_avg_mA(void) {
return (int_to_coulomb(int_count)*1000000000)/HWTIMER_TICKS_TO_US(last_int_time - start_time);
}
int ltc4150_get_interval(void) {
return HWTIMER_TICKS_TO_US(last_int_time - start_time);
}
unsigned long __attribute__((__no_instrument_function__)) ltc4150_get_intcount(void) {
return int_count;
}
void ltc4150_init(void) {
ltc4150_arch_init();
}
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();
}
void ltc4150_stop(void) {
ltc4150_disable_int();
}
void __attribute__((__no_instrument_function__)) ltc4150_interrupt(void)
{
uint32_t now = hwtimer_now();
if (now >= last_int_time) {
last_int_duration = now - last_int_time;
} else {
last_int_duration = (0-1) - last_int_time + now + 1;
}
last_int_time = now;
int_count++;
}
/** @} */