2021-03-10 02:30:57 +00:00
|
|
|
// Initial TinyUSB RNDIS written by Peter Lawrence.
|
2021-03-11 22:44:49 +00:00
|
|
|
// Modifications were made by Luigi Cruz.
|
|
|
|
|
2021-03-10 02:30:57 +00:00
|
|
|
#include <stdio.h>
|
2021-03-11 22:50:48 +00:00
|
|
|
#include "bsp/board.h"
|
2021-03-10 02:30:57 +00:00
|
|
|
#include "pico/stdlib.h"
|
|
|
|
#include "hardware/adc.h"
|
|
|
|
#include "hardware/irq.h"
|
2021-03-12 01:50:45 +00:00
|
|
|
#include "hardware/gpio.h"
|
2021-03-03 20:10:07 +00:00
|
|
|
|
|
|
|
#include "tusb.h"
|
|
|
|
#include "dhserver.h"
|
|
|
|
#include "dnserver.h"
|
2021-03-11 22:50:48 +00:00
|
|
|
|
2021-03-03 20:10:07 +00:00
|
|
|
#include "lwip/init.h"
|
|
|
|
#include "lwip/timeouts.h"
|
2021-03-04 02:48:48 +00:00
|
|
|
#include "lwip/api.h"
|
|
|
|
#include "lwip/sys.h"
|
2021-03-10 02:30:57 +00:00
|
|
|
#include "lwip/tcp.h"
|
2021-03-04 02:48:48 +00:00
|
|
|
|
2021-03-03 20:10:07 +00:00
|
|
|
/* 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 */
|
|
|
|
const uint8_t tud_network_mac_address[6] = {0x02,0x02,0x84,0x6A,0x96,0x00};
|
|
|
|
|
|
|
|
/* network parameters of this MCU */
|
|
|
|
static const ip_addr_t ipaddr = IPADDR4_INIT_BYTES(192, 168, 7, 1);
|
|
|
|
static const ip_addr_t netmask = IPADDR4_INIT_BYTES(255, 255, 255, 0);
|
|
|
|
static const ip_addr_t gateway = IPADDR4_INIT_BYTES(0, 0, 0, 0);
|
|
|
|
|
|
|
|
/* database IP addresses that can be offered to the host; this must be in RAM to store assigned MAC addresses */
|
2021-03-10 02:30:57 +00:00
|
|
|
static dhcp_entry_t entries[] = {
|
2021-03-03 20:10:07 +00:00
|
|
|
/* mac ip address lease time */
|
|
|
|
{ {0}, IPADDR4_INIT_BYTES(192, 168, 7, 2), 24 * 60 * 60 },
|
|
|
|
{ {0}, IPADDR4_INIT_BYTES(192, 168, 7, 3), 24 * 60 * 60 },
|
|
|
|
{ {0}, IPADDR4_INIT_BYTES(192, 168, 7, 4), 24 * 60 * 60 },
|
|
|
|
};
|
|
|
|
|
2021-03-10 02:30:57 +00:00
|
|
|
static const dhcp_config_t dhcp_config = {
|
2021-03-03 20:10:07 +00:00
|
|
|
.router = IPADDR4_INIT_BYTES(0, 0, 0, 0), /* router address (if any) */
|
|
|
|
.port = 67, /* listen port */
|
|
|
|
.dns = IPADDR4_INIT_BYTES(192, 168, 7, 1), /* dns server (if any) */
|
|
|
|
"usb", /* dns suffix */
|
|
|
|
TU_ARRAY_SIZE(entries), /* num entry */
|
|
|
|
entries /* entries */
|
|
|
|
};
|
2021-03-10 02:30:57 +00:00
|
|
|
static err_t linkoutput_fn(struct netif *netif, struct pbuf *p) {
|
|
|
|
(void)netif;
|
2021-03-03 20:10:07 +00:00
|
|
|
|
2021-03-10 02:30:57 +00:00
|
|
|
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;
|
2021-03-03 20:10:07 +00:00
|
|
|
|
2021-03-10 02:30:57 +00:00
|
|
|
/* if the network driver can accept another packet, we make it happen */
|
|
|
|
if (tud_network_can_xmit()) {
|
|
|
|
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();
|
|
|
|
}
|
2021-03-03 20:10:07 +00:00
|
|
|
}
|
|
|
|
|
2021-03-10 02:30:57 +00:00
|
|
|
static err_t output_fn(struct netif *netif, struct pbuf *p, const ip_addr_t *addr) {
|
|
|
|
return etharp_output(netif, p, addr);
|
2021-03-03 20:10:07 +00:00
|
|
|
}
|
|
|
|
|
2021-03-10 02:30:57 +00:00
|
|
|
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 = output_fn;
|
|
|
|
return ERR_OK;
|
2021-03-03 20:10:07 +00:00
|
|
|
}
|
|
|
|
|
2021-03-10 02:30:57 +00:00
|
|
|
static void init_lwip(void) {
|
|
|
|
struct netif *netif = &netif_data;
|
2021-03-03 20:10:07 +00:00
|
|
|
|
2021-03-10 02:30:57 +00:00
|
|
|
lwip_init();
|
2021-03-03 20:10:07 +00:00
|
|
|
|
2021-03-10 02:30:57 +00:00
|
|
|
/* 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;
|
2021-03-03 20:10:07 +00:00
|
|
|
|
2021-03-10 02:30:57 +00:00
|
|
|
netif = netif_add(netif, &ipaddr, &netmask, &gateway, NULL, netif_init_cb, ip_input);
|
|
|
|
netif_set_default(netif);
|
2021-03-03 20:10:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* handle any DNS requests from dns-server */
|
|
|
|
bool dns_query_proc(const char *name, ip_addr_t *addr)
|
|
|
|
{
|
|
|
|
if (0 == strcmp(name, "tiny.usb"))
|
|
|
|
{
|
|
|
|
*addr = ipaddr;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-03-10 02:30:57 +00:00
|
|
|
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;
|
2021-03-03 20:10:07 +00:00
|
|
|
|
2021-03-10 02:30:57 +00:00
|
|
|
if (size) {
|
|
|
|
struct pbuf *p = pbuf_alloc(PBUF_RAW, size, PBUF_POOL);
|
2021-03-03 20:10:07 +00:00
|
|
|
|
2021-03-10 02:30:57 +00:00
|
|
|
if (p) {
|
|
|
|
/* pbuf_alloc() has already initialized struct; all we need to do is copy the data */
|
|
|
|
memcpy(p->payload, src, size);
|
2021-03-03 20:10:07 +00:00
|
|
|
|
2021-03-10 02:30:57 +00:00
|
|
|
/* store away the pointer for service_traffic() to later handle */
|
|
|
|
received_frame = p;
|
|
|
|
}
|
2021-03-03 20:10:07 +00:00
|
|
|
}
|
|
|
|
|
2021-03-10 02:30:57 +00:00
|
|
|
return true;
|
2021-03-03 20:10:07 +00:00
|
|
|
}
|
|
|
|
|
2021-03-10 02:30:57 +00:00
|
|
|
uint16_t tud_network_xmit_cb(uint8_t *dst, void *ref, uint16_t arg) {
|
|
|
|
struct pbuf *p = (struct pbuf *)ref;
|
|
|
|
struct pbuf *q;
|
|
|
|
uint16_t len = 0;
|
2021-03-03 20:10:07 +00:00
|
|
|
|
2021-03-10 02:30:57 +00:00
|
|
|
(void)arg; /* unused for this example */
|
2021-03-03 20:10:07 +00:00
|
|
|
|
2021-03-10 02:30:57 +00:00
|
|
|
/* traverse the "pbuf chain"; see ./lwip/src/core/pbuf.c for more info */
|
|
|
|
for(q = p; q != NULL; q = q->next) {
|
|
|
|
memcpy(dst, (char *)q->payload, q->len);
|
|
|
|
dst += q->len;
|
|
|
|
len += q->len;
|
|
|
|
if (q->len == q->tot_len) break;
|
|
|
|
}
|
2021-03-03 20:10:07 +00:00
|
|
|
|
2021-03-10 02:30:57 +00:00
|
|
|
return len;
|
2021-03-03 20:10:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void service_traffic(void)
|
|
|
|
{
|
2021-03-10 02:30:57 +00:00
|
|
|
/* 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();
|
|
|
|
}
|
2021-03-03 20:10:07 +00:00
|
|
|
|
2021-03-10 02:30:57 +00:00
|
|
|
sys_check_timeouts();
|
2021-03-03 20:10:07 +00:00
|
|
|
}
|
|
|
|
|
2021-03-10 02:30:57 +00:00
|
|
|
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;
|
|
|
|
}
|
2021-03-03 20:10:07 +00:00
|
|
|
}
|
|
|
|
|
2021-03-10 02:30:57 +00:00
|
|
|
struct tcp_pcb* client;
|
2021-03-11 22:44:49 +00:00
|
|
|
struct repeating_timer timer;
|
2021-03-04 02:48:48 +00:00
|
|
|
|
2021-03-12 01:50:45 +00:00
|
|
|
bool send_timer(struct repeating_timer *t) {
|
|
|
|
const float conversion_factor = 3.3f / (1 << 12);
|
|
|
|
float ADC_voltage = adc_read() * conversion_factor;
|
2021-03-10 02:30:57 +00:00
|
|
|
|
|
|
|
char str[64];
|
2021-03-12 01:50:45 +00:00
|
|
|
int len = sprintf(str, "TEMP: %f °C\n", 27 - (ADC_voltage - 0.706) / 0.001721);
|
2021-03-11 22:44:49 +00:00
|
|
|
tcp_write(client, str, len, 0);
|
|
|
|
tcp_output(client);
|
2021-03-10 02:30:57 +00:00
|
|
|
|
2021-03-12 01:50:45 +00:00
|
|
|
return true;
|
2021-03-10 02:30:57 +00:00
|
|
|
}
|
|
|
|
|
2021-03-11 22:44:49 +00:00
|
|
|
static void srv_close(struct tcp_pcb *pcb){
|
2021-03-12 01:50:45 +00:00
|
|
|
// Cancel send timer.
|
|
|
|
cancel_repeating_timer(&timer);
|
2021-03-11 22:44:49 +00:00
|
|
|
|
2021-03-10 02:30:57 +00:00
|
|
|
tcp_arg(pcb, NULL);
|
|
|
|
tcp_sent(pcb, NULL);
|
|
|
|
tcp_recv(pcb, NULL);
|
|
|
|
tcp_close(pcb);
|
2021-03-11 22:44:49 +00:00
|
|
|
}
|
2021-03-10 02:30:57 +00:00
|
|
|
|
2021-03-11 22:44:49 +00:00
|
|
|
static void srv_err(void *arg, err_t err) {
|
|
|
|
// Probably an indication that the client connection went kaput! Stopping stream...
|
|
|
|
srv_close(client);
|
2021-03-10 02:30:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static err_t srv_receive(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err) {
|
2021-03-11 23:05:30 +00:00
|
|
|
if (err != ERR_OK && p != NULL) {
|
2021-03-10 02:30:57 +00:00
|
|
|
goto exception;
|
2021-03-11 23:05:30 +00:00
|
|
|
}
|
2021-03-10 02:30:57 +00:00
|
|
|
|
|
|
|
tcp_recved(pcb, p->tot_len);
|
|
|
|
tcp_sent(pcb, NULL);
|
|
|
|
|
2021-03-11 22:44:49 +00:00
|
|
|
// The connection is closed if the client sends "X".
|
2021-03-11 23:05:30 +00:00
|
|
|
if (((char*)p->payload)[0] == 'X') {
|
2021-03-11 22:44:49 +00:00
|
|
|
srv_close(pcb);
|
2021-03-11 23:05:30 +00:00
|
|
|
}
|
2021-03-10 02:30:57 +00:00
|
|
|
|
|
|
|
exception:
|
|
|
|
pbuf_free(p);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static err_t srv_accept(void * arg, struct tcp_pcb * pcb, err_t err) {
|
2021-03-11 23:05:30 +00:00
|
|
|
if (err != ERR_OK) {
|
2021-03-11 22:44:49 +00:00
|
|
|
return err;
|
2021-03-11 23:05:30 +00:00
|
|
|
}
|
|
|
|
|
2021-03-11 22:44:49 +00:00
|
|
|
tcp_setprio(pcb, TCP_PRIO_MAX);
|
2021-03-10 02:30:57 +00:00
|
|
|
tcp_recv(pcb, srv_receive);
|
2021-03-11 22:44:49 +00:00
|
|
|
tcp_err(pcb, srv_err);
|
2021-03-10 02:30:57 +00:00
|
|
|
tcp_poll(pcb, NULL, 4);
|
2021-03-11 22:44:49 +00:00
|
|
|
|
|
|
|
client = pcb;
|
|
|
|
|
2021-03-12 01:50:45 +00:00
|
|
|
// Start send timer.
|
|
|
|
add_repeating_timer_ms(50, send_timer, NULL, &timer);
|
2021-03-11 22:44:49 +00:00
|
|
|
|
2021-03-12 01:50:45 +00:00
|
|
|
return err;
|
2021-03-10 02:30:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(void) {
|
2021-03-11 22:44:49 +00:00
|
|
|
// Init network stack.
|
2021-03-12 01:50:45 +00:00
|
|
|
board_init();
|
2021-03-10 02:30:57 +00:00
|
|
|
tusb_init();
|
|
|
|
init_lwip();
|
2021-03-12 01:50:45 +00:00
|
|
|
adc_init();
|
2021-03-10 02:30:57 +00:00
|
|
|
|
|
|
|
// Startup lwIP stack.
|
|
|
|
while (!netif_is_up(&netif_data));
|
|
|
|
while (dhserv_init(&dhcp_config) != ERR_OK);
|
|
|
|
while (dnserv_init(&ipaddr, 53, dns_query_proc) != ERR_OK);
|
|
|
|
|
|
|
|
// Start TCP server.
|
|
|
|
struct tcp_pcb* pcb = tcp_new();
|
|
|
|
pcb->so_options |= SOF_KEEPALIVE;
|
|
|
|
pcb->keep_intvl = 75000000;
|
|
|
|
tcp_bind(pcb, IP_ADDR_ANY, 7777);
|
2021-03-11 22:44:49 +00:00
|
|
|
|
|
|
|
// Start listening for connections.
|
2021-03-10 02:30:57 +00:00
|
|
|
struct tcp_pcb* listen = tcp_listen(pcb);
|
|
|
|
tcp_accept(listen, srv_accept);
|
|
|
|
|
2021-03-12 01:50:45 +00:00
|
|
|
// Start ADC.
|
|
|
|
adc_set_temp_sensor_enabled(true);
|
|
|
|
adc_select_input(4);
|
2021-03-10 02:30:57 +00:00
|
|
|
|
|
|
|
// Listen to events.
|
|
|
|
while (1) {
|
|
|
|
tud_task();
|
|
|
|
service_traffic();
|
|
|
|
}
|
2021-03-03 20:10:07 +00:00
|
|
|
|
2021-03-10 02:30:57 +00:00
|
|
|
return 0;
|
2021-03-11 22:44:49 +00:00
|
|
|
}
|