blob: cb4dbb8dbb158c028c663455a5ab007f5fa2d229 [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"
23#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
66static volatile uint32_t * const misc_regs = (void*)(MISC_BASE);
67
68void main()
69{
70 bool e1_active = false;
71 int cmd = 0;
72
73 /* Init console IO */
74 console_init();
75 puts("Booting App image..\n");
76
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
98 /* Setup clock tuning */
99 pdm_set(PDM_CLK_HI, true, 2048, false);
100 pdm_set(PDM_CLK_LO, false, 0, false);
101
102 /* Enable USB directly */
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200103 usb_init(&app_stack_desc);
104 usb_dfu_rt_init();
105 usb_e1_init();
106
107 /* Start */
108 e1_init(false); // local tick
109 e1_active = true;
110 led_state(true);
111 usb_connect();
112
113 /* Main loop */
114 while (1)
115 {
116 /* Prompt ? */
117 if (cmd >= 0)
118 printf("Command> ");
119
120 /* Poll for command */
121 cmd = getchar_nowait();
122
123 if (cmd >= 0) {
124 if (cmd > 32 && cmd < 127) {
125 putchar(cmd);
126 putchar('\r');
127 putchar('\n');
128 }
129
130 switch (cmd)
131 {
132 case 'p':
133 usb_debug_print();
134 break;
135 case 'b':
136 boot_dfu();
137 break;
138 case 'o':
139 e1_debug_print(false);
140 break;
141 case 'O':
142 e1_debug_print(true);
143 break;
144 case 't':
145 printf("%08x\n", misc_regs[0]);
146 case 'e':
147 e1_init(true);
148 e1_active = true;
149 led_state(true);
150 break;
151 case 'E':
152 e1_init(false);
153 e1_active = true;
154 led_state(true);
155 break;
156 case 'c':
157 usb_connect();
158 break;
159 case 'd':
160 usb_disconnect();
161 break;
162 default:
163 break;
164 }
165 }
166
167 /* USB poll */
168 usb_poll();
169
170 /* E1 poll */
171 if (e1_active) {
172 e1_poll();
173 usb_e1_run();
174 }
175 }
176}