summaryrefslogtreecommitdiff
path: root/src/cortexelf-v1/ao_1802.c
blob: 9fb36595d3797376d32b06b042eb0e8f1f361936 (plain) (blame)
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*
 * Copyright © 2017 Keith Packard <keithp@keithp.com>
 *
 * 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.
 */

#include <ao.h>
#include <ao_flip_bits.h>
#include <ao_1802.h>
#include <ao_exti.h>

/* Decoded address driven by TPA/TPB signals */
uint16_t	ADDRESS;

/* Decoded data, driven by TPB signal */
uint8_t		DATA;

/* Mux control */
#define _MUX_1802		0
#define _MUX_STM		1

uint8_t		MUX_CONTROL;

/* Signals muxed between 1802 and STM */
uint8_t
MRD(void) {
	return ao_gpio_get(MRD_PORT, MRD_BIT, MRD_PIN);
}

void
MRD_set(uint8_t value) {
	ao_gpio_set(MRD_PORT, MRD_BIT, MRD_PIN, value);
}

uint8_t
MWR(void) {
	return ao_gpio_get(MWR_PORT, MWR_BIT, MWR_PIN);
}

void
MWR_set(uint8_t value) {
	ao_gpio_set(MWR_PORT, MWR_BIT, MWR_PIN, value);
}

static void
TPA_rising(void)
{
	ADDRESS = (ADDRESS & 0x00ff) | ((uint16_t) MA() << 8);
	ao_wakeup(&ADDRESS);
}

uint8_t
TPA(void) {
	return ao_gpio_get(TPA_PORT, TPA_BIT, TPA_PIN);
}

void
TPA_set(uint8_t tpa) {
	ao_gpio_set(TPA_PORT, TPA_BIT, TPA_PIN, tpa);
	if (tpa)
		TPA_rising();
}

static void
TPB_rising(void)
{
	ADDRESS = (ADDRESS & 0xff00) | MA();
	if (MWR() == 0 || MRD() == 0)
		DATA = BUS();
	ao_wakeup(&ADDRESS);
}

static void
TPB_falling(void)
{
}

uint8_t
TPB(void) {
	return ao_gpio_get(TPB_PORT, TPB_BIT, TPB_PIN);
}

void
TPB_set(uint8_t tpb) {
	ao_gpio_set(TPB_PORT, TPB_BIT, TPB_PIN, tpb);
	if (tpb)
		TPB_rising();
	else
		TPB_falling();
}

uint8_t
MA(void) {
	return (ao_gpio_get_all(MA_PORT) >> MA_SHIFT) & MA_MASK;
}

void
MA_set(uint8_t ma) {
	ao_gpio_set_mask(MA_PORT, ((uint16_t) ma) << MA_SHIFT, MA_MASK << MA_SHIFT);
}

/* Tri-state data bus */

uint8_t
BUS(void) {
	return ao_flip_bits_8[(ao_gpio_get_all(BUS_PORT) >> BUS_SHIFT) & BUS_MASK];
}

void
BUS_set(uint8_t bus) {
	ao_gpio_set_mask(BUS_PORT, ao_flip_bits_8[bus] << BUS_SHIFT, BUS_MASK << BUS_SHIFT);
}

void
BUS_stm(void)
{
	ao_set_output_mask(BUS_PORT, BUS_MASK << BUS_SHIFT);
}

void
BUS_1802(void)
{
	ao_set_input_mask(BUS_PORT, BUS_MASK << BUS_SHIFT);
}

/* Pins controlled by 1802 */
uint8_t
SC(void) {
	return ao_flip_bits_2[(ao_gpio_get_all(SC_PORT) >> SC_SHIFT) & SC_MASK];
}

uint8_t
Q(void) {
	return ao_gpio_get(Q_PORT, Q_BIT, Q_PIN);
}

uint8_t
N(void) {
	return (ao_gpio_get_all(N_PORT) >> N_SHIFT) & N_MASK;
}

/* Pins controlled by STM */
uint8_t
EF(void) {
	return (ao_gpio_get_all(EF_PORT) >> EF_SHIFT) & EF_MASK;
}

void
EF_set(uint8_t ef) {
	ao_gpio_set_mask(EF_PORT, ef << EF_SHIFT, EF_MASK << EF_SHIFT);
}

uint8_t
DMA_IN(void) {
	return ao_gpio_get(DMA_IN_PORT, DMA_IN_BIT, DMA_IN_PIN);
}

void
DMA_IN_set(uint8_t dma_in) {
	ao_gpio_set(DMA_IN_PORT, DMA_IN_BIT, DMA_IN_PIN, dma_in);
}

uint8_t
DMA_OUT(void) {
	return ao_gpio_get(DMA_OUT_PORT, DMA_OUT_BIT, DMA_OUT_PIN);
}

void
DMA_OUT_set(uint8_t dma_out) {
	ao_gpio_set(DMA_OUT_PORT, DMA_OUT_BIT, DMA_OUT_PIN, dma_out);
}

uint8_t
INT(void) {
	return ao_gpio_get(INT_PORT, INT_BIT, INT_PIN);
}

void
INT_set(uint8_t dma_out) {
	ao_gpio_set(INT_PORT, INT_BIT, INT_PIN, dma_out);
}

uint8_t
CLEAR(void) {
	return ao_gpio_get(CLEAR_PORT, CLEAR_BIT, CLEAR_PIN);
}

void
CLEAR_set(uint8_t dma_out) {
	ao_gpio_set(CLEAR_PORT, CLEAR_BIT, CLEAR_PIN, dma_out);
}

uint8_t
WAIT(void) {
	return ao_gpio_get(WAIT_PORT, WAIT_BIT, WAIT_PIN);
}

void
WAIT_set(uint8_t dma_out) {
	ao_gpio_set(WAIT_PORT, WAIT_BIT, WAIT_PIN, dma_out);
}

void
tpb_isr(void) {
	/* Latch low address and data on rising edge of TPB */
	if (TPB())
		TPB_rising();
	else
		TPB_falling();
}

void
tpa_isr(void) {
	/* Latch high address on rising edge of TPA */
	if (TPA())
		TPA_rising();
}

#define ao_1802_in(port, bit, mode) do {		\
		ao_gpio_set_mode(port, bit, mode);	\
		ao_set_input(port, bit);		\
	} while (0)

#define ao_1802_in_isr(port, bit, mode) do {		\
		ao_gpio_set_mode(port, bit, mode);	\
		ao_set_input(port, bit);		\
		ao_exti_enable(port, bit);		\
	} while (0)

#define ao_1802_out_isr(port, bit) do { \
		ao_exti_disable(port, bit); \
		ao_set_output(port, bit); \
	} while (0)

void
MUX_1802(void)
{
	if (MUX_CONTROL != _MUX_1802) {
		/* Set pins to input, but pulled to idle value */
		ao_1802_in(MRD_PORT, MRD_BIT, AO_EXTI_MODE_PULL_UP);
		ao_1802_in(MWR_PORT, MWR_BIT, AO_EXTI_MODE_PULL_UP);
		ao_1802_in_isr(TPB_PORT, TPB_BIT, AO_EXTI_MODE_PULL_DOWN);
		ao_1802_in_isr(TPA_PORT, TPA_BIT, AO_EXTI_MODE_PULL_DOWN);
		ao_set_input_mask(MA_PORT, MA_MASK << MA_SHIFT);

		ao_gpio_set(MUX_PORT, MUX_BIT, MUX_PIN, 0);

		/* Now change the pins to eliminate the pull up/down */
		ao_gpio_set_mode(MRD_PORT, MRD_BIT, 0);
		ao_gpio_set_mode(MWR_PORT, MWR_BIT, 0);
		ao_gpio_set_mode(TPB_PORT, TPB_BIT, 0);
		ao_gpio_set_mode(TPA_PORT, TPA_BIT, 0);

		MUX_CONTROL = _MUX_1802;
	}
}

void
MUX_stm(void)
{
	if (MUX_CONTROL != _MUX_STM) {
		/* Set the pins back to pull to the idle value */
		ao_gpio_set_mode(MRD_PORT, MRD_BIT, AO_EXTI_MODE_PULL_UP);
		ao_gpio_set_mode(MWR_PORT, MWR_BIT, AO_EXTI_MODE_PULL_UP);
		ao_gpio_set_mode(TPB_PORT, TPB_BIT, AO_EXTI_MODE_PULL_DOWN);
		ao_gpio_set_mode(TPA_PORT, TPA_BIT, AO_EXTI_MODE_PULL_DOWN);

		ao_gpio_set(MUX_PORT, MUX_BIT, MUX_PIN, 1);

		/* Now set the pins as output, driven to the idle value */
		ao_set_output(MRD_PORT, MRD_BIT, MRD_PIN, 1);
		ao_set_output(MWR_PORT, MWR_BIT, MWR_PIN, 1);
		ao_set_output(TPB_PORT, TPB_BIT, TPB_PIN, 0);
		ao_set_output(TPA_PORT, TPA_BIT, TPA_PIN, 0);
		ao_set_output_mask(MA_PORT, MA_MASK << MA_SHIFT);
		MUX_CONTROL = _MUX_STM;
	}
}

void
ao_1802_init(void)
{
	/* Multiplexed signals*/

	/* active low signals */
	ao_enable_input(MRD_PORT, MRD_BIT, AO_EXTI_MODE_PULL_UP);
	ao_enable_input(MWR_PORT, MWR_BIT, AO_EXTI_MODE_PULL_UP);

	/* active high signals with interrupts */
	ao_exti_setup(TPA_PORT, TPA_BIT,
		      AO_EXTI_MODE_PULL_DOWN | AO_EXTI_MODE_RISING | AO_EXTI_MODE_FALLING,
		      tpa_isr);
	ao_exti_setup(TPB_PORT, TPB_BIT,
		      AO_EXTI_MODE_PULL_DOWN | AO_EXTI_MODE_RISING | AO_EXTI_MODE_FALLING,
		      tpb_isr);

	/* multiplexed address bus */
	ao_enable_input_mask(MA_PORT, MA_MASK << MA_SHIFT, 0);

	/* Data bus */

	ao_enable_input_mask(BUS_PORT, BUS_MASK << BUS_SHIFT, 0);

	/* Pins controlled by 1802 */
	ao_enable_input_mask(SC_PORT, SC_MASK << SC_SHIFT, 0);
	ao_enable_input(Q_PORT, Q_BIT, 0);
	ao_enable_input_mask(N_PORT, N_MASK << N_SHIFT, 0);

	/* Pins controlled by STM */
	ao_enable_output_mask(EF_PORT, 0, EF_MASK << EF_SHIFT);
	ao_enable_output(DMA_IN_PORT, DMA_IN_BIT, DMA_IN_PIN, 1);
	ao_enable_output(DMA_OUT_PORT, DMA_OUT_BIT, DMA_OUT_PIN, 1);
	ao_enable_output(INT_PORT, INT_BIT, INT_PIN, 1);
	ao_enable_output(CLEAR_PORT, CLEAR_BIT, CLEAR_PIN, 1);
	ao_enable_output(WAIT_PORT, WAIT_BIT, WAIT_PIN, 1);

	/* Force configuration to STM so that MUX_1802 will do something */
	MUX_CONTROL = _MUX_STM;
	MUX_1802();
}