aboutsummaryrefslogtreecommitdiffstats
path: root/firmware/libboard/octsimtest/source/mux.c
blob: 8800ba26483e090e676c955fe3a03dbd4c55b7ec (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
/* sysmoOCTSIMTEST support for multiplexers
 *
 * (C) 2021 by Harald Welte <laforge@gnumonks.org>
 *
 * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307, USA
 */

#include "board.h"
#include "mux.h"
#include <stdbool.h>
#include <errno.h>

/* 3-bit S0..S2 signal for slot selection */
static const Pin pin_in_sel[3] = {
	{ PIO_PA1, PIOA, ID_PIOA, PIO_OUTPUT_0, PIO_DEFAULT },
	{ PIO_PA2, PIOA, ID_PIOA, PIO_OUTPUT_0, PIO_DEFAULT },
	{ PIO_PA3, PIOA, ID_PIOA, PIO_OUTPUT_0, PIO_DEFAULT },
};

/* 3-bit S0..S2 signal for frequency divider selection */
static const Pin pin_freq_sel[3] = {
	{ PIO_PA16, PIOA, ID_PIOA, PIO_OUTPUT_0, PIO_DEFAULT },
	{ PIO_PA17, PIOA, ID_PIOA, PIO_OUTPUT_0, PIO_DEFAULT },
	{ PIO_PA18, PIOA, ID_PIOA, PIO_OUTPUT_0, PIO_DEFAULT },
};

/* low-active output enable for all muxes */
static const Pin pin_oe = { PIO_PA19, PIOA, ID_PIOA, PIO_OUTPUT_1, PIO_DEFAULT };

static uint8_t g_mux_slot = 0;

/* initialize the external 1:8 multiplexers */
void mux_init(void)
{
	PIO_Configure(&pin_oe, PIO_LISTSIZE(pin_oe));
	PIO_Configure(pin_in_sel, PIO_LISTSIZE(pin_in_sel));
	PIO_Configure(pin_freq_sel, PIO_LISTSIZE(pin_freq_sel));

	mux_set_slot(0);
}

/* set the slot selection mux */
int mux_set_slot(uint8_t s)
{
	TRACE_INFO("%s(%u)\r\n", __func__, s);

	if (s > 7)
		return -EINVAL;

	/* !OE = H: disconnect input and output of muxes */
	PIO_Set(&pin_oe);

	if (s & 1)
		PIO_Set(&pin_in_sel[0]);
	else
		PIO_Clear(&pin_in_sel[0]);
	if (s & 2)
		PIO_Set(&pin_in_sel[1]);
	else
		PIO_Clear(&pin_in_sel[1]);
	if (s & 4)
		PIO_Set(&pin_in_sel[2]);
	else
		PIO_Clear(&pin_in_sel[2]);

	/* !OE = L: (re-)enable the output of muxes */
	PIO_Clear(&pin_oe);

	g_mux_slot = s;
	return s;
}

int mux_get_slot(void)
{
	return g_mux_slot;
}

/* set the frequency divider mux */
void mux_set_freq(uint8_t s)
{
	TRACE_INFO("%s(%u)\r\n", __func__, s);

	/* no need for 'break before make' here, this would also affect
	 * the SIM card I/O signals which we don't want to disturb */

	if (s & 1)
		PIO_Set(&pin_freq_sel[0]);
	else
		PIO_Clear(&pin_freq_sel[0]);
	if (s & 2)
		PIO_Set(&pin_freq_sel[1]);
	else
		PIO_Clear(&pin_freq_sel[1]);
	if (s & 4)
		PIO_Set(&pin_freq_sel[2]);
	else
		PIO_Clear(&pin_freq_sel[2]);

	/* !OE = L: ensure enable the output of muxes */
	PIO_Clear(&pin_oe);
}