-
-
Notifications
You must be signed in to change notification settings - Fork 914
Expand file tree
/
Copy pathusb_serial_device_to_host.c
More file actions
172 lines (142 loc) · 5.24 KB
/
Copy pathusb_serial_device_to_host.c
File metadata and controls
172 lines (142 loc) · 5.24 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
/*
* Copyright (C) 2023 Bernd Herzog
*
* This file is part of PortaPack.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/
#include "usb_serial_device_to_host.h"
#include "usb_serial_endpoints.h"
#pragma GCC diagnostic push
// external code, so ignore warnings
#pragma GCC diagnostic ignored "-Wstrict-prototypes"
#include <common/usb.h>
#include <common/usb_request.h>
#include <common/usb_standard_request.h>
#include <hackrf_usb/usb_device.h>
#include <hackrf_usb/usb_endpoint.h>
#pragma GCC diagnostic pop
#include <usb_request.h>
#include <string.h>
SerialUSBDriver SUSBD1;
/* TX buffers handed to the USB DMA engine. They must stay valid until the
* transfer retires, so they cannot live on the caller's stack. Two buffers
* are alternated: with a transfer pool of 1, usb_transfer_schedule() only
* succeeds once the previous transfer has retired, so by the time a buffer
* is refilled the transfer-before-last that used it is guaranteed done. */
static uint8_t tx_buffers[2][USBSERIAL_BUFFERS_SIZE];
static uint8_t tx_buffer_index = 0;
static bool tx_draining = FALSE;
static void onotify(GenericQueue* qp) {
SerialUSBDriver* sdp = chQGetLink(qp);
/* Called with the system locked. Another thread may already be draining
* inside the unlocked window below; its loop will pick up our bytes. */
if (tx_draining)
return;
tx_draining = TRUE;
int n;
while ((n = chOQGetFullI(&sdp->oqueue)) > 0) {
if (n > USBSERIAL_BUFFERS_SIZE) n = USBSERIAL_BUFFERS_SIZE; // don't overflow
uint8_t* buff = tx_buffers[tx_buffer_index];
tx_buffer_index ^= 1;
for (int i = 0; i < n; i++) {
buff[i] = chOQGetI(&sdp->oqueue);
}
int ret;
chSysUnlock();
do {
ret = usb_transfer_schedule(
&usb_endpoint_bulk_in,
&buff[0],
n,
NULL,
NULL);
if (ret == -1)
chThdSleepMilliseconds(1);
} while (ret == -1);
chSysLock();
}
tx_draining = FALSE;
}
static size_t write(void* ip, const uint8_t* bp, size_t n) {
return chOQWriteTimeout(&((SerialUSBDriver*)ip)->oqueue, bp,
n, TIME_INFINITE);
}
static size_t read(void* ip, uint8_t* bp, size_t n) {
return chIQReadTimeout(&((SerialUSBDriver*)ip)->iqueue, bp,
n, TIME_INFINITE);
}
static msg_t put(void* ip, uint8_t b) {
return chOQPutTimeout(&((SerialUSBDriver*)ip)->oqueue, b, TIME_INFINITE);
}
static msg_t get(void* ip) {
return chIQGetTimeout(&((SerialUSBDriver*)ip)->iqueue, TIME_INFINITE);
}
static msg_t putt(void* ip, uint8_t b, systime_t timeout) {
return chOQPutTimeout(&((SerialUSBDriver*)ip)->oqueue, b, timeout);
}
static msg_t gett(void* ip, systime_t timeout) {
return chIQGetTimeout(&((SerialUSBDriver*)ip)->iqueue, timeout);
}
static size_t writet(void* ip, const uint8_t* bp, size_t n, systime_t time) {
return chOQWriteTimeout(&((SerialUSBDriver*)ip)->oqueue, bp, n, time);
}
static size_t readt(void* ip, uint8_t* bp, size_t n, systime_t time) {
return chIQReadTimeout(&((SerialUSBDriver*)ip)->iqueue, bp, n, time);
}
static const struct SerialUSBDriverVMT vmt = {
write, read, put, get,
putt, gett, writet, readt};
void init_serial_usb_driver(SerialUSBDriver* sdp) {
sdp->vmt = &vmt;
chIQInit(&sdp->iqueue, sdp->ib, USBSERIAL_BUFFERS_SIZE, NULL, sdp);
chOQInit(&sdp->oqueue, sdp->ob, USBSERIAL_BUFFERS_SIZE, onotify, sdp);
}
// queue handler from ch
static msg_t qwait(GenericQueue* qp, systime_t time) {
if (TIME_IMMEDIATE == time)
return Q_TIMEOUT;
currp->p_u.wtobjp = qp;
queue_insert(currp, &qp->q_waiting);
return chSchGoSleepTimeoutS(THD_STATE_WTQUEUE, time);
}
// This function fills the output buffer, and sends all data in 1 packet
size_t fillOBuffer(OutputQueue* oqp, const uint8_t* bp, size_t n) {
qnotify_t nfy = oqp->q_notify;
size_t w = 0;
chDbgCheck(n > 0, "chOQWriteTimeout");
chSysLock();
while (TRUE) {
while (chOQIsFullI(oqp)) {
if (qwait((GenericQueue*)oqp, TIME_INFINITE) != Q_OK) {
chSysUnlock();
return w;
}
}
while (!chOQIsFullI(oqp) && n > 0) {
oqp->q_counter--;
*oqp->q_wrptr++ = *bp++;
if (oqp->q_wrptr >= oqp->q_top)
oqp->q_wrptr = oqp->q_buffer;
w++;
--n;
}
if (nfy) nfy(oqp);
chSysUnlock(); /* Gives a preemption chance in a controlled point.*/
if (n == 0) return w;
chSysLock();
}
}