-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathusb.cpp
220 lines (183 loc) · 7.34 KB
/
usb.cpp
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
// Copyright (c) 2017-2023, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md
#include "log.hpp"
#include <module-bsp/bsp/usb/usb.hpp>
extern "C"
{
#include "board.h"
#include "usb_device_config.h"
#include "composite.h"
#include "usb_phy.h"
}
#include <cstring>
namespace bsp
{
namespace
{
usb_device_composite_struct_t *usbDeviceComposite = nullptr;
xQueueHandle USBReceiveQueue;
xQueueHandle USBIrqQueue;
char usbSerialBuffer[constants::serial::bufferLength];
#if USBCDC_ECHO_ENABLED
bool usbCdcEchoEnabled = false;
constexpr std::string_view usbCDCEchoOnCmd("UsbCdcEcho=ON");
constexpr std::string_view usbCDCEchoOffCmd("UsbCdcEcho=OFF");
constexpr auto usbCDCEchoOnCmdLength = usbCDCEchoOnCmd.length();
constexpr auto usbCDCEchoOffCmdLength = usbCDCEchoOffCmd.length();
#endif
TimerHandle_t usbTickTimer;
constexpr auto usbTickTimerName = "usbHWTick";
constexpr auto usbTickTimerInterval = pdMS_TO_TICKS(10);
constexpr auto usbTickTimerCommandTimeout = 500;
void usbOnTickTimer([[maybe_unused]] TimerHandle_t timerHandle)
{
#if (defined(USB_DEVICE_CONFIG_CHARGER_DETECT) && (USB_DEVICE_CONFIG_CHARGER_DETECT > 0U)) && \
(defined(FSL_FEATURE_SOC_USB_ANALOG_COUNT) && (FSL_FEATURE_SOC_USB_ANALOG_COUNT > 0U))
/// NXP low-level code calls for the 1ms tick rate, preferably provided by hardware timer. In this case we
/// are using FreeRTOS software timer configured to 10ms instead. It is still not the best solution, but at
/// least we won't choke the OS by spamming OS with 1ms timer events.
USB_UpdateHwTick();
#endif
}
void usbDeviceStateCB(void *const /*unused*/, const usb_events_t event)
{
USBDeviceStatus notification;
switch (event) {
case USB_EVENT_CONFIGURED:
notification = USBDeviceStatus::Configured;
break;
case USB_EVENT_ATTACHED:
notification = USBDeviceStatus::Connected;
break;
case USB_EVENT_DETACHED:
notification = USBDeviceStatus::Disconnected;
break;
case USB_EVENT_RESET:
notification = USBDeviceStatus::Reset;
break;
case USB_EVENT_DATA_RECEIVED:
notification = USBDeviceStatus::DataReceived;
break;
default:
return;
}
if (0U != __get_IPSR()) {
BaseType_t shouldYield = 0;
xQueueOverwriteFromISR(USBIrqQueue, ¬ification, &shouldYield);
portYIELD_FROM_ISR(shouldYield);
}
else {
xQueueOverwrite(USBIrqQueue, ¬ification);
}
}
} // namespace
int usbInit(const bsp::usbInitParams &initParams)
{
if ((initParams.queueHandle == nullptr) || (initParams.irqQueueHandle == nullptr)) {
log_error("Invalid argument(s): 0x%p/0x%p", initParams.queueHandle, initParams.irqQueueHandle);
return -EINVAL;
}
usbTickTimer = xTimerCreate(usbTickTimerName, usbTickTimerInterval, pdTRUE, nullptr, usbOnTickTimer);
if (usbTickTimer == nullptr) {
return -ENOMEM;
}
USBReceiveQueue = initParams.queueHandle;
USBIrqQueue = initParams.irqQueueHandle;
usbDeviceComposite = composite_init(
usbDeviceStateCB,
initParams.serialNumber.c_str(),
initParams.deviceVersion,
initParams.rootPath.c_str(),
initParams.mtpLockedAtInit
);
if (usbDeviceComposite == nullptr) {
xTimerDelete(usbTickTimer, usbTickTimerCommandTimeout);
return -ENOMEM;
}
xTimerStart(usbTickTimer, usbTickTimerCommandTimeout);
return 0;
}
void usbDeinit()
{
log_debug("usbDeinit");
xTimerStop(usbTickTimer, usbTickTimerCommandTimeout);
xTimerDelete(usbTickTimer, usbTickTimerCommandTimeout);
composite_deinit(usbDeviceComposite);
}
void usbUnlockMTP()
{
#if defined(USB_DEVICE_CONFIG_MTP) && (USB_DEVICE_CONFIG_MTP > 0U)
log_debug("mtpUnlock");
MtpUnlock(&usbDeviceComposite->mtpApp);
#endif
}
ssize_t usbCDCReceive(void *buffer)
{
if (usbDeviceComposite->cdcVcom.inputStream == nullptr) {
return 0;
}
std::memset(buffer, 0, constants::serial::bufferLength);
return VirtualComRecv(&usbDeviceComposite->cdcVcom, buffer, constants::serial::bufferLength);
}
void usbHandleDataReceived()
{
const auto dataReceivedLength = usbCDCReceive(&usbSerialBuffer);
if (dataReceivedLength == 0) {
log_error("No data received!");
return;
}
if (dataReceivedLength < 0) {
log_error("Error in usbCDCReceive, retcode %zd", dataReceivedLength);
return;
}
#if USBCDC_ECHO_ENABLED
bool usbCdcEchoEnabledPrev = usbCdcEchoEnabled;
auto usbEchoCmd = std::string_view{usbSerialBuffer, static_cast<std::size_t>(dataReceivedLength)};
if ((dataReceivedLength == usbCDCEchoOnCmdLength) && (usbCDCEchoOnCmd == usbEchoCmd)) {
usbCdcEchoEnabled = true;
}
else if ((dataReceivedLength == usbCDCEchoOffCmdLength) && (usbCDCEchoOffCmd == usbEchoCmd)) {
usbCdcEchoEnabled = false;
}
if (usbCdcEchoEnabled || usbCdcEchoEnabledPrev) {
usbCDCSendRaw(usbSerialBuffer, dataReceivedLength);
log_debug(
"usbDeviceTask echoed: %d signs: [%s]", static_cast<int>(dataReceivedLength), usbSerialBuffer);
continue;
}
#endif
if (uxQueueSpacesAvailable(USBReceiveQueue) == 0) {
log_error("USB receive queue is full!");
}
auto receiveMessage = new std::string(usbSerialBuffer, dataReceivedLength);
if (xQueueSend(USBReceiveQueue, &receiveMessage, portMAX_DELAY) == errQUEUE_FULL) {
log_error("usbDeviceTask can't send data to receiveQueue");
}
}
std::size_t usbCDCSendRaw(const char *dataPtr, std::size_t dataLen)
{
constexpr std::uint32_t maxRetryDelayMs = 10;
constexpr std::uint32_t maxRetriesCount = 250;
std::uint32_t retriesCounter = 0;
std::size_t dataSent = 0;
do {
const auto bytesSent = VirtualComSend(&usbDeviceComposite->cdcVcom, &dataPtr[dataSent], dataLen - dataSent);
if (bytesSent < 0) {
log_error("VCOM already deinitialized!");
break;
}
if (bytesSent == 0) {
const auto retryDelayMs = (retriesCounter++ % maxRetryDelayMs) + 1;
log_error("VCOM failed to send data, retrying in %lums...", retryDelayMs);
vTaskDelay(retryDelayMs / portTICK_PERIOD_MS);
continue;
}
dataSent += bytesSent;
} while ((dataSent < dataLen) && (retriesCounter < maxRetriesCount));
return dataSent;
}
std::size_t usbCDCSend(std::string *message)
{
return usbCDCSendRaw(message->c_str(), message->length());
}
} // namespace bsp