smbus.c 1.89 KB
Newer Older
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
#include "smbus.h"
#include "time_base.h"
#include "mem.h"

#define FUEL_GAUGE_ADDRESS 0x16

typedef struct
{
  unsigned char smbus_cmd;
  unsigned char mem_address;
}TSMBusCmd;

/* private variables */
TSMBusCmd commands[]={{Temperature,Bat_temperature_L},
                      {Voltage,Bat_voltage_L},
                      {Current,Bat_current_L},
                      {AverageCurrent,Bat_avg_current_L},
                      {RelativeStateOfCharge,Bat_rel_state_charge_L},
                      {AbsoluteStateOfCharge,Bat_abs_state_charge_L},
                      {RemainingCapacity,Bat_rem_capacity_L},
                      {FullChargeCapacity,Bat_full_capacity_L},
                      {RunTimeToEmpty,Bat_run_time_empty_L},
                      {AverageTimeToEmpty,Bat_avg_time_empty_L},
                      {AverageTimeToFull,Bat_avg_time_full_L},
                      {ChargingCurrent,Bat_charge_current_L},
                      {ChargingVoltage,Bat_charge_voltage_L},
27
                      {BatteryStatus,Bat_status_L},
28
29
                      {DesignCapacity,Bat_design_capacity_L},
                      {DesignVoltage,Bat_design_voltage_L},
30
                      {PackStatus,Bat_pack_status_L},
31
32
33
34
35
36
                      {VCELL4,Bat_cell4_voltage_L},
                      {VCELL3,Bat_cell3_voltage_L},
                      {VCELL2,Bat_cell2_voltage_L},
                      {VCELL1,Bat_cell1_voltage_L}};
unsigned char current_cmd;

37
#define NUM_COMMANDS 21
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58

/* private functions */

/* public functions */
void smbus_init(void)
{
  /* initialize I2C bus */  
  i2cInit();
  /* initialize internal variables */
  current_cmd=0;
}

void smbus_loop(void)
{
  if(time_base_period())
  {
    if(i2cMasterSendNI(FUEL_GAUGE_ADDRESS,1,&commands[current_cmd].smbus_cmd)==I2C_OK)   
      i2cMasterReceiveNI(FUEL_GAUGE_ADDRESS,2,&ram_data[commands[current_cmd].mem_address]);
    current_cmd=(current_cmd+1)%NUM_COMMANDS;
  }
}