Kévin Redon | 9a12d68 | 2018-07-08 13:21:16 +0200 | [diff] [blame] | 1 | /* sysmocom quad-modem sysmoQMOD application code |
| 2 | * |
| 3 | * (C) 2016-2017 by Harald Welte <hwelte@hmw-consulting.de> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA |
| 18 | */ |
Harald Welte | aa3b867 | 2017-02-10 19:21:10 +0100 | [diff] [blame] | 19 | #include "board.h" |
| 20 | #include "simtrace.h" |
| 21 | #include "utils.h" |
Harald Welte | aa3b867 | 2017-02-10 19:21:10 +0100 | [diff] [blame] | 22 | #include "wwan_led.h" |
| 23 | #include "wwan_perst.h" |
Harald Welte | 5c583d3 | 2017-05-09 06:46:47 +0200 | [diff] [blame] | 24 | #include "sim_switch.h" |
Harald Welte | aa3b867 | 2017-02-10 19:21:10 +0100 | [diff] [blame] | 25 | #include "boardver_adc.h" |
Harald Welte | 6d1128e | 2017-05-05 20:23:10 +0200 | [diff] [blame] | 26 | #include "card_pres.h" |
Harald Welte | 9d90d28 | 2018-06-29 22:25:42 +0200 | [diff] [blame] | 27 | #include <osmocom/core/timer.h> |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 28 | #include "usb_buf.h" |
Harald Welte | aa3b867 | 2017-02-10 19:21:10 +0100 | [diff] [blame] | 29 | |
| 30 | static const Pin pin_hubpwr_override = PIN_PRTPWR_OVERRIDE; |
| 31 | static const Pin pin_hub_rst = {PIO_PA13, PIOA, ID_PIOA, PIO_OUTPUT_0, PIO_DEFAULT}; |
| 32 | static const Pin pin_1234_detect = {PIO_PA14, PIOA, ID_PIOA, PIO_INPUT, PIO_PULLUP}; |
| 33 | static const Pin pin_peer_rst = {PIO_PA0, PIOA, ID_PIOA, PIO_OUTPUT_0, PIO_DEFAULT}; |
| 34 | static const Pin pin_peer_erase = {PIO_PA11, PIOA, ID_PIOA, PIO_OUTPUT_0, PIO_DEFAULT}; |
| 35 | |
Harald Welte | 912b183 | 2017-03-06 09:28:13 +0100 | [diff] [blame] | 36 | /* array of generated USB Strings */ |
| 37 | extern unsigned char *usb_strings[]; |
| 38 | |
Harald Welte | aa3b867 | 2017-02-10 19:21:10 +0100 | [diff] [blame] | 39 | static int qmod_sam3_is_12(void) |
| 40 | { |
| 41 | if (PIO_Get(&pin_1234_detect) == 0) |
| 42 | return 1; |
| 43 | else |
| 44 | return 0; |
| 45 | } |
| 46 | |
| 47 | const unsigned char __eeprom_bin[256] = { |
Harald Welte | c6e482d | 2017-03-05 16:24:29 +0100 | [diff] [blame] | 48 | USB_VENDOR_OPENMOKO & 0xff, |
| 49 | USB_VENDOR_OPENMOKO >> 8, |
| 50 | USB_PRODUCT_QMOD_HUB & 0xff, |
| 51 | USB_PRODUCT_QMOD_HUB >> 8, |
Harald Welte | 705e899 | 2017-03-05 17:10:26 +0100 | [diff] [blame] | 52 | 0x00, 0x00, 0x9b, 0x20, 0x09, 0x00, 0x00, 0x00, 0x32, 0x32, 0x32, 0x32, /* 0x00 - 0x0f */ |
Harald Welte | aa3b867 | 2017-02-10 19:21:10 +0100 | [diff] [blame] | 53 | 0x32, 0x04, 0x09, 0x18, 0x0d, 0x00, 0x73, 0x00, 0x79, 0x00, 0x73, 0x00, 0x6d, 0x00, 0x6f, 0x00, /* 0x10 - 0x1f */ |
| 54 | 0x63, 0x00, 0x6f, 0x00, 0x6d, 0x00, 0x20, 0x00, 0x2d, 0x00, 0x20, 0x00, 0x73, 0x00, 0x2e, 0x00, /* 0x20 - 0x2f */ |
| 55 | 0x66, 0x00, 0x2e, 0x00, 0x6d, 0x00, 0x2e, 0x00, 0x63, 0x00, 0x2e, 0x00, 0x20, 0x00, 0x47, 0x00, /* 0x30 - 0x3f */ |
| 56 | 0x6d, 0x00, 0x62, 0x00, 0x48, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x40 - 0x4f */ |
| 57 | 0x00, 0x00, 0x00, 0x00, 0x71, 0x00, 0x75, 0x00, 0x61, 0x00, 0x64, 0x00, 0x20, 0x00, 0x6d, 0x00, /* 0x50 - 0x5f */ |
| 58 | 0x6f, 0x00, 0x64, 0x00, 0x65, 0x00, 0x6d, 0x00, 0x20, 0x00, 0x76, 0x00, 0x32, 0x00, 0x00, 0x00, /* 0x60 - 0x6f */ |
| 59 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x70 - 0x7f */ |
| 60 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x80 - 0x8f */ |
| 61 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x90 - 0x9f */ |
| 62 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0xa0 - 0xaf */ |
| 63 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0xb0 - 0xbf */ |
| 64 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0xc0 - 0xcf */ |
| 65 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0xd0 - 0xdf */ |
| 66 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0xe0 - 0xef */ |
| 67 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xA0, 0x56, 0x23, 0x71, 0x04, 0x00, /* 0xf0 - 0xff */ |
| 68 | }; |
| 69 | |
| 70 | #include "i2c.h" |
| 71 | static int write_hub_eeprom(void) |
| 72 | { |
Harald Welte | aa3b867 | 2017-02-10 19:21:10 +0100 | [diff] [blame] | 73 | int i; |
| 74 | |
| 75 | /* wait */ |
Harald Welte | 0e29598 | 2017-03-05 16:48:47 +0100 | [diff] [blame] | 76 | mdelay(100); |
Harald Welte | aa3b867 | 2017-02-10 19:21:10 +0100 | [diff] [blame] | 77 | |
Harald Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 78 | TRACE_INFO("Writing EEPROM...\n\r"); |
Harald Welte | aa3b867 | 2017-02-10 19:21:10 +0100 | [diff] [blame] | 79 | /* write the EEPROM once */ |
Kévin Redon | a71a6f4 | 2018-07-24 09:54:12 +0200 | [diff] [blame] | 80 | for (i = 0; i < ARRAY_SIZE(__eeprom_bin); i++) { |
Harald Welte | aa3b867 | 2017-02-10 19:21:10 +0100 | [diff] [blame] | 81 | int rc = eeprom_write_byte(0x50, i, __eeprom_bin[i]); |
Kévin Redon | a71a6f4 | 2018-07-24 09:54:12 +0200 | [diff] [blame] | 82 | if (rc < 0) { |
| 83 | TRACE_ERROR("Writing EEPROM failed at byte %u: 0x%02x\n\r", |
| 84 | i, __eeprom_bin[i]); |
| 85 | return 1; |
| 86 | } |
Harald Welte | aa3b867 | 2017-02-10 19:21:10 +0100 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | /* then pursue re-reading it again and again */ |
Harald Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 90 | TRACE_INFO("Verifying EEPROM...\n\r"); |
Kévin Redon | a71a6f4 | 2018-07-24 09:54:12 +0200 | [diff] [blame] | 91 | for (i = 0; i < ARRAY_SIZE(__eeprom_bin); i++) { |
Harald Welte | aa3b867 | 2017-02-10 19:21:10 +0100 | [diff] [blame] | 92 | int byte = eeprom_read_byte(0x50, i); |
Kévin Redon | a6bd717 | 2018-08-28 19:41:10 +0200 | [diff] [blame^] | 93 | TRACE_DEBUG("0x%02x: %02x\n\r", i, byte); |
Harald Welte | aa3b867 | 2017-02-10 19:21:10 +0100 | [diff] [blame] | 94 | if (byte != __eeprom_bin[i]) |
Harald Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 95 | TRACE_ERROR("Byte %u is wrong, expected 0x%02x, found 0x%02x\n\r", |
Harald Welte | aa3b867 | 2017-02-10 19:21:10 +0100 | [diff] [blame] | 96 | i, __eeprom_bin[i], byte); |
| 97 | } |
Kévin Redon | a6bd717 | 2018-08-28 19:41:10 +0200 | [diff] [blame^] | 98 | TRACE_INFO("EEPROM written\n\r"); |
Harald Welte | aa3b867 | 2017-02-10 19:21:10 +0100 | [diff] [blame] | 99 | |
| 100 | /* FIXME: Release PIN_PRTPWR_OVERRIDE after we know the hub is |
| 101 | * again powering us up */ |
| 102 | |
| 103 | return 0; |
| 104 | } |
| 105 | |
Harald Welte | 9547e41 | 2018-07-03 22:31:16 +0200 | [diff] [blame] | 106 | static int erase_hub_eeprom(void) |
| 107 | { |
| 108 | int i; |
| 109 | |
| 110 | /* wait */ |
| 111 | mdelay(100); |
| 112 | |
| 113 | TRACE_INFO("Erasing EEPROM...\n\r"); |
| 114 | /* write the EEPROM once */ |
| 115 | for (i = 0; i < 256; i++) { |
| 116 | int rc = eeprom_write_byte(0x50, i, 0xff); |
Kévin Redon | c428516 | 2018-08-07 12:12:47 +0200 | [diff] [blame] | 117 | if (rc < 0) { |
| 118 | TRACE_ERROR("Erasing EEPROM failed at byte %u: 0x%02x\n\r", |
| 119 | i, __eeprom_bin[i]); |
| 120 | return 1; |
| 121 | } |
Harald Welte | 9547e41 | 2018-07-03 22:31:16 +0200 | [diff] [blame] | 122 | } |
Kévin Redon | a6bd717 | 2018-08-28 19:41:10 +0200 | [diff] [blame^] | 123 | TRACE_INFO("EEPROM erased\n\r"); |
Harald Welte | 9547e41 | 2018-07-03 22:31:16 +0200 | [diff] [blame] | 124 | |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | |
Harald Welte | ed1efc5 | 2017-05-07 22:49:45 +0200 | [diff] [blame] | 129 | static void board_exec_dbg_cmd_st12only(int ch) |
Harald Welte | aa3b867 | 2017-02-10 19:21:10 +0100 | [diff] [blame] | 130 | { |
| 131 | uint32_t addr, val; |
| 132 | |
Harald Welte | ed1efc5 | 2017-05-07 22:49:45 +0200 | [diff] [blame] | 133 | /* functions below only work on primary (ST12) */ |
| 134 | if (!qmod_sam3_is_12()) |
| 135 | return; |
| 136 | |
| 137 | switch (ch) { |
| 138 | case 'E': |
| 139 | write_hub_eeprom(); |
| 140 | break; |
Harald Welte | 9547e41 | 2018-07-03 22:31:16 +0200 | [diff] [blame] | 141 | case 'e': |
| 142 | erase_hub_eeprom(); |
| 143 | break; |
Harald Welte | ed1efc5 | 2017-05-07 22:49:45 +0200 | [diff] [blame] | 144 | case 'O': |
| 145 | printf("Setting PRTPWR_OVERRIDE\n\r"); |
| 146 | PIO_Set(&pin_hubpwr_override); |
| 147 | break; |
| 148 | case 'o': |
| 149 | printf("Clearing PRTPWR_OVERRIDE\n\r"); |
| 150 | PIO_Clear(&pin_hubpwr_override); |
| 151 | break; |
| 152 | case 'H': |
| 153 | printf("Clearing _HUB_RESET -> HUB_RESET high (inactive)\n\r"); |
| 154 | PIO_Clear(&pin_hub_rst); |
| 155 | break; |
| 156 | case 'h': |
| 157 | /* high level drives transistor -> HUB_RESET low */ |
| 158 | printf("Asserting _HUB_RESET -> HUB_RESET low (active)\n\r"); |
| 159 | PIO_Set(&pin_hub_rst); |
| 160 | break; |
| 161 | case 'w': |
| 162 | if (PIO_GetOutputDataStatus(&pin_hub_rst) == 0) |
| 163 | printf("WARNING: attempting EEPROM access while HUB not in reset\n\r"); |
| 164 | printf("Please enter EEPROM offset:\n\r"); |
| 165 | UART_GetIntegerMinMax(&addr, 0, 255); |
| 166 | printf("Please enter EEPROM value:\n\r"); |
| 167 | UART_GetIntegerMinMax(&val, 0, 255); |
Kévin Redon | a6bd717 | 2018-08-28 19:41:10 +0200 | [diff] [blame^] | 168 | printf("Writing value 0x%02x to EEPROM offset 0x%02x\n\r", val, addr); |
Harald Welte | ed1efc5 | 2017-05-07 22:49:45 +0200 | [diff] [blame] | 169 | eeprom_write_byte(0x50, addr, val); |
| 170 | break; |
| 171 | case 'r': |
| 172 | printf("Please enter EEPROM offset:\n\r"); |
| 173 | UART_GetIntegerMinMax(&addr, 0, 255); |
Kévin Redon | a6bd717 | 2018-08-28 19:41:10 +0200 | [diff] [blame^] | 174 | printf("EEPROM[0x%02x] = 0x%02x\n\r", addr, eeprom_read_byte(0x50, addr)); |
Harald Welte | ed1efc5 | 2017-05-07 22:49:45 +0200 | [diff] [blame] | 175 | break; |
| 176 | default: |
| 177 | printf("Unknown command '%c'\n\r", ch); |
| 178 | break; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | /* returns '1' in case we should break any endless loop */ |
| 183 | void board_exec_dbg_cmd(int ch) |
| 184 | { |
Harald Welte | aa3b867 | 2017-02-10 19:21:10 +0100 | [diff] [blame] | 185 | switch (ch) { |
| 186 | case '?': |
Harald Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 187 | printf("\t?\thelp\n\r"); |
Harald Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 188 | printf("\tR\treset SAM3\n\r"); |
Harald Welte | 9164a6d | 2017-05-05 20:55:21 +0200 | [diff] [blame] | 189 | if (qmod_sam3_is_12()) { |
| 190 | printf("\tE\tprogram EEPROM\n\r"); |
Harald Welte | 9547e41 | 2018-07-03 22:31:16 +0200 | [diff] [blame] | 191 | printf("\te\tErase EEPROM\n\r"); |
Harald Welte | 9164a6d | 2017-05-05 20:55:21 +0200 | [diff] [blame] | 192 | printf("\tO\tEnable PRTPWR_OVERRIDE\n\r"); |
| 193 | printf("\to\tDisable PRTPWR_OVERRIDE\n\r"); |
| 194 | printf("\tH\tRelease HUB RESET (high)\n\r"); |
| 195 | printf("\th\tAssert HUB RESET (low)\n\r"); |
| 196 | printf("\tw\tWrite single byte in EEPROM\n\r"); |
| 197 | printf("\tr\tRead single byte from EEPROM\n\r"); |
| 198 | } |
Harald Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 199 | printf("\tX\tRelease peer SAM3 from reset\n\r"); |
| 200 | printf("\tx\tAssert peer SAM3 reset\n\r"); |
| 201 | printf("\tY\tRelease peer SAM3 ERASE signal\n\r"); |
| 202 | printf("\ty\tAssert peer SAM3 ERASE signal\n\r"); |
| 203 | printf("\tU\tProceed to USB Initialization\n\r"); |
| 204 | printf("\t1\tGenerate 1ms reset pulse on WWAN1\n\r"); |
| 205 | printf("\t2\tGenerate 1ms reset pulse on WWAN2\n\r"); |
Harald Welte | aa3b867 | 2017-02-10 19:21:10 +0100 | [diff] [blame] | 206 | break; |
Harald Welte | aa3b867 | 2017-02-10 19:21:10 +0100 | [diff] [blame] | 207 | case 'R': |
Harald Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 208 | printf("Asking NVIC to reset us\n\r"); |
Harald Welte | f415d71 | 2017-03-03 01:51:43 +0100 | [diff] [blame] | 209 | USBD_Disconnect(); |
Harald Welte | aa3b867 | 2017-02-10 19:21:10 +0100 | [diff] [blame] | 210 | NVIC_SystemReset(); |
| 211 | break; |
Harald Welte | 9164a6d | 2017-05-05 20:55:21 +0200 | [diff] [blame] | 212 | case 'X': |
| 213 | printf("Clearing _SIMTRACExx_RST -> SIMTRACExx_RST high (inactive)\n\r"); |
| 214 | PIO_Clear(&pin_peer_rst); |
| 215 | break; |
| 216 | case 'x': |
| 217 | printf("Setting _SIMTRACExx_RST -> SIMTRACExx_RST low (active)\n\r"); |
| 218 | PIO_Set(&pin_peer_rst); |
| 219 | break; |
| 220 | case 'Y': |
| 221 | printf("Clearing SIMTRACExx_ERASE (inactive)\n\r"); |
| 222 | PIO_Clear(&pin_peer_erase); |
| 223 | break; |
| 224 | case 'y': |
| 225 | printf("Seetting SIMTRACExx_ERASE (active)\n\r"); |
| 226 | PIO_Set(&pin_peer_erase); |
| 227 | break; |
| 228 | case '1': |
| 229 | printf("Resetting Modem 1 (of this SAM3)\n\r"); |
Harald Welte | 44622df | 2017-05-11 00:04:50 +0200 | [diff] [blame] | 230 | wwan_perst_do_reset_pulse(0, 300); |
Harald Welte | 9164a6d | 2017-05-05 20:55:21 +0200 | [diff] [blame] | 231 | break; |
| 232 | case '2': |
| 233 | printf("Resetting Modem 2 (of this SAM3)\n\r"); |
Harald Welte | 44622df | 2017-05-11 00:04:50 +0200 | [diff] [blame] | 234 | wwan_perst_do_reset_pulse(1, 300); |
Harald Welte | 9164a6d | 2017-05-05 20:55:21 +0200 | [diff] [blame] | 235 | break; |
| 236 | case '!': |
Harald Welte | 44622df | 2017-05-11 00:04:50 +0200 | [diff] [blame] | 237 | sim_switch_use_physical(0, 0); |
Harald Welte | 9164a6d | 2017-05-05 20:55:21 +0200 | [diff] [blame] | 238 | break; |
| 239 | case '@': |
Harald Welte | 44622df | 2017-05-11 00:04:50 +0200 | [diff] [blame] | 240 | sim_switch_use_physical(0, 0); |
Harald Welte | 9164a6d | 2017-05-05 20:55:21 +0200 | [diff] [blame] | 241 | break; |
| 242 | default: |
Harald Welte | ed1efc5 | 2017-05-07 22:49:45 +0200 | [diff] [blame] | 243 | if (!qmod_sam3_is_12()) |
Harald Welte | 9164a6d | 2017-05-05 20:55:21 +0200 | [diff] [blame] | 244 | printf("Unknown command '%c'\n\r", ch); |
Harald Welte | ed1efc5 | 2017-05-07 22:49:45 +0200 | [diff] [blame] | 245 | else |
| 246 | board_exec_dbg_cmd_st12only(ch); |
Harald Welte | aa3b867 | 2017-02-10 19:21:10 +0100 | [diff] [blame] | 247 | break; |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | void board_main_top(void) |
| 252 | { |
Harald Welte | 2539917 | 2017-05-11 00:47:29 +0200 | [diff] [blame] | 253 | #ifndef APPLICATION_dfu |
Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 254 | usb_buf_init(); |
| 255 | |
Harald Welte | aa3b867 | 2017-02-10 19:21:10 +0100 | [diff] [blame] | 256 | wwan_led_init(); |
| 257 | wwan_perst_init(); |
Harald Welte | 5c583d3 | 2017-05-09 06:46:47 +0200 | [diff] [blame] | 258 | sim_switch_init(); |
Harald Welte | 2539917 | 2017-05-11 00:47:29 +0200 | [diff] [blame] | 259 | #endif |
Harald Welte | aa3b867 | 2017-02-10 19:21:10 +0100 | [diff] [blame] | 260 | |
Harald Welte | 2817498 | 2017-05-07 16:45:10 +0200 | [diff] [blame] | 261 | /* make sure we can detect whether running in ST12 or ST34 */ |
Harald Welte | aa3b867 | 2017-02-10 19:21:10 +0100 | [diff] [blame] | 262 | PIO_Configure(&pin_1234_detect, 1); |
Harald Welte | 2817498 | 2017-05-07 16:45:10 +0200 | [diff] [blame] | 263 | |
| 264 | if (qmod_sam3_is_12()) { |
| 265 | /* set PIN_PRTPWR_OVERRIDE to output-low to avoid the internal |
| 266 | * pull-up on the input to keep SIMTRACE12 alive */ |
| 267 | PIO_Configure(&pin_hubpwr_override, 1); |
| 268 | PIO_Configure(&pin_hub_rst, 1); |
| 269 | } |
Harald Welte | aa3b867 | 2017-02-10 19:21:10 +0100 | [diff] [blame] | 270 | PIO_Configure(&pin_peer_rst, 1); |
| 271 | PIO_Configure(&pin_peer_erase, 1); |
Harald Welte | 2539917 | 2017-05-11 00:47:29 +0200 | [diff] [blame] | 272 | |
| 273 | #ifndef APPLICATION_dfu |
Harald Welte | aa3b867 | 2017-02-10 19:21:10 +0100 | [diff] [blame] | 274 | i2c_pin_init(); |
Harald Welte | 2539917 | 2017-05-11 00:47:29 +0200 | [diff] [blame] | 275 | #endif |
Harald Welte | aa3b867 | 2017-02-10 19:21:10 +0100 | [diff] [blame] | 276 | |
| 277 | if (qmod_sam3_is_12()) { |
Harald Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 278 | TRACE_INFO("Detected Quad-Modem ST12\n\r"); |
Harald Welte | aa3b867 | 2017-02-10 19:21:10 +0100 | [diff] [blame] | 279 | } else { |
Harald Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 280 | TRACE_INFO("Detected Quad-Modem ST34\n\r"); |
Harald Welte | 912b183 | 2017-03-06 09:28:13 +0100 | [diff] [blame] | 281 | /* make sure we use the second set of USB Strings |
| 282 | * calling the interfaces "Modem 3" and "Modem 4" rather |
| 283 | * than 1+2 */ |
| 284 | usb_strings[7] = usb_strings[9]; |
| 285 | usb_strings[8] = usb_strings[10]; |
Harald Welte | aa3b867 | 2017-02-10 19:21:10 +0100 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | /* Obtain the circuit board version (currently just prints voltage */ |
| 289 | get_board_version_adc(); |
Harald Welte | 2539917 | 2017-05-11 00:47:29 +0200 | [diff] [blame] | 290 | #ifndef APPLICATION_dfu |
Harald Welte | 6d1128e | 2017-05-05 20:23:10 +0200 | [diff] [blame] | 291 | /* Initialize checking for card insert/remove events */ |
| 292 | card_present_init(); |
Harald Welte | 2539917 | 2017-05-11 00:47:29 +0200 | [diff] [blame] | 293 | #endif |
Harald Welte | aa3b867 | 2017-02-10 19:21:10 +0100 | [diff] [blame] | 294 | } |
Harald Welte | 27f5fc6 | 2017-11-28 22:15:56 +0100 | [diff] [blame] | 295 | |
| 296 | static int uart_has_loopback_jumper(void) |
| 297 | { |
| 298 | unsigned int i; |
| 299 | const Pin uart_loopback_pins[] = { |
| 300 | {PIO_PA9A_URXD0, PIOA, ID_PIOA, PIO_INPUT, PIO_DEFAULT}, |
| 301 | {PIO_PA10A_UTXD0, PIOA, ID_PIOA, PIO_OUTPUT_0, PIO_DEFAULT} |
| 302 | }; |
| 303 | |
| 304 | /* Configure UART pins as I/O */ |
| 305 | PIO_Configure(uart_loopback_pins, PIO_LISTSIZE(uart_loopback_pins)); |
| 306 | |
Kévin Redon | d24e9bd | 2018-07-07 14:56:43 +0200 | [diff] [blame] | 307 | /* Send pattern over UART TX and check if it is received on RX |
| 308 | * If the loop doesn't get interrupted, RxD always follows TxD and thus a |
| 309 | * loopback jumper has been placed on RxD/TxD, and we will boot |
| 310 | * into DFU unconditionally |
| 311 | */ |
| 312 | int has_loopback_jumper = 1; |
Harald Welte | 27f5fc6 | 2017-11-28 22:15:56 +0100 | [diff] [blame] | 313 | for (i = 0; i < 10; i++) { |
| 314 | /* Set TxD high; abort if RxD doesn't go high either */ |
| 315 | PIO_Set(&uart_loopback_pins[1]); |
Kévin Redon | d24e9bd | 2018-07-07 14:56:43 +0200 | [diff] [blame] | 316 | if (!PIO_Get(&uart_loopback_pins[0])) { |
| 317 | has_loopback_jumper = 0; |
| 318 | break; |
| 319 | } |
Harald Welte | 27f5fc6 | 2017-11-28 22:15:56 +0100 | [diff] [blame] | 320 | /* Set TxD low, abort if RxD doesn't go low either */ |
| 321 | PIO_Clear(&uart_loopback_pins[1]); |
Kévin Redon | d24e9bd | 2018-07-07 14:56:43 +0200 | [diff] [blame] | 322 | if (PIO_Get(&uart_loopback_pins[0])) { |
| 323 | has_loopback_jumper = 0; |
| 324 | break; |
| 325 | } |
Harald Welte | 27f5fc6 | 2017-11-28 22:15:56 +0100 | [diff] [blame] | 326 | } |
Kévin Redon | d24e9bd | 2018-07-07 14:56:43 +0200 | [diff] [blame] | 327 | |
| 328 | /* Put pins back to UART mode */ |
| 329 | const Pin uart_pins[] = {PINS_UART}; |
| 330 | PIO_Configure(uart_pins, PIO_LISTSIZE(uart_pins)); |
| 331 | |
| 332 | return has_loopback_jumper; |
Harald Welte | 27f5fc6 | 2017-11-28 22:15:56 +0100 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | int board_override_enter_dfu(void) |
| 336 | { |
| 337 | /* If the loopback jumper is set, we enter DFU mode */ |
| 338 | if (uart_has_loopback_jumper()) |
| 339 | return 1; |
| 340 | |
| 341 | return 0; |
| 342 | } |