-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdev_conf.c
More file actions
387 lines (340 loc) · 10.8 KB
/
Copy pathdev_conf.c
File metadata and controls
387 lines (340 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/*
** Filename: dev_conf.c
** Author: Ondrej
**
** Handle device configuration and low-level access
*/
#include "dev_conf.h"
#include "usb_handler.h"
/* Device globals */
VOS_HANDLE bus_spi = NULL;
VOS_HANDLE payload_spi = NULL;
VOS_HANDLE uart = NULL;
VOS_HANDLE usb = NULL;
VOS_HANDLE boms = NULL;
/* Configure IOMUX connections */
void dev_conf_iomux()
{
vos_iomux_define_input(15, IOMUX_IN_SPI_SLAVE_0_CLK);
vos_iomux_define_input(16, IOMUX_IN_SPI_SLAVE_0_MOSI);
vos_iomux_define_output(18, IOMUX_OUT_SPI_SLAVE_0_MISO);
vos_iomux_define_input(19, IOMUX_IN_SPI_SLAVE_0_CS);
vos_iomux_define_input(20, IOMUX_IN_SPI_SLAVE_1_CLK);
vos_iomux_define_input(21, IOMUX_IN_SPI_SLAVE_1_MOSI);
vos_iomux_define_output(22, IOMUX_OUT_SPI_SLAVE_1_MISO);
vos_iomux_define_input(23, IOMUX_IN_SPI_SLAVE_1_CS);
vos_iomux_define_output(31, IOMUX_OUT_UART_TXD);
vos_iomux_define_input(32, IOMUX_IN_UART_RXD);
vos_iomux_define_output(33, IOMUX_OUT_GPIO_PORT_A_2);
vos_iomux_define_bidi(34, IOMUX_IN_GPIO_PORT_A_7, IOMUX_OUT_GPIO_PORT_A_7);
vos_iomux_define_output(45, IOMUX_OUT_GPIO_PORT_A_4);
}
/* Configure SPI slave */
void dev_conf_spi(VOS_HANDLE spi, uint8 polarity, uint8 phase)
{
common_ioctl_cb_t iocb;
iocb.ioctl_code = VOS_IOCTL_SPI_SLAVE_SET_MODE;
iocb.set.param = SPI_SLAVE_MODE_UNMANAGED;
vos_dev_ioctl(spi, &iocb);
iocb.ioctl_code = VOS_IOCTL_SPI_SLAVE_SCK_CPHA;
iocb.set.param = phase;
vos_dev_ioctl(spi, &iocb);
iocb.ioctl_code = VOS_IOCTL_SPI_SLAVE_SCK_CPOL;
iocb.set.param = polarity;
vos_dev_ioctl(spi, &iocb);
}
/* Configure UART */
uint8 dev_uart_start()
{
uart_context_t uart_conf;
common_ioctl_cb_t iocb;
if (uart == NULL)
{
uart_conf.buffer_size = VOS_BUFFER_SIZE_512_BYTES;
if (uart_init(VOS_DEV_UART, &uart_conf) == 0)
{
uart = vos_dev_open(VOS_DEV_UART);
if (uart)
{
iocb.ioctl_code = VOS_IOCTL_UART_SET_BAUD_RATE;
iocb.set.uart_baud_rate = 921600;
vos_dev_ioctl(uart, &iocb);
iocb.ioctl_code = VOS_IOCTL_UART_SET_FLOW_CONTROL;
iocb.set.param = UART_FLOW_NONE;
vos_dev_ioctl(uart, &iocb);
dev_dma_acquire(uart);
}
}
}
return (uart != NULL);
}
/* Prepare the USB stack */
uint8 dev_usb_start()
{
usbhost_context_t usb_conf;
if (usb == NULL)
{
usb_conf.if_count = 2;
usb_conf.ep_count = 4;
usb_conf.xfer_count = 2;
usb_conf.iso_xfer_count = 0;
if (usbhost_init(VOS_DEV_USBHOST_1, -1, &usb_conf) == 0)
{
usb = vos_dev_open(VOS_DEV_USBHOST_1);
}
}
return (usb != NULL);
}
/* Get USB port status */
uint8 dev_usb_status()
{
usbhost_ioctl_cb_t iocb;
uint8 state = PORT_STATE_DISCONNECTED;
if (usb != NULL)
{
iocb.ioctl_code = VOS_IOCTL_USBHOST_GET_CONNECT_STATE;
iocb.get = &state;
vos_dev_ioctl(usb, &iocb);
}
return state;
}
/* Reset a halted endpoint */
void dev_usb_reset_ep(usbhost_ep_handle ep)
{
uint8 status = 0;
usbhost_ioctl_cb_t iocb;
iocb.handle.ep = ep;
iocb.ioctl_code = VOS_IOCTL_USBHOST_DEVICE_CLEAR_ENDPOINT_TRANSFER;
status = vos_dev_ioctl(usb, &iocb);
iocb.ioctl_code = VOS_IOCTL_USBHOST_DEVICE_CLEAR_ENDPOINT_CARRY;
status = vos_dev_ioctl(usb, &iocb);
iocb.ioctl_code = VOS_IOCTL_USBHOST_DEVICE_CLEAR_HOST_HALT;
status = vos_dev_ioctl(usb, &iocb);
}
/* Wait for USB enumeration until timeout */
uint8 dev_usb_wait(uint32 timeout_ms)
{
uint8 state = PORT_STATE_DISCONNECTED;
usbhost_ioctl_cb_t iocb;
iocb.get = &state;
/* Poll state until timeout */
for(; timeout_ms > 250; timeout_ms -= 250)
{
iocb.ioctl_code = VOS_IOCTL_USBHOST_GET_CONNECT_STATE;
vos_dev_ioctl(usb, &iocb);
if (state == PORT_STATE_ENUMERATED)
{
iocb.ioctl_code = VOS_IOCTL_USBHOST_DEVICE_GET_COUNT;
vos_dev_ioctl(usb, &iocb);
if (state > 0)
{
return state;
}
}
vos_delay_msecs(250);
}
return FALSE;
}
/* Acquire a USB boot device */
uint8 dev_usb_boot_acquire(dev_usb_boot_t *dev)
{
uint8 status = 0, success = FALSE;
usbhost_ioctl_cb_t iocb;
usb_deviceRequest_t request;
usb_deviceDescriptor_t descriptor;
usbhost_device_handle interface = NULL;
usbhost_ep_handle endpoint = NULL;
usbhost_ioctl_cb_vid_pid_t vid_pid = { USB_VID, USB_PID };
/* Prepare descriptor request */
vos_memset(dev, 0, sizeof(dev_usb_boot_t));
vos_memset(&descriptor, 0, sizeof(usb_deviceDescriptor_t));
request.bRequest = USB_REQUEST_CODE_GET_DESCRIPTOR;
request.bmRequestType = USB_BMREQUESTTYPE_DEV_TO_HOST |
USB_BMREQUESTTYPE_STANDARD |
USB_BMREQUESTTYPE_DEVICE;
request.wValue = (USB_DESCRIPTOR_TYPE_DEVICE << 8) | 0x00;
request.wIndex = 0x0000;
request.wLength = 0x0012;
/* Get interface via VID/PID */
iocb.ioctl_code = VOS_IOCTL_USBHOST_DEVICE_FIND_HANDLE_BY_VID_PID;
iocb.handle.dif = NULL;
iocb.set = &vid_pid;
iocb.get = &interface;
status = vos_dev_ioctl(usb, &iocb);
status = (status == USBHOST_OK && interface != NULL);
/* Acquire control endpoint */
if (status)
{
iocb.ioctl_code = VOS_IOCTL_USBHOST_DEVICE_GET_CONTROL_ENDPOINT_HANDLE;
iocb.handle.dif = interface;
iocb.set = NULL;
iocb.get = &dev->ctrl;
status = vos_dev_ioctl(usb, &iocb);
status = (status == USBHOST_OK && dev->ctrl != NULL);
}
/* Save the serial number from the device descriptor */
if (status)
{
iocb.ioctl_code = VOS_IOCTL_USBHOST_DEVICE_SETUP_TRANSFER;
iocb.handle.ep = dev->ctrl;
iocb.set = &request;
iocb.get = &descriptor;
status = vos_dev_ioctl(usb, &iocb);
status = (status == USBHOST_OK);
dev->sn = descriptor.iSerialNumber;
}
/* Acquire bulk-out endpoint */
if (status)
{
iocb.ioctl_code = VOS_IOCTL_USBHOST_DEVICE_GET_BULK_OUT_ENDPOINT_HANDLE;
iocb.handle.dif = interface;
iocb.set = NULL;
iocb.get = &dev->bulk;
status = vos_dev_ioctl(usb, &iocb);
status = (status == USBHOST_OK && dev->bulk != NULL);
success = status;
}
/* Try to acquire the second bulk-out endpoint.
** For some reason, if there are two, the first one doesn't work.
** Could be related to the comment in Initialize_Device:
** https://github.com/raspberrypi/usbboot/blob/master/main.c */
if (status)
{
iocb.ioctl_code = VOS_IOCTL_USBHOST_DEVICE_GET_NEXT_ENDPOINT_HANDLE;
iocb.handle.ep = dev->bulk;
iocb.get = &endpoint;
if (vos_dev_ioctl(usb, &iocb) == USBHOST_OK && endpoint != NULL)
{
dev->bulk = endpoint;
}
}
return success;
}
/* Acquire the EMMC mass storage device */
uint8 dev_usb_boms_acquire()
{
uint8 status = 0;
uint16 sector_size = 0;
usbhost_ioctl_cb_t iocb;
usbhost_device_handle interface = NULL;
usbhost_ioctl_cb_class_t class;
msi_ioctl_cb_t boms_iocb;
boms_ioctl_cb_attach_t boms_att;
/* Find BOMS class device interface */
class.dev_class = USB_CLASS_MASS_STORAGE;
class.dev_subclass = USB_SUBCLASS_MASS_STORAGE_SCSI;
class.dev_protocol = USB_PROTOCOL_MASS_STORAGE_BOMS;
iocb.ioctl_code = VOS_IOCTL_USBHOST_DEVICE_FIND_HANDLE_BY_CLASS;
iocb.handle.dif = NULL;
iocb.set = &class;
iocb.get = &interface;
status = vos_dev_ioctl(usb, &iocb);
status = (status == USBHOST_OK && interface != NULL);
/* Initialize the BOMS driver if needed */
if (status && boms == NULL)
{
if (boms_init(VOS_DEV_BOMS_DRV) == 0)
{
boms = vos_dev_open(VOS_DEV_BOMS_DRV);
}
status = (boms != NULL);
}
/* Attach the BOMS driver to the interface */
if (status)
{
boms_att.hc_handle = usb;
boms_att.ifDev = interface;
boms_iocb.ioctl_code = MSI_IOCTL_BOMS_ATTACH;
boms_iocb.set = &boms_att;
boms_iocb.get = NULL;
status = vos_dev_ioctl(boms, &boms_iocb);
status = (status == MSI_OK);
}
/* Verify sector size */
if (status)
{
boms_iocb.ioctl_code = MSI_IOCTL_GET_SECTOR_SIZE;
boms_iocb.set = NULL;
boms_iocb.get = §or_size;
status = vos_dev_ioctl(boms, &boms_iocb);
status = (status == MSI_OK && sector_size == USB_SECTOR_LEN);
}
return status;
}
/* Close and clean up auxiliary drivers after reprogramming */
void dev_reprogramming_cleanup()
{
msi_ioctl_cb_t boms_iocb;
if (boms != NULL)
{
boms_iocb.ioctl_code = MSI_IOCTL_BOMS_DETACH;
vos_dev_ioctl(boms, &boms_iocb);
vos_dev_close(boms);
boms = NULL;
}
if (usb != NULL)
{
vos_dev_close(usb);
usb = NULL;
}
if (uart != NULL)
{
dev_dma_release(uart);
vos_dev_close(uart);
uart = NULL;
}
}
/* Reset RPi CPU */
void dev_rpi_common_reset()
{
/* Configure pin as output */
vos_gpio_set_pin_mode(GPIO_RPI_RESET, 1);
/* Run reset sequence */
vos_gpio_write_pin(GPIO_RPI_RESET, GPIO_RPI_RESET_INACTIVE);
vos_delay_msecs(100);
vos_gpio_write_pin(GPIO_RPI_RESET, GPIO_RPI_RESET_ACTIVE);
vos_delay_msecs(100);
vos_gpio_write_pin(GPIO_RPI_RESET, GPIO_RPI_RESET_INACTIVE);
}
/* Reset RPi CPU into USB boot mode
** Needs to be reset twice with about 2 sec delay for some reason
** Afterward, the USB enumeration takes about 8-10 sec */
void dev_rpi_bootloader_reset()
{
/* Configure pin as output */
vos_gpio_set_pin_mode(GPIO_RPI_RESET, 1);
/* Run reset sequence */
vos_gpio_write_pin(GPIO_RPI_RESET, GPIO_RPI_RESET_INACTIVE);
vos_delay_msecs(100);
vos_gpio_write_pin(GPIO_RPI_RESET, GPIO_RPI_RESET_ACTIVE);
vos_delay_msecs(1);
vos_gpio_write_pin(GPIO_RPI_RESET, GPIO_RPI_RESET_INACTIVE);
vos_delay_msecs(2000);
vos_gpio_write_pin(GPIO_RPI_RESET, GPIO_RPI_RESET_ACTIVE);
vos_delay_msecs(1);
vos_gpio_write_pin(GPIO_RPI_RESET, GPIO_RPI_RESET_INACTIVE);
}
/* Check status of the Rx queue */
uint16 dev_rx_avail(VOS_HANDLE dev)
{
common_ioctl_cb_t iocb;
iocb.ioctl_code = VOS_IOCTL_COMMON_GET_RX_QUEUE_STATUS;
vos_dev_ioctl(dev, &iocb);
return iocb.get.queue_stat;
}
/* Acquire exclusive DMA access for device
** Decreases overhead for read/write operations */
void dev_dma_acquire(VOS_HANDLE dev)
{
common_ioctl_cb_t iocb;
iocb.ioctl_code = VOS_IOCTL_COMMON_ENABLE_DMA;
iocb.set.param = DMA_ACQUIRE_AND_RETAIN;
vos_dev_ioctl(dev, &iocb);
}
/* Release exclusive DMA access for device */
void dev_dma_release(VOS_HANDLE dev)
{
common_ioctl_cb_t iocb;
iocb.ioctl_code = VOS_IOCTL_COMMON_DISABLE_DMA;
vos_dev_ioctl(dev, &iocb);
}