Blame view

RIOT/drivers/ltc4150/ltc4150.c 2.39 KB
fb11e647   vrobic   reseau statique a...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
  /*
   * Copyright 2008-2009, Freie Universitaet Berlin (FUB). All rights reserved.
   *
   * 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.
   */
  
  /**
   * @ingroup ltc4150
   * @{
   */
  
  /**
   * @file
   * @brief       LTC4150 Coulomb Counter
   *
   * @author      Heiko Will
   * @author      Kaspar Schleiser <kaspar@schleiser.de>
   */
  
  #include <xtimer.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 = xtimer_now_usec();
      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 = xtimer_now_usec();
  
      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++;
  }
  
  /** @} */