blob: fe1dd01f4b0e2291499e967e79a624742b7afba8 [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 */
56 volatile uint32_t *boot = (void*)0x80000000;
57 *boot = (1 << 2) | (1 << 0);
58}
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();
83
84 /* Setup E1 Vref */
85 int d = 25;
86 pdm_set(PDM_E1_CT, true, 128, false);
87 pdm_set(PDM_E1_P, true, 128 - d, false);
88 pdm_set(PDM_E1_N, true, 128 + d, false);
89
90 /* Setup clock tuning */
91 pdm_set(PDM_CLK_HI, true, 2048, false);
92 pdm_set(PDM_CLK_LO, false, 0, false);
93
94 /* Enable USB directly */
95 serial_no_init();
96 usb_init(&app_stack_desc);
97 usb_dfu_rt_init();
98 usb_e1_init();
99
100 /* Start */
101 e1_init(false); // local tick
102 e1_active = true;
103 led_state(true);
104 usb_connect();
105
106 /* Main loop */
107 while (1)
108 {
109 /* Prompt ? */
110 if (cmd >= 0)
111 printf("Command> ");
112
113 /* Poll for command */
114 cmd = getchar_nowait();
115
116 if (cmd >= 0) {
117 if (cmd > 32 && cmd < 127) {
118 putchar(cmd);
119 putchar('\r');
120 putchar('\n');
121 }
122
123 switch (cmd)
124 {
125 case 'p':
126 usb_debug_print();
127 break;
128 case 'b':
129 boot_dfu();
130 break;
131 case 'o':
132 e1_debug_print(false);
133 break;
134 case 'O':
135 e1_debug_print(true);
136 break;
137 case 't':
138 printf("%08x\n", misc_regs[0]);
139 case 'e':
140 e1_init(true);
141 e1_active = true;
142 led_state(true);
143 break;
144 case 'E':
145 e1_init(false);
146 e1_active = true;
147 led_state(true);
148 break;
149 case 'c':
150 usb_connect();
151 break;
152 case 'd':
153 usb_disconnect();
154 break;
155 default:
156 break;
157 }
158 }
159
160 /* USB poll */
161 usb_poll();
162
163 /* E1 poll */
164 if (e1_active) {
165 e1_poll();
166 usb_e1_run();
167 }
168 }
169}