summaryrefslogtreecommitdiffstats
path: root/pilc/raspi-hat/firmware/conf.c
blob: b4de898f458cb1f80004acd8c87d65a1dcdcfa7b (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
/*
 * PiLC HAT firmware
 * I2C configuration interface
 *
 * Copyright (c) 2016 Michael Buesch <m@bues.ch>
 *
 * 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 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "conf.h"
#include "i2c_slave.h"
#include "util.h"
#include "pb_txen.h"
#include "eepemu_24cxx.h"

#include <string.h>

#include <avr/io.h>


enum conf_items {
	CONF_NONE,
	CONF_XTALCAL,		/* Crystal calibration */
	CONF_EEMUWE,		/* EEPROM emulation write enable */
	CONF_PBTXENDBG,		/* TX-en debug mode */
	CONF_PBTXENTO,		/* TX-en timeout */
};

struct conf_context {
	enum conf_items item;
	uint8_t count;
	uint8_t buf[4];
};


static struct conf_context conf;


static void set_debug(uint8_t mode)
{
	enum pb_txen_debugmode m = (enum pb_txen_debugmode)mode;

	if (m == PBTXEN_DBG_OFF ||
	    m == PBTXEN_DBG_RETRIG ||
	    m == PBTXEN_DBG_NOTRIG)
		pb_txen_set_debug(m);
}

static uint8_t get_debug(void)
{
	return (uint8_t)pb_txen_get_debug();
}

static void set_osccal(uint8_t osccal)
{
	OSCCAL = osccal;
}

static uint8_t get_osccal(void)
{
	return OSCCAL;
}

static uint8_t handle_bool_read(bool (*getter)(void))
{
	struct conf_context *pc = &conf;
	uint8_t ret;

	ret = (uint8_t)getter();
	pc->item = CONF_NONE;

	return ret;
}

static uint8_t handle_u8_read(uint8_t (*getter)(void))
{
	struct conf_context *pc = &conf;
	uint8_t ret;

	ret = getter();
	pc->item = CONF_NONE;

	return ret;
}

static uint8_t handle_u16_read(uint16_t (*getter)(void))
{
	struct conf_context *pc = &conf;
	uint16_t value;
	uint8_t ret;

	build_assert(ARRAY_SIZE(pc->buf) >= 2);

	if (pc->count == 0) {
		value = getter();
		pc->buf[0] = (uint8_t)value;
		pc->buf[1] = (uint8_t)(value >> 8);
	}
	ret = pc->buf[pc->count++];
	if (pc->count >= ARRAY_SIZE(pc->buf))
		pc->item = CONF_NONE;

	return ret;
}

static uint8_t conf_transmit(bool start)
{
	struct conf_context *pc = &conf;
	uint8_t ret = 0;

	if (start)
		pc->count = 0;

	switch (pc->item) {
	case CONF_NONE:
		/* error */
		break;
	case CONF_XTALCAL:
		ret = handle_u8_read(get_osccal);
		break;
	case CONF_EEMUWE:
		ret = handle_bool_read(ee24cxx_get_we);
		break;
	case CONF_PBTXENDBG:
		ret = handle_u8_read(get_debug);
		break;
	case CONF_PBTXENTO:
		ret = handle_u16_read(pb_txen_get_timeout);
		break;
	}

	return ret;
}

static void handle_safe_bool_write(uint8_t data, void (*handler)(bool value))
{
	struct conf_context *pc = &conf;
	uint8_t a, b;

	build_assert(ARRAY_SIZE(pc->buf) >= 2);

	pc->buf[pc->count++] = data;
	if (pc->count >= 2) {
		a = pc->buf[0];
		b = (uint8_t)~(pc->buf[1]);
		if (a == b && (a == 0 || a == 1))
			handler(!!a);
		pc->item = CONF_NONE;
	}
}

static void handle_safe_u8_write(uint8_t data, void (*handler)(uint8_t value))
{
	struct conf_context *pc = &conf;
	uint8_t a, b;

	build_assert(ARRAY_SIZE(pc->buf) >= 2);

	pc->buf[pc->count++] = data;
	if (pc->count >= 2) {
		a = pc->buf[0];
		b = (uint8_t)~(pc->buf[1]);
		if (a == b)
			handler(a);
		pc->item = CONF_NONE;
	}
}

static void handle_safe_u16_write(uint8_t data, void (*handler)(uint16_t value))
{
	struct conf_context *pc = &conf;
	uint16_t a, b;

	build_assert(ARRAY_SIZE(pc->buf) >= 4);

	pc->buf[pc->count++] = data;
	if (pc->count >= 4) {
		a = (uint16_t)pc->buf[0];
		a |= (uint16_t)pc->buf[1] << 8;
		b = (uint16_t)pc->buf[2];
		b |= (uint16_t)pc->buf[3] << 8;
		b = ~b;
		if (a == b)
			handler(a);
		pc->item = CONF_NONE;
	}
}

static bool conf_receive(bool start, uint8_t data)
{
	struct conf_context *pc = &conf;

	if (start) {
		pc->item = CONF_NONE;
		pc->count = 0;
	}

	switch (pc->item) {
	case CONF_NONE:
		pc->item = (enum conf_items)data;
		pc->count = 0;
		break;
	case CONF_XTALCAL:
		handle_safe_u8_write(data, set_osccal);
		break;
	case CONF_EEMUWE:
		handle_safe_bool_write(data, ee24cxx_set_we);
		break;
	case CONF_PBTXENDBG:
		handle_safe_u8_write(data, set_debug);
		break;
	case CONF_PBTXENTO:
		handle_safe_u16_write(data, pb_txen_set_timeout);
		break;
	}

	return true;
}

static const struct i2c_slave_ops __flash conf_i2c_slave_ops = {
	.transmit	= conf_transmit,
	.receive	= conf_receive,
};

void conf_init(void)
{
	memset(&conf, 0, sizeof(conf));
	conf.item = CONF_NONE;

	i2cs_add_slave(CONF_ADDR, &conf_i2c_slave_ops);
}
bues.ch cgit interface