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