55 bool begin(
const char* productName)
override {
57 product_ = productName;
58 if (!toHost_) toHost_ = kDefaultToHost;
59 if (!fromHost_) fromHost_ = kDefaultFromHost;
60 txPool_ = (Frame*)calloc(toHost_,
sizeof(Frame));
61 rxPool_ = (Frame*)calloc(fromHost_,
sizeof(Frame));
62 if (!txPool_ || !rxPool_) {
63 TWD_LOGGER.error(
"USB: not enough memory for %d + %d frame buffers", toHost_, fromHost_);
66 txPool_ = rxPool_ =
nullptr;
69 queue_init(&txFreeQ_,
sizeof(Frame*), toHost_);
70 queue_init(&txQ_,
sizeof(Frame*), toHost_);
71 queue_init(&rxFreeQ_,
sizeof(Frame*), fromHost_);
72 queue_init(&rxQ_,
sizeof(Frame*), fromHost_);
73 for (
int i = 0; i < toHost_; i++) {
74 Frame* f = &txPool_[i];
76 queue_try_add(&txFreeQ_, &f);
78 for (
int i = 0; i < fromHost_; i++) {
79 Frame* f = &rxPool_[i];
81 queue_try_add(&rxFreeQ_, &f);
83 worker_.user_data =
this;
84 worker_.do_work = &Rp2040UsbNcm::work;
85 if (!async_context_add_when_pending_worker(__getEthernetContext(), &worker_)) {
86 TWD_LOGGER.error(
"USB: could not add the worker to the async context");
91 snprintf(macStr_,
sizeof(macStr_),
"%02X%02X%02X%02X%02X%02X", hostMac_[0], hostMac_[1], hostMac_[2], hostMac_[3],
92 hostMac_[4], hostMac_[5]);
95 epIn_ = USB.registerEndpointIn();
96 epOut_ = USB.registerEndpointOut();
97 epNotif_ = USB.registerEndpointIn();
98 strDesc_ = USB.registerString(product_.c_str());
99 strMac_ = USB.registerString(macStr_);
100 if (!epIn_ || !epOut_ || !epNotif_) {
101 TWD_LOGGER.error(
"USB: no free endpoints for the network interface");
105 id_ = USB.registerInterface(2, &Rp2040UsbNcm::interfaceCb,
this, TUD_CDC_NCM_DESC_LEN, 3, 0);
110 bool ready()
override {
return tud_ready(); }
114 void setHostMac(
const uint8_t mac[6])
override { memcpy(hostMac_, mac, 6); }
120 fromHost_ = fromHost;
124 bool send(
const uint8_t* frame, uint16_t len)
override {
125 if (!tud_ready() || len == 0 || len > kMaxFrame)
return false;
126 Frame* f = allocToHost();
132 memcpy(f->data, frame, len);
133 if (!queue_try_add(&txQ_, &f)) {
146 uint32_t
txQueued() {
return queue_get_level(&txQ_); }
154 Frame* f = (Frame*)ref;
155 memcpy(dst, f->data, f->len);
161 Frame* f = allocFromHost();
162 if (!f || size > kMaxFrame) {
169 memcpy(f->data, src, size);
170 if (!queue_try_add(&rxQ_, &f)) {
183 static constexpr uint16_t kNotifEndpointSize = 64;
184#if defined(PICO_RP2350)
185 static constexpr int kDefaultToHost = 48;
186 static constexpr int kDefaultFromHost = 16;
188 static constexpr int kDefaultToHost = 24;
189 static constexpr int kDefaultFromHost = 8;
191 static constexpr uint16_t kMaxFrame = 1536;
195 uint8_t data[kMaxFrame];
199 FrameReceiver* receiver_ =
nullptr;
200 uint8_t hostMac_[6] = {};
201 char macStr_[13] = {};
202 uint8_t epIn_ = 0, epOut_ = 0, epNotif_ = 0, strDesc_ = 0, strMac_ = 0, id_ = 0;
203 Frame* txPool_ =
nullptr;
204 Frame* rxPool_ =
nullptr;
205 int toHost_ = 0, fromHost_ = 0;
206 queue_t txFreeQ_, rxFreeQ_, rxQ_, txQ_;
207 async_when_pending_worker_t worker_ = {};
208 volatile bool gotRx_ =
false;
211 static Rp2040UsbNcm*& instance() {
212 static Rp2040UsbNcm* p =
nullptr;
216 Frame* allocToHost() {
218 return queue_try_remove(&txFreeQ_, &f) ? f :
nullptr;
220 Frame* allocFromHost() {
222 return queue_try_remove(&rxFreeQ_, &f) ? f :
nullptr;
224 void release(Frame* f) { queue_try_add(f->rx ? &rxFreeQ_ : &txFreeQ_, &f); }
225 void schedule() { async_context_set_work_pending(__getEthernetContext(), &worker_); }
227 static void interfaceCb(
int itf, uint8_t* dst,
int len,
void* param) {
228 Rp2040UsbNcm* s = (Rp2040UsbNcm*)param;
229 uint8_t desc[TUD_CDC_NCM_DESC_LEN] = {
232 TUD_CDC_NCM_DESCRIPTOR((uint8_t)itf, s->strDesc_, s->strMac_, s->epNotif_, kNotifEndpointSize, s->epOut_, s->epIn_,
233 CFG_TUD_NET_ENDPOINT_SIZE, CFG_TUD_NET_MTU)};
234 memcpy(dst, desc, len);
238 static void work(async_context_t*, async_when_pending_worker_t* w) { ((Rp2040UsbNcm*)w->user_data)->pump(w); }
240 void pump(async_when_pending_worker_t* w) {
243 while (queue_try_remove(&rxQ_, &f)) {
244 if (receiver_) receiver_->onFrame(f->data, f->len);
249 if (!mutex_try_enter(&USB.mutex,
nullptr)) {
250 w->work_pending =
true;
253 while (queue_try_peek(&txQ_, &f)) {
255 queue_try_remove(&txQ_, &f);
260 if (!tud_network_can_xmit(f->len))
break;
261 tud_network_xmit(f, 0);
262 queue_try_remove(&txQ_, &f);
270 tud_network_recv_renew();
272 }
while (gotRx_ && --limit > 0);
273 mutex_exit(&USB.mutex);
275 if (!queue_is_empty(&txQ_) || !queue_is_empty(&rxQ_)) w->work_pending =
true;