blob: 369e3efdf32d43a6611836ddba62fc5a7551e499 [file] [log] [blame]
Kévin Redon69b92d92019-01-24 16:39:20 +01001/*
Kévin Redon78d2f442019-01-24 18:45:59 +01002 * Copyright (C) 2019 sysmocom -s.f.m.c. GmbH, Author: Kevin Redon <kredon@sysmocom.de>
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17*/
Kévin Redon69b92d92019-01-24 16:39:20 +010018
Harald Welte1b9a5b82019-02-24 23:04:45 +010019#include <stdlib.h>
20#include <stdio.h>
Harald Weltef53f2262019-02-24 11:01:08 +010021#include <parts.h>
22#include <hal_cache.h>
Harald Welte93f628a2019-02-24 14:32:30 +010023#include <hri_port_e54.h>
Harald Weltef53f2262019-02-24 11:01:08 +010024
Kévin Redon69b92d92019-01-24 16:39:20 +010025#include "atmel_start.h"
26#include "atmel_start_pins.h"
27
Harald Weltec3f170d2019-02-24 09:06:59 +010028#include "i2c_bitbang.h"
29#include "octsim_i2c.h"
30#include "ncn8025.h"
31
Harald Welteff9f4ce2019-02-24 22:51:09 +010032#include "command.h"
33
Kévin Redon78d2f442019-01-24 18:45:59 +010034
Harald Weltec3f170d2019-02-24 09:06:59 +010035static void board_init()
36{
37 int i;
38
39 for (i = 0; i < 4; i++)
40 i2c_init(&i2c[i]);
41
Harald Welte255da5e2019-04-16 18:19:53 +020042 for (i = 0; i < 8; i++)
Harald Weltec3f170d2019-02-24 09:06:59 +010043 ncn8025_init(i);
Harald Weltef53f2262019-02-24 11:01:08 +010044
45 cache_init();
46 cache_enable(CMCC);
Harald Welte93f628a2019-02-24 14:32:30 +010047
48 /* increase drive strength of 20Mhz SIM clock output to 8mA
49 * (there are 8 inputs + traces to drive!) */
50 hri_port_set_PINCFG_DRVSTR_bit(PORT, 0, 11);
Harald Weltec3f170d2019-02-24 09:06:59 +010051}
52
Harald Welte1b9a5b82019-02-24 23:04:45 +010053static int validate_slotnr(int argc, char **argv, int idx)
54{
55 int slotnr;
56 if (argc < idx+1) {
57 printf("You have to specify the slot number (0..7)\r\n");
58 return -1;
59 }
60 slotnr = atoi(argv[idx]);
61 if (slotnr < 0 || slotnr > 7) {
62 printf("You have to specify the slot number (0..7)\r\n");
63 return -1;
64 }
65 return slotnr;
66}
67
68DEFUN(sim_status, cmd_sim_status, "sim-status", "Get state of specified NCN8025")
69{
70 struct ncn8025_settings settings;
71 int slotnr = validate_slotnr(argc, argv, 1);
72 if (slotnr < 0)
73 return;
74 ncn8025_get(slotnr, &settings);
75 printf("SIM%d: ", slotnr);
76 ncn8025_dump(&settings);
77 printf("\r\n");
78}
79
80DEFUN(sim_power, cmd_sim_power, "sim-power", "Enable/disable SIM card power")
81{
82 struct ncn8025_settings settings;
83 int slotnr = validate_slotnr(argc, argv, 1);
84 int enable;
85
86 if (slotnr < 0)
87 return;
88
89 if (argc < 3) {
90 printf("You have to specify 0=disable or 1=enable\r\n");
91 return;
92 }
93 enable = atoi(argv[2]);
94 ncn8025_get(slotnr, &settings);
95 if (enable)
96 settings.cmdvcc = true;
97 else
98 settings.cmdvcc = false;
99 ncn8025_set(slotnr, &settings);
100}
101
102DEFUN(sim_reset, cmd_sim_reset, "sim-reset", "Enable/disable SIM reset")
103{
104 struct ncn8025_settings settings;
105 int slotnr = validate_slotnr(argc, argv, 1);
106 int enable;
107
108 if (slotnr < 0)
109 return;
110
111 if (argc < 3) {
112 printf("You have to specify 0=disable or 1=enable\r\n");
113 return;
114 }
115 enable = atoi(argv[2]);
116 ncn8025_get(slotnr, &settings);
117 if (enable)
118 settings.rstin = true;
119 else
120 settings.rstin = false;
121 ncn8025_set(slotnr, &settings);
122}
123
124DEFUN(sim_clkdiv, cmd_sim_clkdiv, "sim-clkdiv", "Set SIM clock divider (1,2,4,8)")
125{
126 struct ncn8025_settings settings;
127 int slotnr = validate_slotnr(argc, argv, 1);
128 int clkdiv;
129
130 if (slotnr < 0)
131 return;
132
133 if (argc < 3) {
134 printf("You have to specify a valid divider (1,2,4,8)\r\n");
135 return;
136 }
137 clkdiv = atoi(argv[2]);
138 if (clkdiv != 1 && clkdiv != 2 && clkdiv != 4 && clkdiv != 8) {
139 printf("You have to specify a valid divider (1,2,4,8)\r\n");
140 return;
141 }
142 ncn8025_get(slotnr, &settings);
143 switch (clkdiv) {
144 case 1:
145 settings.clkdiv = SIM_CLKDIV_1;
146 break;
147 case 2:
148 settings.clkdiv = SIM_CLKDIV_2;
149 break;
150 case 4:
151 settings.clkdiv = SIM_CLKDIV_4;
152 break;
153 case 8:
154 settings.clkdiv = SIM_CLKDIV_8;
155 break;
156 }
157 ncn8025_set(slotnr, &settings);
158}
159
160DEFUN(sim_voltage, cmd_sim_voltage, "sim-voltage", "Set SIM voltage (5/3/1.8)")
161{
162 struct ncn8025_settings settings;
163 int slotnr = validate_slotnr(argc, argv, 1);
164
165 if (slotnr < 0)
166 return;
167
168 if (argc < 3) {
169 printf("You have to specify a valid voltage (5/3/1.8)\r\n");
170 return;
171 }
172 ncn8025_get(slotnr, &settings);
173 if (!strcmp(argv[2], "5"))
174 settings.vsel = SIM_VOLT_5V0;
175 else if (!strcmp(argv[2], "3"))
176 settings.vsel = SIM_VOLT_3V0;
177 else if (!strcmp(argv[2], "1.8"))
178 settings.vsel = SIM_VOLT_1V8;
179 else {
180 printf("You have to specify a valid voltage (5/3/1.8)\r\n");
181 return;
182 }
183 ncn8025_set(slotnr, &settings);
184}
185
186DEFUN(sim_led, cmd_sim_led, "sim-led", "Set SIM LED (1=on, 0=off)")
187{
188 struct ncn8025_settings settings;
189 int slotnr = validate_slotnr(argc, argv, 1);
190
191 if (slotnr < 0)
192 return;
193
194 if (argc < 3) {
195 printf("You have to specify 0=disable or 1=enable\r\n");
196 return;
197 }
198 ncn8025_get(slotnr, &settings);
199 if (atoi(argv[2]))
200 settings.led = true;
201 else
202 settings.led = false;
203 ncn8025_set(slotnr, &settings);
204}
205
206
207
Harald Welte67b2aba2019-04-16 20:47:22 +0200208extern void testmode_init(void);
Harald Welte1b9a5b82019-02-24 23:04:45 +0100209
Kévin Redon69b92d92019-01-24 16:39:20 +0100210int main(void)
211{
212 atmel_start_init();
Kévin Redon78d2f442019-01-24 18:45:59 +0100213
Harald Welte361ed202019-02-24 21:15:39 +0100214 usart_sync_enable(&UART_debug);
Kévin Redon78d2f442019-01-24 18:45:59 +0100215
Kévin Redon8e538002019-01-30 11:19:19 +0100216 usb_start();
217
Harald Weltec3f170d2019-02-24 09:06:59 +0100218 board_init();
Harald Welteff9f4ce2019-02-24 22:51:09 +0100219 command_init("sysmoOCTSIM> ");
Harald Welte1b9a5b82019-02-24 23:04:45 +0100220 command_register(&cmd_sim_status);
221 command_register(&cmd_sim_power);
222 command_register(&cmd_sim_reset);
223 command_register(&cmd_sim_clkdiv);
224 command_register(&cmd_sim_voltage);
225 command_register(&cmd_sim_led);
Harald Welte67b2aba2019-04-16 20:47:22 +0200226 testmode_init();
Harald Weltec3f170d2019-02-24 09:06:59 +0100227
Harald Welte361ed202019-02-24 21:15:39 +0100228 printf("\r\n\r\nsysmocom sysmoOCTSIM\r\n");
Harald Weltee7aa5342019-04-16 21:11:14 +0200229
230 command_print_prompt();
Kévin Redon8e538002019-01-30 11:19:19 +0100231 while (true) { // main loop
Harald Welteff9f4ce2019-02-24 22:51:09 +0100232 command_try_recv();
Kévin Redon8e538002019-01-30 11:19:19 +0100233 }
Kévin Redon69b92d92019-01-24 16:39:20 +0100234}