blob: 5e9812c012b886d6697f1b5c90859f180b356b9c [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 Munaut632a3002020-10-29 13:25:45 +010021#include "usb_e1.h"
Sylvain Munautbc9f5c42020-09-14 10:22:29 +020022#include "utils.h"
23
24
25extern const struct usb_stack_descriptors app_stack_desc;
26
27static void
28serial_no_init()
29{
30 uint8_t buf[8];
31 char *id, *desc;
32 int i;
33
34 flash_manuf_id(buf);
35 printf("Flash Manufacturer : %s\n", hexstr(buf, 3, true));
36
37 flash_unique_id(buf);
38 printf("Flash Unique ID : %s\n", hexstr(buf, 8, true));
39
40 /* Overwrite descriptor string */
41 /* In theory in rodata ... but nothing is ro here */
42 id = hexstr(buf, 8, false);
43 desc = (char*)app_stack_desc.str[1];
44 for (i=0; i<16; i++)
45 desc[2 + (i << 1)] = id[i];
46}
47
48static void
49boot_dfu(void)
50{
51 /* Force re-enumeration */
52 usb_disconnect();
53
54 /* Boot firmware */
Sylvain Munaut46d6b412020-10-29 13:19:05 +010055 reboot(1);
Sylvain Munautbc9f5c42020-09-14 10:22:29 +020056}
57
58void
59usb_dfu_rt_cb_reboot(void)
60{
61 boot_dfu();
62}
63
64
Sylvain Munautbc9f5c42020-09-14 10:22:29 +020065void main()
66{
Sylvain Munautbc9f5c42020-09-14 10:22:29 +020067 int cmd = 0;
68
69 /* Init console IO */
70 console_init();
71 puts("Booting App image..\n");
72
73 /* LED */
74 led_init();
75
76 /* SPI */
77 spi_init();
Sylvain Munaut5e860472020-09-15 22:20:21 +020078 serial_no_init();
79
80 /* Enable LED now that we're done with SPI */
81 e1_led_set(true, 0x00);
Sylvain Munautbc9f5c42020-09-14 10:22:29 +020082
83 /* Setup E1 Vref */
84 int d = 25;
Sylvain Munaut5e860472020-09-15 22:20:21 +020085#if defined(BOARD_ICE1USB_PROTO_ICEBREAKER) || defined(BOARD_ICE1USB_PROTO_BITSY)
Sylvain Munautbc9f5c42020-09-14 10:22:29 +020086 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);
Sylvain Munaut5e860472020-09-15 22:20:21 +020089#else
90 pdm_set(PDM_E1_RX0, true, 128 + d, false);
91 pdm_set(PDM_E1_RX1, true, 128 + d, false);
92#endif
Sylvain Munautbc9f5c42020-09-14 10:22:29 +020093
94 /* Setup clock tuning */
95 pdm_set(PDM_CLK_HI, true, 2048, false);
96 pdm_set(PDM_CLK_LO, false, 0, false);
97
98 /* Enable USB directly */
Sylvain Munautbc9f5c42020-09-14 10:22:29 +020099 usb_init(&app_stack_desc);
100 usb_dfu_rt_init();
101 usb_e1_init();
102
103 /* Start */
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200104 led_state(true);
105 usb_connect();
106
107 /* Main loop */
108 while (1)
109 {
110 /* Prompt ? */
111 if (cmd >= 0)
112 printf("Command> ");
113
114 /* Poll for command */
115 cmd = getchar_nowait();
116
117 if (cmd >= 0) {
Sylvain Munautb4d25792022-01-06 22:04:21 +0100118 if (cmd > 32 && cmd < 127)
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200119 putchar(cmd);
Sylvain Munautb4d25792022-01-06 22:04:21 +0100120 putchar('\r');
121 putchar('\n');
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200122
123 switch (cmd)
124 {
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200125 case 'b':
126 boot_dfu();
127 break;
Sylvain Munautb4d25792022-01-06 22:04:21 +0100128 case 'p':
129 panic("Test panic");
130 break;
131 case 'q':
Sylvain Munaut3da51512022-01-03 22:12:59 +0100132 e1_debug_print(0, false);
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200133 break;
Sylvain Munautb4d25792022-01-06 22:04:21 +0100134 case 'Q':
Sylvain Munaut3da51512022-01-03 22:12:59 +0100135 e1_debug_print(0, true);
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200136 break;
Sylvain Munautb4d25792022-01-06 22:04:21 +0100137 case 'w':
138 e1_debug_print(1, false);
139 break;
140 case 'W':
141 e1_debug_print(1, true);
142 break;
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200143 case 'c':
144 usb_connect();
145 break;
146 case 'd':
147 usb_disconnect();
148 break;
Sylvain Munautb4d25792022-01-06 22:04:21 +0100149 case 'u':
150 usb_debug_print();
151 break;
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200152 default:
153 break;
154 }
155 }
156
157 /* USB poll */
158 usb_poll();
159
160 /* E1 poll */
Sylvain Munaut41c98b62022-01-05 21:11:35 +0100161 for (int port=0; port<2; port++) {
162 e1_poll(port);
163 usb_e1_run(port);
164 }
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200165 }
166}