Skip to content

Commit

Permalink
Add dirty work folder for ongoing USB Ethernet experimentation.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sylwester committed Oct 13, 2024
1 parent 59bc6c9 commit 79b57c2
Show file tree
Hide file tree
Showing 7 changed files with 293 additions and 8 deletions.
6 changes: 5 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ project(main C CXX ASM)

pico_sdk_init()

include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include ${CMAKE_CURRENT_BINARY_DIR} ".")
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include ${CMAKE_CURRENT_SOURCE_DIR}/lib/pico-sdk/lib/tinyusb/lib/networking ${CMAKE_CURRENT_BINARY_DIR} ".")

if (USE_PRECOMPILED_LIBS)
add_library(audio STATIC IMPORTED)
Expand Down Expand Up @@ -68,6 +68,8 @@ add_executable(main
src/picoadk_hw.cpp
src/midi_input_usb.cpp
src/get_serial.c
${CMAKE_CURRENT_SOURCE_DIR}/lib/pico-sdk/lib/tinyusb/lib/networking/dhserver.c
${CMAKE_CURRENT_SOURCE_DIR}/lib/pico-sdk/lib/tinyusb/lib/networking/dnserver.c
# src/psram.cpp
lib/FreeRTOS-Kernel/portable/MemMang/heap_3.c
)
Expand Down Expand Up @@ -106,6 +108,7 @@ if (USE_PRECOMPILED_LIBS)
audio
FreeRTOS-Kernel
heavy
lwip
)
else()
target_link_libraries(main
Expand All @@ -131,6 +134,7 @@ else()
Audio
FreeRTOS-Kernel
heavy
lwip
)
endif()

Expand Down
71 changes: 71 additions & 0 deletions include/lwipopts.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright (c) 2001-2003 Swedish Institute of Computer Science.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
*
* This file is part of the lwIP TCP/IP stack.
*
* Author: Simon Goldschmidt
*
*/
#ifndef __LWIPOPTS_H__
#define __LWIPOPTS_H__

/* Prevent having to link sys_arch.c (we don't test the API layers in unit tests) */
#define NO_SYS 1
#define MEM_ALIGNMENT 4
#define LWIP_RAW 0
#define LWIP_NETCONN 0
#define LWIP_SOCKET 0
#define LWIP_DHCP 0
#define LWIP_ICMP 1
#define LWIP_UDP 1
#define LWIP_TCP 1
#define LWIP_IPV4 1
#define LWIP_IPV6 0
#define ETH_PAD_SIZE 0
#define LWIP_IP_ACCEPT_UDP_PORT(p) ((p) == PP_NTOHS(67))

#define TCP_MSS (1500 /*mtu*/ - 20 /*iphdr*/ - 20 /*tcphhr*/)
#define TCP_SND_BUF (4 * TCP_MSS)
#define TCP_WND (4 * TCP_MSS)

#define ETHARP_SUPPORT_STATIC_ENTRIES 1

#define LWIP_HTTPD_CGI 0
#define LWIP_HTTPD_SSI 0
#define LWIP_HTTPD_SSI_INCLUDE_TAG 0

#define LWIP_SINGLE_NETIF 1

#define PBUF_POOL_SIZE 4

#define HTTPD_USE_CUSTOM_FSDATA 0

#define LWIP_MULTICAST_PING 1
#define LWIP_BROADCAST_PING 1
#define LWIP_IPV6_MLD 0
#define LWIP_IPV6_SEND_ROUTER_SOLICIT 0

#endif /* __LWIPOPTS_H__ */
28 changes: 28 additions & 0 deletions include/tusb_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
#error CFG_TUSB_MCU must be defined
#endif

#define TCP_MSS (1500 /*mtu*/ - 20 /*iphdr*/ - 20 /*tcphhr*/)
#define TCP_SND_BUF (4 * TCP_MSS)
#define TCP_WND (4 * TCP_MSS)

// RHPort number used for device can be defined by board.mk, default to port 0
#ifndef BOARD_DEVICE_RHPORT_NUM
Expand Down Expand Up @@ -98,12 +101,37 @@
#define CFG_TUD_ENDPOINT0_SIZE 64
#endif

//--------------------------------------------------------------------
// NCM CLASS CONFIGURATION, SEE "ncm.h" FOR PERFORMANCE TUNING
//--------------------------------------------------------------------

// Must be >> MTU
// Can be set to 2048 without impact
#define CFG_TUD_NCM_IN_NTB_MAX_SIZE (2 * TCP_MSS + 100)

// Must be >> MTU
// Can be set to smaller values if wNtbOutMaxDatagrams==1
#define CFG_TUD_NCM_OUT_NTB_MAX_SIZE (2 * TCP_MSS + 100)

// Number of NCM transfer blocks for reception side
#ifndef CFG_TUD_NCM_OUT_NTB_N
#define CFG_TUD_NCM_OUT_NTB_N 1
#endif

// Number of NCM transfer blocks for transmission side
#ifndef CFG_TUD_NCM_IN_NTB_N
#define CFG_TUD_NCM_IN_NTB_N 1
#endif


//------------- CLASS -------------//
#define CFG_TUD_CDC 0
#define CFG_TUD_MSC 0
#define CFG_TUD_HID 0
#define CFG_TUD_MIDI 1
#define CFG_TUD_VENDOR 0
#define CFG_TUD_ECM_RNDIS USE_ECM
#define CFG_TUD_NCM (1 - CFG_TUD_ECM_RNDIS)

#define CFG_TUH_MIDI CFG_TUD_MIDI

Expand Down
Binary file added libpico.a
Binary file not shown.
187 changes: 187 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@

#include "arduino_compat.h"

#include <dhserver.h>
#include <dnserver.h>
#include <httpd.h>
#include <lwip/ethip6.h>
#include <lwip/init.h>
#include <lwip/timeouts.h>


#ifdef INCLUDE_IPERF
#include "lwip/apps/lwiperf.h"
#endif

// Audio Buffer (Size is set in lib/audio/include/audio_subsystem.h)
audio_buffer_pool_t *audio_pool;

Expand Down Expand Up @@ -66,6 +78,168 @@ TaskHandle_t usb_task_handle;
extern "C" {
#endif


// IP



#define INIT_IP4(a, b, c, d) \
{ PP_HTONL(LWIP_MAKEU32(a, b, c, d)) }

/* lwip context */
static struct netif netif_data;

/* shared between tud_network_recv_cb() and service_traffic() */
static struct pbuf *received_frame;

/* this is used by this code, ./class/net/net_driver.c, and usb_descriptors.c */
/* ideally speaking, this should be generated from the hardware's unique ID (if available) */
/* it is suggested that the first byte is 0x02 to indicate a link-local address */
uint8_t tud_network_mac_address[6] = {0x02, 0x02, 0x84, 0x6A, 0x96, 0x00};

/* network parameters of this MCU */
static const ip4_addr_t ipaddr = INIT_IP4(192, 168, 7, 1);
static const ip4_addr_t netmask = INIT_IP4(255, 255, 255, 0);
static const ip4_addr_t gateway = INIT_IP4(0, 0, 0, 0);

/* database IP addresses that can be offered to the host; this must be in RAM to store assigned MAC addresses */
static dhcp_entry_t entries[] = {
/* mac ip address lease time */
{{0}, INIT_IP4(192, 168, 7, 2), 24 * 60 * 60},
{{0}, INIT_IP4(192, 168, 7, 3), 24 * 60 * 60},
{{0}, INIT_IP4(192, 168, 7, 4), 24 * 60 * 60},
};

static const dhcp_config_t dhcp_config = {
.router = INIT_IP4(0, 0, 0, 0), /* router address (if any) */
.port = 67, /* listen port */
.dns = INIT_IP4(192, 168, 7, 1), /* dns server (if any) */
"usb", /* dns suffix */
TU_ARRAY_SIZE(entries), /* num entry */
entries /* entries */
};

static err_t linkoutput_fn(struct netif *netif, struct pbuf *p) {
(void) netif;

for (;;) {
/* if TinyUSB isn't ready, we must signal back to lwip that there is nothing we can do */
if (!tud_ready())
return ERR_USE;

/* if the network driver can accept another packet, we make it happen */
if (tud_network_can_xmit(p->tot_len)) {
tud_network_xmit(p, 0 /* unused for this example */);
return ERR_OK;
}

/* transfer execution to TinyUSB in the hopes that it will finish transmitting the prior packet */
tud_task();
}
}

static err_t ip4_output_fn(struct netif *netif, struct pbuf *p, const ip4_addr_t *addr) {
return etharp_output(netif, p, addr);
}

#if LWIP_IPV6
static err_t ip6_output_fn(struct netif *netif, struct pbuf *p, const ip6_addr_t *addr) {
return ethip6_output(netif, p, addr);
}
#endif

static err_t netif_init_cb(struct netif *netif) {
LWIP_ASSERT("netif != NULL", (netif != NULL));
netif->mtu = CFG_TUD_NET_MTU;
netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP | NETIF_FLAG_UP;
netif->state = NULL;
netif->name[0] = 'E';
netif->name[1] = 'X';
netif->linkoutput = linkoutput_fn;
netif->output = ip4_output_fn;
#if LWIP_IPV6
netif->output_ip6 = ip6_output_fn;
#endif
return ERR_OK;
}

static void init_lwip(void) {
struct netif *netif = &netif_data;

lwip_init();

/* the lwip virtual MAC address must be different from the host's; to ensure this, we toggle the LSbit */
netif->hwaddr_len = sizeof(tud_network_mac_address);
memcpy(netif->hwaddr, tud_network_mac_address, sizeof(tud_network_mac_address));
netif->hwaddr[5] ^= 0x01;

netif = netif_add(netif, &ipaddr, &netmask, &gateway, NULL, netif_init_cb, ip_input);
#if LWIP_IPV6
netif_create_ip6_linklocal_address(netif, 1);
#endif
netif_set_default(netif);
}

/* handle any DNS requests from dns-server */
bool dns_query_proc(const char *name, ip4_addr_t *addr) {
if (0 == strcmp(name, "tiny.usb")) {
*addr = ipaddr;
return true;
}
return false;
}

bool tud_network_recv_cb(const uint8_t *src, uint16_t size) {
/* this shouldn't happen, but if we get another packet before
parsing the previous, we must signal our inability to accept it */
if (received_frame) return false;

if (size) {
struct pbuf *p = pbuf_alloc(PBUF_RAW, size, PBUF_POOL);

if (p) {
/* pbuf_alloc() has already initialized struct; all we need to do is copy the data */
memcpy(p->payload, src, size);

/* store away the pointer for service_traffic() to later handle */
received_frame = p;
}
}

return true;
}

uint16_t tud_network_xmit_cb(uint8_t *dst, void *ref, uint16_t arg) {
struct pbuf *p = (struct pbuf *) ref;

(void) arg; /* unused for this example */

return pbuf_copy_partial(p, dst, p->tot_len, 0);
}

static void service_traffic(void) {
/* handle any packet received by tud_network_recv_cb() */
if (received_frame) {
ethernet_input(received_frame, &netif_data);
pbuf_free(received_frame);
received_frame = NULL;
tud_network_recv_renew();
}

sys_check_timeouts();
}

void tud_network_init_cb(void) {
/* if the network is re-initializing and we have a leftover packet, we must do a cleanup */
if (received_frame) {
pbuf_free(received_frame);
received_frame = NULL;
}
}


// IP END

/**
* Task to handle USB MIDI input processing.
*
Expand Down Expand Up @@ -155,9 +329,22 @@ extern "C" {
// Start the FreeRTOS scheduler
vTaskStartScheduler();

/* initialize lwip, dhcp-server, dns-server, and http */
init_lwip();
while (!netif_is_up(&netif_data));
while (dhserv_init(&dhcp_config) != ERR_OK);
while (dnserv_init(IP_ADDR_ANY, 53, dns_query_proc) != ERR_OK);
httpd_init();

#ifdef INCLUDE_IPERF
// test with: iperf -c 192.168.7.1 -e -i 1 -M 5000 -l 8192 -r
lwiperf_start_tcp_server_default(NULL, NULL);
#endif

// Idle loop (this is fine for Cortex-M33)
while (1)
{
service_traffic();
__wfi();
}
}
Expand Down
8 changes: 1 addition & 7 deletions src/picoadk_hw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,7 @@ void picoadk_init()
// Initialize TinyUSB
board_init();

// tusb_init(0, TUSB_ROLE_DEVICE);
#if TUSB_VERSION_NUMBER >= 1700
tusb_init(0, TUSB_ROLE_DEVICE);
#else
tusb_init();
#endif

tusb_init();
stdio_init_all();

// set gpio 25 (soft mute) to output and set to 1 (unmute)
Expand Down
1 change: 1 addition & 0 deletions src/usb_descriptors.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ enum
{
ITF_NUM_MIDI = 0,
ITF_NUM_MIDI_STREAMING,
ITF_NUM_NET,
ITF_NUM_TOTAL
};

Expand Down

0 comments on commit 79b57c2

Please sign in to comment.