blob: 9dee504d6b6b6b2a705a4192c3a06149889b0b9c [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 Welteff9f4ce2019-02-24 22:51:09 +010053DEFUN(hello_fn, cmd_hello,
54 "hello", "Hello World example command")
55{
56 printf("Hello World!\r\n");
57}
58
Harald Welte1b9a5b82019-02-24 23:04:45 +010059static int validate_slotnr(int argc, char **argv, int idx)
60{
61 int slotnr;
62 if (argc < idx+1) {
63 printf("You have to specify the slot number (0..7)\r\n");
64 return -1;
65 }
66 slotnr = atoi(argv[idx]);
67 if (slotnr < 0 || slotnr > 7) {
68 printf("You have to specify the slot number (0..7)\r\n");
69 return -1;
70 }
71 return slotnr;
72}
73
74DEFUN(sim_status, cmd_sim_status, "sim-status", "Get state of specified NCN8025")
75{
76 struct ncn8025_settings settings;
77 int slotnr = validate_slotnr(argc, argv, 1);
78 if (slotnr < 0)
79 return;
80 ncn8025_get(slotnr, &settings);
81 printf("SIM%d: ", slotnr);
82 ncn8025_dump(&settings);
83 printf("\r\n");
84}
85
86DEFUN(sim_power, cmd_sim_power, "sim-power", "Enable/disable SIM card power")
87{
88 struct ncn8025_settings settings;
89 int slotnr = validate_slotnr(argc, argv, 1);
90 int enable;
91
92 if (slotnr < 0)
93 return;
94
95 if (argc < 3) {
96 printf("You have to specify 0=disable or 1=enable\r\n");
97 return;
98 }
99 enable = atoi(argv[2]);
100 ncn8025_get(slotnr, &settings);
101 if (enable)
102 settings.cmdvcc = true;
103 else
104 settings.cmdvcc = false;
105 ncn8025_set(slotnr, &settings);
106}
107
108DEFUN(sim_reset, cmd_sim_reset, "sim-reset", "Enable/disable SIM reset")
109{
110 struct ncn8025_settings settings;
111 int slotnr = validate_slotnr(argc, argv, 1);
112 int enable;
113
114 if (slotnr < 0)
115 return;
116
117 if (argc < 3) {
118 printf("You have to specify 0=disable or 1=enable\r\n");
119 return;
120 }
121 enable = atoi(argv[2]);
122 ncn8025_get(slotnr, &settings);
123 if (enable)
124 settings.rstin = true;
125 else
126 settings.rstin = false;
127 ncn8025_set(slotnr, &settings);
128}
129
130DEFUN(sim_clkdiv, cmd_sim_clkdiv, "sim-clkdiv", "Set SIM clock divider (1,2,4,8)")
131{
132 struct ncn8025_settings settings;
133 int slotnr = validate_slotnr(argc, argv, 1);
134 int clkdiv;
135
136 if (slotnr < 0)
137 return;
138
139 if (argc < 3) {
140 printf("You have to specify a valid divider (1,2,4,8)\r\n");
141 return;
142 }
143 clkdiv = atoi(argv[2]);
144 if (clkdiv != 1 && clkdiv != 2 && clkdiv != 4 && clkdiv != 8) {
145 printf("You have to specify a valid divider (1,2,4,8)\r\n");
146 return;
147 }
148 ncn8025_get(slotnr, &settings);
149 switch (clkdiv) {
150 case 1:
151 settings.clkdiv = SIM_CLKDIV_1;
152 break;
153 case 2:
154 settings.clkdiv = SIM_CLKDIV_2;
155 break;
156 case 4:
157 settings.clkdiv = SIM_CLKDIV_4;
158 break;
159 case 8:
160 settings.clkdiv = SIM_CLKDIV_8;
161 break;
162 }
163 ncn8025_set(slotnr, &settings);
164}
165
166DEFUN(sim_voltage, cmd_sim_voltage, "sim-voltage", "Set SIM voltage (5/3/1.8)")
167{
168 struct ncn8025_settings settings;
169 int slotnr = validate_slotnr(argc, argv, 1);
170
171 if (slotnr < 0)
172 return;
173
174 if (argc < 3) {
175 printf("You have to specify a valid voltage (5/3/1.8)\r\n");
176 return;
177 }
178 ncn8025_get(slotnr, &settings);
179 if (!strcmp(argv[2], "5"))
180 settings.vsel = SIM_VOLT_5V0;
181 else if (!strcmp(argv[2], "3"))
182 settings.vsel = SIM_VOLT_3V0;
183 else if (!strcmp(argv[2], "1.8"))
184 settings.vsel = SIM_VOLT_1V8;
185 else {
186 printf("You have to specify a valid voltage (5/3/1.8)\r\n");
187 return;
188 }
189 ncn8025_set(slotnr, &settings);
190}
191
192DEFUN(sim_led, cmd_sim_led, "sim-led", "Set SIM LED (1=on, 0=off)")
193{
194 struct ncn8025_settings settings;
195 int slotnr = validate_slotnr(argc, argv, 1);
196
197 if (slotnr < 0)
198 return;
199
200 if (argc < 3) {
201 printf("You have to specify 0=disable or 1=enable\r\n");
202 return;
203 }
204 ncn8025_get(slotnr, &settings);
205 if (atoi(argv[2]))
206 settings.led = true;
207 else
208 settings.led = false;
209 ncn8025_set(slotnr, &settings);
210}
211
212
213
214
215
Kévin Redon69b92d92019-01-24 16:39:20 +0100216int main(void)
217{
218 atmel_start_init();
Kévin Redon78d2f442019-01-24 18:45:59 +0100219
Harald Welte361ed202019-02-24 21:15:39 +0100220 usart_sync_enable(&UART_debug);
Kévin Redon78d2f442019-01-24 18:45:59 +0100221
Kévin Redon8e538002019-01-30 11:19:19 +0100222 usb_start();
223
Harald Weltec3f170d2019-02-24 09:06:59 +0100224 board_init();
Harald Welteff9f4ce2019-02-24 22:51:09 +0100225 command_init("sysmoOCTSIM> ");
226 command_register(&cmd_hello);
Harald Welte1b9a5b82019-02-24 23:04:45 +0100227 command_register(&cmd_sim_status);
228 command_register(&cmd_sim_power);
229 command_register(&cmd_sim_reset);
230 command_register(&cmd_sim_clkdiv);
231 command_register(&cmd_sim_voltage);
232 command_register(&cmd_sim_led);
Harald Weltec3f170d2019-02-24 09:06:59 +0100233
Harald Welte361ed202019-02-24 21:15:39 +0100234 printf("\r\n\r\nsysmocom sysmoOCTSIM\r\n");
Kévin Redon8e538002019-01-30 11:19:19 +0100235 while (true) { // main loop
Harald Welteff9f4ce2019-02-24 22:51:09 +0100236 command_try_recv();
Kévin Redon8e538002019-01-30 11:19:19 +0100237 }
Kévin Redon69b92d92019-01-24 16:39:20 +0100238}