blob: eebc21f65192d7a1aced4ddb65e691a7e4c1d5e0 [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{
67 bool e1_active = false;
68 int cmd = 0;
69
70 /* Init console IO */
71 console_init();
72 puts("Booting App image..\n");
73
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);
101 usb_dfu_rt_init();
102 usb_e1_init();
103
104 /* Start */
Harald Welte9469e042020-12-15 23:09:40 +0100105 e1_init(0, 0);
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200106 e1_active = true;
107 led_state(true);
108 usb_connect();
109
110 /* Main loop */
111 while (1)
112 {
113 /* Prompt ? */
114 if (cmd >= 0)
115 printf("Command> ");
116
117 /* Poll for command */
118 cmd = getchar_nowait();
119
120 if (cmd >= 0) {
121 if (cmd > 32 && cmd < 127) {
122 putchar(cmd);
123 putchar('\r');
124 putchar('\n');
125 }
126
127 switch (cmd)
128 {
129 case 'p':
130 usb_debug_print();
131 break;
132 case 'b':
133 boot_dfu();
134 break;
135 case 'o':
136 e1_debug_print(false);
137 break;
138 case 'O':
139 e1_debug_print(true);
140 break;
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200141 case 'c':
142 usb_connect();
143 break;
144 case 'd':
145 usb_disconnect();
146 break;
147 default:
148 break;
149 }
150 }
151
152 /* USB poll */
153 usb_poll();
154
155 /* E1 poll */
156 if (e1_active) {
157 e1_poll();
158 usb_e1_run();
159 }
160 }
161}