-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
222 lines (169 loc) · 4.96 KB
/
main.c
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*----------------------------------------------------------------------------
* Name: Blinky.c
* Purpose: Freescale MKL25Z128xxx4 LED Flasher
*----------------------------------------------------------------------------
* This file is part of the uVision/ARM development tools.
* This software may only be used under the terms of a valid, current,
* end user licence from KEIL for a compatible version of KEIL software
* development tools. Nothing else gives you the right to use this software.
*
* This software is supplied "AS IS" without warranties of any kind.
*
* Copyright (c) 2012 Keil - An ARM Company. All rights reserved.
*----------------------------------------------------------------------------*/
#include <MKL25Z4.H>
#include <string.h>
#include "spi.h"
volatile uint32_t msTicks; /* counts 1ms timeTicks */
/*----------------------------------------------------------------------------
SysTick_Handler
*----------------------------------------------------------------------------*/
void SysTick_Handler(void) {
msTicks++; /* increment counter necessary in Delay() */
}
#include "ticker.h"
#include "lwip\opt.h"
#include "lwip\stats.h"
#include "lwip\sys.h"
#include "lwip\pbuf.h"
#include "lwip\udp.h"
#include "lwip\tcp.h"
#include "lwip\tcp_impl.h"
#include "netif\etharp.h"
#include "arch\ethernetif.h"
#include "arch\clock.h"
static struct netif netif_eth0_data;
static struct netif* netif_eth0 = &netif_eth0_data;
static struct ip_addr my_ipaddr_data;
static struct ip_addr my_netmask_data;
static struct ip_addr my_gw_data;
static u32_t last_arp_time;
static u32_t last_tcpslow_time;
static u32_t last_tcpfast_time;
static u32_t last_led_time;
static u32_t last_led_state;
static char webpage[] =
"HTTP/1.1 200 OK\r\nContent-type: text/html\r\n\r\n\
<html><head><title>Congrats!</title></head><body><h1>Welcome to our lwIP HTTP server!</h1><p>This is a small test page.</body></html>";
static void lwip_init(void)
{
stats_init();
sys_init();
mem_init();
memp_init();
pbuf_init();
etharp_init();
ip_init();
udp_init();
tcp_init();
IP4_ADDR(&my_ipaddr_data, 192, 168, 1, 189);
IP4_ADDR(&my_netmask_data, 255, 255, 255, 0);
IP4_ADDR(&my_gw_data, 192, 168, 1, 1);
netif_add(netif_eth0, &my_ipaddr_data, &my_netmask_data, &my_gw_data, NULL, enc28j60_if_init, ip_input);
netif_set_default(netif_eth0);
netif_set_up(netif_eth0);
}
err_t http_recv_callback(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
{
char* rq;
if (p != NULL)
{
rq = p->payload;
if (rq[0] == 'G' && rq[1] == 'E' &&
rq[2] == 'T' && rq[3] == ' ' &&
rq[4] == '/')
{
u32_t bytestosend;
u32_t* newarg;
bytestosend = sizeof(webpage);
newarg = (u32_t*) mem_malloc(sizeof(u32_t));
*newarg = bytestosend;
if (tcp_write(tpcb, webpage, sizeof(webpage), 0) != ERR_OK)
{
mem_free(newarg);
tcp_close(tpcb);
}
else
{
tcp_arg(tpcb, newarg);
}
}
tcp_recved(tpcb, p->len);
pbuf_free(p);
}
return ERR_OK;
}
err_t http_poll_callback(void *arg, struct tcp_pcb *tpcb)
{
if (arg != NULL)
{
u32_t bytestosend = *((u32_t*) arg);
if (bytestosend == 0)
{
mem_free(arg);
tcp_arg(tpcb, NULL);
tcp_close(tpcb);
}
}
return ERR_OK;
}
err_t http_send_callback(void *arg, struct tcp_pcb *tpcb, u16_t len)
{
u32_t bytestosend;
if (arg != NULL)
{
bytestosend = *((u32_t*) arg);
if ((bytestosend - len) >= 0)
bytestosend -= len;
else
bytestosend = 0;
*((u32_t*) arg) = bytestosend;
}
return ERR_OK;
}
err_t http_accept_callback(void *arg, struct tcp_pcb *newpcb, err_t err)
{
tcp_arg(newpcb, NULL);
tcp_recv(newpcb, http_recv_callback);
tcp_sent(newpcb, http_send_callback);
tcp_poll(newpcb, http_poll_callback, 1);
return ERR_OK;
}
static void http_init(void)
{
struct tcp_pcb* tcpweb;
struct tcp_pcb* tcpweb_listen;
tcpweb = tcp_new();
if (tcpweb == NULL)
return;
/* Bind to port 80 for any address */
if (tcp_bind(tcpweb, IP_ADDR_ANY, 80) != ERR_OK)
return;
tcpweb_listen = tcp_listen(tcpweb);
if (tcpweb_listen == NULL)
{
tcp_abort(tcpweb);
tcpweb = NULL;
return;
}
tcpweb = tcpweb_listen;
tcp_accept(tcpweb, http_accept_callback);
}
/*----------------------------------------------------------------------------
MAIN function
*----------------------------------------------------------------------------*/
int main (void) {
int num = -1;
int dir = 1;
SystemCoreClockUpdate(); /* Get Core Clock Frequency */
SysTick_Config(SystemCoreClock/1000); /* Generate interrupt each 1 ms */
spi_init();
lwip_init();
http_init();
TICKER_Start();
while(1)
{
enc28j60_if_input(netif_eth0);
tcp_tmr();
}
}