Kévin Redon | 69b92d9 | 2019-01-24 16:39:20 +0100 | [diff] [blame] | 1 | /* |
Kévin Redon | 78d2f44 | 2019-01-24 18:45:59 +0100 | [diff] [blame] | 2 | * 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 Redon | 69b92d9 | 2019-01-24 16:39:20 +0100 | [diff] [blame] | 18 | |
Harald Welte | 1b9a5b8 | 2019-02-24 23:04:45 +0100 | [diff] [blame] | 19 | #include <stdlib.h> |
| 20 | #include <stdio.h> |
Harald Welte | f53f226 | 2019-02-24 11:01:08 +0100 | [diff] [blame] | 21 | #include <parts.h> |
| 22 | #include <hal_cache.h> |
Harald Welte | 93f628a | 2019-02-24 14:32:30 +0100 | [diff] [blame] | 23 | #include <hri_port_e54.h> |
Harald Welte | f53f226 | 2019-02-24 11:01:08 +0100 | [diff] [blame] | 24 | |
Kévin Redon | 69b92d9 | 2019-01-24 16:39:20 +0100 | [diff] [blame] | 25 | #include "atmel_start.h" |
| 26 | #include "atmel_start_pins.h" |
| 27 | |
Harald Welte | c3f170d | 2019-02-24 09:06:59 +0100 | [diff] [blame] | 28 | #include "i2c_bitbang.h" |
| 29 | #include "octsim_i2c.h" |
| 30 | #include "ncn8025.h" |
| 31 | |
Harald Welte | ff9f4ce | 2019-02-24 22:51:09 +0100 | [diff] [blame] | 32 | #include "command.h" |
| 33 | |
Kévin Redon | 78d2f44 | 2019-01-24 18:45:59 +0100 | [diff] [blame] | 34 | |
Harald Welte | c3f170d | 2019-02-24 09:06:59 +0100 | [diff] [blame] | 35 | static void board_init() |
| 36 | { |
| 37 | int i; |
| 38 | |
| 39 | for (i = 0; i < 4; i++) |
| 40 | i2c_init(&i2c[i]); |
| 41 | |
Harald Welte | 255da5e | 2019-04-16 18:19:53 +0200 | [diff] [blame] | 42 | for (i = 0; i < 8; i++) |
Harald Welte | c3f170d | 2019-02-24 09:06:59 +0100 | [diff] [blame] | 43 | ncn8025_init(i); |
Harald Welte | f53f226 | 2019-02-24 11:01:08 +0100 | [diff] [blame] | 44 | |
| 45 | cache_init(); |
| 46 | cache_enable(CMCC); |
Harald Welte | 93f628a | 2019-02-24 14:32:30 +0100 | [diff] [blame] | 47 | |
| 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 Welte | c3f170d | 2019-02-24 09:06:59 +0100 | [diff] [blame] | 51 | } |
| 52 | |
Harald Welte | 1b9a5b8 | 2019-02-24 23:04:45 +0100 | [diff] [blame] | 53 | static 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 | |
| 68 | DEFUN(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 | |
| 80 | DEFUN(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 | |
| 102 | DEFUN(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 | |
| 124 | DEFUN(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 | |
| 160 | DEFUN(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 | |
| 186 | DEFUN(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 Welte | 67b2aba | 2019-04-16 20:47:22 +0200 | [diff] [blame] | 208 | extern void testmode_init(void); |
Harald Welte | 1b9a5b8 | 2019-02-24 23:04:45 +0100 | [diff] [blame] | 209 | |
Kévin Redon | 69b92d9 | 2019-01-24 16:39:20 +0100 | [diff] [blame] | 210 | int main(void) |
| 211 | { |
| 212 | atmel_start_init(); |
Kévin Redon | 78d2f44 | 2019-01-24 18:45:59 +0100 | [diff] [blame] | 213 | |
Harald Welte | 361ed20 | 2019-02-24 21:15:39 +0100 | [diff] [blame] | 214 | usart_sync_enable(&UART_debug); |
Kévin Redon | 78d2f44 | 2019-01-24 18:45:59 +0100 | [diff] [blame] | 215 | |
Kévin Redon | 8e53800 | 2019-01-30 11:19:19 +0100 | [diff] [blame] | 216 | usb_start(); |
| 217 | |
Harald Welte | c3f170d | 2019-02-24 09:06:59 +0100 | [diff] [blame] | 218 | board_init(); |
Harald Welte | ff9f4ce | 2019-02-24 22:51:09 +0100 | [diff] [blame] | 219 | command_init("sysmoOCTSIM> "); |
Harald Welte | 1b9a5b8 | 2019-02-24 23:04:45 +0100 | [diff] [blame] | 220 | 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 Welte | 67b2aba | 2019-04-16 20:47:22 +0200 | [diff] [blame] | 226 | testmode_init(); |
Harald Welte | c3f170d | 2019-02-24 09:06:59 +0100 | [diff] [blame] | 227 | |
Harald Welte | 361ed20 | 2019-02-24 21:15:39 +0100 | [diff] [blame] | 228 | printf("\r\n\r\nsysmocom sysmoOCTSIM\r\n"); |
Harald Welte | e7aa534 | 2019-04-16 21:11:14 +0200 | [diff] [blame] | 229 | |
| 230 | command_print_prompt(); |
Kévin Redon | 8e53800 | 2019-01-30 11:19:19 +0100 | [diff] [blame] | 231 | while (true) { // main loop |
Harald Welte | ff9f4ce | 2019-02-24 22:51:09 +0100 | [diff] [blame] | 232 | command_try_recv(); |
Kévin Redon | 8e53800 | 2019-01-30 11:19:19 +0100 | [diff] [blame] | 233 | } |
Kévin Redon | 69b92d9 | 2019-01-24 16:39:20 +0100 | [diff] [blame] | 234 | } |