aboutsummaryrefslogtreecommitdiffstats
path: root/firmware/e300/battery/fpga.c
blob: 20404e5a18dcbf5928069a1310a3ceee029e994e (plain)
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/* USRP E310 FPGA driver
 * Copyright (C) 2014 Ettus Research
 * This file is part of the USRP E310 Firmware
 * The USRP E310 Firmware 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 2 of the License, or
 * (at your option) any later version.
 * The USRP E310 Firmware 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 the USRP E310 Firmware. If not, see <http://www.gnu.org/licenses/>.
 */

#include "eeprom.h"
#include "fpga.h"
#include "spi.h"
#include "mcu_settings.h"
#include "utils.h"
#include <util/delay.h>
#include <string.h>
#include <stdbool.h>

typedef struct fpga_tx_mem_map0 {
	uint16_t battery_voltage;
	uint8_t battery_status;
	uint8_t charger_status;
	uint8_t unused[2];
	uint8_t version;
	uint8_t type;
} fpga_tx_mem_map0_t;

typedef struct fpga_tx_mem_map1 {
	uint8_t status;
	uint16_t voltage;
	uint16_t temp;
	uint16_t charge;
	uint8_t type;
} fpga_tx_mem_map1_t;

typedef struct fpga_tx_mem_map2 {
	uint8_t unused[4];
	uint8_t settings;
	uint16_t charge_last_full;
	uint8_t type;
} fpga_tx_mem_map2_t;

typedef struct fpga_rx_mem_map0 {
	uint8_t unused[2];
	uint16_t value;
	uint8_t reg;
	uint8_t os_status;
} fpga_rx_mem_map0_t;

typedef struct fpga_rx_mem_map1 {
	uint8_t unused[3];
	uint16_t value;
	uint8_t reg;
} fpga_rx_mem_map1_t;

typedef struct fpga_rx_mem_map {
	uint8_t valid;
	union {
		fpga_rx_mem_map0_t map0;
		fpga_rx_mem_map1_t map1;
	};
	uint8_t type;
} fpga_rx_mem_map_t;

static bool shutdown = false;
static bool write_charge = false;
static bool write_settings = false;

static volatile fpga_tx_mem_map0_t fpga_tx0;
static volatile fpga_tx_mem_map1_t fpga_tx1;
static volatile fpga_tx_mem_map2_t fpga_tx2;
static volatile fpga_rx_mem_map_t fpga_rx;

/* battery status */
static const uint8_t BATTERY_TEMP_ALERT_MASK = BIT(7) | BIT(6);
static const uint8_t BATTERY_TEMP_ALERT_SHIFT = 6;
static const uint8_t BATTERY_ONLINE_MASK = BIT(5);
static const uint8_t BATTERY_ONLINE_SHIFT = 5;
static const uint8_t BATTERY_HEALTH_MASK = BIT(4) | BIT(3) | BIT(2);
static const uint8_t BATTERY_HEALTH_SHIFT = 2;
static const uint8_t BATTERY_STATUS_MASK = BIT(1) | BIT(0);
static const uint8_t BATTERY_STATUS_SHIFT = 0;

/* charger_status */
static const uint8_t CHARGER_HEALTH_MASK = BIT(5) | BIT(4);
static const uint8_t CHARGER_HEALTH_SHIFT = 4;
static const uint8_t CHARGER_ONLINE_MASK = BIT(3);
static const uint8_t CHARGER_ONLINE_SHIFT = 3;
/* BIT(2) is unused */
static const uint8_t CHARGER_CHARGE_TYPE_MASK = BIT(1) | BIT(0);
static const uint8_t CHARGER_CHARGE_TYPE_SHIFT = 0;


void fpga_set_battery_voltage(uint16_t voltage)
{
	fpga_tx0.battery_voltage = voltage;
}

void fpga_set_battery_temp_alert(uint8_t alert)
{
	uint8_t status = fpga_tx0.battery_status;

	status &= ~BATTERY_TEMP_ALERT_MASK;
	status |= alert << BATTERY_TEMP_ALERT_SHIFT;

	fpga_tx0.battery_status = status;
}

void fpga_set_battery_online(bool online)
{
	uint8_t status = fpga_tx0.battery_status;

	status &= ~BATTERY_ONLINE_MASK;
	status |= (online ? 1 : 0) << BATTERY_ONLINE_SHIFT;

	fpga_tx0.battery_status = status;
}

void fpga_set_battery_health(uint8_t health)
{
	uint8_t status = fpga_tx0.battery_status;

	status &= ~BATTERY_HEALTH_MASK;
	status |= health << BATTERY_HEALTH_SHIFT;

	fpga_tx0.battery_status = status;
}

void fpga_set_battery_status(uint8_t st)
{
	uint8_t status = fpga_tx0.battery_status;

	status &= ~BATTERY_STATUS_MASK;
	status |= st << BATTERY_STATUS_SHIFT;

	fpga_tx0.battery_status = status;
}

void fpga_set_charger_health(uint8_t health)
{
	uint8_t status = fpga_tx0.charger_status;

	status &= ~CHARGER_HEALTH_MASK;
	status |= health << CHARGER_HEALTH_SHIFT;

	fpga_tx0.charger_status = status;
}

void fpga_set_charger_online(bool online)
{
	uint8_t status = fpga_tx0.charger_status;

	status &= ~CHARGER_ONLINE_MASK;
	status |= (online ? 1 : 0) << CHARGER_ONLINE_SHIFT;

	fpga_tx0.charger_status = status;
}

void fpga_set_charger_charge_type(uint8_t type)
{
	uint8_t status = fpga_tx0.charger_status;

	status &= ~CHARGER_CHARGE_TYPE_MASK;
	status |= type << CHARGER_CHARGE_TYPE_SHIFT;

	fpga_tx0.charger_status = status;
}

void fpga_set_gauge_charge(uint16_t charge)
{
	fpga_tx1.charge = charge;
}

uint16_t fpga_get_gauge_charge(void)
{
	return fpga_tx1.charge;
}

bool fpga_get_write_charge(void)
{
	bool ret = false;

	if (write_charge) {
		ret = write_charge;
		write_charge = false;
	}

	return ret;
}

uint8_t fpga_get_settings(void)
{
	return fpga_tx2.settings;
}

bool fpga_get_write_settings(void)
{
	bool ret = false;

	if (write_settings) {
		ret = write_settings;
		write_settings = false;
	}

	return ret;
}

void fpga_set_gauge_charge_last_full(uint16_t charge)
{
	fpga_tx2.charge_last_full = charge;
}

void fpga_set_gauge_temp(uint16_t temp)
{
	fpga_tx1.temp = temp;
}

void fpga_set_gauge_voltage(uint16_t volt)
{
	fpga_tx1.voltage = volt;
}

void fpga_set_gauge_status(uint8_t status)
{
	fpga_tx1.status = status;
}

bool fpga_get_shutdown(void)
{
	return shutdown;
}

void fpga_init(void)
{
	memset((void *) &fpga_tx0, 0, sizeof(fpga_tx0));
	memset((void *) &fpga_tx1, 0, sizeof(fpga_tx1));
	memset((void *) &fpga_tx2, 0, sizeof(fpga_tx2));
	fpga_tx0.type = 0;
	fpga_tx0.version = VERSION_MAJ << 4 | VERSION_MIN;

	fpga_tx1.type = 1;
	fpga_tx2.type = 2;

	/* get autoboot value from eeprom, keep TX reg on */
	fpga_tx2.settings = BIT(1) | eeprom_get_autoboot() ? 0x1 : 0x0;

	memset((void *) &fpga_rx, 0, sizeof(fpga_rx));

	shutdown = false;
}

void fpga_handle_write(uint8_t reg, uint16_t value)
{
	if (reg == 0x10) {
		fpga_tx1.charge = value;
		write_charge = true;
	} else if (reg == 0x14) {
		fpga_tx1.status = value;
	} else if (reg == 0x1c) {
		fpga_tx2.settings = (uint8_t) value;
		write_settings = true;
	}
}

void fpga_sync(void)
{
	fpga_rx_mem_map_t rx;

	spi_transact_buf((uint8_t *) &fpga_tx0, (uint8_t *) &rx, 8);
	if (rx.valid) {
		if (rx.type == 0 && rx.map0.os_status == 0x7a)
			shutdown = true;
		else if (rx.type == 1)
			fpga_handle_write(rx.map1.reg, rx.map1.value);
	}
	spi_transact_buf((uint8_t *) &fpga_tx1, (uint8_t *) &rx, 8);
	if (rx.valid) {
		if (rx.type == 0 && rx.map0.os_status == 0x7a)
			shutdown = true;
		else if (rx.type == 1)
			fpga_handle_write(rx.map1.reg, rx.map1.value);
	}
	spi_transact_buf((uint8_t *) &fpga_tx2, (uint8_t *) &rx, 8);
	if (rx.valid) {
		if (rx.type == 0 && rx.map0.os_status == 0x7a)
			shutdown = true;
		else if (rx.type == 1)
			fpga_handle_write(rx.map1.reg, rx.map1.value);
	}
}