blob: c1fe295417b6bc47b420b37856cd64bb98c05770 [file] [log] [blame]
Sylvain Munautbc9f5c42020-09-14 10:22:29 +02001/*
2 * fw_app.c
3 *
4 * Copyright (C) 2019-2020 Sylvain Munaut <tnt@246tNt.com>
5 * SPDX-License-Identifier: GPL-3.0-or-later
6 */
7
8#include <stdint.h>
9#include <stdbool.h>
10#include <string.h>
11
12#include <no2usb/usb.h>
13#include <no2usb/usb_dfu_rt.h>
14
Sylvain Munautbc9f5c42020-09-14 10:22:29 +020015#include "console.h"
16#include "e1.h"
Sylvain Munautef5fe382022-01-12 11:55:44 +010017#include "gps.h"
Sylvain Munaute76b6432022-01-12 22:41:01 +010018#include "gpsdo.h"
Sylvain Munautbc9f5c42020-09-14 10:22:29 +020019#include "led.h"
20#include "misc.h"
21#include "mini-printf.h"
22#include "spi.h"
Sylvain Munautcaf8cf92022-01-12 13:35:12 +010023#include "usb_dev.h"
Sylvain Munaut632a3002020-10-29 13:25:45 +010024#include "usb_e1.h"
Sylvain Munaut70c10f02022-01-12 11:58:34 +010025#include "usb_gps.h"
Sylvain Munautbc9f5c42020-09-14 10:22:29 +020026#include "utils.h"
27
28
29extern const struct usb_stack_descriptors app_stack_desc;
30
31static void
32serial_no_init()
33{
34 uint8_t buf[8];
35 char *id, *desc;
36 int i;
37
38 flash_manuf_id(buf);
39 printf("Flash Manufacturer : %s\n", hexstr(buf, 3, true));
40
41 flash_unique_id(buf);
42 printf("Flash Unique ID : %s\n", hexstr(buf, 8, true));
43
44 /* Overwrite descriptor string */
45 /* In theory in rodata ... but nothing is ro here */
46 id = hexstr(buf, 8, false);
47 desc = (char*)app_stack_desc.str[1];
48 for (i=0; i<16; i++)
49 desc[2 + (i << 1)] = id[i];
50}
51
52static void
53boot_dfu(void)
54{
55 /* Force re-enumeration */
56 usb_disconnect();
57
58 /* Boot firmware */
Sylvain Munaut46d6b412020-10-29 13:19:05 +010059 reboot(1);
Sylvain Munautbc9f5c42020-09-14 10:22:29 +020060}
61
62void
63usb_dfu_rt_cb_reboot(void)
64{
65 boot_dfu();
66}
67
68
Sylvain Munautbc9f5c42020-09-14 10:22:29 +020069void main()
70{
Sylvain Munautbc9f5c42020-09-14 10:22:29 +020071 int cmd = 0;
72
73 /* Init console IO */
74 console_init();
Sylvain Munaut2c33f6d2022-01-12 14:12:31 +010075 printf("\n\nBooting %s\n", fw_build_str);
Sylvain Munautbc9f5c42020-09-14 10:22:29 +020076
77 /* LED */
78 led_init();
79
80 /* SPI */
81 spi_init();
Sylvain Munaut5e860472020-09-15 22:20:21 +020082 serial_no_init();
83
84 /* Enable LED now that we're done with SPI */
85 e1_led_set(true, 0x00);
Sylvain Munautbc9f5c42020-09-14 10:22:29 +020086
87 /* Setup E1 Vref */
88 int d = 25;
Sylvain Munaut5e860472020-09-15 22:20:21 +020089#if defined(BOARD_ICE1USB_PROTO_ICEBREAKER) || defined(BOARD_ICE1USB_PROTO_BITSY)
Sylvain Munautbc9f5c42020-09-14 10:22:29 +020090 pdm_set(PDM_E1_CT, true, 128, false);
91 pdm_set(PDM_E1_P, true, 128 - d, false);
92 pdm_set(PDM_E1_N, true, 128 + d, false);
Sylvain Munaut5e860472020-09-15 22:20:21 +020093#else
94 pdm_set(PDM_E1_RX0, true, 128 + d, false);
95 pdm_set(PDM_E1_RX1, true, 128 + d, false);
96#endif
Sylvain Munautbc9f5c42020-09-14 10:22:29 +020097
Sylvain Munautef5fe382022-01-12 11:55:44 +010098 /* GPS init */
99 gps_init();
Sylvain Munaute76b6432022-01-12 22:41:01 +0100100#if defined(BOARD_ICE1USB_PROTO_ICEBREAKER) || defined(BOARD_ICE1USB_PROTO_BITSY)
101 gpsdo_init(VCTXO_TAITIEN_VT40);
102#else
103 gpsdo_init(VCTXO_SITIME_SIT3808_E);
104#endif
Sylvain Munautef5fe382022-01-12 11:55:44 +0100105
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200106 /* Enable USB directly */
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200107 usb_init(&app_stack_desc);
Sylvain Munautcaf8cf92022-01-12 13:35:12 +0100108 usb_dev_init();
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200109 usb_dfu_rt_init();
110 usb_e1_init();
Sylvain Munaut70c10f02022-01-12 11:58:34 +0100111 usb_gps_init();
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200112
113 /* Start */
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200114 led_state(true);
115 usb_connect();
116
117 /* Main loop */
118 while (1)
119 {
120 /* Prompt ? */
121 if (cmd >= 0)
122 printf("Command> ");
123
124 /* Poll for command */
125 cmd = getchar_nowait();
126
127 if (cmd >= 0) {
Sylvain Munautb4d25792022-01-06 22:04:21 +0100128 if (cmd > 32 && cmd < 127)
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200129 putchar(cmd);
Sylvain Munautb4d25792022-01-06 22:04:21 +0100130 putchar('\r');
131 putchar('\n');
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200132
133 switch (cmd)
134 {
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200135 case 'b':
136 boot_dfu();
137 break;
Sylvain Munautb4d25792022-01-06 22:04:21 +0100138 case 'p':
139 panic("Test panic");
140 break;
141 case 'q':
Sylvain Munaut3da51512022-01-03 22:12:59 +0100142 e1_debug_print(0, false);
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200143 break;
Sylvain Munautb4d25792022-01-06 22:04:21 +0100144 case 'Q':
Sylvain Munaut3da51512022-01-03 22:12:59 +0100145 e1_debug_print(0, true);
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200146 break;
Sylvain Munautb4d25792022-01-06 22:04:21 +0100147 case 'w':
148 e1_debug_print(1, false);
149 break;
150 case 'W':
151 e1_debug_print(1, true);
152 break;
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200153 case 'c':
154 usb_connect();
155 break;
156 case 'd':
157 usb_disconnect();
158 break;
Sylvain Munautb4d25792022-01-06 22:04:21 +0100159 case 'u':
160 usb_debug_print();
161 break;
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200162 default:
163 break;
164 }
165 }
166
167 /* USB poll */
168 usb_poll();
169
170 /* E1 poll */
Sylvain Munautc9c22a62022-01-12 11:47:08 +0100171 usb_e1_poll();
Sylvain Munautef5fe382022-01-12 11:55:44 +0100172
173 /* GPS poll */
174 gps_poll();
Sylvain Munaute76b6432022-01-12 22:41:01 +0100175 gpsdo_poll();
Sylvain Munaut70c10f02022-01-12 11:58:34 +0100176 usb_gps_poll();
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200177 }
178}