forked from cnlohr/espthernet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnet_compat.c
287 lines (238 loc) · 5.25 KB
/
net_compat.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
//Copyright 2014-2016 <>< Charles Lohr, see LICENSE file. This file may be licensed under the 2-Clause BSD License or the MIT/x11 Licenses.
//This is a modification of the avrcraft IP stack.
#include "net_compat.h"
#include <string.h>
#include "packetmater.h"
#include <i2sduplex.h>
#include <manchestrate.h>
#include <osapi.h>
#include "crc32.h"
unsigned char ETbuffer[RX_BUFFER_END] __attribute__((aligned(32)));
unsigned short ETsendplace;
uint16_t sendbaseaddress;
unsigned short ETchecksum;
uint32_t g_process_paktime;
unsigned char MyMAC[6];
unsigned char MyIP[4] = { 10, 1, 10, 5 };
unsigned char MyMask[4] = { 255, 0, 0, 0 };
//Internal functs
ICACHE_FLASH_ATTR uint16_t internet_checksum( const unsigned char * start, uint16_t len )
{
uint16_t i;
const uint16_t * wptr = (uint16_t*) start;
uint32_t csum = 0;
for (i=1;i<len;i+=2)
{
csum = csum + (uint32_t)(*(wptr++));
}
if( len & 1 ) //See if there's an odd number of bytes?
{
uint8_t * tt = (uint8_t*)wptr;
csum += *tt;
}
while (csum>>16)
csum = (csum & 0xFFFF)+(csum >> 16);
csum = (csum>>8) | ((csum&0xff)<<8);
return ~csum;
}
void ICACHE_FLASH_ATTR et_backend_tick_quick()
{
int i;
for( i = 0; i < RXBUFS; i++ )
{
if( rx_pack_flags[i] != 2 ) continue;
uint16_t byr = rx_pack_lens[i];
uint16_t sendplace = PTR_TO_RX_BUF(i);
uint32_t cmpcrc = crc32( ETbuffer+sendplace, byr - 4 );
uint32_t checkcrc = (ETbuffer[sendplace+byr-1] << 24)|(ETbuffer[sendplace+byr-2] << 16)|(ETbuffer[sendplace+byr-3] << 8)|(ETbuffer[sendplace+byr-4]);
if( cmpcrc == checkcrc )
{
//If you ever get to this code, a miracle has happened.
//Pray that many more continue.
ETsendplace = sendplace;
et_receivecallback( byr - 4 );
}
else
{
#ifdef ALLOW_FRAME_DEBUGGING
//If we're debugging, be sure to capture this packet as a failure.
if( KeepNextPacket == 2 )
{
KeepNextPacket = 4;
}
#endif
printf( "!CRC\n" );
}
rx_pack_flags[i] = 0; //Release packet.
}
}
ICACHE_FLASH_ATTR void et_backend_tick_slow()
{
if( gotdma > 5 )
{
printf( "I2S Down. Restarting\n" );
testi2s_init();
gotdma = 0;
return;
}
if( gotlink > 250 )
{
printf( "Link Down too much. Restarting\n" );
testi2s_init();
gotlink = 0;
return;
}
gotlink++;
gotdma++;
}
//
uint16_t et_pop16()
{
uint16_t ret = et_pop8();
return (ret<<8) | et_pop8();
}
void et_popblob( uint8_t * data, uint8_t len )
{
while( len-- )
{
*(data++) = et_pop8();
}
}
void et_pushpgmstr( const char * msg )
{
uint8_t r;
do
{
r = pgm_read_byte(msg++);
if( !r ) break;
et_push8( r );
} while( 1 );
}
void et_pushpgmblob( const uint8_t * data, uint16_t len )
{
while( len-- )
{
et_push8( pgm_read_byte(data++) );
}
}
void et_pushstr( const char * msg )
{
for( ; *msg; msg++ )
et_push8( *msg );
}
void et_pushblob( const uint8_t * data, uint16_t len )
{
while( len-- )
{
et_push8( *(data++) );
}
}
void et_push16( uint16_t p )
{
et_push8( p>>8 );
et_push8( p&0xff );
}
//return 0 if OK, otherwise nonzero.
int8_t et_init( const unsigned char * macaddy )
{
MyMAC[0] = macaddy[0]+0x80; //Make sure this MAC is different from the MAC in the ESP.
MyMAC[1] = macaddy[1];
MyMAC[2] = macaddy[2];
MyMAC[3] = macaddy[3];
MyMAC[4] = macaddy[4];
MyMAC[5] = macaddy[5];
testi2s_init();
return 0;
}
void ICACHE_FLASH_ATTR et_endsend()
{
et_xmitpacket( sendbaseaddress, ETsendplace - sendbaseaddress );
}
int8_t ICACHE_FLASH_ATTR et_xmitpacket( uint16_t start, uint16_t len )
{
//If we're here, ETbuffer[start] points to the first byte (dst MAC address)
//Gotta calculate the checksum.
//First, round up the length and make sure it meets minimum requirements.
if( len < 60 ) len = 60;
len = ((len-1) & 0xfffc) + 4; //round up to 4.
uint8_t * buffer = &ETbuffer[start];
uint32_t crc = crc32( buffer, len );
uint16_t i = len;
buffer[i++] = crc & 0xff;
buffer[i++] = (crc>>8) & 0xff;
buffer[i++] = (crc>>16) & 0xff;
buffer[i++] = (crc>>24) & 0xff;
//Actually emit the packet.
SendPacketData( buffer, (len>>2) + 1 ); //Don't forget the CRC!
return 0;
}
unsigned short et_recvpack()
{
//Stub function.
return 0;
}
void ICACHE_FLASH_ATTR et_start_checksum( uint16_t start, uint16_t len )
{
uint16_t i;
const uint16_t * wptr = (uint16_t*)&ETbuffer[start+sendbaseaddress];
uint32_t csum = 0;
for (i=1;i<len;i+=2)
{
csum = csum + (uint32_t)(*(wptr++));
}
if( len & 1 ) //See if there's an odd number of bytes?
{
uint8_t * tt = (uint8_t*)wptr;
csum += *tt;
}
while (csum>>16)
csum = (csum & 0xFFFF)+(csum >> 16);
csum = (csum>>8) | ((csum&0xff)<<8);
ETchecksum = ~csum;
}
void ICACHE_FLASH_ATTR et_copy_memory( uint16_t to, uint16_t from, uint16_t length, uint16_t range_start, uint16_t range_end )
{
uint16_t i;
if( to == from )
{
return;
}
else if( to < from )
{
for( i = 0; i < length; i++ )
{
ETbuffer[to++] = ETbuffer[from++];
}
}
else
{
to += length;
from += length;
for( i = 0; i < length; i++ )
{
ETbuffer[to--] = ETbuffer[from--];
}
}
}
void et_write_ctrl_reg16( uint8_t addy, uint16_t value )
{
switch (addy )
{
case EERXRDPTL:
case EEGPWRPTL:
ETsendplace = value;
default:
break;
}
}
uint16_t et_read_ctrl_reg16( uint8_t addy )
{
switch( addy )
{
case EERXRDPTL:
case EEGPWRPTL:
return ETsendplace;
default:
return 0;
}
}