blob: 880d2d6c04fb05df4904be448deca0daf2013f35 [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"
17#include "led.h"
18#include "misc.h"
19#include "mini-printf.h"
20#include "spi.h"
Sylvain Munautcaf8cf92022-01-12 13:35:12 +010021#include "usb_dev.h"
Sylvain Munaut632a3002020-10-29 13:25:45 +010022#include "usb_e1.h"
Sylvain Munautbc9f5c42020-09-14 10:22:29 +020023#include "utils.h"
24
25
26extern const struct usb_stack_descriptors app_stack_desc;
27
28static void
29serial_no_init()
30{
31 uint8_t buf[8];
32 char *id, *desc;
33 int i;
34
35 flash_manuf_id(buf);
36 printf("Flash Manufacturer : %s\n", hexstr(buf, 3, true));
37
38 flash_unique_id(buf);
39 printf("Flash Unique ID : %s\n", hexstr(buf, 8, true));
40
41 /* Overwrite descriptor string */
42 /* In theory in rodata ... but nothing is ro here */
43 id = hexstr(buf, 8, false);
44 desc = (char*)app_stack_desc.str[1];
45 for (i=0; i<16; i++)
46 desc[2 + (i << 1)] = id[i];
47}
48
49static void
50boot_dfu(void)
51{
52 /* Force re-enumeration */
53 usb_disconnect();
54
55 /* Boot firmware */
Sylvain Munaut46d6b412020-10-29 13:19:05 +010056 reboot(1);
Sylvain Munautbc9f5c42020-09-14 10:22:29 +020057}
58
59void
60usb_dfu_rt_cb_reboot(void)
61{
62 boot_dfu();
63}
64
65
Sylvain Munautbc9f5c42020-09-14 10:22:29 +020066void main()
67{
Sylvain Munautbc9f5c42020-09-14 10:22:29 +020068 int cmd = 0;
69
70 /* Init console IO */
71 console_init();
Sylvain Munaut2c33f6d2022-01-12 14:12:31 +010072 printf("\n\nBooting %s\n", fw_build_str);
Sylvain Munautbc9f5c42020-09-14 10:22:29 +020073
74 /* LED */
75 led_init();
76
77 /* SPI */
78 spi_init();
Sylvain Munaut5e860472020-09-15 22:20:21 +020079 serial_no_init();
80
81 /* Enable LED now that we're done with SPI */
82 e1_led_set(true, 0x00);
Sylvain Munautbc9f5c42020-09-14 10:22:29 +020083
84 /* Setup E1 Vref */
85 int d = 25;
Sylvain Munaut5e860472020-09-15 22:20:21 +020086#if defined(BOARD_ICE1USB_PROTO_ICEBREAKER) || defined(BOARD_ICE1USB_PROTO_BITSY)
Sylvain Munautbc9f5c42020-09-14 10:22:29 +020087 pdm_set(PDM_E1_CT, true, 128, false);
88 pdm_set(PDM_E1_P, true, 128 - d, false);
89 pdm_set(PDM_E1_N, true, 128 + d, false);
Sylvain Munaut5e860472020-09-15 22:20:21 +020090#else
91 pdm_set(PDM_E1_RX0, true, 128 + d, false);
92 pdm_set(PDM_E1_RX1, true, 128 + d, false);
93#endif
Sylvain Munautbc9f5c42020-09-14 10:22:29 +020094
95 /* Setup clock tuning */
96 pdm_set(PDM_CLK_HI, true, 2048, false);
97 pdm_set(PDM_CLK_LO, false, 0, false);
98
99 /* Enable USB directly */
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200100 usb_init(&app_stack_desc);
Sylvain Munautcaf8cf92022-01-12 13:35:12 +0100101 usb_dev_init();
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200102 usb_dfu_rt_init();
103 usb_e1_init();
104
105 /* Start */
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200106 led_state(true);
107 usb_connect();
108
109 /* Main loop */
110 while (1)
111 {
112 /* Prompt ? */
113 if (cmd >= 0)
114 printf("Command> ");
115
116 /* Poll for command */
117 cmd = getchar_nowait();
118
119 if (cmd >= 0) {
Sylvain Munautb4d25792022-01-06 22:04:21 +0100120 if (cmd > 32 && cmd < 127)
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200121 putchar(cmd);
Sylvain Munautb4d25792022-01-06 22:04:21 +0100122 putchar('\r');
123 putchar('\n');
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200124
125 switch (cmd)
126 {
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200127 case 'b':
128 boot_dfu();
129 break;
Sylvain Munautb4d25792022-01-06 22:04:21 +0100130 case 'p':
131 panic("Test panic");
132 break;
133 case 'q':
Sylvain Munaut3da51512022-01-03 22:12:59 +0100134 e1_debug_print(0, false);
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200135 break;
Sylvain Munautb4d25792022-01-06 22:04:21 +0100136 case 'Q':
Sylvain Munaut3da51512022-01-03 22:12:59 +0100137 e1_debug_print(0, true);
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200138 break;
Sylvain Munautb4d25792022-01-06 22:04:21 +0100139 case 'w':
140 e1_debug_print(1, false);
141 break;
142 case 'W':
143 e1_debug_print(1, true);
144 break;
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200145 case 'c':
146 usb_connect();
147 break;
148 case 'd':
149 usb_disconnect();
150 break;
Sylvain Munautb4d25792022-01-06 22:04:21 +0100151 case 'u':
152 usb_debug_print();
153 break;
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200154 default:
155 break;
156 }
157 }
158
159 /* USB poll */
160 usb_poll();
161
162 /* E1 poll */
Sylvain Munautc9c22a62022-01-12 11:47:08 +0100163 usb_e1_poll();
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200164 }
165}