-
Notifications
You must be signed in to change notification settings - Fork 6
/
lwip.cpp
169 lines (140 loc) · 4.47 KB
/
lwip.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
#include "lwip/inet.h"
#include "lwip/tcp.h"
#include "lwip/netif.h"
#include "lwip/init.h"
#include "lwip/stats.h"
#include "lwip/dhcp.h"
#include "lwip/timeouts.h"
#include "netif/etharp.h"
#include <cstring>
#include <cstdio>
#include <cstddef>
#include "pico/stdlib.h"
#include "hardware/spi.h"
extern "C"{
#include "enc28j60.h"
}
// SPI Defines
// We are going to use SPI 0, and allocate it to the following GPIO pins
// Pins can be changed, see the GPIO function select table in the datasheet for information on GPIO assignments
#define SPI_PORT spi0
#define PIN_MISO 16
#define PIN_CS 17
#define PIN_SCK 18
#define PIN_MOSI 19
// based on example from: https://www.nongnu.org/lwip/2_0_x/group__lwip__nosys.html
#define ETHERNET_MTU 1500
uint8_t mac[6] = {0xAA, 0x6F, 0x77, 0x47, 0x75, 0x8C};
static err_t netif_output(struct netif *netif, struct pbuf *p)
{
LINK_STATS_INC(link.xmit);
// lock_interrupts();
// pbuf_copy_partial(p, mac_send_buffer, p->tot_len, 0);
/* Start MAC transmit here */
printf("enc28j60: Sending packet of len %d\n", p->len);
enc28j60PacketSend(p->len, (uint8_t *)p->payload);
// pbuf_free(p);
// error sending
if (enc28j60Read(ESTAT) & ESTAT_TXABRT)
{
// a seven-byte transmit status vector will be
// written to the location pointed to by ETXND + 1,
printf("ERR - transmit aborted\n");
}
if (enc28j60Read(EIR) & EIR_TXERIF)
{
printf("ERR - transmit interrupt flag set\n");
}
// unlock_interrupts();
return ERR_OK;
}
static void netif_status_callback(struct netif *netif)
{
printf("netif status changed %s\n", ip4addr_ntoa(netif_ip4_addr(netif)));
}
static err_t netif_initialize(struct netif *netif)
{
enc28j60Init(mac);
netif->linkoutput = netif_output;
netif->output = etharp_output;
// netif->output_ip6 = ethip6_output;
netif->mtu = ETHERNET_MTU;
netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_ETHERNET | NETIF_FLAG_IGMP | NETIF_FLAG_MLD6;
// MIB2_INIT_NETIF(netif, snmp_ifType_ethernet_csmacd, 100000000);
SMEMCPY(netif->hwaddr, mac, sizeof(netif->hwaddr));
netif->hwaddr_len = sizeof(netif->hwaddr);
return ERR_OK;
}
int main(void)
{
stdio_init_all();
// data sheet up to 20 mhz
spi_init(SPI_PORT, 20*1000*1000);
gpio_set_function(PIN_MISO, GPIO_FUNC_SPI);
gpio_set_function(PIN_CS, GPIO_FUNC_SIO);
gpio_set_function(PIN_SCK, GPIO_FUNC_SPI);
gpio_set_function(PIN_MOSI, GPIO_FUNC_SPI);
// Chip select is active-low, so we'll initialise it to a driven-high state
gpio_set_dir(PIN_CS, GPIO_OUT);
gpio_put(PIN_CS, 1);
// END PICO INIT
for (int i = 10; i > 0; i--)
{
printf("Sleeping for %d seconds...\n", i);
sleep_ms(1000);
}
ip_addr_t addr, mask, static_ip;
IP4_ADDR(&static_ip, 192, 168, 20, 111);
IP4_ADDR(&mask, 255, 255, 255, 0);
IP4_ADDR(&addr, 192, 168, 20, 1);
struct netif netif;
lwip_init();
// IP4_ADDR_ANY if using DHCP client
netif_add(&netif, IP4_ADDR_ANY,IP4_ADDR_ANY,IP4_ADDR_ANY, NULL, netif_initialize, netif_input);
netif.name[0] = 'e';
netif.name[1] = '0';
// netif_create_ip6_linklocal_address(&netif, 1);
// netif.ip6_autoconfig_enabled = 1;
netif_set_status_callback(&netif, netif_status_callback);
netif_set_default(&netif);
netif_set_up(&netif);
//dhcp_inform(&netif);
dhcp_start(&netif);
std::byte eth_pkt[ETHERNET_MTU];
struct pbuf *p = NULL;
netif_set_link_up(&netif);
/*
struct tcp_pcb * pcb=tcp_new();
tcp_bind(pcb,&static_ip,1234);
pcb=tcp_listen(pcb);
*/
while (1)
{
uint16_t packet_len = enc28j60PacketReceive(ETHERNET_MTU, (uint8_t *)eth_pkt);
if (packet_len)
{
printf("enc: Received packet of length = %d\n", packet_len);
p = pbuf_alloc(PBUF_RAW, packet_len, PBUF_POOL);
pbuf_take(p, eth_pkt, packet_len);
//free(eth_pkt);
//eth_pkt = malloc(ETHERNET_MTU);
}
else
{
// printf("enc: no packet received\n");
}
if (packet_len && p != NULL)
{
LINK_STATS_INC(link.recv);
if (netif.input(p, &netif) != ERR_OK)
{
pbuf_free(p);
}
}
/* Cyclic lwIP timers check */
sys_check_timeouts();
/* your application goes here */
sleep_ms(5);
//if(netif.dhcp->state==DHCP_BOUND) printf("dhcp bound.");
}
}