blob: c764b2e9e9607ecc5990d060f7e997203990db75 [file] [log] [blame]
Sylvain Munautf5d7bf22020-09-14 10:23:50 +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 "console.h"
16#include "e1.h"
17#include "led.h"
18#include "misc.h"
19#include "mini-printf.h"
20#include "spi.h"
21#include "utils.h"
22
23
24extern const struct usb_stack_descriptors app_stack_desc;
25
26void usb_e1_init(void);
27void usb_e1_run(void);
28
29
30static void
31serial_no_init()
32{
33 uint8_t buf[8];
34 char *id, *desc;
35 int i;
36
37 flash_manuf_id(buf);
38 printf("Flash Manufacturer : %s\n", hexstr(buf, 3, true));
39
40 flash_unique_id(buf);
41 printf("Flash Unique ID : %s\n", hexstr(buf, 8, true));
42
43 /* Overwrite descriptor string */
44 /* In theory in rodata ... but nothing is ro here */
45 id = hexstr(buf, 8, false);
46 desc = (char*)app_stack_desc.str[1];
47 for (i=0; i<16; i++)
48 desc[2 + (i << 1)] = id[i];
49}
50
51static void
52boot_dfu(void)
53{
54 /* Force re-enumeration */
55 usb_disconnect();
56
57 /* Boot firmware */
Sylvain Munaut46d6b412020-10-29 13:19:05 +010058 reboot(1);
Sylvain Munautf5d7bf22020-09-14 10:23:50 +020059}
60
61void
62usb_dfu_rt_cb_reboot(void)
63{
64 boot_dfu();
65}
66
67
68
69static uint8_t
70liu_read_reg(unsigned chan, uint8_t reg)
71{
72 uint8_t cmd = reg | 0x20;
73 uint8_t rv;
74 struct spi_xfer_chunk xfer[2] = {
75 { .data = (void*)&cmd, .len = 1, .read = false, .write = true, },
76 { .data = (void*)&rv, .len = 1, .read = true, .write = false, },
77 };
78 spi_xfer(SPI_CS_LIU(chan), xfer, 2);
79 return rv;
80}
81
82static void
83liu_write_reg(unsigned chan, uint8_t reg, uint8_t val)
84{
85 uint8_t cmd[2] = { reg, val };
86 struct spi_xfer_chunk xfer[2] = {
87 { .data = (void*)cmd, .len = 2, .read = false, .write = true, },
88 };
89 spi_xfer(SPI_CS_LIU(chan), xfer, 1);
90}
91
92
93
94#define USB_RT_LIU_REG_WRITE ((1 << 8) | 0x42)
95#define USB_RT_LIU_REG_READ ((2 << 8) | 0xc2)
96
97
98static bool
99_liu_reg_write_done_cb(struct usb_xfer *xfer)
100{
101 struct usb_ctrl_req *req = xfer->cb_ctx;
102 unsigned chan = req->wIndex - 0x81;
103
104 liu_write_reg(chan, req->wValue, xfer->data[0]);
105
106 return true;
107}
108
109static enum usb_fnd_resp
110_liu_ctrl_req(struct usb_ctrl_req *req, struct usb_xfer *xfer)
111{
112 unsigned chan;
113
114 /* If this a vendor request for the E1 endpoints */
115 if (USB_REQ_RCPT(req) != USB_REQ_RCPT_EP)
116 return USB_FND_CONTINUE;
117
118 if (req->wIndex < 0x81 || req->wIndex > 0x82)
119 return USB_FND_CONTINUE;
120
121 if (USB_REQ_TYPE(req) != USB_REQ_TYPE_VENDOR)
122 return USB_FND_CONTINUE;
123
124 chan = req->wIndex - 0x81;
125
126 /* */
127 switch (req->wRequestAndType)
128 {
129 case USB_RT_LIU_REG_WRITE:
130 xfer->len = 1;
131 xfer->cb_done = _liu_reg_write_done_cb;
132 xfer->cb_ctx = req;
133 break;
134
135 case USB_RT_LIU_REG_READ:
136 xfer->len = 1;
137 xfer->data[0] = liu_read_reg(chan, req->wValue);
138 break;
139
140 default:
141 goto error;
142 }
143
144 return USB_FND_SUCCESS;
145
146error:
147 return USB_FND_ERROR;
148}
149
150static struct usb_fn_drv _liu_drv = {
151 .ctrl_req = _liu_ctrl_req,
152};
153
154
155
156void main()
157{
158 int cmd = 0;
159
160 /* Init console IO */
161 console_init();
162 puts("Booting App image..\n");
163
164 /* LED */
165 led_init();
166 led_state(true);
167
168 /* SPI */
169 spi_init();
170
171 puts("XXX\n");
172
173 /* Enable USB directly */
174 serial_no_init();
175 usb_init(&app_stack_desc);
176 usb_dfu_rt_init();
177 usb_register_function_driver(&_liu_drv);
178 usb_e1_init();
179
180 usb_connect();
181 e1_init();
182
183 /* Main loop */
184 while (1)
185 {
186 /* Prompt ? */
187 if (cmd >= 0)
188 printf("Command> ");
189
190 /* Poll for command */
191 cmd = getchar_nowait();
192
193 if (cmd >= 0) {
194 if (cmd > 32 && cmd < 127) {
195 putchar(cmd);
196 putchar('\r');
197 putchar('\n');
198 }
199
200 switch (cmd)
201 {
202 case 'u':
203 usb_debug_print();
204 break;
205 case 'e':
206 e1_debug_print(false);
207 break;
208 case 'E':
209 e1_debug_print(true);
210 break;
211 default:
212 break;
213 }
214 }
215
216 /* USB poll */
217 usb_poll();
218
219 /* E1 poll */
220 e1_poll();
221 usb_e1_run();
222 }
223}