-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
279 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
dependencies: | ||
espressif/esp_tinyusb: "^1.4.2" | ||
idf: "^5.1.0" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
#include "config.h" | ||
#include "net_usb.h" | ||
#include "net_common.h" | ||
|
||
#include <assert.h> | ||
|
||
#include <tinyusb.h> | ||
#include <tinyusb_net.h> | ||
|
||
#include <esp_netif.h> | ||
#include <esp_eth.h> | ||
#include <esp_eth_driver.h> | ||
#include <esp_mac.h> | ||
#include <esp_log.h> | ||
#include <esp_check.h> | ||
#include <esp_event.h> | ||
|
||
|
||
using namespace Hardware; | ||
|
||
namespace | ||
{ | ||
static const char* TAG = "hardware::net::usb"; | ||
|
||
esp_err_t wired_send(void *buffer, uint16_t len, void *buff_free_arg) | ||
{ | ||
return tinyusb_net_send_sync(buffer, len, buff_free_arg, pdMS_TO_TICKS(100)); | ||
} | ||
|
||
static void l2_free(void *h, void *buffer) | ||
{ | ||
free(buffer); | ||
} | ||
|
||
static esp_err_t netif_transmit (void *h, void *buffer, size_t len) | ||
{ | ||
if (wired_send(buffer, len, NULL) != ESP_OK) { | ||
ESP_LOGE(TAG, "Failed to send buffer to USB!"); | ||
} | ||
return ESP_OK; | ||
} | ||
|
||
esp_err_t netif_recv_callback(void *buffer, uint16_t len, void *ctx) | ||
{ | ||
esp_netif_t* netif = reinterpret_cast<esp_netif_t *>(ctx); | ||
if (!netif) { | ||
return ESP_OK; | ||
} | ||
|
||
void *buf_copy = malloc(len); | ||
if (!buf_copy) { | ||
return ESP_ERR_NO_MEM; | ||
} | ||
memcpy(buf_copy, buffer, len); | ||
return esp_netif_receive(netif, buf_copy, len, NULL); | ||
} | ||
} | ||
|
||
esp_err_t NetUsb::Initialize() | ||
{ | ||
const tinyusb_config_t usbCfg = { | ||
.external_phy = false, | ||
}; | ||
esp_err_t err = tinyusb_driver_install(&usbCfg); | ||
ESP_RETURN_ON_ERROR(err, TAG, "install TinyUSB driver"); | ||
|
||
BuildIPInfo(&this->ipInfo, true); | ||
this->netifCfg = { | ||
.flags = static_cast<esp_netif_flags_t>(ESP_NETIF_DHCP_SERVER | ESP_NETIF_FLAG_AUTOUP | ESP_NETIF_FLAG_EVENT_IP_MODIFIED), | ||
.ip_info = &this->ipInfo, | ||
.get_ip_event = IP_EVENT_ETH_GOT_IP, | ||
.lost_ip_event = IP_EVENT_ETH_LOST_IP, | ||
.if_key = "wired", | ||
.if_desc = "usb ncm config device", | ||
}; | ||
PatchNetifCfg(this->netifCfg); | ||
|
||
this->driverCfg = { | ||
.handle = (void *)1, // not using an instance, USB-NCM is a static singleton (must be != NULL) | ||
.transmit = netif_transmit, // point to static Tx function | ||
.driver_free_rx_buffer = l2_free // point to Free Rx buffer function | ||
}; | ||
|
||
this->stackCfg = { | ||
.lwip = { | ||
.init_fn = ethernetif_init, | ||
.input_fn = ethernetif_input | ||
} | ||
}; | ||
|
||
return ESP_OK; | ||
} | ||
|
||
esp_netif_config_t NetUsb::NetifConfig() const | ||
{ | ||
return { | ||
.base = &netifCfg, | ||
.driver = &driverCfg, | ||
.stack = &stackCfg, | ||
}; | ||
} | ||
|
||
esp_err_t NetUsb::Attach(esp_netif_t* netif) | ||
{ | ||
assert(netif); | ||
|
||
tinyusb_net_config_t netCfg = { | ||
.on_recv_callback = netif_recv_callback, | ||
.user_context = netif, | ||
}; | ||
|
||
esp_err_t err = esp_read_mac(netCfg.mac_addr, ESP_MAC_WIFI_SOFTAP); | ||
ESP_RETURN_ON_ERROR(err, TAG, "read softap mac"); | ||
|
||
err = tinyusb_net_init(TINYUSB_USBDEV_0, &netCfg); | ||
ESP_RETURN_ON_ERROR(err, TAG, "initialize USB Net device"); | ||
|
||
err = esp_netif_set_mac(netif, netCfg.mac_addr); | ||
ESP_RETURN_ON_ERROR(err, TAG, "set netif mac"); | ||
|
||
// start the interface manually (as the driver has been started already) | ||
esp_netif_action_start(netif, 0, 0, 0); | ||
|
||
// TODO(buglloc): do smth better plz | ||
ip_event_got_ip_t evt = { | ||
.esp_netif = netif, | ||
.ip_changed = true, | ||
}; | ||
BuildIPInfo(&evt.ip_info, true); | ||
esp_event_post(IP_EVENT, IP_EVENT_ETH_GOT_IP, &evt, sizeof(evt), 0); | ||
|
||
return ESP_OK; | ||
} |
Oops, something went wrong.