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 | |
| 42 | /* only 7 slots, as last slot is debug uart! */ |
| 43 | for (i = 0; i < 7; i++) |
| 44 | ncn8025_init(i); |
Harald Welte | f53f226 | 2019-02-24 11:01:08 +0100 | [diff] [blame] | 45 | |
| 46 | cache_init(); |
| 47 | cache_enable(CMCC); |
Harald Welte | 93f628a | 2019-02-24 14:32:30 +0100 | [diff] [blame] | 48 | |
| 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 Welte | c3f170d | 2019-02-24 09:06:59 +0100 | [diff] [blame] | 52 | } |
| 53 | |
Harald Welte | ff9f4ce | 2019-02-24 22:51:09 +0100 | [diff] [blame] | 54 | DEFUN(hello_fn, cmd_hello, |
| 55 | "hello", "Hello World example command") |
| 56 | { |
| 57 | printf("Hello World!\r\n"); |
| 58 | } |
| 59 | |
Harald Welte | 1b9a5b8 | 2019-02-24 23:04:45 +0100 | [diff] [blame] | 60 | static 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 | |
| 75 | DEFUN(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 | |
| 87 | DEFUN(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 | |
| 109 | DEFUN(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 | |
| 131 | DEFUN(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 | |
| 167 | DEFUN(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 | |
| 193 | DEFUN(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 Redon | 69b92d9 | 2019-01-24 16:39:20 +0100 | [diff] [blame] | 217 | int main(void) |
| 218 | { |
| 219 | atmel_start_init(); |
Kévin Redon | 78d2f44 | 2019-01-24 18:45:59 +0100 | [diff] [blame] | 220 | |
Harald Welte | 361ed20 | 2019-02-24 21:15:39 +0100 | [diff] [blame] | 221 | usart_sync_enable(&UART_debug); |
Kévin Redon | 78d2f44 | 2019-01-24 18:45:59 +0100 | [diff] [blame] | 222 | |
Kévin Redon | 8e53800 | 2019-01-30 11:19:19 +0100 | [diff] [blame] | 223 | usb_start(); |
| 224 | |
Harald Welte | c3f170d | 2019-02-24 09:06:59 +0100 | [diff] [blame] | 225 | board_init(); |
Harald Welte | ff9f4ce | 2019-02-24 22:51:09 +0100 | [diff] [blame] | 226 | command_init("sysmoOCTSIM> "); |
| 227 | command_register(&cmd_hello); |
Harald Welte | 1b9a5b8 | 2019-02-24 23:04:45 +0100 | [diff] [blame] | 228 | 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 Welte | c3f170d | 2019-02-24 09:06:59 +0100 | [diff] [blame] | 234 | |
Harald Welte | 361ed20 | 2019-02-24 21:15:39 +0100 | [diff] [blame] | 235 | printf("\r\n\r\nsysmocom sysmoOCTSIM\r\n"); |
Kévin Redon | 8e53800 | 2019-01-30 11:19:19 +0100 | [diff] [blame] | 236 | while (true) { // main loop |
Harald Welte | ff9f4ce | 2019-02-24 22:51:09 +0100 | [diff] [blame] | 237 | command_try_recv(); |
Kévin Redon | 8e53800 | 2019-01-30 11:19:19 +0100 | [diff] [blame] | 238 | } |
Kévin Redon | 69b92d9 | 2019-01-24 16:39:20 +0100 | [diff] [blame] | 239 | } |