blob: 3340f4aa84bc2af8e89ff79dc1cac4409cb21625 [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 Munaut3da51512022-01-03 22:12:59 +0100104 e1_init(0, 0, 0);
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200105 led_state(true);
106 usb_connect();
107
108 /* Main loop */
109 while (1)
110 {
111 /* Prompt ? */
112 if (cmd >= 0)
113 printf("Command> ");
114
115 /* Poll for command */
116 cmd = getchar_nowait();
117
118 if (cmd >= 0) {
119 if (cmd > 32 && cmd < 127) {
120 putchar(cmd);
121 putchar('\r');
122 putchar('\n');
123 }
124
125 switch (cmd)
126 {
127 case 'p':
128 usb_debug_print();
129 break;
130 case 'b':
131 boot_dfu();
132 break;
133 case 'o':
Sylvain Munaut3da51512022-01-03 22:12:59 +0100134 e1_debug_print(0, false);
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200135 break;
136 case 'O':
Sylvain Munaut3da51512022-01-03 22:12:59 +0100137 e1_debug_print(0, true);
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200138 break;
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200139 case 'c':
140 usb_connect();
141 break;
142 case 'd':
143 usb_disconnect();
144 break;
145 default:
146 break;
147 }
148 }
149
150 /* USB poll */
151 usb_poll();
152
153 /* E1 poll */
Sylvain Munaut3da51512022-01-03 22:12:59 +0100154 e1_poll(0);
Sylvain Munaut0749d572022-01-03 19:19:21 +0100155 usb_e1_run();
Sylvain Munautbc9f5c42020-09-14 10:22:29 +0200156 }
157}