blob: 882e0ca191925594180c8e7e7d5e97928445ad38 [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
15#include "config.h"
16
17#include "console.h"
18#include "e1.h"
19#include "led.h"
20#include "misc.h"
21#include "mini-printf.h"
22#include "spi.h"
Sylvain Munaut632a3002020-10-29 13:25:45 +010023#include "usb_e1.h"
Sylvain Munautbc9f5c42020-09-14 10:22:29 +020024#include "utils.h"
25
26
27extern const struct usb_stack_descriptors app_stack_desc;
28
29static void
30serial_no_init()
31{
32 uint8_t buf[8];
33 char *id, *desc;
34 int i;
35
36 flash_manuf_id(buf);
37 printf("Flash Manufacturer : %s\n", hexstr(buf, 3, true));
38
39 flash_unique_id(buf);
40 printf("Flash Unique ID : %s\n", hexstr(buf, 8, true));
41
42 /* Overwrite descriptor string */
43 /* In theory in rodata ... but nothing is ro here */
44 id = hexstr(buf, 8, false);
45 desc = (char*)app_stack_desc.str[1];
46 for (i=0; i<16; i++)
47 desc[2 + (i << 1)] = id[i];
48}
49
50static void
51boot_dfu(void)
52{
53 /* Force re-enumeration */
54 usb_disconnect();
55
56 /* Boot firmware */
Sylvain Munaut46d6b412020-10-29 13:19:05 +010057 reboot(1);
Sylvain Munautbc9f5c42020-09-14 10:22:29 +020058}
59
60void
61usb_dfu_rt_cb_reboot(void)
62{
63 boot_dfu();
64}
65
66
67static volatile uint32_t * const misc_regs = (void*)(MISC_BASE);
68
69void main()
70{
71 bool e1_active = false;
72 int cmd = 0;
73
74 /* Init console IO */
75 console_init();
76 puts("Booting App image..\n");
77
78 /* LED */
79 led_init();
80
81 /* SPI */
82 spi_init();
Sylvain Munaut5e860472020-09-15 22:20:21 +020083 serial_no_init();
84
85 /* Enable LED now that we're done with SPI */
86 e1_led_set(true, 0x00);
Sylvain Munautbc9f5c42020-09-14 10:22:29 +020087
88 /* Setup E1 Vref */
89 int d = 25;
Sylvain Munaut5e860472020-09-15 22:20:21 +020090#if defined(BOARD_ICE1USB_PROTO_ICEBREAKER) || defined(BOARD_ICE1USB_PROTO_BITSY)
Sylvain Munautbc9f5c42020-09-14 10:22:29 +020091 pdm_set(PDM_E1_CT, true, 128, false);
92 pdm_set(PDM_E1_P, true, 128 - d, false);
93 pdm_set(PDM_E1_N, true, 128 + d, false);
Sylvain Munaut5e860472020-09-15 22:20:21 +020094#else
95 pdm_set(PDM_E1_RX0, true, 128 + d, false);
96 pdm_set(PDM_E1_RX1, true, 128 + d, false);
97#endif
Sylvain Munautbc9f5c42020-09-14 10:22:29 +020098
99 /* Setup clock tuning */
100 pdm_set(PDM_CLK_HI, true, 2048, false);
101 pdm_set(PDM_CLK_LO, false, 0, false);
102
103 /* Enable USB directly */
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200104 usb_init(&app_stack_desc);
105 usb_dfu_rt_init();
106 usb_e1_init();
107
108 /* Start */
109 e1_init(false); // local tick
110 e1_active = true;
111 led_state(true);
112 usb_connect();
113
114 /* Main loop */
115 while (1)
116 {
117 /* Prompt ? */
118 if (cmd >= 0)
119 printf("Command> ");
120
121 /* Poll for command */
122 cmd = getchar_nowait();
123
124 if (cmd >= 0) {
125 if (cmd > 32 && cmd < 127) {
126 putchar(cmd);
127 putchar('\r');
128 putchar('\n');
129 }
130
131 switch (cmd)
132 {
133 case 'p':
134 usb_debug_print();
135 break;
136 case 'b':
137 boot_dfu();
138 break;
139 case 'o':
140 e1_debug_print(false);
141 break;
142 case 'O':
143 e1_debug_print(true);
144 break;
145 case 't':
146 printf("%08x\n", misc_regs[0]);
147 case 'e':
148 e1_init(true);
149 e1_active = true;
150 led_state(true);
151 break;
152 case 'E':
153 e1_init(false);
154 e1_active = true;
155 led_state(true);
156 break;
157 case 'c':
158 usb_connect();
159 break;
160 case 'd':
161 usb_disconnect();
162 break;
163 default:
164 break;
165 }
166 }
167
168 /* USB poll */
169 usb_poll();
170
171 /* E1 poll */
172 if (e1_active) {
173 e1_poll();
174 usb_e1_run();
175 }
176 }
177}