blob: 4d75b98ad5d102d94b29c33fa297d61d8d653c32 [file] [log] [blame]
Kévin Redon9a12d682018-07-08 13:21:16 +02001/* sysmocom quad-modem sysmoQMOD application code
2 *
3 * (C) 2016-2017 by Harald Welte <hwelte@hmw-consulting.de>
Kévin Redona634c0e2019-05-23 16:31:58 +02004 * (C) 2018-2019, sysmocom -s.f.m.c. GmbH, Author: Kevin Redon <kredon@sysmocom.de>
Kévin Redon9a12d682018-07-08 13:21:16 +02005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
19 */
Harald Welteaa3b8672017-02-10 19:21:10 +010020#include "board.h"
21#include "simtrace.h"
22#include "utils.h"
Kévin Redon1dbcf622018-09-04 16:15:10 +020023#include "led.h"
Harald Welteaa3b8672017-02-10 19:21:10 +010024#include "wwan_led.h"
25#include "wwan_perst.h"
Harald Welte5c583d32017-05-09 06:46:47 +020026#include "sim_switch.h"
Harald Welteaa3b8672017-02-10 19:21:10 +010027#include "boardver_adc.h"
Harald Welte6d1128e2017-05-05 20:23:10 +020028#include "card_pres.h"
Harald Welte9d90d282018-06-29 22:25:42 +020029#include <osmocom/core/timer.h>
Harald Welte8e7fca32017-05-07 16:14:33 +020030#include "usb_buf.h"
Harald Welteaa3b8672017-02-10 19:21:10 +010031
32static const Pin pin_hubpwr_override = PIN_PRTPWR_OVERRIDE;
33static const Pin pin_hub_rst = {PIO_PA13, PIOA, ID_PIOA, PIO_OUTPUT_0, PIO_DEFAULT};
34static const Pin pin_1234_detect = {PIO_PA14, PIOA, ID_PIOA, PIO_INPUT, PIO_PULLUP};
35static const Pin pin_peer_rst = {PIO_PA0, PIOA, ID_PIOA, PIO_OUTPUT_0, PIO_DEFAULT};
36static const Pin pin_peer_erase = {PIO_PA11, PIOA, ID_PIOA, PIO_OUTPUT_0, PIO_DEFAULT};
37
Harald Welte912b1832017-03-06 09:28:13 +010038/* array of generated USB Strings */
39extern unsigned char *usb_strings[];
40
Harald Welteaa3b8672017-02-10 19:21:10 +010041static int qmod_sam3_is_12(void)
42{
43 if (PIO_Get(&pin_1234_detect) == 0)
44 return 1;
45 else
46 return 0;
47}
48
49const unsigned char __eeprom_bin[256] = {
Harald Weltec6e482d2017-03-05 16:24:29 +010050 USB_VENDOR_OPENMOKO & 0xff,
51 USB_VENDOR_OPENMOKO >> 8,
52 USB_PRODUCT_QMOD_HUB & 0xff,
53 USB_PRODUCT_QMOD_HUB >> 8,
Harald Welte705e8992017-03-05 17:10:26 +010054 0x00, 0x00, 0x9b, 0x20, 0x09, 0x00, 0x00, 0x00, 0x32, 0x32, 0x32, 0x32, /* 0x00 - 0x0f */
Harald Welteaa3b8672017-02-10 19:21:10 +010055 0x32, 0x04, 0x09, 0x18, 0x0d, 0x00, 0x73, 0x00, 0x79, 0x00, 0x73, 0x00, 0x6d, 0x00, 0x6f, 0x00, /* 0x10 - 0x1f */
56 0x63, 0x00, 0x6f, 0x00, 0x6d, 0x00, 0x20, 0x00, 0x2d, 0x00, 0x20, 0x00, 0x73, 0x00, 0x2e, 0x00, /* 0x20 - 0x2f */
57 0x66, 0x00, 0x2e, 0x00, 0x6d, 0x00, 0x2e, 0x00, 0x63, 0x00, 0x2e, 0x00, 0x20, 0x00, 0x47, 0x00, /* 0x30 - 0x3f */
58 0x6d, 0x00, 0x62, 0x00, 0x48, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x40 - 0x4f */
59 0x00, 0x00, 0x00, 0x00, 0x71, 0x00, 0x75, 0x00, 0x61, 0x00, 0x64, 0x00, 0x20, 0x00, 0x6d, 0x00, /* 0x50 - 0x5f */
60 0x6f, 0x00, 0x64, 0x00, 0x65, 0x00, 0x6d, 0x00, 0x20, 0x00, 0x76, 0x00, 0x32, 0x00, 0x00, 0x00, /* 0x60 - 0x6f */
61 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x70 - 0x7f */
62 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x80 - 0x8f */
63 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x90 - 0x9f */
64 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0xa0 - 0xaf */
65 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0xb0 - 0xbf */
66 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0xc0 - 0xcf */
67 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0xd0 - 0xdf */
68 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0xe0 - 0xef */
69 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xA0, 0x56, 0x23, 0x71, 0x04, 0x00, /* 0xf0 - 0xff */
70};
71
72#include "i2c.h"
73static int write_hub_eeprom(void)
74{
Harald Welteaa3b8672017-02-10 19:21:10 +010075 int i;
76
77 /* wait */
Harald Welte0e295982017-03-05 16:48:47 +010078 mdelay(100);
Harald Welteaa3b8672017-02-10 19:21:10 +010079
Harald Welteec0837c2017-03-03 01:33:24 +010080 TRACE_INFO("Writing EEPROM...\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +010081 /* write the EEPROM once */
Kévin Redona71a6f42018-07-24 09:54:12 +020082 for (i = 0; i < ARRAY_SIZE(__eeprom_bin); i++) {
Harald Welteaa3b8672017-02-10 19:21:10 +010083 int rc = eeprom_write_byte(0x50, i, __eeprom_bin[i]);
Kévin Redona71a6f42018-07-24 09:54:12 +020084 if (rc < 0) {
85 TRACE_ERROR("Writing EEPROM failed at byte %u: 0x%02x\n\r",
86 i, __eeprom_bin[i]);
87 return 1;
88 }
Harald Welteaa3b8672017-02-10 19:21:10 +010089 }
90
91 /* then pursue re-reading it again and again */
Harald Welteec0837c2017-03-03 01:33:24 +010092 TRACE_INFO("Verifying EEPROM...\n\r");
Kévin Redona71a6f42018-07-24 09:54:12 +020093 for (i = 0; i < ARRAY_SIZE(__eeprom_bin); i++) {
Harald Welteaa3b8672017-02-10 19:21:10 +010094 int byte = eeprom_read_byte(0x50, i);
Kévin Redona6bd7172018-08-28 19:41:10 +020095 TRACE_DEBUG("0x%02x: %02x\n\r", i, byte);
Harald Welteaa3b8672017-02-10 19:21:10 +010096 if (byte != __eeprom_bin[i])
Harald Welteec0837c2017-03-03 01:33:24 +010097 TRACE_ERROR("Byte %u is wrong, expected 0x%02x, found 0x%02x\n\r",
Harald Welteaa3b8672017-02-10 19:21:10 +010098 i, __eeprom_bin[i], byte);
99 }
Kévin Redona6bd7172018-08-28 19:41:10 +0200100 TRACE_INFO("EEPROM written\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +0100101
102 /* FIXME: Release PIN_PRTPWR_OVERRIDE after we know the hub is
103 * again powering us up */
104
105 return 0;
106}
107
Harald Welte9547e412018-07-03 22:31:16 +0200108static int erase_hub_eeprom(void)
109{
110 int i;
111
112 /* wait */
113 mdelay(100);
114
115 TRACE_INFO("Erasing EEPROM...\n\r");
116 /* write the EEPROM once */
117 for (i = 0; i < 256; i++) {
118 int rc = eeprom_write_byte(0x50, i, 0xff);
Kévin Redonc4285162018-08-07 12:12:47 +0200119 if (rc < 0) {
120 TRACE_ERROR("Erasing EEPROM failed at byte %u: 0x%02x\n\r",
121 i, __eeprom_bin[i]);
122 return 1;
123 }
Harald Welte9547e412018-07-03 22:31:16 +0200124 }
Kévin Redona6bd7172018-08-28 19:41:10 +0200125 TRACE_INFO("EEPROM erased\n\r");
Harald Welte9547e412018-07-03 22:31:16 +0200126
127 return 0;
128}
129
130
Harald Welteed1efc52017-05-07 22:49:45 +0200131static void board_exec_dbg_cmd_st12only(int ch)
Harald Welteaa3b8672017-02-10 19:21:10 +0100132{
133 uint32_t addr, val;
134
Harald Welteed1efc52017-05-07 22:49:45 +0200135 /* functions below only work on primary (ST12) */
136 if (!qmod_sam3_is_12())
137 return;
138
139 switch (ch) {
140 case 'E':
141 write_hub_eeprom();
142 break;
Harald Welte9547e412018-07-03 22:31:16 +0200143 case 'e':
144 erase_hub_eeprom();
145 break;
Harald Welteed1efc52017-05-07 22:49:45 +0200146 case 'O':
147 printf("Setting PRTPWR_OVERRIDE\n\r");
148 PIO_Set(&pin_hubpwr_override);
149 break;
150 case 'o':
151 printf("Clearing PRTPWR_OVERRIDE\n\r");
152 PIO_Clear(&pin_hubpwr_override);
153 break;
154 case 'H':
155 printf("Clearing _HUB_RESET -> HUB_RESET high (inactive)\n\r");
156 PIO_Clear(&pin_hub_rst);
157 break;
158 case 'h':
159 /* high level drives transistor -> HUB_RESET low */
160 printf("Asserting _HUB_RESET -> HUB_RESET low (active)\n\r");
161 PIO_Set(&pin_hub_rst);
162 break;
163 case 'w':
164 if (PIO_GetOutputDataStatus(&pin_hub_rst) == 0)
165 printf("WARNING: attempting EEPROM access while HUB not in reset\n\r");
166 printf("Please enter EEPROM offset:\n\r");
167 UART_GetIntegerMinMax(&addr, 0, 255);
168 printf("Please enter EEPROM value:\n\r");
169 UART_GetIntegerMinMax(&val, 0, 255);
Kévin Redone5f891a2018-09-04 13:31:26 +0200170 printf("Writing value 0x%02lx to EEPROM offset 0x%02lx\n\r", val, addr);
Harald Welteed1efc52017-05-07 22:49:45 +0200171 eeprom_write_byte(0x50, addr, val);
172 break;
173 case 'r':
174 printf("Please enter EEPROM offset:\n\r");
175 UART_GetIntegerMinMax(&addr, 0, 255);
Kévin Redone5f891a2018-09-04 13:31:26 +0200176 printf("EEPROM[0x%02lx] = 0x%02x\n\r", addr, eeprom_read_byte(0x50, addr));
Harald Welteed1efc52017-05-07 22:49:45 +0200177 break;
178 default:
179 printf("Unknown command '%c'\n\r", ch);
180 break;
181 }
182}
183
184/* returns '1' in case we should break any endless loop */
185void board_exec_dbg_cmd(int ch)
186{
Kévin Redon6228d182019-05-23 17:33:45 +0200187#if (ALLOW_PEER_ERASE > 0)
Kévin Redona634c0e2019-05-23 16:31:58 +0200188 /* this variable controls if it is allowed to assert/release the ERASE line.
189 this is done to prevent accidental ERASE on noisy serial input since only one character can trigger the ERASE.
190 */
191 static bool allow_erase = false;
Kévin Redon6228d182019-05-23 17:33:45 +0200192#endif
Kévin Redona634c0e2019-05-23 16:31:58 +0200193
Harald Welteaa3b8672017-02-10 19:21:10 +0100194 switch (ch) {
195 case '?':
Harald Welteec0837c2017-03-03 01:33:24 +0100196 printf("\t?\thelp\n\r");
Harald Welteec0837c2017-03-03 01:33:24 +0100197 printf("\tR\treset SAM3\n\r");
Kévin Redon1dbcf622018-09-04 16:15:10 +0200198 printf("\tl\tswitch off LED 1\n\r");
199 printf("\tL\tswitch off LED 1\n\r");
200 printf("\tg\tswitch off LED 2\n\r");
201 printf("\tG\tswitch off LED 2\n\r");
Harald Welte9164a6d2017-05-05 20:55:21 +0200202 if (qmod_sam3_is_12()) {
203 printf("\tE\tprogram EEPROM\n\r");
Harald Welte9547e412018-07-03 22:31:16 +0200204 printf("\te\tErase EEPROM\n\r");
Harald Welte9164a6d2017-05-05 20:55:21 +0200205 printf("\tO\tEnable PRTPWR_OVERRIDE\n\r");
206 printf("\to\tDisable PRTPWR_OVERRIDE\n\r");
207 printf("\tH\tRelease HUB RESET (high)\n\r");
208 printf("\th\tAssert HUB RESET (low)\n\r");
209 printf("\tw\tWrite single byte in EEPROM\n\r");
210 printf("\tr\tRead single byte from EEPROM\n\r");
211 }
Harald Welteec0837c2017-03-03 01:33:24 +0100212 printf("\tX\tRelease peer SAM3 from reset\n\r");
213 printf("\tx\tAssert peer SAM3 reset\n\r");
Kévin Redon6228d182019-05-23 17:33:45 +0200214#if (ALLOW_PEER_ERASE > 0)
Harald Welteec0837c2017-03-03 01:33:24 +0100215 printf("\tY\tRelease peer SAM3 ERASE signal\n\r");
Kévin Redona634c0e2019-05-23 16:31:58 +0200216 printf("\ta\tAllow asserting peer SAM3 ERASE signal\n\r");
Harald Welteec0837c2017-03-03 01:33:24 +0100217 printf("\ty\tAssert peer SAM3 ERASE signal\n\r");
Kévin Redon6228d182019-05-23 17:33:45 +0200218#endif
Harald Welteec0837c2017-03-03 01:33:24 +0100219 printf("\tU\tProceed to USB Initialization\n\r");
220 printf("\t1\tGenerate 1ms reset pulse on WWAN1\n\r");
221 printf("\t2\tGenerate 1ms reset pulse on WWAN2\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +0100222 break;
Harald Welteaa3b8672017-02-10 19:21:10 +0100223 case 'R':
Harald Welteec0837c2017-03-03 01:33:24 +0100224 printf("Asking NVIC to reset us\n\r");
Harald Weltef415d712017-03-03 01:51:43 +0100225 USBD_Disconnect();
Harald Welteaa3b8672017-02-10 19:21:10 +0100226 NVIC_SystemReset();
227 break;
Kévin Redon1dbcf622018-09-04 16:15:10 +0200228 case 'l':
229 led_blink(LED_GREEN, BLINK_ALWAYS_OFF);
230 printf("LED 1 switched off\n\r");
231 break;
232 case 'L':
233 led_blink(LED_GREEN, BLINK_ALWAYS_ON);
234 printf("LED 1 switched on\n\r");
235 break;
236 case 'g':
237 led_blink(LED_RED, BLINK_ALWAYS_OFF);
238 printf("LED 2 switched off\n\r");
239 break;
240 case 'G':
241 led_blink(LED_RED, BLINK_ALWAYS_ON);
242 printf("LED 2 switched on\n\r");
243 break;
Harald Welte9164a6d2017-05-05 20:55:21 +0200244 case 'X':
245 printf("Clearing _SIMTRACExx_RST -> SIMTRACExx_RST high (inactive)\n\r");
246 PIO_Clear(&pin_peer_rst);
247 break;
248 case 'x':
249 printf("Setting _SIMTRACExx_RST -> SIMTRACExx_RST low (active)\n\r");
250 PIO_Set(&pin_peer_rst);
251 break;
Kévin Redon6228d182019-05-23 17:33:45 +0200252#if (ALLOW_PEER_ERASE > 0)
Harald Welte9164a6d2017-05-05 20:55:21 +0200253 case 'Y':
254 printf("Clearing SIMTRACExx_ERASE (inactive)\n\r");
255 PIO_Clear(&pin_peer_erase);
256 break;
Kévin Redona634c0e2019-05-23 16:31:58 +0200257 case 'a':
258 printf("Asserting SIMTRACExx_ERASE allowed on next command\n\r");
259 allow_erase = true;
260 break;
Harald Welte9164a6d2017-05-05 20:55:21 +0200261 case 'y':
Kévin Redona634c0e2019-05-23 16:31:58 +0200262 if (allow_erase) {
263 printf("Setting SIMTRACExx_ERASE (active)\n\r");
264 PIO_Set(&pin_peer_erase);
265 } else {
266 printf("Please first allow setting SIMTRACExx_ERASE\n\r");
267 }
Harald Welte9164a6d2017-05-05 20:55:21 +0200268 break;
Kévin Redon6228d182019-05-23 17:33:45 +0200269#endif
Harald Welte9164a6d2017-05-05 20:55:21 +0200270 case '1':
271 printf("Resetting Modem 1 (of this SAM3)\n\r");
Harald Welte44622df2017-05-11 00:04:50 +0200272 wwan_perst_do_reset_pulse(0, 300);
Harald Welte9164a6d2017-05-05 20:55:21 +0200273 break;
274 case '2':
275 printf("Resetting Modem 2 (of this SAM3)\n\r");
Harald Welte44622df2017-05-11 00:04:50 +0200276 wwan_perst_do_reset_pulse(1, 300);
Harald Welte9164a6d2017-05-05 20:55:21 +0200277 break;
278 case '!':
Harald Welte44622df2017-05-11 00:04:50 +0200279 sim_switch_use_physical(0, 0);
Harald Welte9164a6d2017-05-05 20:55:21 +0200280 break;
281 case '@':
Harald Welte44622df2017-05-11 00:04:50 +0200282 sim_switch_use_physical(0, 0);
Harald Welte9164a6d2017-05-05 20:55:21 +0200283 break;
284 default:
Harald Welteed1efc52017-05-07 22:49:45 +0200285 if (!qmod_sam3_is_12())
Harald Welte9164a6d2017-05-05 20:55:21 +0200286 printf("Unknown command '%c'\n\r", ch);
Harald Welteed1efc52017-05-07 22:49:45 +0200287 else
288 board_exec_dbg_cmd_st12only(ch);
Harald Welteaa3b8672017-02-10 19:21:10 +0100289 break;
290 }
Kévin Redona634c0e2019-05-23 16:31:58 +0200291
Kévin Redon6228d182019-05-23 17:33:45 +0200292#if (ALLOW_PEER_ERASE > 0)
Kévin Redona634c0e2019-05-23 16:31:58 +0200293 // set protection back so it can only run for one command
294 if ('a' != ch) {
295 allow_erase = false;
296 }
Kévin Redon6228d182019-05-23 17:33:45 +0200297#endif
Harald Welteaa3b8672017-02-10 19:21:10 +0100298}
299
300void board_main_top(void)
301{
Harald Welte25399172017-05-11 00:47:29 +0200302#ifndef APPLICATION_dfu
Harald Welte8e7fca32017-05-07 16:14:33 +0200303 usb_buf_init();
304
Harald Welteaa3b8672017-02-10 19:21:10 +0100305 wwan_led_init();
306 wwan_perst_init();
Harald Welte5c583d32017-05-09 06:46:47 +0200307 sim_switch_init();
Harald Welte25399172017-05-11 00:47:29 +0200308#endif
Harald Welteaa3b8672017-02-10 19:21:10 +0100309
Harald Welte28174982017-05-07 16:45:10 +0200310 /* make sure we can detect whether running in ST12 or ST34 */
Harald Welteaa3b8672017-02-10 19:21:10 +0100311 PIO_Configure(&pin_1234_detect, 1);
Harald Welte28174982017-05-07 16:45:10 +0200312
313 if (qmod_sam3_is_12()) {
314 /* set PIN_PRTPWR_OVERRIDE to output-low to avoid the internal
315 * pull-up on the input to keep SIMTRACE12 alive */
316 PIO_Configure(&pin_hubpwr_override, 1);
317 PIO_Configure(&pin_hub_rst, 1);
318 }
Harald Welteaa3b8672017-02-10 19:21:10 +0100319 PIO_Configure(&pin_peer_rst, 1);
320 PIO_Configure(&pin_peer_erase, 1);
Harald Welte25399172017-05-11 00:47:29 +0200321
322#ifndef APPLICATION_dfu
Harald Welteaa3b8672017-02-10 19:21:10 +0100323 i2c_pin_init();
Harald Welte25399172017-05-11 00:47:29 +0200324#endif
Harald Welteaa3b8672017-02-10 19:21:10 +0100325
326 if (qmod_sam3_is_12()) {
Harald Welteec0837c2017-03-03 01:33:24 +0100327 TRACE_INFO("Detected Quad-Modem ST12\n\r");
Harald Welteaa3b8672017-02-10 19:21:10 +0100328 } else {
Harald Welteec0837c2017-03-03 01:33:24 +0100329 TRACE_INFO("Detected Quad-Modem ST34\n\r");
Harald Welte912b1832017-03-06 09:28:13 +0100330 /* make sure we use the second set of USB Strings
331 * calling the interfaces "Modem 3" and "Modem 4" rather
332 * than 1+2 */
333 usb_strings[7] = usb_strings[9];
334 usb_strings[8] = usb_strings[10];
Harald Welteaa3b8672017-02-10 19:21:10 +0100335 }
336
337 /* Obtain the circuit board version (currently just prints voltage */
338 get_board_version_adc();
Harald Welte25399172017-05-11 00:47:29 +0200339#ifndef APPLICATION_dfu
Harald Welte6d1128e2017-05-05 20:23:10 +0200340 /* Initialize checking for card insert/remove events */
341 card_present_init();
Harald Welte25399172017-05-11 00:47:29 +0200342#endif
Harald Welteaa3b8672017-02-10 19:21:10 +0100343}
Harald Welte27f5fc62017-11-28 22:15:56 +0100344
345static int uart_has_loopback_jumper(void)
346{
347 unsigned int i;
348 const Pin uart_loopback_pins[] = {
349 {PIO_PA9A_URXD0, PIOA, ID_PIOA, PIO_INPUT, PIO_DEFAULT},
350 {PIO_PA10A_UTXD0, PIOA, ID_PIOA, PIO_OUTPUT_0, PIO_DEFAULT}
351 };
352
353 /* Configure UART pins as I/O */
354 PIO_Configure(uart_loopback_pins, PIO_LISTSIZE(uart_loopback_pins));
355
Kévin Redond24e9bd2018-07-07 14:56:43 +0200356 /* Send pattern over UART TX and check if it is received on RX
357 * If the loop doesn't get interrupted, RxD always follows TxD and thus a
358 * loopback jumper has been placed on RxD/TxD, and we will boot
359 * into DFU unconditionally
360 */
361 int has_loopback_jumper = 1;
Harald Welte27f5fc62017-11-28 22:15:56 +0100362 for (i = 0; i < 10; i++) {
363 /* Set TxD high; abort if RxD doesn't go high either */
364 PIO_Set(&uart_loopback_pins[1]);
Kévin Redond24e9bd2018-07-07 14:56:43 +0200365 if (!PIO_Get(&uart_loopback_pins[0])) {
366 has_loopback_jumper = 0;
367 break;
368 }
Harald Welte27f5fc62017-11-28 22:15:56 +0100369 /* Set TxD low, abort if RxD doesn't go low either */
370 PIO_Clear(&uart_loopback_pins[1]);
Kévin Redond24e9bd2018-07-07 14:56:43 +0200371 if (PIO_Get(&uart_loopback_pins[0])) {
372 has_loopback_jumper = 0;
373 break;
374 }
Harald Welte27f5fc62017-11-28 22:15:56 +0100375 }
Kévin Redond24e9bd2018-07-07 14:56:43 +0200376
377 /* Put pins back to UART mode */
378 const Pin uart_pins[] = {PINS_UART};
379 PIO_Configure(uart_pins, PIO_LISTSIZE(uart_pins));
380
381 return has_loopback_jumper;
Harald Welte27f5fc62017-11-28 22:15:56 +0100382}
383
384int board_override_enter_dfu(void)
385{
386 /* If the loopback jumper is set, we enter DFU mode */
387 if (uart_has_loopback_jumper())
388 return 1;
389
390 return 0;
391}