-
Notifications
You must be signed in to change notification settings - Fork 0
/
ina.c
85 lines (70 loc) · 1.72 KB
/
ina.c
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
//
// ina.c
// Copyright (c) 2017 João Baptista de Paula e Silva
// Este arquivo está sob a licença MIT
//
//
// Este arquivo tem as funções para interfacear com o
// sensor de corrente
//
#include "default.h"
#define INA_CONFIG 0
#define INA_SHUNT_V 1
#define INA_BUS_V 2
#define INA_POWER 3
#define INA_CURRENT 4
#define INA_CALIBRATION 5
#define INA_NUM_REGS 6
#define ADDR B1000000
static volatile uint16_t reg_temps[INA_NUM_REGS];
static uint16_t regs[INA_NUM_REGS];
const uint8_t reg_addr[] = { 0, 1, 2, 3, 4, 5 };
uint8_t reg_cmds[INA_NUM_REGS];
#pragma pack(push, 1)
struct { uint8_t reg; uint16_t param; }
cfg_reg = { 0, 0 }, cal_reg = { 5, 0 };
#pragma pack(pop)
static void ina_command_read_register(uint8_t reg)
{
twi_write(ADDR, ®_addr[reg], sizeof(uint8_t));
reg_cmds[reg] = twi_read(ADDR, ®_temps[reg], sizeof(uint16_t));
}
static uint16_t ina_read_register_async(uint8_t reg)
{
if (reg_cmds[reg] != -1 && twi_cmd_ready(reg_cmds[reg]))
{
regs[reg] = reg_temps[reg];
reg_cmds[reg] = -1;
}
return regs[reg];
}
static uint16_t ina_read_register_sync(uint8_t reg)
{
ina_command_read_register(reg);
while (!twi_cmd_ready(reg_cmds[reg]));
regs[reg] = reg_temps[reg];
return regs[reg];
}
static void ina_write_config_register(uint16_t param)
{
cfg_reg.param = param;
twi_write(ADDR, &cfg_reg, sizeof(cfg_reg));
}
static void ina_write_calibration_register(uint16_t param)
{
cal_reg.param = param;
twi_write(ADDR, &cal_reg, sizeof(cal_reg));
}
int16_t ina_get_shunt_voltage_10uv()
{
return ina_read_register_sync(INA_SHUNT_V);
}
void ina_init()
{
for (uint8_t i = 0; i < INA_NUM_REGS; i++)
{
reg_temps[i] = regs[i] = 0;
reg_cmds[i] = -1;
}
ina_write_config_register(0x8000); // reseta o INA
}